diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fc5859..871a813 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). > [!NOTE] > While we strive to maintain backwards compatibility as much as possible, we can't guarantee semantic versioning will be strictly followed, as this provider depends on the underlying [bunny.net API](https://docs.bunny.net/reference/bunnynet-api-overview). +## [0.5.3] - 2025-01-17 +### Added +- resource compute_script: expose deployment_key and release attributes ([#26](https://github.com/BunnyWay/terraform-provider-bunnynet/issues/26)) + ## [0.5.2] - 2025-01-14 ### Fixed - resource compute_script: workaround for updating code in published scripts ([#25](https://github.com/BunnyWay/terraform-provider-bunnynet/issues/25)) diff --git a/docs/resources/compute_script.md b/docs/resources/compute_script.md index 8a959c3..cd1bf59 100644 --- a/docs/resources/compute_script.md +++ b/docs/resources/compute_script.md @@ -40,7 +40,9 @@ resource "bunnynet_compute_script" "test" { ### Read-Only +- `deployment_key` (String) The deployment key for the script. - `id` (Number) The ID of the script. +- `release` (String) The current release identifier for the script. ## Import diff --git a/internal/api/compute_script.go b/internal/api/compute_script.go index 9201e30..54f1d26 100644 --- a/internal/api/compute_script.go +++ b/internal/api/compute_script.go @@ -13,10 +13,13 @@ import ( ) type ComputeScript struct { - Id int64 `json:"Id,omitempty"` - ScriptType uint8 `json:"ScriptType"` - Name string `json:"Name"` - Content string `json:"Content"` + Id int64 `json:"Id,omitempty"` + ScriptType uint8 `json:"ScriptType"` + Name string `json:"Name"` + Content string `json:"Content"` + DeploymentKey string `json:"DeploymentKey,omitempty"` + Release string `json:"-"` + CurrentReleaseId int64 `json:"CurrentReleaseId,omitempty"` } func (c *Client) GetComputeScript(id int64) (ComputeScript, error) { @@ -68,6 +71,18 @@ func (c *Client) GetComputeScript(id int64) (ComputeScript, error) { data.Content = codeData["Code"] } + // current release + if data.CurrentReleaseId > 0 { + release, err := c.GetComputeScriptActiveRelease(data.Id) + if err != nil { + if !errors.Is(err, ErrComputeScriptReleaseNotFound) { + return data, err + } + } else { + data.Release = release.Uuid + } + } + return data, nil } diff --git a/internal/api/compute_script_release.go b/internal/api/compute_script_release.go new file mode 100644 index 0000000..4d23649 --- /dev/null +++ b/internal/api/compute_script_release.go @@ -0,0 +1,47 @@ +// Copyright (c) BunnyWay d.o.o. +// SPDX-License-Identifier: MPL-2.0 + +package api + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "net/http" +) + +type ComputeScriptRelease struct { + Id int64 `json:"Id,omitempty"` + Uuid string `json:"Uuid"` + Note string `json:"Note"` + Code string `json:"Code"` +} + +var ErrComputeScriptReleaseNotFound = errors.New("compute script release not found") + +func (c *Client) GetComputeScriptActiveRelease(scriptId int64) (ComputeScriptRelease, error) { + var response ComputeScriptRelease + + resp, err := c.doRequest(http.MethodGet, fmt.Sprintf("%s/compute/script/%d/releases/active", c.apiUrl, scriptId), nil) + if err != nil { + return response, err + } + + if resp.StatusCode == http.StatusNotFound { + return response, ErrComputeScriptReleaseNotFound + } + + if resp.StatusCode != http.StatusOK { + return response, errors.New(resp.Status) + } + + bodyResp, err := io.ReadAll(resp.Body) + if err != nil { + return response, err + } + + _ = resp.Body.Close() + err = json.Unmarshal(bodyResp, &response) + return response, err +} diff --git a/internal/provider/resource_compute_script.go b/internal/provider/resource_compute_script.go index 19fe5c0..d365f59 100644 --- a/internal/provider/resource_compute_script.go +++ b/internal/provider/resource_compute_script.go @@ -33,10 +33,12 @@ type ComputeScriptResource struct { } type ComputeScriptResourceModel struct { - Id types.Int64 `tfsdk:"id"` - Type types.String `tfsdk:"type"` - Name types.String `tfsdk:"name"` - Content types.String `tfsdk:"content"` + Id types.Int64 `tfsdk:"id"` + Type types.String `tfsdk:"type"` + Name types.String `tfsdk:"name"` + Content types.String `tfsdk:"content"` + DeploymentKey types.String `tfsdk:"deployment_key"` + Release types.String `tfsdk:"release"` } var computeScriptTypeMap = map[uint8]string{ @@ -84,6 +86,20 @@ func (r *ComputeScriptResource) Schema(ctx context.Context, req resource.SchemaR }, Description: "The code of the script.", }, + "deployment_key": schema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + Description: "The deployment key for the script.", + }, + "release": schema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + Description: "The current release identifier for the script.", + }, }, } } @@ -236,6 +252,13 @@ func (r *ComputeScriptResource) convertApiToModel(dataApi api.ComputeScript) (Co dataTf.Type = types.StringValue(mapKeyToValue(computeScriptTypeMap, dataApi.ScriptType)) dataTf.Name = types.StringValue(dataApi.Name) dataTf.Content = types.StringValue(dataApi.Content) + dataTf.DeploymentKey = types.StringValue(dataApi.DeploymentKey) + + if dataApi.Release != "" { + dataTf.Release = types.StringValue(dataApi.Release) + } else { + dataTf.Release = types.StringNull() + } return dataTf, nil }