@@ -264,8 +264,8 @@ func extractTagsLabelsDynamicBlock(resourceb *hclwrite.Body, name string) (hclwr
264264 if key == nil || value == nil {
265265 return nil , fmt .Errorf ("dynamic block %s: %s or %s not found" , name , nKey , nValue )
266266 }
267- keyExpr := replaceDynamicBlockExpr (key , nKey , name , "" )
268- valueExpr := replaceDynamicBlockExpr (value , nValue , name , "" )
267+ keyExpr := replaceDynamicBlockExpr (key , name , nKey )
268+ valueExpr := replaceDynamicBlockExpr (value , name , nValue )
269269 collectionExpr := hcl .GetAttrExpr (d .forEach )
270270 forExpr := fmt .Sprintf ("for key, value in %s : %s => %s" , collectionExpr , keyExpr , valueExpr )
271271 tokens := hcl .TokensObjectFromExpr (forExpr )
@@ -321,16 +321,24 @@ func fillRegionConfigsDynamicBlock(specbSrc *hclwrite.Body, root attrVals) (dyna
321321 return dynamicBlock {}, fmt .Errorf ("%s: %s not found" , errRepSpecs , nNumShards )
322322 }
323323 zoneName := hcl .GetAttrExpr (specbSrc .GetAttribute (nZoneName ))
324- const nRegion = "region"
325324 configSrc := d .content
326325 configSrcb := configSrc .Body ()
326+ var priorityStr string
327327
328- oldPrefix := fmt .Sprintf ("%s.%s" , nConfigSrc , nValue )
329328 // Change value references in all attributes, e.g. regions_config.value.electable_nodes to region.electable_nodes
330329 for name , attr := range configSrcb .Attributes () {
331- expr := replaceDynamicBlockExpr (attr , name , oldPrefix , nRegion )
330+ expr := hcl .GetAttrExpr (attr )
331+ expr = strings .ReplaceAll (expr ,
332+ fmt .Sprintf ("%s.%s." , nConfigSrc , nValue ),
333+ fmt .Sprintf ("%s." , nRegion ))
334+ if name == nPriority {
335+ priorityStr = expr
336+ }
332337 configSrcb .SetAttributeRaw (name , hcl .TokensFromExpr (expr ))
333338 }
339+ if priorityStr == "" {
340+ return dynamicBlock {}, fmt .Errorf ("%s: %s not found" , errRepSpecs , nPriority )
341+ }
334342 region , err := getRegionConfig (configSrc , root , true )
335343 if err != nil {
336344 return dynamicBlock {}, err
@@ -343,7 +351,7 @@ func fillRegionConfigsDynamicBlock(specbSrc *hclwrite.Body, root attrVals) (dyna
343351 }
344352 config := region .BuildTokens (nil )
345353 config = hcl .EncloseBraces (config , true )
346- config = append (config , hcl .TokensFromExpr ("if region. priority == priority" )... )
354+ config = append (config , hcl .TokensFromExpr (fmt . Sprintf ( "if priority == %s" , priorityStr ) )... )
347355 forRegion := hcl .TokensFromExpr (fmt .Sprintf ("for region in %s :" , hcl .GetAttrExpr (d .forEach )))
348356 forRegion = append (forRegion , config ... )
349357
@@ -546,13 +554,9 @@ func getDynamicBlock(body *hclwrite.Body, name string) (dynamicBlock, error) {
546554 return dynamicBlock {}, nil
547555}
548556
549- func replaceDynamicBlockExpr (attr * hclwrite.Attribute , attrName , oldPrefix , newPrefix string ) string {
557+ func replaceDynamicBlockExpr (attr * hclwrite.Attribute , blockName , attrName string ) string {
550558 expr := hcl .GetAttrExpr (attr )
551- newStr := attrName
552- if newPrefix != "" {
553- newStr = fmt .Sprintf ("%s.%s" , newPrefix , attrName )
554- }
555- return strings .ReplaceAll (expr , fmt .Sprintf ("%s.%s" , oldPrefix , attrName ), newStr )
559+ return strings .ReplaceAll (expr , fmt .Sprintf ("%s.%s" , blockName , attrName ), attrName )
556560}
557561
558562func sortConfigsByPriority (configs []* hclwrite.Body ) []* hclwrite.Body {
0 commit comments