@@ -198,19 +198,8 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
198198 }
199199
200200 // Check if any replication_specs has a variable num_shards
201- hasVariableNumShards := false
202- for _ , block := range repSpecBlocks {
203- shardsAttr := block .Body ().GetAttribute (nNumShards )
204- if shardsAttr != nil {
205- _ , err := hcl .GetAttrInt (shardsAttr , errNumShards )
206- if err != nil {
207- hasVariableNumShards = true
208- break
209- }
210- }
211- }
201+ hasVariableNumShards := HasVariableNumShards (repSpecBlocks )
212202
213- // If we have any variable num_shards, we need to use concat
214203 if hasVariableNumShards {
215204 var concatParts []hclwrite.Tokens
216205
@@ -238,27 +227,15 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
238227 return fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
239228 }
240229
241- shardsVal , err := hcl .GetAttrInt (shardsAttr , errNumShards )
242-
243230 if errConfig := fillRegionConfigs (specb , specbSrc , root ); errConfig != nil {
244231 return errConfig
245232 }
246233
234+ tokens , err := ProcessNumShards (shardsAttr , specb )
247235 if err != nil {
248- // num_shards is a variable/expression
249- shardsExpr := hcl .GetAttrExpr (shardsAttr )
250- forExpr := fmt .Sprintf ("for i in range(%s) :" , shardsExpr )
251- tokens := hcl .TokensFromExpr (forExpr )
252- tokens = append (tokens , hcl .TokensObject (specb )... )
253- concatParts = append (concatParts , hcl .EncloseBracketsNewLines (tokens ))
254- } else {
255- // num_shards is a literal number - create explicit array
256- var specs []* hclwrite.Body
257- for range shardsVal {
258- specs = append (specs , specb )
259- }
260- concatParts = append (concatParts , hcl .TokensArray (specs ))
236+ return err
261237 }
238+ concatParts = append (concatParts , tokens )
262239 }
263240
264241 // Use concat to combine all parts
@@ -299,8 +276,8 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
299276
300277 shardsVal , _ := hcl .GetAttrInt (shardsAttr , errNumShards )
301278
302- if err := fillRegionConfigs (specb , specbSrc , root ); err != nil {
303- return err
279+ if errConfig := fillRegionConfigs (specb , specbSrc , root ); errConfig != nil {
280+ return errConfig
304281 }
305282
306283 for range shardsVal {
@@ -383,29 +360,6 @@ func extractTagsLabelsIndividual(resourceb *hclwrite.Body, name string) (hclwrit
383360 return hcl .TokensObject (fileb ), nil
384361}
385362
386- func fillBlockOpt (resourceb * hclwrite.Body , name string ) {
387- block := resourceb .FirstMatchingBlock (name , nil )
388- if block == nil {
389- return
390- }
391- resourceb .RemoveBlock (block )
392- resourceb .SetAttributeRaw (name , hcl .TokensObject (block .Body ()))
393- }
394-
395- func fillAdvConfigOpt (resourceb * hclwrite.Body ) {
396- block := resourceb .FirstMatchingBlock (nAdvConfig , nil )
397- if block == nil {
398- return
399- }
400- blockBody := block .Body ()
401-
402- // Remove deprecated attributes from advanced_configuration
403- blockBody .RemoveAttribute (nFailIndexKeyTooLong )
404- blockBody .RemoveAttribute (nDefaultReadConcern )
405-
406- fillBlockOpt (resourceb , nAdvConfig )
407- }
408-
409363// fillReplicationSpecsWithDynamicBlock used for dynamic blocks in replication_specs
410364func fillReplicationSpecsWithDynamicBlock (resourceb * hclwrite.Body , root attrVals ) (dynamicBlock , error ) {
411365 dSpec , err := getDynamicBlock (resourceb , nRepSpecs )
@@ -571,16 +525,6 @@ func setResourceName(resource *hclwrite.Block, name string) {
571525 resource .SetLabels (labels )
572526}
573527
574- // getResourceName returns the first label of a block, if it exists.
575- // e.g. in resource "mongodbatlas_cluster" "mycluster", the first label is "mongodbatlas_cluster".
576- func getResourceName (resource * hclwrite.Block ) string {
577- labels := resource .Labels ()
578- if len (labels ) == 0 {
579- return ""
580- }
581- return labels [0 ]
582- }
583-
584528// getResourceLabel returns the second label of a block, if it exists.
585529// e.g. in resource "mongodbatlas_cluster" "mycluster", the second label is "mycluster".
586530func getResourceLabel (resource * hclwrite.Block ) string {
@@ -591,17 +535,6 @@ func getResourceLabel(resource *hclwrite.Block) string {
591535 return labels [1 ]
592536}
593537
594- type dynamicBlock struct {
595- block * hclwrite.Block
596- forEach * hclwrite.Attribute
597- content * hclwrite.Block
598- tokens hclwrite.Tokens
599- }
600-
601- func (d dynamicBlock ) IsPresent () bool {
602- return d .block != nil
603- }
604-
605538func checkDynamicBlock (body * hclwrite.Body ) error {
606539 for _ , block := range body .Blocks () {
607540 name := getResourceName (block )
@@ -613,25 +546,6 @@ func checkDynamicBlock(body *hclwrite.Body) error {
613546 return nil
614547}
615548
616- func getDynamicBlock (body * hclwrite.Body , name string ) (dynamicBlock , error ) {
617- for _ , block := range body .Blocks () {
618- if block .Type () != nDynamic || name != getResourceName (block ) {
619- continue
620- }
621- blockb := block .Body ()
622- forEach := blockb .GetAttribute (nForEach )
623- if forEach == nil {
624- return dynamicBlock {}, fmt .Errorf ("dynamic block %s: attribute %s not found" , name , nForEach )
625- }
626- content := blockb .FirstMatchingBlock (nContent , nil )
627- if content == nil {
628- return dynamicBlock {}, fmt .Errorf ("dynamic block %s: block %s not found" , name , nContent )
629- }
630- return dynamicBlock {forEach : forEach , block : block , content : content }, nil
631- }
632- return dynamicBlock {}, nil
633- }
634-
635549func replaceDynamicBlockExpr (attr * hclwrite.Attribute , blockName , attrName string ) string {
636550 expr := hcl .GetAttrExpr (attr )
637551 return strings .ReplaceAll (expr , fmt .Sprintf ("%s.%s" , blockName , attrName ), attrName )
@@ -655,21 +569,6 @@ func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root
655569 return hcl .EncloseBracketsNewLines (tokens ), nil
656570}
657571
658- func transformDynamicBlockReferences (configSrcb * hclwrite.Body , blockName , varName string ) {
659- for name , attr := range configSrcb .Attributes () {
660- expr := replaceDynamicBlockReferences (hcl .GetAttrExpr (attr ), blockName , varName )
661- configSrcb .SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
662- }
663- }
664-
665- // replaceDynamicBlockReferences changes value references,
666- // e.g. regions_config.value.electable_nodes to region.electable_nodes
667- func replaceDynamicBlockReferences (expr , blockName , varName string ) string {
668- return strings .ReplaceAll (expr ,
669- fmt .Sprintf ("%s.%s." , blockName , nValue ),
670- fmt .Sprintf ("%s." , varName ))
671- }
672-
673572func sortConfigsByPriority (configs []* hclwrite.Body ) []* hclwrite.Body {
674573 for _ , config := range configs {
675574 if _ , err := hcl .GetAttrInt (config .GetAttribute (nPriority ), errPriority ); err != nil {
0 commit comments