Skip to content

Commit 0b66033

Browse files
committed
refactor convertConfigsWithDynamicBlock
1 parent bc83bc6 commit 0b66033

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

internal/convert/adv2v2.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,14 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
8080
blockb := block.Body()
8181
shardsAttr := blockb.GetAttribute(nNumShards)
8282
blockb.RemoveAttribute(nNumShards)
83-
dConfig, err := getDynamicBlock(blockb, nConfig)
83+
dConfig, err := convertConfigsWithDynamicBlock(blockb, diskSizeGB, false)
8484
if err != nil {
8585
return err
8686
}
8787
if dConfig.IsPresent() {
88-
if len(collectBlocks(blockb, nConfig)) > 0 {
89-
return errDynamicBlockAlone
90-
}
91-
transformReferences(dConfig.content.Body(), getResourceName(dConfig.block), nRegion)
92-
copyAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes())
93-
processAllSpecs(dConfig.content.Body(), diskSizeGB)
94-
tokens := hcl.TokensFromExpr(buildForExpr(nRegion, hcl.GetAttrExpr(dConfig.forEach), false))
95-
tokens = append(tokens, hcl.TokensObject(dConfig.content.Body())...)
96-
blockb.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(tokens))
88+
// Process the dynamic config and set it on this block
9789
blockb.RemoveBlock(dConfig.block)
90+
blockb.SetAttributeRaw(nConfig, dConfig.tokens)
9891
} else {
9992
var configs []*hclwrite.Body
10093
for _, configBlock := range collectBlocks(blockb, nConfig) {
@@ -136,7 +129,7 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
136129
return dynamicBlock{}, errDynamicBlockAlone
137130
}
138131
transformReferences(dSpec.content.Body(), nRepSpecs, nSpec)
139-
dConfig, err := convertConfigsWithDynamicBlock(dSpec.content.Body(), diskSizeGB)
132+
dConfig, err := convertConfigsWithDynamicBlock(dSpec.content.Body(), diskSizeGB, true)
140133
if err != nil {
141134
return dynamicBlock{}, err
142135
}
@@ -145,9 +138,11 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
145138
return dSpec, nil
146139
}
147140

148-
func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite.Tokens) (dynamicBlock, error) {
141+
// convertConfigsWithDynamicBlock is used for processing dynamic blocks in region_configs
142+
func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite.Tokens,
143+
insideDynamicRepSpec bool) (dynamicBlock, error) {
149144
d, err := getDynamicBlock(specbSrc, nConfig)
150-
if err != nil {
145+
if err != nil || !d.IsPresent() {
151146
return dynamicBlock{}, err
152147
}
153148
if len(collectBlocks(specbSrc, nConfig)) > 0 {
@@ -167,17 +162,25 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
167162
}
168163
regionConfigBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
169164
}
165+
forEach := hcl.GetAttrExpr(d.forEach)
166+
if insideDynamicRepSpec {
167+
forEach = fmt.Sprintf("%s.%s", nSpec, nConfig)
168+
}
169+
regionTokens := hcl.TokensFromExpr(buildForExpr(nRegion, forEach, false))
170+
regionTokens = append(regionTokens, hcl.TokensObject(regionConfigBody)...)
171+
if !insideDynamicRepSpec {
172+
d.tokens = hcl.EncloseBracketsNewLines(regionTokens)
173+
return d, nil
174+
}
170175
repSpecb := hclwrite.NewEmptyFile().Body()
171176
if zoneNameAttr := specbSrc.GetAttribute(nZoneName); zoneNameAttr != nil {
172-
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(
173-
transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)))
177+
zoneNameExpr := transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
178+
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(zoneNameExpr))
174179
}
175-
regionTokens := hcl.TokensFromExpr(buildForExpr(nRegion, fmt.Sprintf("%s.%s", nSpec, nConfig), false))
176-
regionTokens = append(regionTokens, hcl.TokensObject(regionConfigBody)...)
177180
repSpecb.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(regionTokens))
178181
if numShardsAttr := specbSrc.GetAttribute(nNumShards); numShardsAttr != nil {
179-
tokens := hcl.TokensFromExpr(buildForExpr("i",
180-
fmt.Sprintf("range(%s)", transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)), false))
182+
numShardsExpr := transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
183+
tokens := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr), false))
181184
tokens = append(tokens, hcl.TokensObject(repSpecb)...)
182185
return dynamicBlock{tokens: hcl.EncloseBracketsNewLines(tokens)}, nil
183186
}

0 commit comments

Comments
 (0)