Skip to content

Commit b2f7de3

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

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

  • stackit/internal/services/loadbalancer/loadbalancer

stackit/internal/services/loadbalancer/loadbalancer/resource.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1717
"github.com/hashicorp/terraform-plugin-framework/attr"
1818
"github.com/hashicorp/terraform-plugin-framework/diag"
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/boolplanmodifier"
@@ -773,6 +772,21 @@ func (r *loadBalancerResource) Create(ctx context.Context, req resource.CreateRe
773772

774773
ctx = core.LogResponse(ctx)
775774

775+
if createResp.Name == nil {
776+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating load balancer", "Got empty load balancer name")
777+
return
778+
}
779+
780+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
781+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
782+
"project_id": projectId,
783+
"region": region,
784+
"name": *createResp.Name,
785+
})
786+
if resp.Diagnostics.HasError() {
787+
return
788+
}
789+
776790
waitResp, err := wait.CreateLoadBalancerWaitHandler(ctx, r.client, projectId, region, *createResp.Name).SetTimeout(90 * time.Minute).WaitWithContext(ctx)
777791
if err != nil {
778792
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating load balancer", fmt.Sprintf("Load balancer creation waiting: %v", err))
@@ -965,9 +979,11 @@ func (r *loadBalancerResource) ImportState(ctx context.Context, req resource.Imp
965979
return
966980
}
967981

968-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
969-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...)
970-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[2])...)
982+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
983+
"project_id": idParts[0],
984+
"region": idParts[1],
985+
"name": idParts[2],
986+
})
971987
tflog.Info(ctx, "Load balancer state imported")
972988
}
973989

0 commit comments

Comments
 (0)