Skip to content

Commit ac2a49c

Browse files
authored
fix(git): store IDs immediately after provisioning (#1224)
relates to STACKITTPR-378
1 parent 81f136e commit ac2a49c

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1111
"github.com/hashicorp/terraform-plugin-framework/diag"
12-
"github.com/hashicorp/terraform-plugin-framework/path"
1312
"github.com/hashicorp/terraform-plugin-framework/resource"
1413
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1514
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
@@ -220,7 +219,21 @@ func (g *gitResource) Create(ctx context.Context, req resource.CreateRequest, re
220219

221220
ctx = core.LogResponse(ctx)
222221

222+
if gitInstanceResp.Id == nil {
223+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating git instance", "Got empty git instance id")
224+
return
225+
}
226+
223227
gitInstanceId := *gitInstanceResp.Id
228+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
229+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
230+
"project_id": projectId,
231+
"instance_id": gitInstanceId,
232+
})
233+
if resp.Diagnostics.HasError() {
234+
return
235+
}
236+
224237
_, err = wait.CreateGitInstanceWaitHandler(ctx, g.client, projectId, gitInstanceId).WaitWithContext(ctx)
225238
if err != nil {
226239
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating git instance", fmt.Sprintf("Git instance creation waiting: %v", err))
@@ -345,13 +358,11 @@ func (g *gitResource) ImportState(ctx context.Context, req resource.ImportStateR
345358
)
346359
return
347360
}
348-
349-
projectId := idParts[0]
350-
instanceId := idParts[1]
351-
352361
// Set the project ID and instance ID attributes in the state.
353-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
354-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...)
362+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
363+
"project_id": idParts[0],
364+
"instance_id": idParts[1],
365+
})
355366
tflog.Info(ctx, "Git instance state imported")
356367
}
357368

0 commit comments

Comments
 (0)