Skip to content

Commit

Permalink
resource compute_script: expose deployment_key and release attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-at-bunny committed Jan 17, 2025
1 parent 890cb2d commit f03ccc0
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/compute_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 19 additions & 4 deletions internal/api/compute_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
47 changes: 47 additions & 0 deletions internal/api/compute_script_release.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 27 additions & 4 deletions internal/provider/resource_compute_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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.",
},
},
}
}
Expand Down Expand Up @@ -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
}

0 comments on commit f03ccc0

Please sign in to comment.