Skip to content

Commit 5291ccb

Browse files
authored
fix(resourcemanager): Store IDs immediately after provisioning (#1243)
STACKITTPR-392 Signed-off-by: Alexander Dahmen <[email protected]>
1 parent ad3f4d3 commit 5291ccb

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

stackit/internal/services/resourcemanager/folder/resource.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2929
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
3030
resourcemanagerUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/resourcemanager/utils"
31+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
3132
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
3233
)
3334

@@ -220,6 +221,9 @@ func (r *folderResource) Create(ctx context.Context, req resource.CreateRequest,
220221

221222
// This sleep is currently needed due to the IAM Cache.
222223
time.Sleep(10 * time.Second)
224+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
225+
"container_id": *folderCreateResp.ContainerId,
226+
})
223227

224228
folderGetResponse, err := r.client.GetFolderDetails(ctx, *folderCreateResp.ContainerId).Execute()
225229
if err != nil {
@@ -376,9 +380,9 @@ func (r *folderResource) ImportState(ctx context.Context, req resource.ImportSta
376380
return
377381
}
378382

379-
ctx = tflog.SetField(ctx, "container_id", req.ID)
380-
381-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("container_id"), req.ID)...)
383+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
384+
"container_id": req.ID,
385+
})
382386
tflog.Info(ctx, "Resource Manager folder state imported")
383387
}
384388

stackit/internal/services/resourcemanager/project/resource.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
resourcemanagerUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/resourcemanager/utils"
12+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
1213

1314
"github.com/google/uuid"
1415
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
@@ -216,7 +217,18 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest
216217

217218
ctx = core.LogResponse(ctx)
218219

220+
if createResp.ContainerId == nil {
221+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating project", "Got empty container id")
222+
return
223+
}
219224
respContainerId := *createResp.ContainerId
225+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
226+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
227+
"container_id": respContainerId,
228+
})
229+
if resp.Diagnostics.HasError() {
230+
return
231+
}
220232

221233
// If the request has not been processed yet and the containerId doesn't exist,
222234
// the waiter will fail with authentication error, so wait some time before checking the creation
@@ -379,9 +391,9 @@ func (r *projectResource) ImportState(ctx context.Context, req resource.ImportSt
379391
return
380392
}
381393

382-
ctx = tflog.SetField(ctx, "container_id", req.ID)
383-
384-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("container_id"), req.ID)...)
394+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
395+
"container_id": req.ID,
396+
})
385397
tflog.Info(ctx, "Resource Manager Project state imported")
386398
}
387399

0 commit comments

Comments
 (0)