Skip to content

Commit c82cca0

Browse files
authored
fix(secretsmanager): Store IDs immediately after provisioning (#1260)
SACKITTPR-394 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent e3c8e42 commit c82cca0

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

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

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

22-
"github.com/hashicorp/terraform-plugin-framework/path"
2322
"github.com/hashicorp/terraform-plugin-framework/resource"
2423
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2524
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -180,8 +179,15 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
180179

181180
ctx = core.LogResponse(ctx)
182181

182+
if createResp.Id == nil {
183+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "Got empty instance id")
184+
return
185+
}
183186
instanceId := *createResp.Id
184-
ctx = tflog.SetField(ctx, "instance_id", instanceId)
187+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
188+
"project_id": projectId,
189+
"instance_id": instanceId,
190+
})
185191

186192
// Create ACLs
187193
err = updateACLs(ctx, projectId, instanceId, acls, r.client)
@@ -364,9 +370,10 @@ func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportS
364370
)
365371
return
366372
}
367-
368-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
369-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
373+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
374+
"project_id": idParts[0],
375+
"instance_id": idParts[1],
376+
})
370377
tflog.Info(ctx, "Secrets Manager instance state imported")
371378
}
372379

stackit/internal/services/secretsmanager/user/resource.go

Lines changed: 10 additions & 6 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"
@@ -191,7 +190,11 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
191190
return
192191
}
193192
userId := *userResp.Id
194-
ctx = tflog.SetField(ctx, "user_id", userId)
193+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
194+
"project_id": projectId,
195+
"instance_id": instanceId,
196+
"user_id": userId,
197+
})
195198

196199
// Map response body to schema
197200
err = mapFields(userResp, &model)
@@ -354,10 +357,11 @@ func (r *userResource) ImportState(ctx context.Context, req resource.ImportState
354357
)
355358
return
356359
}
357-
358-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
359-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
360-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), idParts[2])...)
360+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
361+
"project_id": idParts[0],
362+
"instance_id": idParts[1],
363+
"user_id": idParts[2],
364+
})
361365
core.LogAndAddWarning(ctx, &resp.Diagnostics,
362366
"Secrets Manager user imported with empty password",
363367
"The user password is not imported as it is only available upon creation of a new user. The password field will be empty.",

0 commit comments

Comments
 (0)