Skip to content

Commit c0e0bac

Browse files
committed
refactors
1 parent 7584850 commit c0e0bac

File tree

3 files changed

+213
-204
lines changed

3 files changed

+213
-204
lines changed

internal/convert/adv2v2.go

Lines changed: 61 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ func AdvancedClusterToV2(config []byte) ([]byte, error) {
1818
}
1919
parserb := parser.Body()
2020
for _, block := range parserb.Blocks() {
21-
updated, err := updateResource(block)
21+
updated, err := processResource(block)
2222
if err != nil {
2323
return nil, err
2424
}
25-
if updated { // If the resource was converted, add a comment at the end so user knows the resource was updated
26-
blockb := block.Body()
27-
blockb.AppendNewline()
28-
hcl.AppendComment(blockb, commentUpdatedBy)
25+
if updated {
26+
addConversionComments(block, true)
2927
}
3028
}
3129
return parser.Bytes(), nil
3230
}
3331

34-
func updateResource(resource *hclwrite.Block) (bool, error) {
32+
func processResource(resource *hclwrite.Block) (bool, error) {
3533
if resource.Type() != resourceType || getResourceName(resource) != advCluster {
3634
return false, nil
3735
}
@@ -43,24 +41,17 @@ func updateResource(resource *hclwrite.Block) (bool, error) {
4341
return false, nil
4442
}
4543
diskSizeGB, _ := hcl.PopAttr(resourceb, nDiskSizeGB, errRoot) // ok to fail as it's optional
46-
if err := convertRepSpecs(resourceb, diskSizeGB); err != nil {
44+
if err := processRepSpecs(resourceb, diskSizeGB); err != nil {
4745
return false, err
4846
}
49-
if err := fillTagsLabelsOpt(resourceb, nTags); err != nil {
47+
if err := processCommonOptionalBlocks(resourceb); err != nil {
5048
return false, err
5149
}
52-
if err := fillTagsLabelsOpt(resourceb, nLabels); err != nil {
53-
return false, err
54-
}
55-
fillAdvConfigOpt(resourceb)
56-
fillBlockOpt(resourceb, nBiConnector)
57-
fillBlockOpt(resourceb, nPinnedFCV)
58-
fillBlockOpt(resourceb, nTimeouts)
5950
return true, nil
6051
}
6152

62-
func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
63-
d, err := convertRepSpecsWithDynamicBlock(resourceb, diskSizeGB)
53+
func processRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
54+
d, err := processRepSpecsWithDynamicBlock(resourceb, diskSizeGB)
6455
if err != nil {
6556
return err
6657
}
@@ -80,7 +71,7 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
8071
blockb := block.Body()
8172
shardsAttr := blockb.GetAttribute(nNumShards)
8273
blockb.RemoveAttribute(nNumShards)
83-
dConfig, err := convertConfigsWithDynamicBlock(blockb, diskSizeGB, false)
74+
dConfig, err := processConfigsWithDynamicBlock(blockb, diskSizeGB, false)
8475
if err != nil {
8576
return err
8677
}
@@ -119,18 +110,16 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
119110
return nil
120111
}
121112

122-
func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) (dynamicBlock, error) {
113+
func processRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) (dynamicBlock, error) {
123114
dSpec, err := getDynamicBlock(resourceb, nRepSpecs, true)
124115
if err != nil || !dSpec.IsPresent() {
125116
return dynamicBlock{}, err
126117
}
127118
transformReferences(dSpec.content.Body(), nRepSpecs, nSpec)
128-
dConfig, err := convertConfigsWithDynamicBlock(dSpec.content.Body(), diskSizeGB, true)
119+
dConfig, err := processConfigsWithDynamicBlock(dSpec.content.Body(), diskSizeGB, true)
129120
if err != nil {
130121
return dynamicBlock{}, err
131122
}
132-
133-
// Check if we have a dynamic region_configs block that was successfully processed
134123
if dConfig.tokens != nil {
135124
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
136125
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, dConfig.tokens...))
@@ -163,83 +152,36 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
163152
repSpecb := hclwrite.NewEmptyFile().Body()
164153

165154
// Handle zone_name attribute
166-
if zoneNameAttr := specBody.GetAttribute(nZoneName); zoneNameAttr != nil {
167-
zoneNameExpr := transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
168-
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(zoneNameExpr))
169-
}
155+
handleZoneName(repSpecb, specBody, nRepSpecs, nSpec)
170156

171157
// Process static region_configs blocks
172158
var configs []*hclwrite.Body
173159
for _, configBlock := range staticConfigs {
174160
configBlockb := configBlock.Body()
175-
// Create a new body with sorted attributes
176-
newConfigBody := hclwrite.NewEmptyFile().Body()
177-
178-
// Copy attributes in the expected order
179-
attrs := configBlockb.Attributes()
180-
// Priority, provider_name, region_name should come first
181-
if priority := attrs["priority"]; priority != nil {
182-
newConfigBody.SetAttributeRaw("priority", priority.Expr().BuildTokens(nil))
183-
}
184-
if provider := attrs["provider_name"]; provider != nil {
185-
newConfigBody.SetAttributeRaw("provider_name", provider.Expr().BuildTokens(nil))
186-
}
187-
if region := attrs["region_name"]; region != nil {
188-
newConfigBody.SetAttributeRaw("region_name", region.Expr().BuildTokens(nil))
189-
}
190-
191-
// Process spec blocks and convert them to attributes
192-
for _, block := range configBlockb.Blocks() {
193-
blockType := block.Type()
194-
blockBody := hclwrite.NewEmptyFile().Body()
195-
copyAttributesSorted(blockBody, block.Body().Attributes())
196-
if diskSizeGB != nil &&
197-
(blockType == nElectableSpecs || blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
198-
blockBody.SetAttributeRaw(nDiskSizeGB, diskSizeGB)
199-
}
200-
newConfigBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
201-
}
202-
161+
newConfigBody := processConfigForDynamicBlock(configBlockb, diskSizeGB)
203162
configs = append(configs, newConfigBody)
204163
}
205164

206165
repSpecb.SetAttributeRaw(nConfig, hcl.TokensArray(configs))
207166

208167
// Handle num_shards attribute
209-
if numShardsAttr := specBody.GetAttribute(nNumShards); numShardsAttr != nil {
210-
numShardsExpr := transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
211-
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
212-
innerFor := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr), false))
213-
innerFor = append(innerFor, hcl.TokensObject(repSpecb)...)
214-
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, hcl.EncloseBracketsNewLines(innerFor)...))
215-
} else {
216-
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
217-
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, hcl.TokensArraySingle(repSpecb)...))
218-
}
168+
numShardsAttr := specBody.GetAttribute(nNumShards)
169+
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
170+
numShardsTokens := buildNumShardsTokens(numShardsAttr, repSpecb, nRepSpecs, nSpec)
171+
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, numShardsTokens...))
219172

220173
return dSpec, nil
221174
}
222175

223-
func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite.Tokens,
176+
func processConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite.Tokens,
224177
insideDynamicRepSpec bool) (dynamicBlock, error) {
225178
d, err := getDynamicBlock(specbSrc, nConfig, true)
226179
if err != nil || !d.IsPresent() {
227180
return dynamicBlock{}, err
228181
}
229182
configBody := d.content.Body()
230183
transformReferences(configBody, getResourceName(d.block), nRegion)
231-
regionConfigBody := hclwrite.NewEmptyFile().Body()
232-
copyAttributesSorted(regionConfigBody, configBody.Attributes())
233-
for _, block := range configBody.Blocks() {
234-
blockType := block.Type()
235-
blockBody := hclwrite.NewEmptyFile().Body()
236-
copyAttributesSorted(blockBody, block.Body().Attributes())
237-
if diskSizeGB != nil &&
238-
(blockType == nElectableSpecs || blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
239-
blockBody.SetAttributeRaw(nDiskSizeGB, diskSizeGB)
240-
}
241-
regionConfigBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
242-
}
184+
regionConfigBody := processConfigForDynamicBlock(configBody, diskSizeGB)
243185
forEach := hcl.GetAttrExpr(d.forEach)
244186
if insideDynamicRepSpec {
245187
forEach = fmt.Sprintf("%s.%s", nSpec, nConfig)
@@ -251,18 +193,11 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
251193
return d, nil
252194
}
253195
repSpecb := hclwrite.NewEmptyFile().Body()
254-
if zoneNameAttr := specbSrc.GetAttribute(nZoneName); zoneNameAttr != nil {
255-
zoneNameExpr := transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
256-
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(zoneNameExpr))
257-
}
196+
handleZoneName(repSpecb, specbSrc, nRepSpecs, nSpec)
258197
repSpecb.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(regionTokens))
259-
if numShardsAttr := specbSrc.GetAttribute(nNumShards); numShardsAttr != nil {
260-
numShardsExpr := transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
261-
tokens := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr), false))
262-
tokens = append(tokens, hcl.TokensObject(repSpecb)...)
263-
return dynamicBlock{tokens: hcl.EncloseBracketsNewLines(tokens)}, nil
264-
}
265-
return dynamicBlock{tokens: hcl.TokensArraySingle(repSpecb)}, nil
198+
numShardsAttr := specbSrc.GetAttribute(nNumShards)
199+
tokens := buildNumShardsTokens(numShardsAttr, repSpecb, nRepSpecs, nSpec)
200+
return dynamicBlock{tokens: tokens}, nil
266201
}
267202

268203
// hasExpectedBlocksAsAttributes checks if any of the expected block names
@@ -291,11 +226,44 @@ func copyAttributesSorted(targetBody *hclwrite.Body, sourceAttrs map[string]*hcl
291226
}
292227

293228
func processAllSpecs(body *hclwrite.Body, diskSizeGB hclwrite.Tokens) {
294-
fillSpecOpt(body, nElectableSpecs, diskSizeGB)
295-
fillSpecOpt(body, nReadOnlySpecs, diskSizeGB)
296-
fillSpecOpt(body, nAnalyticsSpecs, diskSizeGB)
297-
fillSpecOpt(body, nAutoScaling, nil)
298-
fillSpecOpt(body, nAnalyticsAutoScaling, nil)
229+
// Process specs that need diskSizeGB
230+
specsWithDisk := []string{nElectableSpecs, nReadOnlySpecs, nAnalyticsSpecs}
231+
for _, spec := range specsWithDisk {
232+
fillSpecOpt(body, spec, diskSizeGB)
233+
}
234+
// Process specs without diskSizeGB
235+
specsWithoutDisk := []string{nAutoScaling, nAnalyticsAutoScaling}
236+
for _, spec := range specsWithoutDisk {
237+
fillSpecOpt(body, spec, nil)
238+
}
239+
}
240+
241+
func processConfigForDynamicBlock(configBlockb *hclwrite.Body, diskSizeGB hclwrite.Tokens) *hclwrite.Body {
242+
// Create a new body with sorted attributes
243+
newConfigBody := hclwrite.NewEmptyFile().Body()
244+
245+
// Copy attributes in the expected order (priority, provider_name, region_name first)
246+
attrs := configBlockb.Attributes()
247+
orderedAttrs := []string{nPriority, nProviderName, nRegionName}
248+
for _, attrName := range orderedAttrs {
249+
if attr := attrs[attrName]; attr != nil {
250+
newConfigBody.SetAttributeRaw(attrName, attr.Expr().BuildTokens(nil))
251+
}
252+
}
253+
254+
// Process spec blocks and convert them to attributes
255+
for _, block := range configBlockb.Blocks() {
256+
blockType := block.Type()
257+
blockBody := hclwrite.NewEmptyFile().Body()
258+
copyAttributesSorted(blockBody, block.Body().Attributes())
259+
if diskSizeGB != nil &&
260+
(blockType == nElectableSpecs || blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
261+
blockBody.SetAttributeRaw(nDiskSizeGB, diskSizeGB)
262+
}
263+
newConfigBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
264+
}
265+
266+
return newConfigBody
299267
}
300268

301269
func fillSpecOpt(resourceb *hclwrite.Body, name string, diskSizeGBTokens hclwrite.Tokens) {

0 commit comments

Comments
 (0)