@@ -80,21 +80,14 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
80
80
blockb := block .Body ()
81
81
shardsAttr := blockb .GetAttribute (nNumShards )
82
82
blockb .RemoveAttribute (nNumShards )
83
- dConfig , err := getDynamicBlock (blockb , nConfig )
83
+ dConfig , err := convertConfigsWithDynamicBlock (blockb , diskSizeGB , false )
84
84
if err != nil {
85
85
return err
86
86
}
87
87
if dConfig .IsPresent () {
88
- if len (collectBlocks (blockb , nConfig )) > 0 {
89
- return errDynamicBlockAlone
90
- }
91
- transformReferences (dConfig .content .Body (), getResourceName (dConfig .block ), nRegion )
92
- copyAttributesSorted (dConfig .content .Body (), dConfig .content .Body ().Attributes ())
93
- processAllSpecs (dConfig .content .Body (), diskSizeGB )
94
- tokens := hcl .TokensFromExpr (buildForExpr (nRegion , hcl .GetAttrExpr (dConfig .forEach ), false ))
95
- tokens = append (tokens , hcl .TokensObject (dConfig .content .Body ())... )
96
- blockb .SetAttributeRaw (nConfig , hcl .EncloseBracketsNewLines (tokens ))
88
+ // Process the dynamic config and set it on this block
97
89
blockb .RemoveBlock (dConfig .block )
90
+ blockb .SetAttributeRaw (nConfig , dConfig .tokens )
98
91
} else {
99
92
var configs []* hclwrite.Body
100
93
for _ , configBlock := range collectBlocks (blockb , nConfig ) {
@@ -136,7 +129,7 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
136
129
return dynamicBlock {}, errDynamicBlockAlone
137
130
}
138
131
transformReferences (dSpec .content .Body (), nRepSpecs , nSpec )
139
- dConfig , err := convertConfigsWithDynamicBlock (dSpec .content .Body (), diskSizeGB )
132
+ dConfig , err := convertConfigsWithDynamicBlock (dSpec .content .Body (), diskSizeGB , true )
140
133
if err != nil {
141
134
return dynamicBlock {}, err
142
135
}
@@ -145,9 +138,11 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
145
138
return dSpec , nil
146
139
}
147
140
148
- func convertConfigsWithDynamicBlock (specbSrc * hclwrite.Body , diskSizeGB hclwrite.Tokens ) (dynamicBlock , error ) {
141
+ // convertConfigsWithDynamicBlock is used for processing dynamic blocks in region_configs
142
+ func convertConfigsWithDynamicBlock (specbSrc * hclwrite.Body , diskSizeGB hclwrite.Tokens ,
143
+ insideDynamicRepSpec bool ) (dynamicBlock , error ) {
149
144
d , err := getDynamicBlock (specbSrc , nConfig )
150
- if err != nil {
145
+ if err != nil || ! d . IsPresent () {
151
146
return dynamicBlock {}, err
152
147
}
153
148
if len (collectBlocks (specbSrc , nConfig )) > 0 {
@@ -167,17 +162,25 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
167
162
}
168
163
regionConfigBody .SetAttributeRaw (blockType , hcl .TokensObject (blockBody ))
169
164
}
165
+ forEach := hcl .GetAttrExpr (d .forEach )
166
+ if insideDynamicRepSpec {
167
+ forEach = fmt .Sprintf ("%s.%s" , nSpec , nConfig )
168
+ }
169
+ regionTokens := hcl .TokensFromExpr (buildForExpr (nRegion , forEach , false ))
170
+ regionTokens = append (regionTokens , hcl .TokensObject (regionConfigBody )... )
171
+ if ! insideDynamicRepSpec {
172
+ d .tokens = hcl .EncloseBracketsNewLines (regionTokens )
173
+ return d , nil
174
+ }
170
175
repSpecb := hclwrite .NewEmptyFile ().Body ()
171
176
if zoneNameAttr := specbSrc .GetAttribute (nZoneName ); zoneNameAttr != nil {
172
- repSpecb . SetAttributeRaw ( nZoneName , hcl .TokensFromExpr (
173
- transformReference ( hcl . GetAttrExpr ( zoneNameAttr ), nRepSpecs , nSpec ) ))
177
+ zoneNameExpr := transformReference ( hcl .GetAttrExpr ( zoneNameAttr ), nRepSpecs , nSpec )
178
+ repSpecb . SetAttributeRaw ( nZoneName , hcl . TokensFromExpr ( zoneNameExpr ))
174
179
}
175
- regionTokens := hcl .TokensFromExpr (buildForExpr (nRegion , fmt .Sprintf ("%s.%s" , nSpec , nConfig ), false ))
176
- regionTokens = append (regionTokens , hcl .TokensObject (regionConfigBody )... )
177
180
repSpecb .SetAttributeRaw (nConfig , hcl .EncloseBracketsNewLines (regionTokens ))
178
181
if numShardsAttr := specbSrc .GetAttribute (nNumShards ); numShardsAttr != nil {
179
- tokens := hcl .TokensFromExpr ( buildForExpr ( "i" ,
180
- fmt .Sprintf ("range(%s)" , transformReference ( hcl . GetAttrExpr ( numShardsAttr ), nRepSpecs , nSpec ) ), false ))
182
+ numShardsExpr := transformReference ( hcl .GetAttrExpr ( numShardsAttr ), nRepSpecs , nSpec )
183
+ tokens := hcl . TokensFromExpr ( buildForExpr ( "i" , fmt .Sprintf ("range(%s)" , numShardsExpr ), false ))
181
184
tokens = append (tokens , hcl .TokensObject (repSpecb )... )
182
185
return dynamicBlock {tokens : hcl .EncloseBracketsNewLines (tokens )}, nil
183
186
}
0 commit comments