Skip to content

Commit 7904329

Browse files
committed
refactor specs
1 parent b5e661d commit 7904329

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

internal/convert/adv2v2.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import (
88
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl"
99
)
1010

11+
var (
12+
specsWithDisk = []string{nElectableSpecs, nReadOnlySpecs, nAnalyticsSpecs}
13+
specsWithoutDisk = []string{nAutoScaling, nAnalyticsAutoScaling}
14+
)
15+
1116
// AdvancedClusterToV2 transforms all mongodbatlas_advanced_cluster resource definitions in a
1217
// Terraform configuration file from SDKv2 schema to TPF (Terraform Plugin Framework) schema.
1318
// All other resources and data sources are left untouched.
@@ -198,31 +203,22 @@ func copyAttributesSorted(targetBody *hclwrite.Body, sourceAttrs map[string]*hcl
198203
}
199204

200205
func processAllSpecs(body *hclwrite.Body, diskSizeGB hclwrite.Tokens) {
201-
specsWithDisk := []string{nElectableSpecs, nReadOnlySpecs, nAnalyticsSpecs}
202206
for _, spec := range specsWithDisk {
203207
fillSpecOpt(body, spec, diskSizeGB)
204208
}
205-
specsWithoutDisk := []string{nAutoScaling, nAnalyticsAutoScaling}
206209
for _, spec := range specsWithoutDisk {
207210
fillSpecOpt(body, spec, nil)
208211
}
209212
}
210213

211214
func processConfigForDynamicBlock(configBlockb *hclwrite.Body, diskSizeGB hclwrite.Tokens) *hclwrite.Body {
212215
newConfigBody := hclwrite.NewEmptyFile().Body()
213-
attrs := configBlockb.Attributes()
214-
orderedAttrs := []string{nPriority, nProviderName, nRegionName}
215-
for _, attrName := range orderedAttrs {
216-
if attr := attrs[attrName]; attr != nil {
217-
newConfigBody.SetAttributeRaw(attrName, attr.Expr().BuildTokens(nil))
218-
}
219-
}
216+
copyAttributesSorted(newConfigBody, configBlockb.Attributes())
220217
for _, block := range configBlockb.Blocks() {
221218
blockType := block.Type()
222219
blockBody := hclwrite.NewEmptyFile().Body()
223220
copyAttributesSorted(blockBody, block.Body().Attributes())
224-
if diskSizeGB != nil &&
225-
(blockType == nElectableSpecs || blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
221+
if diskSizeGB != nil && slices.Contains(specsWithDisk, blockType) {
226222
blockBody.SetAttributeRaw(nDiskSizeGB, diskSizeGB)
227223
}
228224
newConfigBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))

internal/convert/clu2adv.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -314,36 +314,27 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
314314
if err := hcl.MoveAttr(configSrc.Body(), fileb, nPriority, nPriority, errRepSpecs); err != nil {
315315
return nil, err
316316
}
317-
// Process all spec types
318-
specMappings := []struct {
319-
nodeName string
320-
specName string
321-
}{
322-
{nElectableNodes, nElectableSpecs},
323-
{nReadOnlyNodes, nReadOnlySpecs},
324-
{nAnalyticsNodes, nAnalyticsSpecs},
325-
}
326-
for _, mapping := range specMappings {
327-
if spec, _ := getSpec(configSrc, mapping.nodeName, root, isDynamicBlock); spec != nil {
328-
fileb.SetAttributeRaw(mapping.specName, spec)
329-
}
330-
}
317+
processSpec(fileb, configSrc, nElectableSpecs, nElectableNodes, root, isDynamicBlock)
318+
processSpec(fileb, configSrc, nReadOnlySpecs, nReadOnlyNodes, root, isDynamicBlock)
319+
processSpec(fileb, configSrc, nAnalyticsSpecs, nAnalyticsNodes, root, isDynamicBlock)
320+
331321
if autoScaling := getAutoScalingOpt(root.opt); autoScaling != nil {
332322
fileb.SetAttributeRaw(nAutoScaling, autoScaling)
333323
}
334324
return fileb, nil
335325
}
336326

337-
func getSpec(configSrc *hclwrite.Block, countName string, root attrVals, isDynamicBlock bool) (hclwrite.Tokens, error) {
327+
func processSpec(configb *hclwrite.Body, configSrc *hclwrite.Block,
328+
specName, countName string, root attrVals, isDynamicBlock bool) {
338329
var (
339330
fileb = hclwrite.NewEmptyFile().Body()
340331
count = configSrc.Body().GetAttribute(countName)
341332
)
342333
if count == nil {
343-
return nil, fmt.Errorf("%s: attribute %s not found", errRepSpecs, countName)
334+
return
344335
}
345336
if countVal, errVal := hcl.GetAttrInt(count, errRepSpecs); countVal == 0 && errVal == nil {
346-
return nil, fmt.Errorf("%s: attribute %s is 0", errRepSpecs, countName)
337+
return
347338
}
348339
fileb.SetAttributeRaw(nNodeCount, count.Expr().BuildTokens(nil))
349340
fileb.SetAttributeRaw(nInstanceSize, root.req[nInstanceSizeSrc])
@@ -360,7 +351,7 @@ func getSpec(configSrc *hclwrite.Block, countName string, root attrVals, isDynam
360351
if isDynamicBlock {
361352
tokens = append(hcl.TokensFromExpr(fmt.Sprintf("%s == 0 ? null :", hcl.GetAttrExpr(count))), tokens...)
362353
}
363-
return tokens, nil
354+
configb.SetAttributeRaw(specName, tokens)
364355
}
365356

366357
func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens {

internal/convert/shared.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,21 @@ func fillAdvConfigOpt(resourceb *hclwrite.Body) {
177177
fillBlockOpt(resourceb, nAdvConfig)
178178
}
179179

180-
// processCommonOptionalBlocks processes tags, labels, and other optional blocks
181-
// This function is used by both adv2v2 and clu2adv conversions
180+
// processCommonOptionalBlocks processes tags, labels, and other optional blocks.
182181
func processCommonOptionalBlocks(resourceb *hclwrite.Body) error {
183-
// Process tags and labels
184182
for _, name := range []string{nTags, nLabels} {
185183
if err := fillTagsLabelsOpt(resourceb, name); err != nil {
186184
return err
187185
}
188186
}
189-
// Process optional configuration blocks
190187
fillAdvConfigOpt(resourceb)
191188
for _, name := range []string{nBiConnector, nPinnedFCV, nTimeouts} {
192189
fillBlockOpt(resourceb, name)
193190
}
194191
return nil
195192
}
196193

197-
// buildForExpr builds a for expression with the given variable and collection
194+
// buildForExpr builds a for expression with the given variable and collection.
198195
func buildForExpr(varName, collection string, trailingSpace bool) string {
199196
expr := fmt.Sprintf("for %s in %s :", varName, collection)
200197
if trailingSpace {

0 commit comments

Comments
 (0)