@@ -3,7 +3,6 @@ package convert
33import (
44 "fmt"
55 "slices"
6- "strings"
76
87 "github.com/hashicorp/hcl/v2/hclwrite"
98 "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl"
@@ -275,21 +274,10 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
275274
276275 // Build the expression using HCL functions
277276 configForEach := replaceDynamicBlockReferences (hcl .GetAttrExpr (dConfig .forEach ), nRepSpecs , nSpec )
278- zoneNameExpr := ""
279- if zoneNameAttr := dSpec .content .Body ().GetAttribute (nZoneName ); zoneNameAttr != nil {
280- zoneNameExpr = replaceDynamicBlockReferences (hcl .GetAttrExpr (zoneNameAttr ), nRepSpecs , nSpec )
281- }
282277
283- // Build the nested expression string
284- var exprStr strings.Builder
285- exprStr .WriteString ("flatten([\n " )
286- exprStr .WriteString (fmt .Sprintf (" for %s in %s : [\n " , nSpec , hcl .GetAttrExpr (dSpec .forEach )))
287- exprStr .WriteString (fmt .Sprintf (" for i in range(%s) : {\n " , numShardsExpr ))
288- if zoneNameExpr != "" {
289- exprStr .WriteString (fmt .Sprintf (" zone_name = %s\n " , zoneNameExpr ))
290- }
291- exprStr .WriteString (" region_configs = [\n " )
292- exprStr .WriteString (fmt .Sprintf (" for %s in %s : {\n " , nRegion , configForEach ))
278+ // Create the inner region_configs body
279+ regionConfigFile := hclwrite .NewEmptyFile ()
280+ regionConfigBody := regionConfigFile .Body ()
293281
294282 // Add all attributes generically in alphabetical order
295283 attrs := dConfig .content .Body ().Attributes ()
@@ -301,13 +289,16 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
301289
302290 for _ , name := range attrNames {
303291 attr := attrs [name ]
304- exprStr . WriteString ( fmt . Sprintf ( " %s = %s \n " , name , hcl .GetAttrExpr (attr )))
292+ regionConfigBody . SetAttributeRaw ( name , hcl . TokensFromExpr ( hcl .GetAttrExpr (attr )))
305293 }
306294
307295 // Add all blocks generically as objects
308296 for _ , block := range dConfig .content .Body ().Blocks () {
309297 blockType := block .Type ()
310- exprStr .WriteString (fmt .Sprintf (" %s = {\n " , blockType ))
298+
299+ // Create a new body for the block
300+ blockFile := hclwrite .NewEmptyFile ()
301+ blockBody := blockFile .Body ()
311302
312303 // Add block attributes in alphabetical order
313304 blockAttrs := block .Body ().Attributes ()
@@ -319,18 +310,40 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
319310
320311 for _ , attrName := range blockAttrNames {
321312 attr := blockAttrs [attrName ]
322- exprStr . WriteString ( fmt . Sprintf ( " %s = %s \n " , attrName , hcl .GetAttrExpr (attr )))
313+ blockBody . SetAttributeRaw ( attrName , hcl . TokensFromExpr ( hcl .GetAttrExpr (attr )))
323314 }
324- exprStr .WriteString (" }\n " )
315+
316+ regionConfigBody .SetAttributeRaw (blockType , hcl .TokensObject (blockBody ))
317+ }
318+
319+ // Build the region_configs for expression
320+ regionForExpr := fmt .Sprintf ("for %s in %s :" , nRegion , configForEach )
321+ regionTokens := hcl .TokensFromExpr (regionForExpr )
322+ regionTokens = append (regionTokens , hcl .TokensObject (regionConfigBody )... )
323+
324+ // Create the replication spec body
325+ repSpecFile := hclwrite .NewEmptyFile ()
326+ repSpecBody := repSpecFile .Body ()
327+
328+ if zoneNameAttr := dSpec .content .Body ().GetAttribute (nZoneName ); zoneNameAttr != nil {
329+ zoneNameExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (zoneNameAttr ), nRepSpecs , nSpec )
330+ repSpecBody .SetAttributeRaw (nZoneName , hcl .TokensFromExpr (zoneNameExpr ))
325331 }
326332
327- exprStr .WriteString (" }\n " )
328- exprStr .WriteString (" ]\n " )
329- exprStr .WriteString (" }\n " )
330- exprStr .WriteString (" ]\n " )
331- exprStr .WriteString (" ])" )
333+ repSpecBody .SetAttributeRaw (nConfig , hcl .EncloseBracketsNewLines (regionTokens ))
334+
335+ // Build the inner for expression with range
336+ innerForExpr := fmt .Sprintf ("for i in range(%s) :" , numShardsExpr )
337+ innerTokens := hcl .TokensFromExpr (innerForExpr )
338+ innerTokens = append (innerTokens , hcl .TokensObject (repSpecBody )... )
339+
340+ // Build the outer for expression
341+ outerForExpr := fmt .Sprintf ("for %s in %s : " , nSpec , hcl .GetAttrExpr (dSpec .forEach ))
342+ outerTokens := hcl .TokensFromExpr (outerForExpr )
343+ outerTokens = append (outerTokens , hcl .EncloseBracketsNewLines (innerTokens )... )
332344
333- tokens := hcl .TokensFromExpr (exprStr .String ())
345+ // Apply flatten to the entire expression
346+ tokens := hcl .TokensFuncFlatten (outerTokens )
334347
335348 resourceb .RemoveBlock (dSpec .block )
336349 resourceb .SetAttributeRaw (nRepSpecs , tokens )
0 commit comments