@@ -226,9 +226,9 @@ func convertToToolVersionArchitectures(ctx context.Context, archs types.Set) ([]
226
226
return result , nil
227
227
}
228
228
229
- // PreserveAMD64ArchsOnURLChange creates a plan modifier that preserves AMD64 architecture entries
229
+ // PreserveAMD64ArchsOnChange creates a plan modifier that preserves AMD64 architecture entries
230
230
// when top-level URL or SHA changes, to be used across all tool version resources
231
- func PreserveAMD64ArchsOnURLChange () planmodifier.Set {
231
+ func PreserveAMD64ArchsOnChange () planmodifier.Set {
232
232
return & preserveAMD64ArchsModifier {}
233
233
}
234
234
@@ -248,7 +248,7 @@ func (m *preserveAMD64ArchsModifier) MarkdownDescription(ctx context.Context) st
248
248
// PlanModifySet modifies the plan to ensure AMD64 architecture entries are preserved
249
249
func (m * preserveAMD64ArchsModifier ) PlanModifySet (ctx context.Context , req planmodifier.SetRequest , resp * planmodifier.SetResponse ) {
250
250
// Skip if we're destroying the resource or no state
251
- if req .Plan .Raw .IsNull () || req .State .Raw .IsNull () {
251
+ if req .Plan .Raw .IsNull () || req .State .Raw .IsNull () || req . StateValue . IsNull () {
252
252
return
253
253
}
254
254
@@ -272,76 +272,14 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
272
272
stateArchs := req .StateValue
273
273
planArchs := req .PlanValue
274
274
275
- // If no state archs, nothing to preserve
276
- if stateArchs .IsNull () {
277
- return
278
- }
279
-
280
- var configArchsList []ToolArchitecture
281
- configArchs := req .ConfigValue
282
- configHasArchs := ! configArchs .IsNull () && ! configArchs .IsUnknown ()
283
- if configHasArchs {
284
- diags := configArchs .ElementsAs (ctx , & configArchsList , false )
285
- if diags .HasError () {
286
- tflog .Debug (ctx , "Error extracting config architectures" , map [string ]interface {}{
287
- "diagnostics" : diags ,
288
- })
289
- return
290
- }
291
- }
292
-
293
- // IMPORTANT: Check if AMD64 was intentionally removed in config
294
- if configHasArchs {
295
- var configArchsList []ToolArchitecture
296
- diags := configArchs .ElementsAs (ctx , & configArchsList , false )
297
- if diags .HasError () {
298
- tflog .Debug (ctx , "Error extracting config architectures" , map [string ]interface {}{
299
- "diagnostics" : diags ,
300
- })
301
- return
302
- }
303
-
304
- // Check each arch in config to see if AMD64 is there
305
- foundAMD64 := false
306
- for _ , arch := range configArchsList {
307
- if arch .Arch .ValueString () == "amd64" {
308
- foundAMD64 = true
309
- break
310
- }
311
- }
312
-
313
- // If AMD64 is explicitly NOT in config, don't add it back
314
- if ! foundAMD64 {
315
- tflog .Debug (ctx , "AMD64 arch explicitly removed in config, not preserving" )
316
- return
317
- }
318
- }
319
-
320
-
321
275
// Extract archs from state
322
276
var stateArchsList []ToolArchitecture
323
277
diags := stateArchs .ElementsAs (ctx , & stateArchsList , false )
324
278
if diags .HasError () {
325
279
return
326
280
}
327
281
328
- // Extract AMD64 arch from state
329
- var amd64Arch * ToolArchitecture
330
- for _ , arch := range stateArchsList {
331
- if arch .Arch .ValueString () == "amd64" {
332
- tmpArch := arch
333
- amd64Arch = & tmpArch
334
- break
335
- }
336
- }
337
-
338
- // If no AMD64 in state, nothing to preserve
339
- if amd64Arch == nil {
340
- return
341
- }
342
-
343
- // If we got here, we need to preserve AMD64
344
- // Add the AMD64 architecture from the state to the plan if it's not already present
282
+ // we need to update the plan amd url and sha to match the top level values
345
283
var planArchsList []ToolArchitecture
346
284
diags = planArchs .ElementsAs (ctx , & planArchsList , false )
347
285
if diags .HasError () {
@@ -352,39 +290,38 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
352
290
}
353
291
354
292
// Check if AMD64 is already in the plan
355
- foundAMD64 := false
356
- for _ , arch := range planArchsList {
293
+ for i , arch := range planArchsList {
357
294
if arch .Arch .ValueString () == "amd64" {
358
- foundAMD64 = true
359
- break
360
- }
361
- }
362
-
363
- // If AMD64 is not in the plan, add it
364
- if ! foundAMD64 {
365
- planArchsList = append (planArchsList , * amd64Arch )
366
- newPlanArchs := ToolArchitecturesToSet (planArchsList )
367
- resp .PlanValue = newPlanArchs
368
- }
369
- }
370
-
371
- func ToolArchitecturesToSet (archs []ToolArchitecture ) types.Set {
372
- archObjectType := ObjectTypeForArchitectures ()
373
- attrValues := make ([]attr.Value , len (archs ))
295
+ // If URL or SHA is changing, update the AMD64 arch to match
296
+ if urlChanged {
297
+ arch .URL = planURL
298
+ }
299
+ if shaChanged {
300
+ arch .Sha = planSHA
301
+ }
302
+ // Update the plan architecture list with the modified AMD64 arch
303
+ planArchsList [i ] = arch
304
+
305
+ // Update the plan with the modified AMD64 arch
306
+ archObjectType := ObjectTypeForArchitectures ()
307
+ attrValues := make ([]attr.Value , len (planArchsList ))
308
+
309
+ for i , arch := range planArchsList {
310
+ attrValues [i ] = types .ObjectValueMust (
311
+ archObjectType .AttrTypes ,
312
+ map [string ]attr.Value {
313
+ "url" : arch .URL ,
314
+ "sha" : arch .Sha ,
315
+ "os" : arch .OS ,
316
+ "arch" : arch .Arch ,
317
+ },
318
+ )
319
+ }
374
320
375
- for i , arch := range archs {
376
- attrValues [i ] = types .ObjectValueMust (
377
- archObjectType .AttrTypes ,
378
- map [string ]attr.Value {
379
- "url" : arch .URL ,
380
- "sha" : arch .Sha ,
381
- "os" : arch .OS ,
382
- "arch" : arch .Arch ,
383
- },
384
- )
321
+ resp .PlanValue = types .SetValueMust (archObjectType , attrValues )
322
+ return
323
+ }
385
324
}
386
-
387
- return types .SetValueMust (archObjectType , attrValues )
388
325
}
389
326
390
327
// ValidateToolVersion provides common validation for tool version resources
0 commit comments