Skip to content

Commit 7fc5bfb

Browse files
authored
fix(iaas): Store IDs immediately after provisioning (#1228)
STACKITTPR-379 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 279cecb commit 7fc5bfb

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

stackit/internal/services/iaas/network/resource.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,21 @@ func (r *networkResource) Create(ctx context.Context, req resource.CreateRequest
428428
return
429429
}
430430

431+
if network.Id == nil {
432+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", "Got empty network id")
433+
return
434+
}
435+
431436
networkId := *network.Id
432-
ctx = tflog.SetField(ctx, "network_id", networkId)
437+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
438+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
439+
"project_id": projectId,
440+
"region": region,
441+
"network_id": networkId,
442+
})
443+
if resp.Diagnostics.HasError() {
444+
return
445+
}
433446

434447
network, err = wait.CreateNetworkWaitHandler(ctx, r.client, projectId, region, networkId).WaitWithContext(ctx)
435448
if err != nil {
@@ -592,17 +605,11 @@ func (r *networkResource) ImportState(ctx context.Context, req resource.ImportSt
592605
)
593606
return
594607
}
595-
596-
projectId := idParts[0]
597-
region := idParts[1]
598-
networkId := idParts[2]
599-
ctx = tflog.SetField(ctx, "project_id", projectId)
600-
ctx = tflog.SetField(ctx, "region", region)
601-
ctx = tflog.SetField(ctx, "network_id", networkId)
602-
603-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
604-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), region)...)
605-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("network_id"), networkId)...)
608+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
609+
"project_id": idParts[0],
610+
"region": idParts[1],
611+
"network_id": idParts[2],
612+
})
606613
tflog.Info(ctx, "Network state imported")
607614
}
608615

stackit/internal/services/iaas/server/resource.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,26 @@ func (r *serverResource) Create(ctx context.Context, req resource.CreateRequest,
498498

499499
ctx = core.LogResponse(ctx)
500500

501+
if server.Id == nil {
502+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating server", "Got empty server id")
503+
return
504+
}
501505
serverId := *server.Id
506+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
507+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
508+
"project_id": projectId,
509+
"region": region,
510+
"server_id": serverId,
511+
})
512+
if resp.Diagnostics.HasError() {
513+
return
514+
}
515+
502516
_, err = wait.CreateServerWaitHandler(ctx, r.client, projectId, region, serverId).WaitWithContext(ctx)
503517
if err != nil {
504518
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating server", fmt.Sprintf("server creation waiting: %v", err))
505519
return
506520
}
507-
ctx = tflog.SetField(ctx, "server_id", serverId)
508521

509522
// Get Server with details
510523
serverReq := r.client.GetServer(ctx, projectId, region, serverId)

stackit/internal/services/iaas/volume/resource.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,27 @@ func (r *volumeResource) Create(ctx context.Context, req resource.CreateRequest,
465465

466466
ctx = core.LogResponse(ctx)
467467

468+
if volume.Id == nil {
469+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating volume", "Got empty volume id")
470+
return
471+
}
468472
volumeId := *volume.Id
473+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
474+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
475+
"project_id": projectId,
476+
"region": region,
477+
"volume_id": volumeId,
478+
})
479+
if resp.Diagnostics.HasError() {
480+
return
481+
}
482+
469483
volume, err = wait.CreateVolumeWaitHandler(ctx, r.client, projectId, region, volumeId).WaitWithContext(ctx)
470484
if err != nil {
471485
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating volume", fmt.Sprintf("volume creation waiting: %v", err))
472486
return
473487
}
474488

475-
ctx = tflog.SetField(ctx, "volume_id", volumeId)
476-
477489
// Map response body to schema
478490
err = mapFields(ctx, volume, &model, region)
479491
if err != nil {

stackit/internal/services/iaas/volumeattach/resource.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ func (r *volumeAttachResource) Create(ctx context.Context, req resource.CreateRe
197197
}
198198

199199
ctx = core.LogResponse(ctx)
200+
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
201+
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
202+
"project_id": projectId,
203+
"region": region,
204+
"server_id": serverId,
205+
"volume_id": volumeId,
206+
})
207+
if resp.Diagnostics.HasError() {
208+
return
209+
}
200210

201211
_, err = wait.AddVolumeToServerWaitHandler(ctx, r.client, projectId, region, serverId, volumeId).WaitWithContext(ctx)
202212
if err != nil {

0 commit comments

Comments
 (0)