Skip to content

Commit e2b2c9f

Browse files
committed
refactor common code to shared.go
1 parent 3ef174b commit e2b2c9f

File tree

3 files changed

+154
-168
lines changed

3 files changed

+154
-168
lines changed

internal/convert/adv2v2.go

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -83,73 +83,31 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
8383
}
8484

8585
// Check if any replication_specs has a variable num_shards
86-
hasVariableNumShards := false
87-
for _, block := range repSpecBlocks {
88-
numShardsAttr := block.Body().GetAttribute(nNumShards)
89-
if numShardsAttr != nil {
90-
_, err := hcl.GetAttrInt(numShardsAttr, errNumShards)
91-
if err != nil {
92-
hasVariableNumShards = true
93-
break
94-
}
95-
}
96-
}
86+
hasVariableNumShards := HasVariableNumShards(repSpecBlocks)
9787

98-
// If we have any variable num_shards, we need to use concat
9988
if hasVariableNumShards {
10089
var concatParts []hclwrite.Tokens
101-
hasMultipleParts := false
10290

10391
for _, block := range repSpecBlocks {
10492
blockb := block.Body()
10593
numShardsAttr := blockb.GetAttribute(nNumShards)
94+
blockb.RemoveAttribute(nNumShards)
10695

107-
if numShardsAttr != nil {
108-
numShardsVal, err := hcl.GetAttrInt(numShardsAttr, errNumShards)
109-
110-
if err != nil {
111-
// num_shards is a variable/expression
112-
numShardsExpr := hcl.GetAttrExpr(numShardsAttr)
113-
blockb.RemoveAttribute(nNumShards)
114-
115-
if err := convertConfig(blockb, diskSizeGB); err != nil {
116-
return err
117-
}
118-
119-
forExpr := fmt.Sprintf("for i in range(%s) :", numShardsExpr)
120-
tokens := hcl.TokensFromExpr(forExpr)
121-
tokens = append(tokens, hcl.TokensObject(blockb)...)
122-
concatParts = append(concatParts, hcl.EncloseBracketsNewLines(tokens))
123-
} else {
124-
// num_shards is a literal number - create explicit array
125-
blockb.RemoveAttribute(nNumShards)
126-
127-
if err := convertConfig(blockb, diskSizeGB); err != nil {
128-
return err
129-
}
130-
131-
var repSpecs []*hclwrite.Body
132-
for range numShardsVal {
133-
repSpecs = append(repSpecs, blockb)
134-
}
135-
concatParts = append(concatParts, hcl.TokensArray(repSpecs))
136-
hasMultipleParts = true
137-
}
138-
} else {
139-
// No num_shards, default to 1
140-
if err := convertConfig(blockb, diskSizeGB); err != nil {
141-
return err
142-
}
143-
concatParts = append(concatParts, hcl.TokensArraySingle(blockb))
144-
hasMultipleParts = true
96+
if err := convertConfig(blockb, diskSizeGB); err != nil {
97+
return err
98+
}
99+
100+
tokens, err := ProcessNumShards(numShardsAttr, blockb)
101+
if err != nil {
102+
return err
145103
}
104+
concatParts = append(concatParts, tokens)
146105
}
147106

148-
// Use concat only if we have multiple parts or mixing numerical and variable
149-
if len(concatParts) > 1 || hasMultipleParts {
107+
// Use concat to combine all parts
108+
if len(concatParts) > 1 {
150109
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensFuncConcat(concatParts...))
151110
} else {
152-
// Only one variable num_shards, no need for concat
153111
resourceb.SetAttributeRaw(nRepSpecs, concatParts[0])
154112
}
155113
} else {
@@ -158,21 +116,19 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
158116
for _, block := range repSpecBlocks {
159117
blockb := block.Body()
160118
numShardsAttr := blockb.GetAttribute(nNumShards)
119+
blockb.RemoveAttribute(nNumShards)
120+
121+
if err := convertConfig(blockb, diskSizeGB); err != nil {
122+
return err
123+
}
161124

162125
if numShardsAttr != nil {
163126
numShardsVal, _ := hcl.GetAttrInt(numShardsAttr, errNumShards)
164-
blockb.RemoveAttribute(nNumShards)
165-
if err := convertConfig(blockb, diskSizeGB); err != nil {
166-
return err
167-
}
168127
for range numShardsVal {
169128
repSpecs = append(repSpecs, blockb)
170129
}
171130
} else {
172131
// No num_shards, default to 1
173-
if err := convertConfig(blockb, diskSizeGB); err != nil {
174-
return err
175-
}
176132
repSpecs = append(repSpecs, blockb)
177133
}
178134
}

internal/convert/clu2adv.go

Lines changed: 6 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,8 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
198198
}
199199

200200
// Check if any replication_specs has a variable num_shards
201-
hasVariableNumShards := false
202-
for _, block := range repSpecBlocks {
203-
shardsAttr := block.Body().GetAttribute(nNumShards)
204-
if shardsAttr != nil {
205-
_, err := hcl.GetAttrInt(shardsAttr, errNumShards)
206-
if err != nil {
207-
hasVariableNumShards = true
208-
break
209-
}
210-
}
211-
}
201+
hasVariableNumShards := HasVariableNumShards(repSpecBlocks)
212202

213-
// If we have any variable num_shards, we need to use concat
214203
if hasVariableNumShards {
215204
var concatParts []hclwrite.Tokens
216205

@@ -238,27 +227,15 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
238227
return fmt.Errorf("%s: %s not found", errRepSpecs, nNumShards)
239228
}
240229

241-
shardsVal, err := hcl.GetAttrInt(shardsAttr, errNumShards)
242-
243230
if errConfig := fillRegionConfigs(specb, specbSrc, root); errConfig != nil {
244231
return errConfig
245232
}
246233

234+
tokens, err := ProcessNumShards(shardsAttr, specb)
247235
if err != nil {
248-
// num_shards is a variable/expression
249-
shardsExpr := hcl.GetAttrExpr(shardsAttr)
250-
forExpr := fmt.Sprintf("for i in range(%s) :", shardsExpr)
251-
tokens := hcl.TokensFromExpr(forExpr)
252-
tokens = append(tokens, hcl.TokensObject(specb)...)
253-
concatParts = append(concatParts, hcl.EncloseBracketsNewLines(tokens))
254-
} else {
255-
// num_shards is a literal number - create explicit array
256-
var specs []*hclwrite.Body
257-
for range shardsVal {
258-
specs = append(specs, specb)
259-
}
260-
concatParts = append(concatParts, hcl.TokensArray(specs))
236+
return err
261237
}
238+
concatParts = append(concatParts, tokens)
262239
}
263240

264241
// Use concat to combine all parts
@@ -299,8 +276,8 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
299276

300277
shardsVal, _ := hcl.GetAttrInt(shardsAttr, errNumShards)
301278

302-
if err := fillRegionConfigs(specb, specbSrc, root); err != nil {
303-
return err
279+
if errConfig := fillRegionConfigs(specb, specbSrc, root); errConfig != nil {
280+
return errConfig
304281
}
305282

306283
for range shardsVal {
@@ -383,29 +360,6 @@ func extractTagsLabelsIndividual(resourceb *hclwrite.Body, name string) (hclwrit
383360
return hcl.TokensObject(fileb), nil
384361
}
385362

386-
func fillBlockOpt(resourceb *hclwrite.Body, name string) {
387-
block := resourceb.FirstMatchingBlock(name, nil)
388-
if block == nil {
389-
return
390-
}
391-
resourceb.RemoveBlock(block)
392-
resourceb.SetAttributeRaw(name, hcl.TokensObject(block.Body()))
393-
}
394-
395-
func fillAdvConfigOpt(resourceb *hclwrite.Body) {
396-
block := resourceb.FirstMatchingBlock(nAdvConfig, nil)
397-
if block == nil {
398-
return
399-
}
400-
blockBody := block.Body()
401-
402-
// Remove deprecated attributes from advanced_configuration
403-
blockBody.RemoveAttribute(nFailIndexKeyTooLong)
404-
blockBody.RemoveAttribute(nDefaultReadConcern)
405-
406-
fillBlockOpt(resourceb, nAdvConfig)
407-
}
408-
409363
// fillReplicationSpecsWithDynamicBlock used for dynamic blocks in replication_specs
410364
func fillReplicationSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVals) (dynamicBlock, error) {
411365
dSpec, err := getDynamicBlock(resourceb, nRepSpecs)
@@ -571,16 +525,6 @@ func setResourceName(resource *hclwrite.Block, name string) {
571525
resource.SetLabels(labels)
572526
}
573527

574-
// getResourceName returns the first label of a block, if it exists.
575-
// e.g. in resource "mongodbatlas_cluster" "mycluster", the first label is "mongodbatlas_cluster".
576-
func getResourceName(resource *hclwrite.Block) string {
577-
labels := resource.Labels()
578-
if len(labels) == 0 {
579-
return ""
580-
}
581-
return labels[0]
582-
}
583-
584528
// getResourceLabel returns the second label of a block, if it exists.
585529
// e.g. in resource "mongodbatlas_cluster" "mycluster", the second label is "mycluster".
586530
func getResourceLabel(resource *hclwrite.Block) string {
@@ -591,17 +535,6 @@ func getResourceLabel(resource *hclwrite.Block) string {
591535
return labels[1]
592536
}
593537

594-
type dynamicBlock struct {
595-
block *hclwrite.Block
596-
forEach *hclwrite.Attribute
597-
content *hclwrite.Block
598-
tokens hclwrite.Tokens
599-
}
600-
601-
func (d dynamicBlock) IsPresent() bool {
602-
return d.block != nil
603-
}
604-
605538
func checkDynamicBlock(body *hclwrite.Body) error {
606539
for _, block := range body.Blocks() {
607540
name := getResourceName(block)
@@ -613,25 +546,6 @@ func checkDynamicBlock(body *hclwrite.Body) error {
613546
return nil
614547
}
615548

616-
func getDynamicBlock(body *hclwrite.Body, name string) (dynamicBlock, error) {
617-
for _, block := range body.Blocks() {
618-
if block.Type() != nDynamic || name != getResourceName(block) {
619-
continue
620-
}
621-
blockb := block.Body()
622-
forEach := blockb.GetAttribute(nForEach)
623-
if forEach == nil {
624-
return dynamicBlock{}, fmt.Errorf("dynamic block %s: attribute %s not found", name, nForEach)
625-
}
626-
content := blockb.FirstMatchingBlock(nContent, nil)
627-
if content == nil {
628-
return dynamicBlock{}, fmt.Errorf("dynamic block %s: block %s not found", name, nContent)
629-
}
630-
return dynamicBlock{forEach: forEach, block: block, content: content}, nil
631-
}
632-
return dynamicBlock{}, nil
633-
}
634-
635549
func replaceDynamicBlockExpr(attr *hclwrite.Attribute, blockName, attrName string) string {
636550
expr := hcl.GetAttrExpr(attr)
637551
return strings.ReplaceAll(expr, fmt.Sprintf("%s.%s", blockName, attrName), attrName)
@@ -655,21 +569,6 @@ func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root
655569
return hcl.EncloseBracketsNewLines(tokens), nil
656570
}
657571

658-
func transformDynamicBlockReferences(configSrcb *hclwrite.Body, blockName, varName string) {
659-
for name, attr := range configSrcb.Attributes() {
660-
expr := replaceDynamicBlockReferences(hcl.GetAttrExpr(attr), blockName, varName)
661-
configSrcb.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
662-
}
663-
}
664-
665-
// replaceDynamicBlockReferences changes value references,
666-
// e.g. regions_config.value.electable_nodes to region.electable_nodes
667-
func replaceDynamicBlockReferences(expr, blockName, varName string) string {
668-
return strings.ReplaceAll(expr,
669-
fmt.Sprintf("%s.%s.", blockName, nValue),
670-
fmt.Sprintf("%s.", varName))
671-
}
672-
673572
func sortConfigsByPriority(configs []*hclwrite.Body) []*hclwrite.Body {
674573
for _, config := range configs {
675574
if _, err := hcl.GetAttrInt(config.GetAttribute(nPriority), errPriority); err != nil {

0 commit comments

Comments
 (0)