Skip to content

Commit ad3f4d3

Browse files
fix(redis): Store IDs immediately after provisioning (#1241)
STACKITTPR-391 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de> Co-authored-by: cgoetz-inovex <carlo.goetz@inovex.de>
1 parent c4664e9 commit ad3f4d3

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

stackit/internal/services/redis/credential/resource.go

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

18-
"github.com/hashicorp/terraform-plugin-framework/path"
1918
"github.com/hashicorp/terraform-plugin-framework/resource"
2019
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2120
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -192,7 +191,15 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
192191
return
193192
}
194193
credentialId := *credentialsResp.Id
195-
ctx = tflog.SetField(ctx, "credential_id", credentialId)
194+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
195+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
196+
"project_id": projectId,
197+
"instance_id": instanceId,
198+
"credential_id": credentialId,
199+
})
200+
if resp.Diagnostics.HasError() {
201+
return
202+
}
196203

197204
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
198205
if err != nil {
@@ -313,9 +320,11 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
313320
return
314321
}
315322

316-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
317-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
318-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
323+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
324+
"project_id": idParts[0],
325+
"instance_id": idParts[1],
326+
"credential_id": idParts[2],
327+
})
319328
tflog.Info(ctx, "Redis credential state imported")
320329
}
321330

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

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

23-
"github.com/hashicorp/terraform-plugin-framework/path"
2423
"github.com/hashicorp/terraform-plugin-framework/resource"
2524
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2625
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -433,8 +432,19 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
433432

434433
ctx = core.LogResponse(ctx)
435434

435+
if createResp.InstanceId == nil {
436+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "Got empty instance id")
437+
return
438+
}
436439
instanceId := *createResp.InstanceId
437-
ctx = tflog.SetField(ctx, "instance_id", instanceId)
440+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
441+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
442+
"project_id": projectId,
443+
"instance_id": instanceId,
444+
})
445+
if resp.Diagnostics.HasError() {
446+
return
447+
}
438448
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx)
439449
if err != nil {
440450
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
@@ -624,8 +634,10 @@ func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportS
624634
return
625635
}
626636

627-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
628-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
637+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
638+
"project_id": idParts[0],
639+
"instance_id": idParts[1],
640+
})
629641
tflog.Info(ctx, "Redis instance state imported")
630642
}
631643

0 commit comments

Comments
 (0)