@@ -179,13 +179,13 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
179
179
return fmt .Errorf ("%s: no replication_specs found" , errRepSpecs )
180
180
}
181
181
if hasVariableNumShards (repSpecBlocks ) {
182
- tokens , err := processVariableReplicationSpecs (repSpecBlocks , root )
182
+ tokens , err := processVariableRepSpecs (repSpecBlocks , root )
183
183
if err != nil {
184
184
return err
185
185
}
186
186
resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensFuncConcat (tokens ... ))
187
187
} else {
188
- tokens , err := processStaticReplicationSpecs ( resourceb , repSpecBlocks , root )
188
+ tokens , err := processStaticRepSpecs ( repSpecBlocks , root )
189
189
if err != nil {
190
190
return err
191
191
}
@@ -195,76 +195,6 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
195
195
return nil
196
196
}
197
197
198
- func fillTagsLabelsOpt (resourceb * hclwrite.Body , name string ) error {
199
- tokensDynamic , err := extractTagsLabelsDynamicBlock (resourceb , name )
200
- if err != nil {
201
- return err
202
- }
203
- tokensIndividual , err := extractTagsLabelsIndividual (resourceb , name )
204
- if err != nil {
205
- return err
206
- }
207
- if tokensDynamic != nil && tokensIndividual != nil {
208
- resourceb .SetAttributeRaw (name , hcl .TokensFuncMerge (tokensDynamic , tokensIndividual ))
209
- return nil
210
- }
211
- if tokensDynamic != nil {
212
- resourceb .SetAttributeRaw (name , tokensDynamic )
213
- }
214
- if tokensIndividual != nil {
215
- resourceb .SetAttributeRaw (name , tokensIndividual )
216
- }
217
- return nil
218
- }
219
-
220
- func extractTagsLabelsDynamicBlock (resourceb * hclwrite.Body , name string ) (hclwrite.Tokens , error ) {
221
- d , err := getDynamicBlock (resourceb , name )
222
- if err != nil || ! d .IsPresent () {
223
- return nil , err
224
- }
225
- key := d .content .Body ().GetAttribute (nKey )
226
- value := d .content .Body ().GetAttribute (nValue )
227
- if key == nil || value == nil {
228
- return nil , fmt .Errorf ("dynamic block %s: %s or %s not found" , name , nKey , nValue )
229
- }
230
- keyExpr := replaceDynamicBlockExpr (key , name , nKey )
231
- valueExpr := replaceDynamicBlockExpr (value , name , nValue )
232
- collectionExpr := hcl .GetAttrExpr (d .forEach )
233
- forExpr := fmt .Sprintf ("for key, value in %s : %s => %s" , collectionExpr , keyExpr , valueExpr )
234
- tokens := hcl .EncloseBraces (hcl .EncloseNewLines (hcl .TokensFromExpr (forExpr )), false )
235
- if keyExpr == nKey && valueExpr == nValue { // expression can be simplified and use for_each expression
236
- tokens = hcl .TokensFromExpr (collectionExpr )
237
- }
238
- resourceb .RemoveBlock (d .block )
239
- return tokens , nil
240
- }
241
-
242
- func extractTagsLabelsIndividual (resourceb * hclwrite.Body , name string ) (hclwrite.Tokens , error ) {
243
- var (
244
- file = hclwrite .NewEmptyFile ()
245
- fileb = file .Body ()
246
- found = false
247
- )
248
- for {
249
- block := resourceb .FirstMatchingBlock (name , nil )
250
- if block == nil {
251
- break
252
- }
253
- key := block .Body ().GetAttribute (nKey )
254
- value := block .Body ().GetAttribute (nValue )
255
- if key == nil || value == nil {
256
- return nil , fmt .Errorf ("%s: %s or %s not found" , name , nKey , nValue )
257
- }
258
- setKeyValue (fileb , key , value )
259
- resourceb .RemoveBlock (block )
260
- found = true
261
- }
262
- if ! found {
263
- return nil , nil
264
- }
265
- return hcl .TokensObject (fileb ), nil
266
- }
267
-
268
198
// fillReplicationSpecsWithDynamicBlock used for dynamic blocks in replication_specs
269
199
func fillReplicationSpecsWithDynamicBlock (resourceb * hclwrite.Body , root attrVals ) (dynamicBlock , error ) {
270
200
dSpec , err := getDynamicBlock (resourceb , nRepSpecs )
@@ -307,7 +237,6 @@ func fillWithDynamicRegionConfigs(specbSrc *hclwrite.Body, root attrVals, change
307
237
priorityFor = append (priorityFor , hcl .TokensFromExpr (priorityForStr )... )
308
238
priorityFor = append (priorityFor , regionFor ... )
309
239
repSpecb .SetAttributeRaw (nConfig , hcl .TokensFuncFlatten (priorityFor ))
310
-
311
240
shards := specbSrc .GetAttribute (nNumShards )
312
241
if shards == nil {
313
242
return dynamicBlock {}, fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
@@ -463,10 +392,10 @@ func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root
463
392
return hcl .EncloseBracketsNewLines (tokens ), nil
464
393
}
465
394
466
- func processVariableReplicationSpecs (repSpecBlocks []* hclwrite.Block , root attrVals ) ([]hclwrite.Tokens , error ) {
395
+ func processVariableRepSpecs (repSpecBlocks []* hclwrite.Block , root attrVals ) ([]hclwrite.Tokens , error ) {
467
396
var concatParts []hclwrite.Tokens
468
397
for _ , block := range repSpecBlocks {
469
- tokens , err := processReplicationSpecBlock (block , root , true )
398
+ tokens , err := processReplicationSpecBlock (block , root )
470
399
if err != nil {
471
400
return nil , err
472
401
}
@@ -475,14 +404,12 @@ func processVariableReplicationSpecs(repSpecBlocks []*hclwrite.Block, root attrV
475
404
return concatParts , nil
476
405
}
477
406
478
- func processStaticReplicationSpecs (resourceb * hclwrite.Body , repSpecBlocks []* hclwrite.Block ,
479
- root attrVals ) (hclwrite.Tokens , error ) {
407
+ func processStaticRepSpecs (repSpecBlocks []* hclwrite.Block , root attrVals ) (hclwrite.Tokens , error ) {
480
408
var specbs []* hclwrite.Body
481
409
for _ , block := range repSpecBlocks {
482
410
spec := hclwrite .NewEmptyFile ()
483
411
specb := spec .Body ()
484
412
specbSrc := block .Body ()
485
-
486
413
d , err := fillWithDynamicRegionConfigs (specbSrc , root , false )
487
414
if err != nil {
488
415
return nil , err
@@ -492,50 +419,41 @@ func processStaticReplicationSpecs(resourceb *hclwrite.Body, repSpecBlocks []*hc
492
419
// This is complex, return the dynamic block as is
493
420
return d .tokens , nil
494
421
}
495
-
496
422
_ = hcl .MoveAttr (specbSrc , specb , nZoneName , nZoneName , errRepSpecs )
497
-
498
423
shardsAttr := specbSrc .GetAttribute (nNumShards )
499
424
if shardsAttr == nil {
500
425
return nil , fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
501
426
}
502
-
503
427
shardsVal , _ := hcl .GetAttrInt (shardsAttr , errNumShards )
504
-
505
428
if errConfig := fillRegionConfigs (specb , specbSrc , root ); errConfig != nil {
506
429
return nil , errConfig
507
430
}
508
-
509
431
for range shardsVal {
510
432
specbs = append (specbs , specb )
511
433
}
512
434
}
513
435
return hcl .TokensArray (specbs ), nil
514
436
}
515
437
516
- func processReplicationSpecBlock (block * hclwrite.Block , root attrVals , isVariable bool ) (hclwrite.Tokens , error ) {
438
+ func processReplicationSpecBlock (block * hclwrite.Block , root attrVals ) (hclwrite.Tokens , error ) {
517
439
spec := hclwrite .NewEmptyFile ()
518
440
specb := spec .Body ()
519
441
specbSrc := block .Body ()
520
-
521
442
d , err := fillWithDynamicRegionConfigs (specbSrc , root , false )
522
443
if err != nil {
523
444
return nil , err
524
445
}
525
446
if d .IsPresent () {
526
447
return d .tokens , nil
527
448
}
528
-
529
449
_ = hcl .MoveAttr (specbSrc , specb , nZoneName , nZoneName , errRepSpecs )
530
450
shardsAttr := specbSrc .GetAttribute (nNumShards )
531
451
if shardsAttr == nil {
532
452
return nil , fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
533
453
}
534
-
535
454
if errConfig := fillRegionConfigs (specb , specbSrc , root ); errConfig != nil {
536
455
return nil , errConfig
537
456
}
538
-
539
457
return processNumShards (shardsAttr , specb )
540
458
}
541
459
0 commit comments