Skip to content

Commit 87bf0fc

Browse files
committed
passing test
1 parent ff3fc22 commit 87bf0fc

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

internal/convert/convert.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func fillRegionConfigsDynamicBlock(specbSrc *hclwrite.Body, root attrVals) (dyna
331331
expr := replaceDynamicBlockExpr(attr, name, oldPrefix, nRegion)
332332
configSrcb.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
333333
}
334-
region, err := getRegionConfig(configSrc, root)
334+
region, err := getRegionConfig(configSrc, root, true)
335335
if err != nil {
336336
return dynamicBlock{}, err
337337
}
@@ -342,7 +342,6 @@ func fillRegionConfigsDynamicBlock(specbSrc *hclwrite.Body, root attrVals) (dyna
342342
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(zoneName))
343343
}
344344
config := region.BuildTokens(nil)
345-
config = append(config, hcl.TokenNewLine())
346345
config = hcl.EncloseBraces(config, true)
347346
config = append(config, hcl.TokensFromExpr("if region.priority == priority")...)
348347
forRegion := hcl.TokensFromExpr(fmt.Sprintf("for region in %s :", hcl.GetAttrExpr(d.forEach)))
@@ -367,7 +366,7 @@ func fillRegionConfigs(specb, specbSrc *hclwrite.Body, root attrVals) error {
367366
if configSrc == nil {
368367
break
369368
}
370-
config, err := getRegionConfig(configSrc, root)
369+
config, err := getRegionConfig(configSrc, root, false)
371370
if err != nil {
372371
return err
373372
}
@@ -382,7 +381,7 @@ func fillRegionConfigs(specb, specbSrc *hclwrite.Body, root attrVals) error {
382381
return nil
383382
}
384383

385-
func getRegionConfig(configSrc *hclwrite.Block, root attrVals) (*hclwrite.File, error) {
384+
func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bool) (*hclwrite.File, error) {
386385
file := hclwrite.NewEmptyFile()
387386
fileb := file.Body()
388387
fileb.SetAttributeRaw(nProviderName, root.req[nProviderName])
@@ -393,12 +392,30 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals) (*hclwrite.File,
393392
return nil, err
394393
}
395394
if electable, _ := getSpecs(configSrc, nElectableNodes, root); electable != nil {
395+
if isDynamicBlock {
396+
tokens := hcl.TokensFromExpr(fmt.Sprintf("region.%s > 0 ?", nElectableNodes))
397+
tokens = append(tokens, electable...)
398+
tokens = append(tokens, hcl.TokensFromExpr(": null")...)
399+
electable = tokens
400+
}
396401
fileb.SetAttributeRaw(nElectableSpecs, electable)
397402
}
398403
if readOnly, _ := getSpecs(configSrc, nReadOnlyNodes, root); readOnly != nil {
404+
if isDynamicBlock {
405+
tokens := hcl.TokensFromExpr(fmt.Sprintf("region.%s > 0 ?", nReadOnlyNodes))
406+
tokens = append(tokens, readOnly...)
407+
tokens = append(tokens, hcl.TokensFromExpr(": null")...)
408+
readOnly = tokens
409+
}
399410
fileb.SetAttributeRaw(nReadOnlySpecs, readOnly)
400411
}
401412
if analytics, _ := getSpecs(configSrc, nAnalyticsNodes, root); analytics != nil {
413+
if isDynamicBlock {
414+
tokens := hcl.TokensFromExpr(fmt.Sprintf("region.%s > 0 ?", nAnalyticsNodes))
415+
tokens = append(tokens, analytics...)
416+
tokens = append(tokens, hcl.TokensFromExpr(": null")...)
417+
analytics = tokens
418+
}
402419
fileb.SetAttributeRaw(nAnalyticsSpecs, analytics)
403420
}
404421
if autoScaling := getAutoScalingOpt(root.opt); autoScaling != nil {

0 commit comments

Comments
 (0)