Skip to content

Commit 989553e

Browse files
committed
Update id of newly created resources during update
1 parent 6e4bb1d commit 989553e

30 files changed

+432
-218
lines changed

gen/templates/bulk/resource.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -643,23 +643,26 @@ func (r *{{camelCase .BulkName}}Resource) Update(ctx context.Context, req resour
643643
}
644644
{{- end}}
645645

646+
{{- if not .PutCreate}}
647+
newIdIndexes := make([]int, 0)
648+
{{- end}}
646649
// Check for new and updated items
647-
for _, item := range plan.Items {
650+
for i := range plan.Items {
648651
found := false
649652
for _, itemState := range state.Items {
650-
if item.{{getBulkItemId .}}.ValueString() == itemState.{{getBulkItemId .}}.ValueString() {
653+
if plan.Items[i].{{getBulkItemId .}}.ValueString() == itemState.{{getBulkItemId .}}.ValueString() {
651654
found = true
652655
// If the item is present in both plan and state, we need to check if it has changes
653-
hasChanges := plan.hasChanges(ctx, &state, item.{{getBulkItemId .}}.ValueString())
656+
hasChanges := plan.hasChanges(ctx, &state, plan.Items[i].{{getBulkItemId .}}.ValueString())
654657
if hasChanges {
655658
actions = append(actions, meraki.ActionModel{
656659
Operation: "update",
657660
{{- if .PutCreate}}
658-
Resource: plan.getItemPath(item.{{getBulkItemId .}}.ValueString()),
661+
Resource: plan.getItemPath(plan.Items[i].{{getBulkItemId .}}.ValueString()),
659662
{{- else}}
660-
Resource: plan.getPath() + "/" + item.Id.ValueString(),
663+
Resource: plan.getPath() + "/" + plan.Items[i].Id.ValueString(),
661664
{{- end}}
662-
Body: item.toBody(ctx, itemState),
665+
Body: plan.Items[i].toBody(ctx, itemState),
663666
})
664667
}
665668
break
@@ -670,15 +673,16 @@ func (r *{{camelCase .BulkName}}Resource) Update(ctx context.Context, req resour
670673
{{- if .PutCreate}}
671674
actions = append(actions, meraki.ActionModel{
672675
Operation: "update",
673-
Resource: plan.getItemPath(item.{{getBulkItemId .}}.ValueString()),
674-
Body: item.toBody(ctx, Resource{{camelCase .BulkName}}Items{}),
676+
Resource: plan.getItemPath(plan.Items[i].{{getBulkItemId .}}.ValueString()),
677+
Body: plan.Items[i].toBody(ctx, Resource{{camelCase .BulkName}}Items{}),
675678
})
676679
{{- else}}
677680
actions = append(actions, meraki.ActionModel{
678681
Operation: "create",
679682
Resource: plan.getPath(),
680-
Body: item.toBody(ctx, Resource{{camelCase .BulkName}}Items{}),
683+
Body: plan.Items[i].toBody(ctx, Resource{{camelCase .BulkName}}Items{}),
681684
})
685+
newIdIndexes = append(newIdIndexes, i)
682686
{{- end}}
683687
}
684688
}
@@ -689,6 +693,15 @@ func (r *{{camelCase .BulkName}}Resource) Update(ctx context.Context, req resour
689693
return
690694
}
691695
{{- end}}
696+
{{- if not .PutCreate}}
697+
responseIndex := 0
698+
for i := range plan.Items {
699+
if slices.Contains(newIdIndexes, i) {
700+
plan.Items[i].Id = types.StringValue(res.Get("status.createdResources." + strconv.Itoa(responseIndex) + ".id").String())
701+
responseIndex++
702+
}
703+
}
704+
{{- end}}
692705

693706
tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString()))
694707

internal/provider/resource_meraki_appliance_ports.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,19 @@ func (r *AppliancePortsResource) Update(ctx context.Context, req resource.Update
228228

229229
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString()))
230230
var actions []meraki.ActionModel
231-
232231
// Check for new and updated items
233-
for _, item := range plan.Items {
232+
for i := range plan.Items {
234233
found := false
235234
for _, itemState := range state.Items {
236-
if item.PortId.ValueString() == itemState.PortId.ValueString() {
235+
if plan.Items[i].PortId.ValueString() == itemState.PortId.ValueString() {
237236
found = true
238237
// If the item is present in both plan and state, we need to check if it has changes
239-
hasChanges := plan.hasChanges(ctx, &state, item.PortId.ValueString())
238+
hasChanges := plan.hasChanges(ctx, &state, plan.Items[i].PortId.ValueString())
240239
if hasChanges {
241240
actions = append(actions, meraki.ActionModel{
242241
Operation: "update",
243-
Resource: plan.getItemPath(item.PortId.ValueString()),
244-
Body: item.toBody(ctx, itemState),
242+
Resource: plan.getItemPath(plan.Items[i].PortId.ValueString()),
243+
Body: plan.Items[i].toBody(ctx, itemState),
245244
})
246245
}
247246
break
@@ -251,8 +250,8 @@ func (r *AppliancePortsResource) Update(ctx context.Context, req resource.Update
251250
// If the item is present in plan, but not in state, we need to create it
252251
actions = append(actions, meraki.ActionModel{
253252
Operation: "update",
254-
Resource: plan.getItemPath(item.PortId.ValueString()),
255-
Body: item.toBody(ctx, ResourceAppliancePortsItems{}),
253+
Resource: plan.getItemPath(plan.Items[i].PortId.ValueString()),
254+
Body: plan.Items[i].toBody(ctx, ResourceAppliancePortsItems{}),
256255
})
257256
}
258257
}

internal/provider/resource_meraki_appliance_prefix_delegated_statics.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package provider
2121
import (
2222
"context"
2323
"fmt"
24+
"slices"
2425
"strconv"
2526
"strings"
2627

@@ -253,20 +254,20 @@ func (r *AppliancePrefixDelegatedStaticsResource) Update(ctx context.Context, re
253254
})
254255
}
255256
}
256-
257+
newIdIndexes := make([]int, 0)
257258
// Check for new and updated items
258-
for _, item := range plan.Items {
259+
for i := range plan.Items {
259260
found := false
260261
for _, itemState := range state.Items {
261-
if item.Id.ValueString() == itemState.Id.ValueString() {
262+
if plan.Items[i].Id.ValueString() == itemState.Id.ValueString() {
262263
found = true
263264
// If the item is present in both plan and state, we need to check if it has changes
264-
hasChanges := plan.hasChanges(ctx, &state, item.Id.ValueString())
265+
hasChanges := plan.hasChanges(ctx, &state, plan.Items[i].Id.ValueString())
265266
if hasChanges {
266267
actions = append(actions, meraki.ActionModel{
267268
Operation: "update",
268-
Resource: plan.getPath() + "/" + item.Id.ValueString(),
269-
Body: item.toBody(ctx, itemState),
269+
Resource: plan.getPath() + "/" + plan.Items[i].Id.ValueString(),
270+
Body: plan.Items[i].toBody(ctx, itemState),
270271
})
271272
}
272273
break
@@ -277,8 +278,9 @@ func (r *AppliancePrefixDelegatedStaticsResource) Update(ctx context.Context, re
277278
actions = append(actions, meraki.ActionModel{
278279
Operation: "create",
279280
Resource: plan.getPath(),
280-
Body: item.toBody(ctx, ResourceAppliancePrefixDelegatedStaticsItems{}),
281+
Body: plan.Items[i].toBody(ctx, ResourceAppliancePrefixDelegatedStaticsItems{}),
281282
})
283+
newIdIndexes = append(newIdIndexes, i)
282284
}
283285
}
284286

@@ -287,6 +289,13 @@ func (r *AppliancePrefixDelegatedStaticsResource) Update(ctx context.Context, re
287289
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure objects (Action Batch), got error: %s, %s", err, res.String()))
288290
return
289291
}
292+
responseIndex := 0
293+
for i := range plan.Items {
294+
if slices.Contains(newIdIndexes, i) {
295+
plan.Items[i].Id = types.StringValue(res.Get("status.createdResources." + strconv.Itoa(responseIndex) + ".id").String())
296+
responseIndex++
297+
}
298+
}
290299

291300
tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString()))
292301

internal/provider/resource_meraki_appliance_rf_profiles.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package provider
2121
import (
2222
"context"
2323
"fmt"
24+
"slices"
2425
"strconv"
2526
"strings"
2627

@@ -297,20 +298,20 @@ func (r *ApplianceRFProfilesResource) Update(ctx context.Context, req resource.U
297298
})
298299
}
299300
}
300-
301+
newIdIndexes := make([]int, 0)
301302
// Check for new and updated items
302-
for _, item := range plan.Items {
303+
for i := range plan.Items {
303304
found := false
304305
for _, itemState := range state.Items {
305-
if item.Id.ValueString() == itemState.Id.ValueString() {
306+
if plan.Items[i].Id.ValueString() == itemState.Id.ValueString() {
306307
found = true
307308
// If the item is present in both plan and state, we need to check if it has changes
308-
hasChanges := plan.hasChanges(ctx, &state, item.Id.ValueString())
309+
hasChanges := plan.hasChanges(ctx, &state, plan.Items[i].Id.ValueString())
309310
if hasChanges {
310311
actions = append(actions, meraki.ActionModel{
311312
Operation: "update",
312-
Resource: plan.getPath() + "/" + item.Id.ValueString(),
313-
Body: item.toBody(ctx, itemState),
313+
Resource: plan.getPath() + "/" + plan.Items[i].Id.ValueString(),
314+
Body: plan.Items[i].toBody(ctx, itemState),
314315
})
315316
}
316317
break
@@ -321,8 +322,9 @@ func (r *ApplianceRFProfilesResource) Update(ctx context.Context, req resource.U
321322
actions = append(actions, meraki.ActionModel{
322323
Operation: "create",
323324
Resource: plan.getPath(),
324-
Body: item.toBody(ctx, ResourceApplianceRFProfilesItems{}),
325+
Body: plan.Items[i].toBody(ctx, ResourceApplianceRFProfilesItems{}),
325326
})
327+
newIdIndexes = append(newIdIndexes, i)
326328
}
327329
}
328330

@@ -331,6 +333,13 @@ func (r *ApplianceRFProfilesResource) Update(ctx context.Context, req resource.U
331333
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure objects (Action Batch), got error: %s, %s", err, res.String()))
332334
return
333335
}
336+
responseIndex := 0
337+
for i := range plan.Items {
338+
if slices.Contains(newIdIndexes, i) {
339+
plan.Items[i].Id = types.StringValue(res.Get("status.createdResources." + strconv.Itoa(responseIndex) + ".id").String())
340+
responseIndex++
341+
}
342+
}
334343

335344
tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString()))
336345

internal/provider/resource_meraki_appliance_ssids.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,19 @@ func (r *ApplianceSSIDsResource) Update(ctx context.Context, req resource.Update
279279

280280
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString()))
281281
var actions []meraki.ActionModel
282-
283282
// Check for new and updated items
284-
for _, item := range plan.Items {
283+
for i := range plan.Items {
285284
found := false
286285
for _, itemState := range state.Items {
287-
if item.Number.ValueString() == itemState.Number.ValueString() {
286+
if plan.Items[i].Number.ValueString() == itemState.Number.ValueString() {
288287
found = true
289288
// If the item is present in both plan and state, we need to check if it has changes
290-
hasChanges := plan.hasChanges(ctx, &state, item.Number.ValueString())
289+
hasChanges := plan.hasChanges(ctx, &state, plan.Items[i].Number.ValueString())
291290
if hasChanges {
292291
actions = append(actions, meraki.ActionModel{
293292
Operation: "update",
294-
Resource: plan.getItemPath(item.Number.ValueString()),
295-
Body: item.toBody(ctx, itemState),
293+
Resource: plan.getItemPath(plan.Items[i].Number.ValueString()),
294+
Body: plan.Items[i].toBody(ctx, itemState),
296295
})
297296
}
298297
break
@@ -302,8 +301,8 @@ func (r *ApplianceSSIDsResource) Update(ctx context.Context, req resource.Update
302301
// If the item is present in plan, but not in state, we need to create it
303302
actions = append(actions, meraki.ActionModel{
304303
Operation: "update",
305-
Resource: plan.getItemPath(item.Number.ValueString()),
306-
Body: item.toBody(ctx, ResourceApplianceSSIDsItems{}),
304+
Resource: plan.getItemPath(plan.Items[i].Number.ValueString()),
305+
Body: plan.Items[i].toBody(ctx, ResourceApplianceSSIDsItems{}),
307306
})
308307
}
309308
}

internal/provider/resource_meraki_appliance_traffic_shaping_custom_performance_classes.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package provider
2121
import (
2222
"context"
2323
"fmt"
24+
"slices"
2425
"strconv"
2526
"strings"
2627

@@ -247,20 +248,20 @@ func (r *ApplianceTrafficShapingCustomPerformanceClassesResource) Update(ctx con
247248
})
248249
}
249250
}
250-
251+
newIdIndexes := make([]int, 0)
251252
// Check for new and updated items
252-
for _, item := range plan.Items {
253+
for i := range plan.Items {
253254
found := false
254255
for _, itemState := range state.Items {
255-
if item.Id.ValueString() == itemState.Id.ValueString() {
256+
if plan.Items[i].Id.ValueString() == itemState.Id.ValueString() {
256257
found = true
257258
// If the item is present in both plan and state, we need to check if it has changes
258-
hasChanges := plan.hasChanges(ctx, &state, item.Id.ValueString())
259+
hasChanges := plan.hasChanges(ctx, &state, plan.Items[i].Id.ValueString())
259260
if hasChanges {
260261
actions = append(actions, meraki.ActionModel{
261262
Operation: "update",
262-
Resource: plan.getPath() + "/" + item.Id.ValueString(),
263-
Body: item.toBody(ctx, itemState),
263+
Resource: plan.getPath() + "/" + plan.Items[i].Id.ValueString(),
264+
Body: plan.Items[i].toBody(ctx, itemState),
264265
})
265266
}
266267
break
@@ -271,8 +272,9 @@ func (r *ApplianceTrafficShapingCustomPerformanceClassesResource) Update(ctx con
271272
actions = append(actions, meraki.ActionModel{
272273
Operation: "create",
273274
Resource: plan.getPath(),
274-
Body: item.toBody(ctx, ResourceApplianceTrafficShapingCustomPerformanceClassesItems{}),
275+
Body: plan.Items[i].toBody(ctx, ResourceApplianceTrafficShapingCustomPerformanceClassesItems{}),
275276
})
277+
newIdIndexes = append(newIdIndexes, i)
276278
}
277279
}
278280

@@ -281,6 +283,13 @@ func (r *ApplianceTrafficShapingCustomPerformanceClassesResource) Update(ctx con
281283
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure objects (Action Batch), got error: %s, %s", err, res.String()))
282284
return
283285
}
286+
responseIndex := 0
287+
for i := range plan.Items {
288+
if slices.Contains(newIdIndexes, i) {
289+
plan.Items[i].Id = types.StringValue(res.Get("status.createdResources." + strconv.Itoa(responseIndex) + ".id").String())
290+
responseIndex++
291+
}
292+
}
284293

285294
tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString()))
286295

internal/provider/resource_meraki_appliance_vlans.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package provider
2121
import (
2222
"context"
2323
"fmt"
24+
"slices"
2425
"strconv"
2526
"strings"
2627

@@ -406,20 +407,20 @@ func (r *ApplianceVLANsResource) Update(ctx context.Context, req resource.Update
406407
})
407408
}
408409
}
409-
410+
newIdIndexes := make([]int, 0)
410411
// Check for new and updated items
411-
for _, item := range plan.Items {
412+
for i := range plan.Items {
412413
found := false
413414
for _, itemState := range state.Items {
414-
if item.Id.ValueString() == itemState.Id.ValueString() {
415+
if plan.Items[i].Id.ValueString() == itemState.Id.ValueString() {
415416
found = true
416417
// If the item is present in both plan and state, we need to check if it has changes
417-
hasChanges := plan.hasChanges(ctx, &state, item.Id.ValueString())
418+
hasChanges := plan.hasChanges(ctx, &state, plan.Items[i].Id.ValueString())
418419
if hasChanges {
419420
actions = append(actions, meraki.ActionModel{
420421
Operation: "update",
421-
Resource: plan.getPath() + "/" + item.Id.ValueString(),
422-
Body: item.toBody(ctx, itemState),
422+
Resource: plan.getPath() + "/" + plan.Items[i].Id.ValueString(),
423+
Body: plan.Items[i].toBody(ctx, itemState),
423424
})
424425
}
425426
break
@@ -430,8 +431,9 @@ func (r *ApplianceVLANsResource) Update(ctx context.Context, req resource.Update
430431
actions = append(actions, meraki.ActionModel{
431432
Operation: "create",
432433
Resource: plan.getPath(),
433-
Body: item.toBody(ctx, ResourceApplianceVLANsItems{}),
434+
Body: plan.Items[i].toBody(ctx, ResourceApplianceVLANsItems{}),
434435
})
436+
newIdIndexes = append(newIdIndexes, i)
435437
}
436438
}
437439

@@ -440,6 +442,13 @@ func (r *ApplianceVLANsResource) Update(ctx context.Context, req resource.Update
440442
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure objects (Action Batch), got error: %s, %s", err, res.String()))
441443
return
442444
}
445+
responseIndex := 0
446+
for i := range plan.Items {
447+
if slices.Contains(newIdIndexes, i) {
448+
plan.Items[i].Id = types.StringValue(res.Get("status.createdResources." + strconv.Itoa(responseIndex) + ".id").String())
449+
responseIndex++
450+
}
451+
}
443452

444453
tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString()))
445454

0 commit comments

Comments
 (0)