Skip to content

Commit 07a68c4

Browse files
committed
simplify convertRepSpecs
1 parent 7b6afc3 commit 07a68c4

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

internal/convert/adv2v2.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,18 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
8080
blockb := block.Body()
8181
shardsAttr := blockb.GetAttribute(nNumShards)
8282
blockb.RemoveAttribute(nNumShards)
83-
84-
// Process config - inlined from convertConfig
8583
dConfig, err := getDynamicBlock(blockb, nConfig)
8684
if err != nil {
8785
return err
8886
}
89-
9087
if dConfig.IsPresent() {
91-
blockName := getResourceName(dConfig.block)
92-
transformReferences(dConfig.content.Body(), blockName, nRegion)
88+
transformReferences(dConfig.content.Body(), getResourceName(dConfig.block), nRegion)
9389
copyAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes())
9490
processAllSpecs(dConfig.content.Body(), diskSizeGB)
9591
tokens := hcl.TokensFromExpr(buildForExpr(nRegion, hcl.GetAttrExpr(dConfig.forEach), false))
9692
tokens = append(tokens, hcl.TokensObject(dConfig.content.Body())...)
97-
blockb.RemoveBlock(dConfig.block)
9893
blockb.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(tokens))
94+
blockb.RemoveBlock(dConfig.block)
9995
} else {
10096
var configs []*hclwrite.Body
10197
for _, configBlock := range collectBlocks(blockb, nConfig) {
@@ -149,33 +145,30 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
149145
return dynamicBlock{}, err
150146
}
151147
configBody := d.content.Body()
152-
repSpecb := hclwrite.NewEmptyFile().Body()
153-
if zoneNameAttr := specbSrc.GetAttribute(nZoneName); zoneNameAttr != nil {
154-
expr := transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
155-
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(expr))
156-
}
157-
configForEach := fmt.Sprintf("%s.%s", nSpec, nConfig)
158-
configBlockName := getResourceName(d.block)
159-
transformReferences(configBody, configBlockName, nRegion)
148+
transformReferences(configBody, getResourceName(d.block), nRegion)
160149
regionConfigBody := hclwrite.NewEmptyFile().Body()
161150
copyAttributesSorted(regionConfigBody, configBody.Attributes())
162151
for _, block := range configBody.Blocks() {
163152
blockType := block.Type()
164153
blockBody := hclwrite.NewEmptyFile().Body()
165154
copyAttributesSorted(blockBody, block.Body().Attributes())
166-
if diskSizeGB != nil && (blockType == nElectableSpecs ||
167-
blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
155+
if diskSizeGB != nil &&
156+
(blockType == nElectableSpecs || blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
168157
blockBody.SetAttributeRaw(nDiskSizeGB, diskSizeGB)
169158
}
170159
regionConfigBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
171160
}
172-
regionTokens := hcl.TokensFromExpr(buildForExpr(nRegion, configForEach, false))
161+
repSpecb := hclwrite.NewEmptyFile().Body()
162+
if zoneNameAttr := specbSrc.GetAttribute(nZoneName); zoneNameAttr != nil {
163+
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(
164+
transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)))
165+
}
166+
regionTokens := hcl.TokensFromExpr(buildForExpr(nRegion, fmt.Sprintf("%s.%s", nSpec, nConfig), false))
173167
regionTokens = append(regionTokens, hcl.TokensObject(regionConfigBody)...)
174-
regionConfig := hcl.EncloseBracketsNewLines(regionTokens)
175-
repSpecb.SetAttributeRaw(nConfig, regionConfig)
168+
repSpecb.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(regionTokens))
176169
if numShardsAttr := specbSrc.GetAttribute(nNumShards); numShardsAttr != nil {
177-
numShardsExpr := transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
178-
tokens := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr), false))
170+
tokens := hcl.TokensFromExpr(buildForExpr("i",
171+
fmt.Sprintf("range(%s)", transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)), false))
179172
tokens = append(tokens, hcl.TokensObject(repSpecb)...)
180173
return dynamicBlock{tokens: hcl.EncloseBracketsNewLines(tokens)}, nil
181174
}

0 commit comments

Comments
 (0)