@@ -18,20 +18,18 @@ func AdvancedClusterToV2(config []byte) ([]byte, error) {
18
18
}
19
19
parserb := parser .Body ()
20
20
for _ , block := range parserb .Blocks () {
21
- updated , err := updateResource (block )
21
+ updated , err := processResource (block )
22
22
if err != nil {
23
23
return nil , err
24
24
}
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 )
29
27
}
30
28
}
31
29
return parser .Bytes (), nil
32
30
}
33
31
34
- func updateResource (resource * hclwrite.Block ) (bool , error ) {
32
+ func processResource (resource * hclwrite.Block ) (bool , error ) {
35
33
if resource .Type () != resourceType || getResourceName (resource ) != advCluster {
36
34
return false , nil
37
35
}
@@ -43,24 +41,17 @@ func updateResource(resource *hclwrite.Block) (bool, error) {
43
41
return false , nil
44
42
}
45
43
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 {
47
45
return false , err
48
46
}
49
- if err := fillTagsLabelsOpt (resourceb , nTags ); err != nil {
47
+ if err := processCommonOptionalBlocks (resourceb ); err != nil {
50
48
return false , err
51
49
}
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 )
59
50
return true , nil
60
51
}
61
52
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 )
64
55
if err != nil {
65
56
return err
66
57
}
@@ -80,7 +71,7 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
80
71
blockb := block .Body ()
81
72
shardsAttr := blockb .GetAttribute (nNumShards )
82
73
blockb .RemoveAttribute (nNumShards )
83
- dConfig , err := convertConfigsWithDynamicBlock (blockb , diskSizeGB , false )
74
+ dConfig , err := processConfigsWithDynamicBlock (blockb , diskSizeGB , false )
84
75
if err != nil {
85
76
return err
86
77
}
@@ -119,18 +110,16 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
119
110
return nil
120
111
}
121
112
122
- func convertRepSpecsWithDynamicBlock (resourceb * hclwrite.Body , diskSizeGB hclwrite.Tokens ) (dynamicBlock , error ) {
113
+ func processRepSpecsWithDynamicBlock (resourceb * hclwrite.Body , diskSizeGB hclwrite.Tokens ) (dynamicBlock , error ) {
123
114
dSpec , err := getDynamicBlock (resourceb , nRepSpecs , true )
124
115
if err != nil || ! dSpec .IsPresent () {
125
116
return dynamicBlock {}, err
126
117
}
127
118
transformReferences (dSpec .content .Body (), nRepSpecs , nSpec )
128
- dConfig , err := convertConfigsWithDynamicBlock (dSpec .content .Body (), diskSizeGB , true )
119
+ dConfig , err := processConfigsWithDynamicBlock (dSpec .content .Body (), diskSizeGB , true )
129
120
if err != nil {
130
121
return dynamicBlock {}, err
131
122
}
132
-
133
- // Check if we have a dynamic region_configs block that was successfully processed
134
123
if dConfig .tokens != nil {
135
124
forSpec := hcl .TokensFromExpr (buildForExpr (nSpec , hcl .GetAttrExpr (dSpec .forEach ), true ))
136
125
dSpec .tokens = hcl .TokensFuncFlatten (append (forSpec , dConfig .tokens ... ))
@@ -163,83 +152,36 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
163
152
repSpecb := hclwrite .NewEmptyFile ().Body ()
164
153
165
154
// 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 )
170
156
171
157
// Process static region_configs blocks
172
158
var configs []* hclwrite.Body
173
159
for _ , configBlock := range staticConfigs {
174
160
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 )
203
162
configs = append (configs , newConfigBody )
204
163
}
205
164
206
165
repSpecb .SetAttributeRaw (nConfig , hcl .TokensArray (configs ))
207
166
208
167
// 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 ... ))
219
172
220
173
return dSpec , nil
221
174
}
222
175
223
- func convertConfigsWithDynamicBlock (specbSrc * hclwrite.Body , diskSizeGB hclwrite.Tokens ,
176
+ func processConfigsWithDynamicBlock (specbSrc * hclwrite.Body , diskSizeGB hclwrite.Tokens ,
224
177
insideDynamicRepSpec bool ) (dynamicBlock , error ) {
225
178
d , err := getDynamicBlock (specbSrc , nConfig , true )
226
179
if err != nil || ! d .IsPresent () {
227
180
return dynamicBlock {}, err
228
181
}
229
182
configBody := d .content .Body ()
230
183
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 )
243
185
forEach := hcl .GetAttrExpr (d .forEach )
244
186
if insideDynamicRepSpec {
245
187
forEach = fmt .Sprintf ("%s.%s" , nSpec , nConfig )
@@ -251,18 +193,11 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
251
193
return d , nil
252
194
}
253
195
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 )
258
197
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
266
201
}
267
202
268
203
// hasExpectedBlocksAsAttributes checks if any of the expected block names
@@ -291,11 +226,44 @@ func copyAttributesSorted(targetBody *hclwrite.Body, sourceAttrs map[string]*hcl
291
226
}
292
227
293
228
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
299
267
}
300
268
301
269
func fillSpecOpt (resourceb * hclwrite.Body , name string , diskSizeGBTokens hclwrite.Tokens ) {
0 commit comments