Skip to content

Commit cbdee9f

Browse files
authored
fix(logme): Store IDs immediately after provisioning (#1220)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 8d6e86b commit cbdee9f

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

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

Lines changed: 14 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/core"
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"
@@ -182,7 +181,15 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
182181
return
183182
}
184183
credentialId := *credentialsResp.Id
185-
ctx = tflog.SetField(ctx, "credential_id", credentialId)
184+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
185+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
186+
"project_id": projectId,
187+
"instance_id": instanceId,
188+
"credential_id": credentialId,
189+
})
190+
if resp.Diagnostics.HasError() {
191+
return
192+
}
186193

187194
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
188195
if err != nil {
@@ -303,9 +310,11 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
303310
return
304311
}
305312

306-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
307-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
308-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
313+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
314+
"project_id": idParts[0],
315+
"instance_id": idParts[1],
316+
"credential_id": idParts[2],
317+
})
309318
tflog.Info(ctx, "LogMe credential state imported")
310319
}
311320

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
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"
@@ -427,7 +426,15 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
427426
ctx = core.LogResponse(ctx)
428427

429428
instanceId := *createResp.InstanceId
430-
ctx = tflog.SetField(ctx, "instance_id", instanceId)
429+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
430+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
431+
"project_id": projectId,
432+
"instance_id": instanceId,
433+
})
434+
if resp.Diagnostics.HasError() {
435+
return
436+
}
437+
431438
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).SetTimeout(90 * time.Minute).WaitWithContext(ctx)
432439
if err != nil {
433440
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
@@ -617,8 +624,10 @@ func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportS
617624
return
618625
}
619626

620-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
621-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
627+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
628+
"project_id": idParts[0],
629+
"instance_id": idParts[1],
630+
})
622631
tflog.Info(ctx, "LogMe instance state imported")
623632
}
624633

0 commit comments

Comments
 (0)