From a46c87d67df8ee87ed9e5570cf2e4b86f6ff7a9e Mon Sep 17 00:00:00 2001 From: Rafael Kassner Date: Tue, 10 Sep 2024 17:14:37 +0200 Subject: [PATCH] resource pullzone_hostname: handle internal hostnames transparently internal hostnames (`*.b-cdn.net`) are automatically created when a pullzone is created and cannot be deleted --- CHANGELOG.md | 5 ++++ internal/api/pullzone_hostname.go | 24 ++++++++++++++++++- .../provider/resource_pullzone_hostname.go | 5 ++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a567412..dcb25a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/internal/api/pullzone_hostname.go b/internal/api/pullzone_hostname.go index b917333..924c067 100644 --- a/internal/api/pullzone_hostname.go +++ b/internal/api/pullzone_hostname.go @@ -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 { @@ -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, }) @@ -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 } diff --git a/internal/provider/resource_pullzone_hostname.go b/internal/provider/resource_pullzone_hostname.go index dca32eb..9f49bbc 100644 --- a/internal/provider/resource_pullzone_hostname.go +++ b/internal/provider/resource_pullzone_hostname.go @@ -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()))