@@ -71,95 +71,192 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
71
71
}
72
72
73
73
if hasVariableNumShards (repSpecBlocks ) {
74
- var concatParts []hclwrite.Tokens
75
-
76
- for _ , block := range repSpecBlocks {
77
- blockb := block .Body ()
78
- numShardsAttr := blockb .GetAttribute (nNumShards )
79
- blockb .RemoveAttribute (nNumShards )
80
-
81
- if err := convertConfig (blockb , diskSizeGB ); err != nil {
82
- return err
83
- }
84
-
85
- tokens , err := processNumShards (numShardsAttr , blockb )
86
- if err != nil {
87
- return err
88
- }
89
- concatParts = append (concatParts , tokens )
74
+ tokens , err := processVariableNumShards (repSpecBlocks , diskSizeGB )
75
+ if err != nil {
76
+ return err
90
77
}
91
- resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensFuncConcat (concatParts ... ))
78
+ resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensFuncConcat (tokens ... ))
92
79
} else {
93
- // All num_shards are numeric or missing, use simple array
94
- var repSpecs []* hclwrite.Body
95
- for _ , block := range repSpecBlocks {
96
- blockb := block .Body ()
97
- numShardsAttr := blockb .GetAttribute (nNumShards )
98
- blockb .RemoveAttribute (nNumShards )
99
-
100
- if err := convertConfig (blockb , diskSizeGB ); err != nil {
101
- return err
102
- }
103
-
104
- if numShardsAttr != nil {
105
- numShardsVal , _ := hcl .GetAttrInt (numShardsAttr , errNumShards )
106
- for range numShardsVal {
107
- repSpecs = append (repSpecs , blockb )
108
- }
109
- } else {
110
- // No num_shards, default to 1
111
- repSpecs = append (repSpecs , blockb )
112
- }
80
+ tokens , err := processStaticNumShards (repSpecBlocks , diskSizeGB )
81
+ if err != nil {
82
+ return err
113
83
}
114
- resourceb .SetAttributeRaw (nRepSpecs , hcl . TokensArray ( repSpecs ) )
84
+ resourceb .SetAttributeRaw (nRepSpecs , tokens )
115
85
}
116
86
117
87
return nil
118
88
}
119
89
120
90
func convertDynamicRepSpecs (resourceb * hclwrite.Body , dSpec dynamicBlock , diskSizeGB hclwrite.Tokens ) error {
121
91
transformDynamicBlockReferences (dSpec .content .Body (), nRepSpecs , nSpec )
122
- dConfig , err := getDynamicBlock (dSpec .content .Body (), nConfig )
92
+
93
+ dConfig , err := findDynamicConfigBlock (dSpec .content .Body ())
123
94
if err != nil {
124
95
return err
125
96
}
126
- if ! dConfig .IsPresent () {
127
- dConfig , err = getDynamicBlock (dSpec .content .Body (), nConfigSrc )
97
+ if dConfig .IsPresent () {
98
+ return convertDynamicRepSpecsWithDynamicConfig (resourceb , dSpec , dConfig , diskSizeGB )
99
+ }
100
+
101
+ tokens , err := processDynamicRepSpecsWithoutConfig (dSpec , diskSizeGB )
102
+ if err != nil {
103
+ return err
104
+ }
105
+
106
+ resourceb .RemoveBlock (dSpec .block )
107
+ resourceb .SetAttributeRaw (nRepSpecs , tokens )
108
+ return nil
109
+ }
110
+
111
+ func processVariableNumShards (repSpecBlocks []* hclwrite.Block , diskSizeGB hclwrite.Tokens ) ([]hclwrite.Tokens , error ) {
112
+ var concatParts []hclwrite.Tokens
113
+ for _ , block := range repSpecBlocks {
114
+ blockb := block .Body ()
115
+ numShardsAttr := blockb .GetAttribute (nNumShards )
116
+ blockb .RemoveAttribute (nNumShards )
117
+
118
+ if err := convertConfig (blockb , diskSizeGB ); err != nil {
119
+ return nil , err
120
+ }
121
+
122
+ tokens , err := processNumShards (numShardsAttr , blockb )
128
123
if err != nil {
129
- return err
124
+ return nil , err
130
125
}
126
+ concatParts = append (concatParts , tokens )
131
127
}
132
- if dConfig .IsPresent () {
133
- return convertDynamicRepSpecsWithDynamicConfig (resourceb , dSpec , dConfig , diskSizeGB )
128
+ return concatParts , nil
129
+ }
130
+
131
+ func processStaticNumShards (repSpecBlocks []* hclwrite.Block , diskSizeGB hclwrite.Tokens ) (hclwrite.Tokens , error ) {
132
+ var repSpecs []* hclwrite.Body
133
+ for _ , block := range repSpecBlocks {
134
+ blockb := block .Body ()
135
+ numShardsAttr := blockb .GetAttribute (nNumShards )
136
+ blockb .RemoveAttribute (nNumShards )
137
+
138
+ if err := convertConfig (blockb , diskSizeGB ); err != nil {
139
+ return nil , err
140
+ }
141
+
142
+ if numShardsAttr != nil {
143
+ numShardsVal , _ := hcl .GetAttrInt (numShardsAttr , errNumShards )
144
+ for range numShardsVal {
145
+ repSpecs = append (repSpecs , blockb )
146
+ }
147
+ } else {
148
+ repSpecs = append (repSpecs , blockb )
149
+ }
134
150
}
135
- numShardsAttr := dSpec .content .Body ().GetAttribute (nNumShards )
136
- if numShardsAttr != nil {
137
- numShardsExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (numShardsAttr ), nRepSpecs , nSpec )
138
- dSpec .content .Body ().RemoveAttribute (nNumShards )
139
- if err := convertConfig (dSpec .content .Body (), diskSizeGB ); err != nil {
140
- return err
151
+ return hcl .TokensArray (repSpecs ), nil
152
+ }
153
+
154
+ func findDynamicConfigBlock (body * hclwrite.Body ) (dynamicBlock , error ) {
155
+ dConfig , err := getDynamicBlock (body , nConfig )
156
+ if err != nil {
157
+ return dynamicBlock {}, err
158
+ }
159
+ if ! dConfig .IsPresent () {
160
+ dConfig , err = getDynamicBlock (body , nConfigSrc )
161
+ if err != nil {
162
+ return dynamicBlock {}, err
141
163
}
142
- outerFor := buildForExpr (nSpec , hcl .GetAttrExpr (dSpec .forEach ))
143
- innerFor := buildForExpr ("i" , fmt .Sprintf ("range(%s)" , numShardsExpr ))
144
- forExpr := fmt .Sprintf ("%s [\n %s " , outerFor , innerFor )
145
- tokens := hcl .TokensFromExpr (forExpr )
146
- tokens = append (tokens , hcl .TokensObject (dSpec .content .Body ())... )
147
- tokens = append (tokens , hcl .TokensFromExpr ("\n ]\n ]" )... )
148
- resourceb .RemoveBlock (dSpec .block )
149
- resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensFuncFlatten (tokens ))
150
- return nil
151
164
}
165
+ return dConfig , nil
166
+ }
167
+
168
+ func processDynamicRepSpecsWithoutConfig (dSpec dynamicBlock , diskSizeGB hclwrite.Tokens ) (hclwrite.Tokens , error ) {
169
+ numShardsAttr := dSpec .content .Body ().GetAttribute (nNumShards )
152
170
dSpec .content .Body ().RemoveAttribute (nNumShards )
171
+
153
172
if err := convertConfig (dSpec .content .Body (), diskSizeGB ); err != nil {
154
- return err
173
+ return nil , err
174
+ }
175
+
176
+ if numShardsAttr != nil {
177
+ return buildDynamicRepSpecsWithShards (dSpec , numShardsAttr )
155
178
}
179
+
180
+ return buildSimpleDynamicRepSpecs (dSpec )
181
+ }
182
+
183
+ func buildDynamicRepSpecsWithShards (dSpec dynamicBlock , numShardsAttr * hclwrite.Attribute ) (hclwrite.Tokens , error ) {
184
+ numShardsExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (numShardsAttr ), nRepSpecs , nSpec )
185
+ outerFor := buildForExpr (nSpec , hcl .GetAttrExpr (dSpec .forEach ))
186
+ innerFor := buildForExpr ("i" , fmt .Sprintf ("range(%s)" , numShardsExpr ))
187
+ forExpr := fmt .Sprintf ("%s [\n %s " , outerFor , innerFor )
188
+ tokens := hcl .TokensFromExpr (forExpr )
189
+ tokens = append (tokens , hcl .TokensObject (dSpec .content .Body ())... )
190
+ tokens = append (tokens , hcl .TokensFromExpr ("\n ]\n ]" )... )
191
+ return hcl .TokensFuncFlatten (tokens ), nil
192
+ }
193
+
194
+ func buildSimpleDynamicRepSpecs (dSpec dynamicBlock ) (hclwrite.Tokens , error ) {
156
195
forExpr := buildForExpr (nSpec , hcl .GetAttrExpr (dSpec .forEach ))
157
196
tokens := hcl .TokensFromExpr (forExpr )
158
197
tokens = append (tokens , hcl .TokensObject (dSpec .content .Body ())... )
159
- tokens = hcl .EncloseBracketsNewLines (tokens )
160
- resourceb .RemoveBlock (dSpec .block )
161
- resourceb .SetAttributeRaw (nRepSpecs , tokens )
162
- return nil
198
+ return hcl .EncloseBracketsNewLines (tokens ), nil
199
+ }
200
+
201
+ func buildDynamicRepSpecsWithNumShards (dSpec , dConfig dynamicBlock , diskSizeGB hclwrite.Tokens ,
202
+ configBlockName string , numShardsAttr * hclwrite.Attribute ) (hclwrite.Tokens , error ) {
203
+ numShardsExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (numShardsAttr ), nRepSpecs , nSpec )
204
+ transformDynamicBlockReferencesRecursive (dConfig .content .Body (), configBlockName , nRegion )
205
+
206
+ transform := func (expr string ) string {
207
+ return replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
208
+ }
209
+ transformAttributesSorted (dConfig .content .Body (), dConfig .content .Body ().Attributes (), transform )
210
+ for _ , block := range dConfig .content .Body ().Blocks () {
211
+ transformAttributesSorted (block .Body (), block .Body ().Attributes (), transform )
212
+ }
213
+
214
+ regionConfigBody := buildRegionConfigBody (dConfig , diskSizeGB )
215
+ regionTokens := buildRegionForExpr (nSpec , regionConfigBody )
216
+
217
+ repSpecBody := buildRepSpecBody (dSpec , regionTokens )
218
+ innerTokens := buildInnerForExpr (numShardsExpr , repSpecBody )
219
+ outerTokens := buildOuterForExpr (dSpec , innerTokens )
220
+
221
+ return hcl .TokensFuncFlatten (outerTokens ), nil
222
+ }
223
+
224
+ func buildRegionConfigBody (dConfig dynamicBlock , diskSizeGB hclwrite.Tokens ) * hclwrite.Body {
225
+ regionConfigFile := hclwrite .NewEmptyFile ()
226
+ regionConfigBody := regionConfigFile .Body ()
227
+ copyAttributesSorted (regionConfigBody , dConfig .content .Body ().Attributes ())
228
+ processRegionConfigBlocks (regionConfigBody , dConfig .content .Body ().Blocks (), diskSizeGB )
229
+ return regionConfigBody
230
+ }
231
+
232
+ func buildRegionForExpr (spec string , regionConfigBody * hclwrite.Body ) hclwrite.Tokens {
233
+ configForEach := fmt .Sprintf ("%s.%s" , spec , nConfig )
234
+ regionForExpr := buildForExpr (nRegion , configForEach )
235
+ regionTokens := hcl .TokensFromExpr (regionForExpr )
236
+ return append (regionTokens , hcl .TokensObject (regionConfigBody )... )
237
+ }
238
+
239
+ func buildRepSpecBody (dSpec dynamicBlock , regionTokens hclwrite.Tokens ) * hclwrite.Body {
240
+ repSpecFile := hclwrite .NewEmptyFile ()
241
+ repSpecBody := repSpecFile .Body ()
242
+ if zoneNameAttr := dSpec .content .Body ().GetAttribute (nZoneName ); zoneNameAttr != nil {
243
+ zoneNameExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (zoneNameAttr ), nRepSpecs , nSpec )
244
+ repSpecBody .SetAttributeRaw (nZoneName , hcl .TokensFromExpr (zoneNameExpr ))
245
+ }
246
+ repSpecBody .SetAttributeRaw (nConfig , hcl .EncloseBracketsNewLines (regionTokens ))
247
+ return repSpecBody
248
+ }
249
+
250
+ func buildInnerForExpr (numShardsExpr string , repSpecBody * hclwrite.Body ) hclwrite.Tokens {
251
+ innerForExpr := buildForExpr ("i" , fmt .Sprintf ("range(%s)" , numShardsExpr ))
252
+ innerTokens := hcl .TokensFromExpr (innerForExpr )
253
+ return append (innerTokens , hcl .TokensObject (repSpecBody )... )
254
+ }
255
+
256
+ func buildOuterForExpr (dSpec dynamicBlock , innerTokens hclwrite.Tokens ) hclwrite.Tokens {
257
+ outerForExpr := buildForExpr (nSpec , hcl .GetAttrExpr (dSpec .forEach ))
258
+ outerTokens := hcl .TokensFromExpr (fmt .Sprintf ("%s " , outerForExpr ))
259
+ return append (outerTokens , hcl .EncloseBracketsNewLines (innerTokens )... )
163
260
}
164
261
165
262
func processRegionConfigBlocks (targetBody * hclwrite.Body , blocks []* hclwrite.Block , diskSizeGB hclwrite.Tokens ) {
@@ -181,37 +278,10 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
181
278
configBlockName := getResourceName (dConfig .block )
182
279
numShardsAttr := dSpec .content .Body ().GetAttribute (nNumShards )
183
280
if numShardsAttr != nil {
184
- numShardsExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (numShardsAttr ), nRepSpecs , nSpec )
185
- transformDynamicBlockReferencesRecursive (dConfig .content .Body (), configBlockName , nRegion )
186
- transform := func (expr string ) string {
187
- return replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
188
- }
189
- transformAttributesSorted (dConfig .content .Body (), dConfig .content .Body ().Attributes (), transform )
190
- for _ , block := range dConfig .content .Body ().Blocks () {
191
- transformAttributesSorted (block .Body (), block .Body ().Attributes (), transform )
192
- }
193
- configForEach := fmt .Sprintf ("%s.%s" , nSpec , nConfig )
194
- regionConfigFile := hclwrite .NewEmptyFile ()
195
- regionConfigBody := regionConfigFile .Body ()
196
- copyAttributesSorted (regionConfigBody , dConfig .content .Body ().Attributes ())
197
- processRegionConfigBlocks (regionConfigBody , dConfig .content .Body ().Blocks (), diskSizeGB )
198
- regionForExpr := buildForExpr (nRegion , configForEach )
199
- regionTokens := hcl .TokensFromExpr (regionForExpr )
200
- regionTokens = append (regionTokens , hcl .TokensObject (regionConfigBody )... )
201
- repSpecFile := hclwrite .NewEmptyFile ()
202
- repSpecBody := repSpecFile .Body ()
203
- if zoneNameAttr := dSpec .content .Body ().GetAttribute (nZoneName ); zoneNameAttr != nil {
204
- zoneNameExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (zoneNameAttr ), nRepSpecs , nSpec )
205
- repSpecBody .SetAttributeRaw (nZoneName , hcl .TokensFromExpr (zoneNameExpr ))
281
+ tokens , err := buildDynamicRepSpecsWithNumShards (dSpec , dConfig , diskSizeGB , configBlockName , numShardsAttr )
282
+ if err != nil {
283
+ return err
206
284
}
207
- repSpecBody .SetAttributeRaw (nConfig , hcl .EncloseBracketsNewLines (regionTokens ))
208
- innerForExpr := buildForExpr ("i" , fmt .Sprintf ("range(%s)" , numShardsExpr ))
209
- innerTokens := hcl .TokensFromExpr (innerForExpr )
210
- innerTokens = append (innerTokens , hcl .TokensObject (repSpecBody )... )
211
- outerForExpr := buildForExpr (nSpec , hcl .GetAttrExpr (dSpec .forEach ))
212
- outerTokens := hcl .TokensFromExpr (fmt .Sprintf ("%s " , outerForExpr ))
213
- outerTokens = append (outerTokens , hcl .EncloseBracketsNewLines (innerTokens )... )
214
- tokens := hcl .TokensFuncFlatten (outerTokens )
215
285
resourceb .RemoveBlock (dSpec .block )
216
286
resourceb .SetAttributeRaw (nRepSpecs , tokens )
217
287
return nil
0 commit comments