@@ -48,7 +48,8 @@ type attrVals struct {
48
48
// ClusterToAdvancedCluster transforms all mongodbatlas_cluster definitions in a
49
49
// Terraform configuration file into mongodbatlas_advanced_cluster schema 2.0.0.
50
50
// All other resources and data sources are left untouched.
51
- // Note: hclwrite.Tokens are used instead of cty.Value so expressions with interpolations like var.region can be preserved.
51
+ // Note: hclwrite.Tokens are used instead of cty.Value so expressions with
52
+ // interpolations like var.region can be preserved.
52
53
// cty.Value only supports literal expressions.
53
54
func ClusterToAdvancedCluster (config []byte , includeMoved bool ) ([]byte , error ) {
54
55
var moveLabels []string
@@ -216,7 +217,7 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
216
217
break
217
218
}
218
219
specbSrc := specSrc .Body ()
219
- d , err := fillReplicationSpecsWithDynamicRegionConfigs (specbSrc , root , false )
220
+ d , err := fillWithDynamicRegionConfigs (specbSrc , root , false )
220
221
if err != nil {
221
222
return err
222
223
}
@@ -333,7 +334,7 @@ func fillReplicationSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVal
333
334
return dynamicBlock {}, err
334
335
}
335
336
transformDynamicBlockReferences (dSpec .content .Body (), nRepSpecs , nSpec )
336
- dConfig , err := fillReplicationSpecsWithDynamicRegionConfigs (dSpec .content .Body (), root , true )
337
+ dConfig , err := fillWithDynamicRegionConfigs (dSpec .content .Body (), root , true )
337
338
if err != nil {
338
339
return dynamicBlock {}, err
339
340
}
@@ -344,8 +345,8 @@ func fillReplicationSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVal
344
345
return dSpec , nil
345
346
}
346
347
347
- // fillReplicationSpecsWithDynamicRegionConfigs is used for dynamic blocks in region_configs
348
- func fillReplicationSpecsWithDynamicRegionConfigs (specbSrc * hclwrite.Body , root attrVals , transformRegionReferences bool ) (dynamicBlock , error ) {
348
+ // fillWithDynamicRegionConfigs is used for dynamic blocks in region_configs
349
+ func fillWithDynamicRegionConfigs (specbSrc * hclwrite.Body , root attrVals , changeReferences bool ) (dynamicBlock , error ) {
349
350
d , err := getDynamicBlock (specbSrc , nConfigSrc )
350
351
if err != nil || ! d .IsPresent () {
351
352
return dynamicBlock {}, err
@@ -356,15 +357,16 @@ func fillReplicationSpecsWithDynamicRegionConfigs(specbSrc *hclwrite.Body, root
356
357
repSpecb .SetAttributeRaw (nZoneName , hcl .TokensFromExpr (zoneName ))
357
358
}
358
359
forEach := hcl .GetAttrExpr (d .forEach )
359
- if transformRegionReferences {
360
+ if changeReferences {
360
361
forEach = replaceDynamicBlockReferences (forEach , nRepSpecs , nSpec )
361
362
}
362
- regionFor , err := getDynamicBlockRegionConfigsRegionArray (forEach , d .content , root )
363
+ regionFor , err := getDynamicBlockRegionArray (forEach , d .content , root )
363
364
if err != nil {
364
365
return dynamicBlock {}, err
365
366
}
367
+ priorityForStr := fmt .Sprintf ("for %s in range(%d, %d, -1) : " , nPriority , valMaxPriority , valMinPriority )
366
368
priorityFor := hcl .TokensComment (commentPriorityFor )
367
- priorityFor = append (priorityFor , hcl .TokensFromExpr (fmt . Sprintf ( "for %s in range(%d, %d, -1) : " , nPriority , valMaxPriority , valMinPriority ) )... )
369
+ priorityFor = append (priorityFor , hcl .TokensFromExpr (priorityForStr )... )
368
370
priorityFor = append (priorityFor , regionFor ... )
369
371
repSpecb .SetAttributeRaw (nConfig , hcl .TokensFuncFlatten (priorityFor ))
370
372
@@ -410,13 +412,13 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
410
412
if err := hcl .MoveAttr (configSrc .Body (), fileb , nPriority , nPriority , errRepSpecs ); err != nil {
411
413
return nil , err
412
414
}
413
- if electable , _ := getSpecs (configSrc , nElectableNodes , root , isDynamicBlock ); electable != nil {
415
+ if electable , _ := getSpec (configSrc , nElectableNodes , root , isDynamicBlock ); electable != nil {
414
416
fileb .SetAttributeRaw (nElectableSpecs , electable )
415
417
}
416
- if readOnly , _ := getSpecs (configSrc , nReadOnlyNodes , root , isDynamicBlock ); readOnly != nil {
418
+ if readOnly , _ := getSpec (configSrc , nReadOnlyNodes , root , isDynamicBlock ); readOnly != nil {
417
419
fileb .SetAttributeRaw (nReadOnlySpecs , readOnly )
418
420
}
419
- if analytics , _ := getSpecs (configSrc , nAnalyticsNodes , root , isDynamicBlock ); analytics != nil {
421
+ if analytics , _ := getSpec (configSrc , nAnalyticsNodes , root , isDynamicBlock ); analytics != nil {
420
422
fileb .SetAttributeRaw (nAnalyticsSpecs , analytics )
421
423
}
422
424
if autoScaling := getAutoScalingOpt (root .opt ); autoScaling != nil {
@@ -425,7 +427,7 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
425
427
return file , nil
426
428
}
427
429
428
- func getSpecs (configSrc * hclwrite.Block , countName string , root attrVals , isDynamicBlock bool ) (hclwrite.Tokens , error ) {
430
+ func getSpec (configSrc * hclwrite.Block , countName string , root attrVals , isDynamicBlock bool ) (hclwrite.Tokens , error ) {
429
431
var (
430
432
file = hclwrite .NewEmptyFile ()
431
433
fileb = file .Body ()
@@ -556,9 +558,9 @@ func replaceDynamicBlockExpr(attr *hclwrite.Attribute, blockName, attrName strin
556
558
return strings .ReplaceAll (expr , fmt .Sprintf ("%s.%s" , blockName , attrName ), attrName )
557
559
}
558
560
559
- // getDynamicBlockRegionConfigsRegionArray returns the region array for a dynamic block in replication_specs.
561
+ // getDynamicBlockRegionArray returns the region array for a dynamic block in replication_specs.
560
562
// e.g. [ for region in var.replication_specs.regions_config : { ... } if priority == region.priority ]
561
- func getDynamicBlockRegionConfigsRegionArray (forEach string , configSrc * hclwrite.Block , root attrVals ) (hclwrite.Tokens , error ) {
563
+ func getDynamicBlockRegionArray (forEach string , configSrc * hclwrite.Block , root attrVals ) (hclwrite.Tokens , error ) {
562
564
transformDynamicBlockReferences (configSrc .Body (), nConfigSrc , nRegion )
563
565
priorityStr := hcl .GetAttrExpr (configSrc .Body ().GetAttribute (nPriority ))
564
566
if priorityStr == "" {
@@ -581,7 +583,8 @@ func transformDynamicBlockReferences(configSrcb *hclwrite.Body, blockName, varNa
581
583
}
582
584
}
583
585
584
- // replaceDynamicBlockReferences changes value references, e.g. regions_config.value.electable_nodes to region.electable_nodes
586
+ // replaceDynamicBlockReferences changes value references,
587
+ // e.g. regions_config.value.electable_nodes to region.electable_nodes
585
588
func replaceDynamicBlockReferences (expr , blockName , varName string ) string {
586
589
return strings .ReplaceAll (expr ,
587
590
fmt .Sprintf ("%s.%s." , blockName , nValue ),
@@ -606,7 +609,8 @@ func setKeyValue(body *hclwrite.Body, key, value *hclwrite.Attribute) {
606
609
keyStr , err := hcl .GetAttrString (key , "" )
607
610
if err == nil {
608
611
if ! hclsyntax .ValidIdentifier (keyStr ) {
609
- keyStr = strconv .Quote (keyStr ) // wrap in quotes so invalid identifiers (e.g. with blanks) can be used as attribute names
612
+ // wrap in quotes so invalid identifiers (e.g. with blanks) can be used as attribute names
613
+ keyStr = strconv .Quote (keyStr )
610
614
}
611
615
} else {
612
616
keyStr = strings .TrimSpace (string (key .Expr ().BuildTokens (nil ).Bytes ()))
0 commit comments