@@ -200,51 +200,25 @@ func convertDynamicRepSpecs(resourceb *hclwrite.Body, dSpec dynamicBlock, diskSi
200
200
return nil
201
201
}
202
202
203
- // Helper function to add attributes in order
204
- func addAttributesInOrder (targetBody * hclwrite.Body , sourceAttrs map [string ]* hclwrite.Attribute ,
205
- orderedNames []string ) {
206
- // Add ordered attributes first
207
- for _ , name := range orderedNames {
208
- if attr , exists := sourceAttrs [name ]; exists {
209
- targetBody .SetAttributeRaw (name , hcl .TokensFromExpr (hcl .GetAttrExpr (attr )))
210
- }
211
- }
212
-
213
- // Add any remaining attributes alphabetically
214
- var remainingAttrs []string
215
- for name := range sourceAttrs {
216
- found := false
217
- for _ , orderedName := range orderedNames {
218
- if name == orderedName {
219
- found = true
220
- break
221
- }
222
- }
223
- if ! found {
224
- remainingAttrs = append (remainingAttrs , name )
225
- }
226
- }
227
- slices .Sort (remainingAttrs )
228
- for _ , name := range remainingAttrs {
229
- attr := sourceAttrs [name ]
230
- targetBody .SetAttributeRaw (name , hcl .TokensFromExpr (hcl .GetAttrExpr (attr )))
231
- }
232
- }
233
-
234
203
// Helper function to process blocks for region configs
235
204
func processRegionConfigBlocks (targetBody * hclwrite.Body , blocks []* hclwrite.Block ) {
236
205
for _ , block := range blocks {
237
206
blockType := block .Type ()
238
207
blockFile := hclwrite .NewEmptyFile ()
239
208
blockBody := blockFile .Body ()
240
209
241
- // For electable_specs, use specific order: instance_size, node_count
242
- var attrOrder []string
243
- if blockType == "electable_specs" {
244
- attrOrder = []string {"instance_size" , "node_count" }
210
+ // Copy all attributes in deterministic order
211
+ attrs := block .Body ().Attributes ()
212
+ var names []string
213
+ for name := range attrs {
214
+ names = append (names , name )
215
+ }
216
+ slices .Sort (names )
217
+ for _ , name := range names {
218
+ attr := attrs [name ]
219
+ blockBody .SetAttributeRaw (name , hcl .TokensFromExpr (hcl .GetAttrExpr (attr )))
245
220
}
246
221
247
- addAttributesInOrder (blockBody , block .Body ().Attributes (), attrOrder )
248
222
targetBody .SetAttributeRaw (blockType , hcl .TokensObject (blockBody ))
249
223
}
250
224
}
@@ -261,14 +235,28 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
261
235
262
236
// Transform references in place for the dynamic config content
263
237
transformDynamicBlockReferencesRecursive (dConfig .content .Body (), configBlockName , nRegion )
264
- // Also transform outer references
265
- for name , attr := range dConfig .content .Body ().Attributes () {
238
+ // Also transform outer references (with deterministic ordering)
239
+ attrs := dConfig .content .Body ().Attributes ()
240
+ var attrNames []string
241
+ for name := range attrs {
242
+ attrNames = append (attrNames , name )
243
+ }
244
+ slices .Sort (attrNames )
245
+ for _ , name := range attrNames {
246
+ attr := attrs [name ]
266
247
expr := hcl .GetAttrExpr (attr )
267
248
expr = replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
268
249
dConfig .content .Body ().SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
269
250
}
270
251
for _ , block := range dConfig .content .Body ().Blocks () {
271
- for name , attr := range block .Body ().Attributes () {
252
+ blockAttrs := block .Body ().Attributes ()
253
+ var blockAttrNames []string
254
+ for name := range blockAttrs {
255
+ blockAttrNames = append (blockAttrNames , name )
256
+ }
257
+ slices .Sort (blockAttrNames )
258
+ for _ , name := range blockAttrNames {
259
+ attr := blockAttrs [name ]
272
260
expr := hcl .GetAttrExpr (attr )
273
261
expr = replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
274
262
block .Body ().SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
@@ -283,9 +271,17 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
283
271
regionConfigFile := hclwrite .NewEmptyFile ()
284
272
regionConfigBody := regionConfigFile .Body ()
285
273
286
- // Add attributes in a specific order for consistency
287
- orderedAttrs := []string {"priority" , "provider_name" , "region_name" }
288
- addAttributesInOrder (regionConfigBody , dConfig .content .Body ().Attributes (), orderedAttrs )
274
+ // Copy all attributes in deterministic order
275
+ configAttrs := dConfig .content .Body ().Attributes ()
276
+ var configAttrNames []string
277
+ for name := range configAttrs {
278
+ configAttrNames = append (configAttrNames , name )
279
+ }
280
+ slices .Sort (configAttrNames )
281
+ for _ , name := range configAttrNames {
282
+ attr := configAttrs [name ]
283
+ regionConfigBody .SetAttributeRaw (name , hcl .TokensFromExpr (hcl .GetAttrExpr (attr )))
284
+ }
289
285
290
286
// Add all blocks generically as objects
291
287
processRegionConfigBlocks (regionConfigBody , dConfig .content .Body ().Blocks ())
@@ -329,36 +325,21 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
329
325
330
326
// Helper function to add attributes with transformation
331
327
func addAttributesWithTransform (targetBody * hclwrite.Body , sourceAttrs map [string ]* hclwrite.Attribute ,
332
- orderedNames []string , configBlockName string , transformFunc func (string ) string ) {
333
- // Add ordered attributes first
334
- for _ , name := range orderedNames {
335
- if attr , exists := sourceAttrs [name ]; exists {
336
- expr := hcl .GetAttrExpr (attr )
337
- // First replace nested dynamic block references
338
- expr = replaceDynamicBlockReferences (expr , configBlockName , nRegion )
339
- // Then replace outer dynamic block references
340
- expr = replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
341
- targetBody .SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
342
- }
328
+ configBlockName string ) {
329
+ // Sort attribute names for deterministic output
330
+ var names []string
331
+ for name := range sourceAttrs {
332
+ names = append (names , name )
343
333
}
344
-
345
- // Add any remaining attributes
346
- for name , attr := range sourceAttrs {
347
- found := false
348
- for _ , orderedName := range orderedNames {
349
- if name == orderedName {
350
- found = true
351
- break
352
- }
353
- }
354
- if ! found {
355
- expr := hcl .GetAttrExpr (attr )
356
- // First replace nested dynamic block references
357
- expr = replaceDynamicBlockReferences (expr , configBlockName , nRegion )
358
- // Then replace outer dynamic block references
359
- expr = replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
360
- targetBody .SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
361
- }
334
+ slices .Sort (names )
335
+ for _ , name := range names {
336
+ attr := sourceAttrs [name ]
337
+ expr := hcl .GetAttrExpr (attr )
338
+ // First replace nested dynamic block references
339
+ expr = replaceDynamicBlockReferences (expr , configBlockName , nRegion )
340
+ // Then replace outer dynamic block references
341
+ expr = replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
342
+ targetBody .SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
362
343
}
363
344
}
364
345
@@ -377,22 +358,14 @@ func convertDynamicRepSpecsWithoutNumShards(resourceb *hclwrite.Body, dSpec, dCo
377
358
configFile := hclwrite .NewEmptyFile ()
378
359
configb := configFile .Body ()
379
360
380
- // Copy and transform attributes in a specific order for consistency
381
- orderedAttrs := []string {"priority" , "provider_name" , "region_name" }
382
- addAttributesWithTransform (configb , dConfig .content .Body ().Attributes (), orderedAttrs , configBlockName , nil )
361
+ // Copy and transform attributes
362
+ addAttributesWithTransform (configb , dConfig .content .Body ().Attributes (), configBlockName )
383
363
384
364
// Process blocks and transform their references
385
365
for _ , block := range dConfig .content .Body ().Blocks () {
386
366
newBlock := configb .AppendNewBlock (block .Type (), block .Labels ())
387
367
newBlockb := newBlock .Body ()
388
-
389
- // Order attributes for consistency
390
- var attrOrder []string
391
- if block .Type () == "electable_specs" {
392
- attrOrder = []string {"instance_size" , "node_count" }
393
- }
394
-
395
- addAttributesWithTransform (newBlockb , block .Body ().Attributes (), attrOrder , configBlockName , nil )
368
+ addAttributesWithTransform (newBlockb , block .Body ().Attributes (), configBlockName )
396
369
}
397
370
398
371
// Process specs
@@ -494,8 +467,15 @@ func convertDynamicConfig(repSpecs *hclwrite.Body, dConfig dynamicBlock, diskSiz
494
467
}
495
468
496
469
func transformDynamicBlockReferencesRecursive (body * hclwrite.Body , blockName , varName string ) {
497
- // Transform attributes
498
- for name , attr := range body .Attributes () {
470
+ // Transform attributes in deterministic order
471
+ attrs := body .Attributes ()
472
+ var names []string
473
+ for name := range attrs {
474
+ names = append (names , name )
475
+ }
476
+ slices .Sort (names )
477
+ for _ , name := range names {
478
+ attr := attrs [name ]
499
479
expr := replaceDynamicBlockReferences (hcl .GetAttrExpr (attr ), blockName , varName )
500
480
body .SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
501
481
}
0 commit comments