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
This commit is contained in:
Marcel Jacek 2025-03-31 14:53:45 +02:00 committed by GitHub
parent 513808a8a0
commit a8809a4979
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,10 +33,6 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex/wait"
)
const (
DefaultBackupSchedule = "0 0/6 * * *"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &instanceResource{}
@ -390,8 +386,26 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
return
}
if createResp == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "API response is empty")
return
}
if createResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "API response does not contain instance id")
return
}
instanceId := *createResp.Id
ctx = tflog.SetField(ctx, "instance_id", instanceId)
diags = resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx)
if err != nil {
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
return
}
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
backupScheduleOptionsPayload, err := toUpdateBackupScheduleOptionsPayload(ctx, &model, options)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Creating API payload: %v", err))