Skip to content

Commit 4609386

Browse files
committed
use hcl.TokensFuncFlatten
1 parent 751a804 commit 4609386

File tree

2 files changed

+45
-32
lines changed

2 files changed

+45
-32
lines changed

internal/convert/adv2v2.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package convert
33
import (
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)

internal/convert/testdata/adv2v2/dynamic_replication_specs_basic.out.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
resource "mongodbatlas_advanced_cluster" "dynamic_replication_specs" {
2-
project_id = var.project_id
3-
name = var.cluster_name
4-
cluster_type = "GEOSHARDED"
2+
project_id = var.project_id
3+
name = var.cluster_name
4+
cluster_type = "GEOSHARDED"
55
replication_specs = flatten([
66
for spec in var.replication_specs : [
77
for i in range(spec.num_shards) : {
88
zone_name = spec.zone_name
99
region_configs = [
1010
for region in spec.region_configs : {
11-
priority = region.priority
11+
priority = region.priority
1212
provider_name = region.provider_name
13-
region_name = region.region_name
13+
region_name = region.region_name
1414
electable_specs = {
1515
instance_size = region.instance_size
16-
node_count = region.electable_node_count
16+
node_count = region.electable_node_count
1717
}
1818
read_only_specs = {
1919
instance_size = region.instance_size
20-
node_count = region.read_only_node_count
20+
node_count = region.read_only_node_count
2121
}
2222
}
2323
]

0 commit comments

Comments
 (0)