Skip to content

Commit a8809a4

Browse files
authored
fix(mongodb): store partial state (#752)
* fix: mongodb store partial state - avoid that an instance is created but not stored in terraform because an error occurred * add nil checks for CreateInstance response
1 parent 513808a commit a8809a4

File tree

1 file changed

+24
-4
lines changed
  • stackit/internal/services/mongodbflex/instance

1 file changed

+24
-4
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ import (
3333
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex/wait"
3434
)
3535

36-
const (
37-
DefaultBackupSchedule = "0 0/6 * * *"
38-
)
39-
4036
// Ensure the implementation satisfies the expected interfaces.
4137
var (
4238
_ resource.Resource = &instanceResource{}
@@ -390,8 +386,26 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
390386
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
391387
return
392388
}
389+
if createResp == nil {
390+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "API response is empty")
391+
return
392+
}
393+
if createResp.Id == nil {
394+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "API response does not contain instance id")
395+
return
396+
}
393397
instanceId := *createResp.Id
394398
ctx = tflog.SetField(ctx, "instance_id", instanceId)
399+
diags = resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)
400+
resp.Diagnostics.Append(diags...)
401+
if resp.Diagnostics.HasError() {
402+
return
403+
}
404+
diags = resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)
405+
resp.Diagnostics.Append(diags...)
406+
if resp.Diagnostics.HasError() {
407+
return
408+
}
395409
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx)
396410
if err != nil {
397411
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
@@ -405,6 +419,12 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
405419
return
406420
}
407421

422+
diags = resp.State.Set(ctx, model)
423+
resp.Diagnostics.Append(diags...)
424+
if resp.Diagnostics.HasError() {
425+
return
426+
}
427+
408428
backupScheduleOptionsPayload, err := toUpdateBackupScheduleOptionsPayload(ctx, &model, options)
409429
if err != nil {
410430
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Creating API payload: %v", err))

0 commit comments

Comments
 (0)