Skip to content

Commit c8c93f1

Browse files
authored
fix(postgresflex): Store IDs immediately after provisioning (#1235)
STACKITTPR-389 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 180aef8 commit c8c93f1

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

stackit/internal/services/postgresflex/database/resource.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
1717
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1818

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"
@@ -350,10 +349,12 @@ func (r *databaseResource) ImportState(ctx context.Context, req resource.ImportS
350349
return
351350
}
352351

353-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
354-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...)
355-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...)
356-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_id"), idParts[3])...)
352+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
353+
"project_id": idParts[0],
354+
"region": idParts[1],
355+
"instance_id": idParts[2],
356+
"database_id": idParts[3],
357+
})
357358
core.LogAndAddWarning(ctx, &resp.Diagnostics,
358359
"Postgresflex database imported with empty password",
359360
"The database password is not imported as it is only available upon creation of a new database. The password field will be empty.",

stackit/internal/services/postgresflex/instance/resource.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
2222
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2323

24-
"github.com/hashicorp/terraform-plugin-framework/path"
2524
"github.com/hashicorp/terraform-plugin-framework/resource"
2625
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2726
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -329,9 +328,22 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
329328
}
330329

331330
ctx = core.LogResponse(ctx)
332-
331+
if createResp.Id == nil {
332+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "Got empty instance id")
333+
return
334+
}
333335
instanceId := *createResp.Id
334-
ctx = tflog.SetField(ctx, "instance_id", instanceId)
336+
337+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
338+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
339+
"project_id": projectId,
340+
"region": region,
341+
"instance_id": instanceId,
342+
})
343+
if resp.Diagnostics.HasError() {
344+
return
345+
}
346+
335347
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).WaitWithContext(ctx)
336348
if err != nil {
337349
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
@@ -554,9 +566,11 @@ func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportS
554566
return
555567
}
556568

557-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
558-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...)
559-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...)
569+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
570+
"project_id": idParts[0],
571+
"region": idParts[1],
572+
"instance_id": idParts[2],
573+
})
560574
tflog.Info(ctx, "Postgres Flex instance state imported")
561575
}
562576

0 commit comments

Comments
 (0)