Skip to content

Commit 4d843b9

Browse files
committed
collectBlocks
1 parent a425f53 commit 4d843b9

File tree

3 files changed

+32
-37
lines changed

3 files changed

+32
-37
lines changed

internal/convert/adv2v2.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,7 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
6868
}
6969

7070
// Collect all replication_specs blocks first
71-
var repSpecBlocks []*hclwrite.Block
72-
for {
73-
block := resourceb.FirstMatchingBlock(nRepSpecs, nil)
74-
if block == nil {
75-
break
76-
}
77-
resourceb.RemoveBlock(block)
78-
repSpecBlocks = append(repSpecBlocks, block)
79-
}
71+
repSpecBlocks := collectBlocks(resourceb, nRepSpecs)
8072

8173
if len(repSpecBlocks) == 0 {
8274
return fmt.Errorf("must have at least one replication_specs")
@@ -372,12 +364,7 @@ func convertConfig(repSpecs *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
372364
}
373365

374366
var configs []*hclwrite.Body
375-
for {
376-
block := repSpecs.FirstMatchingBlock(nConfig, nil)
377-
if block == nil {
378-
break
379-
}
380-
repSpecs.RemoveBlock(block)
367+
for _, block := range collectBlocks(repSpecs, nConfig) {
381368
blockb := block.Body()
382369
processAllSpecs(blockb, diskSizeGB)
383370
configs = append(configs, blockb)
@@ -410,19 +397,6 @@ func convertDynamicConfig(repSpecs *hclwrite.Body, dConfig dynamicBlock, diskSiz
410397
return nil
411398
}
412399

413-
func transformDynamicBlockReferencesRecursive(body *hclwrite.Body, blockName, varName string) {
414-
// Transform attributes in deterministic order
415-
transform := func(expr string) string {
416-
return replaceDynamicBlockReferences(expr, blockName, varName)
417-
}
418-
transformAttributesSorted(body, body.Attributes(), transform)
419-
420-
// Transform nested blocks
421-
for _, block := range body.Blocks() {
422-
transformDynamicBlockReferencesRecursive(block.Body(), blockName, varName)
423-
}
424-
}
425-
426400
// hasExpectedBlocksAsAttributes checks if any of the expected block names
427401
// exist as attributes in the resource body. In that case conversion is not done
428402
// as advanced cluster is not in a valid SDKv2 configuration.

internal/convert/clu2adv.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,7 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
183183
}
184184

185185
// Collect all replication_specs blocks first
186-
var repSpecBlocks []*hclwrite.Block
187-
for {
188-
block := resourceb.FirstMatchingBlock(nRepSpecs, nil)
189-
if block == nil {
190-
break
191-
}
192-
resourceb.RemoveBlock(block)
193-
repSpecBlocks = append(repSpecBlocks, block)
194-
}
186+
repSpecBlocks := collectBlocks(resourceb, nRepSpecs)
195187

196188
if len(repSpecBlocks) == 0 {
197189
return fmt.Errorf("%s: no replication_specs found", errRepSpecs)

internal/convert/shared.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ func transformDynamicBlockReferences(configSrcb *hclwrite.Body, blockName, varNa
106106
}
107107
}
108108

109+
// transformDynamicBlockReferencesRecursive transforms attributes and nested blocks recursively
110+
// replacing references from dynamic block format, e.g. regions_config.value.* to region.*
111+
func transformDynamicBlockReferencesRecursive(body *hclwrite.Body, blockName, varName string) {
112+
// Transform attributes in deterministic order
113+
transform := func(expr string) string {
114+
return replaceDynamicBlockReferences(expr, blockName, varName)
115+
}
116+
transformAttributesSorted(body, body.Attributes(), transform)
117+
118+
// Transform nested blocks
119+
for _, block := range body.Blocks() {
120+
transformDynamicBlockReferencesRecursive(block.Body(), blockName, varName)
121+
}
122+
}
123+
124+
// collectBlocks removes and returns all blocks of the given name from body in order of appearance.
125+
func collectBlocks(body *hclwrite.Body, name string) []*hclwrite.Block {
126+
var blocks []*hclwrite.Block
127+
for {
128+
block := body.FirstMatchingBlock(name, nil)
129+
if block == nil {
130+
break
131+
}
132+
body.RemoveBlock(block)
133+
blocks = append(blocks, block)
134+
}
135+
return blocks
136+
}
137+
109138
// fillBlockOpt converts a block to an attribute with object value
110139
func fillBlockOpt(resourceb *hclwrite.Body, name string) {
111140
block := resourceb.FirstMatchingBlock(name, nil)

0 commit comments

Comments
 (0)