Skip to content

Commit c0edcad

Browse files
authored
fix(cdn): Store IDs immediately after provisioning (#1227)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent b2f7de3 commit c0edcad

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

stackit/internal/services/cdn/customdomain/resource.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/google/uuid"
1818
"github.com/hashicorp/terraform-plugin-framework/attr"
19-
"github.com/hashicorp/terraform-plugin-framework/path"
2019
"github.com/hashicorp/terraform-plugin-framework/resource"
2120
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2221
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -212,6 +211,15 @@ func (r *customDomainResource) Create(ctx context.Context, req resource.CreateRe
212211
}
213212

214213
ctx = core.LogResponse(ctx)
214+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
215+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
216+
"project_id": projectId,
217+
"distribution_id": distributionId,
218+
"name": name,
219+
})
220+
if resp.Diagnostics.HasError() {
221+
return
222+
}
215223

216224
_, err = wait.CreateCDNCustomDomainWaitHandler(ctx, r.client, projectId, distributionId, name).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
217225
if err != nil {
@@ -382,9 +390,11 @@ func (r *customDomainResource) ImportState(ctx context.Context, req resource.Imp
382390
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
383391
core.LogAndAddError(ctx, &resp.Diagnostics, "Error importing CDN custom domain", fmt.Sprintf("Expected import identifier on the format: [project_id]%q[distribution_id]%q[custom_domain_name], got %q", core.Separator, core.Separator, req.ID))
384392
}
385-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
386-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("distribution_id"), idParts[1])...)
387-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[2])...)
393+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
394+
"project_id": idParts[0],
395+
"distribution_id": idParts[1],
396+
"name": idParts[2],
397+
})
388398
tflog.Info(ctx, "CDN custom domain state imported")
389399
}
390400

stackit/internal/services/cdn/distribution/resource.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,20 @@ func (r *distributionResource) Create(ctx context.Context, req resource.CreateRe
433433

434434
ctx = core.LogResponse(ctx)
435435

436+
if createResp.Distribution.Id == nil {
437+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN distribution", "Got empty cdn distribution id")
438+
return
439+
}
440+
441+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
442+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
443+
"project_id": projectId,
444+
"distribution_id": *createResp.Distribution.Id,
445+
})
446+
if resp.Diagnostics.HasError() {
447+
return
448+
}
449+
436450
waitResp, err := wait.CreateDistributionPoolWaitHandler(ctx, r.client, projectId, *createResp.Distribution.Id).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
437451
if err != nil {
438452
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN distribution", fmt.Sprintf("Waiting for create: %v", err))
@@ -682,8 +696,10 @@ func (r *distributionResource) ImportState(ctx context.Context, req resource.Imp
682696
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
683697
core.LogAndAddError(ctx, &resp.Diagnostics, "Error importing CDN distribution", fmt.Sprintf("Expected import identifier on the format: [project_id]%q[distribution_id], got %q", core.Separator, req.ID))
684698
}
685-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
686-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("distribution_id"), idParts[1])...)
699+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
700+
"project_id": idParts[0],
701+
"distribution_id": idParts[1],
702+
})
687703
tflog.Info(ctx, "CDN distribution state imported")
688704
}
689705

0 commit comments

Comments
 (0)