@@ -3,7 +3,6 @@ package convert
3
3
import (
4
4
"fmt"
5
5
"slices"
6
- "strings"
7
6
8
7
"github.com/hashicorp/hcl/v2/hclwrite"
9
8
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl"
@@ -275,21 +274,10 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
275
274
276
275
// Build the expression using HCL functions
277
276
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
- }
282
277
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 ()
293
281
294
282
// Add all attributes generically in alphabetical order
295
283
attrs := dConfig .content .Body ().Attributes ()
@@ -301,13 +289,16 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
301
289
302
290
for _ , name := range attrNames {
303
291
attr := attrs [name ]
304
- exprStr . WriteString ( fmt . Sprintf ( " %s = %s \n " , name , hcl .GetAttrExpr (attr )))
292
+ regionConfigBody . SetAttributeRaw ( name , hcl . TokensFromExpr ( hcl .GetAttrExpr (attr )))
305
293
}
306
294
307
295
// Add all blocks generically as objects
308
296
for _ , block := range dConfig .content .Body ().Blocks () {
309
297
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 ()
311
302
312
303
// Add block attributes in alphabetical order
313
304
blockAttrs := block .Body ().Attributes ()
@@ -319,18 +310,40 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
319
310
320
311
for _ , attrName := range blockAttrNames {
321
312
attr := blockAttrs [attrName ]
322
- exprStr . WriteString ( fmt . Sprintf ( " %s = %s \n " , attrName , hcl .GetAttrExpr (attr )))
313
+ blockBody . SetAttributeRaw ( attrName , hcl . TokensFromExpr ( hcl .GetAttrExpr (attr )))
323
314
}
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 ))
325
331
}
326
332
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 )... )
332
344
333
- tokens := hcl .TokensFromExpr (exprStr .String ())
345
+ // Apply flatten to the entire expression
346
+ tokens := hcl .TokensFuncFlatten (outerTokens )
334
347
335
348
resourceb .RemoveBlock (dSpec .block )
336
349
resourceb .SetAttributeRaw (nRepSpecs , tokens )
0 commit comments