Skip to content

Commit

Permalink
resource pullzone_hostname: handle internal hostnames transparently
Browse files Browse the repository at this point in the history
internal hostnames (`*.b-cdn.net`) are automatically created when a pullzone is created and cannot be deleted
  • Loading branch information
rafael-at-bunny committed Sep 10, 2024
1 parent 90ba83d commit a46c87d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ 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.3.14] - 2024-09-10
## Changed
- resource pullzone_hostname: deleting an internal hostname (`*.b-cdn.net`) will remove the resource from state without deleting it, as internal hostnames cannot be deleted;
- resource pullzone_hostname: creating an internal hostname (`*.b-cdn.net`) will adopt the pre-existing default hostname instead of creating a new one, as the default hostname is automatically created with the `pullzone` resource;

## [0.3.13] - 2024-09-09
### Fixed
- resource pullzone_optimizer_class: some fields were wrongly escaped ([#11](https://github.com/BunnyWay/terraform-provider-bunnynet/issues/11))
Expand Down
24 changes: 23 additions & 1 deletion internal/api/pullzone_hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"errors"
"fmt"
"github.com/bunnyway/terraform-provider-bunnynet/internal/utils"
"golang.org/x/exp/slices"
"net/http"
"strings"
)

type PullzoneHostname struct {
Expand All @@ -30,6 +32,26 @@ func (c *Client) CreatePullzoneHostname(data PullzoneHostname) (PullzoneHostname
return PullzoneHostname{}, errors.New("pullzone is required")
}

pullzone, err := c.GetPullzone(data.PullzoneId)
if err != nil {
return PullzoneHostname{}, err
}

// if creating the default hostname, return existing
if strings.HasSuffix(data.Name, "."+pullzone.CnameDomain) {
hostnameIdx := slices.IndexFunc(pullzone.Hostnames, func(hostname PullzoneHostname) bool {
return hostname.IsSystemHostname && hostname.Name == data.Name
})

if hostnameIdx > -1 {
data.Id = pullzone.Hostnames[hostnameIdx].Id
data.PullzoneId = pullzone.Id
return c.UpdatePullzoneHostname(data, pullzone.Hostnames[hostnameIdx])
}

// if system hostname not found, try to create it, the API should return an error
}

body, err := json.Marshal(map[string]string{
"Hostname": data.Name,
})
Expand All @@ -52,7 +74,7 @@ func (c *Client) CreatePullzoneHostname(data PullzoneHostname) (PullzoneHostname
}
}

pullzone, err := c.GetPullzone(pullzoneId)
pullzone, err = c.GetPullzone(pullzoneId)
if err != nil {
return PullzoneHostname{}, err
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/resource_pullzone_hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ func (r *PullzoneHostnameResource) Delete(ctx context.Context, req resource.Dele
return
}

// b-cdn.net hostnames cannot be deleted
if data.IsInternal.ValueBool() {
return
}

err := r.client.DeletePullzoneHostname(data.PullzoneId.ValueInt64(), data.Name.ValueString())
if err != nil {
resp.Diagnostics.Append(diag.NewErrorDiagnostic("Error deleting hostname", err.Error()))
Expand Down

0 comments on commit a46c87d

Please sign in to comment.