Skip to content

Commit e602836

Browse files
committed
fix(git): Store IDs immediately after provisioning
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 81f136e commit e602836

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

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

Lines changed: 13 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"
@@ -221,6 +220,15 @@ func (g *gitResource) Create(ctx context.Context, req resource.CreateRequest, re
221220
ctx = core.LogResponse(ctx)
222221

223222
gitInstanceId := *gitInstanceResp.Id
223+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
224+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
225+
"project_id": projectId,
226+
"instance_id": gitInstanceId,
227+
})
228+
if resp.Diagnostics.HasError() {
229+
return
230+
}
231+
224232
_, err = wait.CreateGitInstanceWaitHandler(ctx, g.client, projectId, gitInstanceId).WaitWithContext(ctx)
225233
if err != nil {
226234
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating git instance", fmt.Sprintf("Git instance creation waiting: %v", err))
@@ -345,13 +353,11 @@ func (g *gitResource) ImportState(ctx context.Context, req resource.ImportStateR
345353
)
346354
return
347355
}
348-
349-
projectId := idParts[0]
350-
instanceId := idParts[1]
351-
352356
// 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)...)
357+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
358+
"project_id": idParts[0],
359+
"instance_id": idParts[1],
360+
})
355361
tflog.Info(ctx, "Git instance state imported")
356362
}
357363

0 commit comments

Comments
 (0)