@@ -178,20 +178,50 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
178
178
if len (repSpecBlocks ) == 0 {
179
179
return fmt .Errorf ("%s: no replication_specs found" , errRepSpecs )
180
180
}
181
- if hasVariableNumShards (repSpecBlocks ) {
182
- tokens , err := processVariableRepSpecs (repSpecBlocks , root )
183
- if err != nil {
184
- return err
181
+ dConfig , err := fillWithDynamicRegionConfigs (repSpecBlocks [0 ].Body (), root , false )
182
+ if err != nil {
183
+ return err
184
+ }
185
+ if dConfig .IsPresent () {
186
+ resourceb .SetAttributeRaw (nRepSpecs , dConfig .tokens )
187
+ return nil
188
+ }
189
+ hasVariableShards := hasVariableNumShards (repSpecBlocks )
190
+ var resultTokens []hclwrite.Tokens
191
+ var resultBodies []* hclwrite.Body
192
+ for _ , block := range repSpecBlocks {
193
+ spec := hclwrite .NewEmptyFile ()
194
+ specb := spec .Body ()
195
+ specbSrc := block .Body ()
196
+ _ = hcl .MoveAttr (specbSrc , specb , nZoneName , nZoneName , errRepSpecs )
197
+ shardsAttr := specbSrc .GetAttribute (nNumShards )
198
+ if shardsAttr == nil {
199
+ return fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
185
200
}
186
- resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensFuncConcat (tokens ... ))
187
- } else {
188
- tokens , err := processStaticRepSpecs (repSpecBlocks , root )
201
+ if errConfig := fillRegionConfigs (specb , specbSrc , root ); errConfig != nil {
202
+ return errConfig
203
+ }
204
+ if hasVariableShards {
205
+ tokens , err := processNumShards (shardsAttr , specb )
206
+ if err != nil {
207
+ return err
208
+ }
209
+ resultTokens = append (resultTokens , tokens )
210
+ continue
211
+ }
212
+ shardsVal , err := hcl .GetAttrInt (shardsAttr , errNumShards )
189
213
if err != nil {
190
214
return err
191
215
}
192
- resourceb .SetAttributeRaw (nRepSpecs , tokens )
216
+ for range shardsVal {
217
+ resultBodies = append (resultBodies , specb )
218
+ }
219
+ }
220
+ if hasVariableShards {
221
+ resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensFuncConcat (resultTokens ... ))
222
+ } else {
223
+ resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensArray (resultBodies ))
193
224
}
194
-
195
225
return nil
196
226
}
197
227
@@ -392,71 +422,6 @@ func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root
392
422
return hcl .EncloseBracketsNewLines (tokens ), nil
393
423
}
394
424
395
- func processVariableRepSpecs (repSpecBlocks []* hclwrite.Block , root attrVals ) ([]hclwrite.Tokens , error ) {
396
- var concatParts []hclwrite.Tokens
397
- for _ , block := range repSpecBlocks {
398
- tokens , err := processReplicationSpecBlock (block , root )
399
- if err != nil {
400
- return nil , err
401
- }
402
- concatParts = append (concatParts , tokens )
403
- }
404
- return concatParts , nil
405
- }
406
-
407
- func processStaticRepSpecs (repSpecBlocks []* hclwrite.Block , root attrVals ) (hclwrite.Tokens , error ) {
408
- var specbs []* hclwrite.Body
409
- for _ , block := range repSpecBlocks {
410
- spec := hclwrite .NewEmptyFile ()
411
- specb := spec .Body ()
412
- specbSrc := block .Body ()
413
- d , err := fillWithDynamicRegionConfigs (specbSrc , root , false )
414
- if err != nil {
415
- return nil , err
416
- }
417
- if d .IsPresent () {
418
- // For dynamic blocks that have numerical num_shards
419
- // This is complex, return the dynamic block as is
420
- return d .tokens , nil
421
- }
422
- _ = hcl .MoveAttr (specbSrc , specb , nZoneName , nZoneName , errRepSpecs )
423
- shardsAttr := specbSrc .GetAttribute (nNumShards )
424
- if shardsAttr == nil {
425
- return nil , fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
426
- }
427
- shardsVal , _ := hcl .GetAttrInt (shardsAttr , errNumShards )
428
- if errConfig := fillRegionConfigs (specb , specbSrc , root ); errConfig != nil {
429
- return nil , errConfig
430
- }
431
- for range shardsVal {
432
- specbs = append (specbs , specb )
433
- }
434
- }
435
- return hcl .TokensArray (specbs ), nil
436
- }
437
-
438
- func processReplicationSpecBlock (block * hclwrite.Block , root attrVals ) (hclwrite.Tokens , error ) {
439
- spec := hclwrite .NewEmptyFile ()
440
- specb := spec .Body ()
441
- specbSrc := block .Body ()
442
- d , err := fillWithDynamicRegionConfigs (specbSrc , root , false )
443
- if err != nil {
444
- return nil , err
445
- }
446
- if d .IsPresent () {
447
- return d .tokens , nil
448
- }
449
- _ = hcl .MoveAttr (specbSrc , specb , nZoneName , nZoneName , errRepSpecs )
450
- shardsAttr := specbSrc .GetAttribute (nNumShards )
451
- if shardsAttr == nil {
452
- return nil , fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
453
- }
454
- if errConfig := fillRegionConfigs (specb , specbSrc , root ); errConfig != nil {
455
- return nil , errConfig
456
- }
457
- return processNumShards (shardsAttr , specb )
458
- }
459
-
460
425
func sortConfigsByPriority (configs []* hclwrite.Body ) []* hclwrite.Body {
461
426
for _ , config := range configs {
462
427
if _ , err := hcl .GetAttrInt (config .GetAttribute (nPriority ), errPriority ); err != nil {
0 commit comments