Skip to content

Commit f7921d5

Browse files
committed
inline back convertConfig
1 parent 4f0cca7 commit f7921d5

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

internal/convert/adv2v2.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,34 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
8080
blockb := block.Body()
8181
shardsAttr := blockb.GetAttribute(nNumShards)
8282
blockb.RemoveAttribute(nNumShards)
83-
if err := convertConfig(blockb, diskSizeGB); err != nil {
83+
84+
// Process config - inlined from convertConfig
85+
dConfig, err := getDynamicBlock(blockb, nConfig)
86+
if err != nil {
8487
return err
8588
}
89+
90+
if dConfig.IsPresent() {
91+
blockName := getResourceName(dConfig.block)
92+
transformReferences(dConfig.content.Body(), blockName, nRegion)
93+
copyAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes())
94+
processAllSpecs(dConfig.content.Body(), diskSizeGB)
95+
tokens := hcl.TokensFromExpr(buildForExpr(nRegion, hcl.GetAttrExpr(dConfig.forEach), false))
96+
tokens = append(tokens, hcl.TokensObject(dConfig.content.Body())...)
97+
blockb.RemoveBlock(dConfig.block)
98+
blockb.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(tokens))
99+
} else {
100+
var configs []*hclwrite.Body
101+
for _, configBlock := range collectBlocks(blockb, nConfig) {
102+
configBlockb := configBlock.Body()
103+
processAllSpecs(configBlockb, diskSizeGB)
104+
configs = append(configs, configBlockb)
105+
}
106+
if len(configs) == 0 {
107+
return fmt.Errorf("replication_specs must have at least one region_configs")
108+
}
109+
blockb.SetAttributeRaw(nConfig, hcl.TokensArray(configs))
110+
}
86111
if hasVariableShards {
87112
resultTokens = append(resultTokens, processNumShards(shardsAttr, blockb))
88113
continue
@@ -161,35 +186,6 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
161186
return dynamicBlock{tokens: hcl.TokensArraySingle(repSpecb)}, nil
162187
}
163188

164-
func convertConfig(repSpecs *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
165-
dConfig, err := getDynamicBlock(repSpecs, nConfig)
166-
if err != nil {
167-
return err
168-
}
169-
if dConfig.IsPresent() {
170-
blockName := getResourceName(dConfig.block)
171-
transformReferences(dConfig.content.Body(), blockName, nRegion)
172-
copyAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes())
173-
processAllSpecs(dConfig.content.Body(), diskSizeGB)
174-
tokens := hcl.TokensFromExpr(buildForExpr(nRegion, hcl.GetAttrExpr(dConfig.forEach), false))
175-
tokens = append(tokens, hcl.TokensObject(dConfig.content.Body())...)
176-
repSpecs.RemoveBlock(dConfig.block)
177-
repSpecs.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(tokens))
178-
return nil
179-
}
180-
var configs []*hclwrite.Body
181-
for _, block := range collectBlocks(repSpecs, nConfig) {
182-
blockb := block.Body()
183-
processAllSpecs(blockb, diskSizeGB)
184-
configs = append(configs, blockb)
185-
}
186-
if len(configs) == 0 {
187-
return fmt.Errorf("replication_specs must have at least one region_configs")
188-
}
189-
repSpecs.SetAttributeRaw(nConfig, hcl.TokensArray(configs))
190-
return nil
191-
}
192-
193189
// hasExpectedBlocksAsAttributes checks if any of the expected block names
194190
// exist as attributes in the resource body. In that case conversion is not done
195191
// as advanced cluster is not in a valid SDKv2 configuration.
@@ -217,9 +213,6 @@ func copyAttributesSorted(targetBody *hclwrite.Body, sourceAttrs map[string]*hcl
217213
}
218214
targetBody.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
219215
}
220-
for _, block := range targetBody.Blocks() {
221-
copyAttributesSorted(block.Body(), block.Body().Attributes(), transforms...)
222-
}
223216
}
224217

225218
func processAllSpecs(body *hclwrite.Body, diskSizeGB hclwrite.Tokens) {

internal/convert/shared.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,24 +208,19 @@ func extractTagsLabelsIndividual(resourceb *hclwrite.Body, name string) (hclwrit
208208
var (
209209
file = hclwrite.NewEmptyFile()
210210
fileb = file.Body()
211-
found = false
212211
)
213-
for {
214-
block := resourceb.FirstMatchingBlock(name, nil)
215-
if block == nil {
216-
break
217-
}
212+
blocks := collectBlocks(resourceb, name)
213+
if len(blocks) == 0 {
214+
return nil, nil
215+
}
216+
for _, block := range blocks {
218217
key := block.Body().GetAttribute(nKey)
219218
value := block.Body().GetAttribute(nValue)
220219
if key == nil || value == nil {
221220
return nil, fmt.Errorf("%s: %s or %s not found", name, nKey, nValue)
222221
}
223222
setKeyValue(fileb, key, value)
224223
resourceb.RemoveBlock(block)
225-
found = true
226-
}
227-
if !found {
228-
return nil, nil
229224
}
230225
return hcl.TokensObject(fileb), nil
231226
}

0 commit comments

Comments
 (0)