mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
core: Fix interpolation tests with nested lists
Some of the tests for splat syntax were from the pre-list-and-map world, and effectively flattened the values if interpolating a resource value which was itself a list. We now set the expected values correctly so that an interpolation like `aws_instance.test.*.security_group_ids` now returns a list of lists. We also fix the implementation to correctly deal with maps.
This commit is contained in:
parent
a0f8e7bd04
commit
d955c5191c
@ -493,7 +493,8 @@ func (i *Interpolater) computeResourceMultiVariable(
|
|||||||
if module == nil || len(module.Resources) == 0 {
|
if module == nil || len(module.Resources) == 0 {
|
||||||
return &unknownVariable, nil
|
return &unknownVariable, nil
|
||||||
}
|
}
|
||||||
var values []string
|
|
||||||
|
var values []interface{}
|
||||||
for j := 0; j < count; j++ {
|
for j := 0; j < count; j++ {
|
||||||
id := fmt.Sprintf("%s.%d", v.ResourceId(), j)
|
id := fmt.Sprintf("%s.%d", v.ResourceId(), j)
|
||||||
|
|
||||||
@ -521,9 +522,10 @@ func (i *Interpolater) computeResourceMultiVariable(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// computed list attribute
|
// computed list or map attribute
|
||||||
_, ok = r.Primary.Attributes[v.Field+".#"]
|
_, isList := r.Primary.Attributes[v.Field+".#"]
|
||||||
if !ok {
|
_, isMap := r.Primary.Attributes[v.Field+".%"]
|
||||||
|
if !(isList || isMap) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
multiAttr, err := i.interpolateComplexTypeAttribute(v.Field, r.Primary.Attributes)
|
multiAttr, err := i.interpolateComplexTypeAttribute(v.Field, r.Primary.Attributes)
|
||||||
@ -535,14 +537,7 @@ func (i *Interpolater) computeResourceMultiVariable(
|
|||||||
return &ast.Variable{Type: ast.TypeString, Value: ""}, nil
|
return &ast.Variable{Type: ast.TypeString, Value: ""}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, element := range multiAttr.Value.([]ast.Variable) {
|
values = append(values, multiAttr)
|
||||||
strVal := element.Value.(string)
|
|
||||||
if strVal == config.UnknownVariableValue {
|
|
||||||
return &unknownVariable, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
values = append(values, strVal)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(values) == 0 {
|
if len(values) == 0 {
|
||||||
@ -595,7 +590,7 @@ func (i *Interpolater) interpolateComplexTypeAttribute(
|
|||||||
|
|
||||||
keys := make([]string, 0)
|
keys := make([]string, 0)
|
||||||
listElementKey := regexp.MustCompile("^" + resourceID + "\\.[0-9]+$")
|
listElementKey := regexp.MustCompile("^" + resourceID + "\\.[0-9]+$")
|
||||||
for id, _ := range attributes {
|
for id := range attributes {
|
||||||
if listElementKey.MatchString(id) {
|
if listElementKey.MatchString(id) {
|
||||||
keys = append(keys, id)
|
keys = append(keys, id)
|
||||||
}
|
}
|
||||||
|
@ -449,11 +449,11 @@ func TestInterpolator_resourceMultiAttributesWithResourceCount(t *testing.T) {
|
|||||||
|
|
||||||
// More than 1 element
|
// More than 1 element
|
||||||
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.name_servers",
|
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.name_servers",
|
||||||
interfaceToVariableSwallowError(name_servers[0:4]))
|
interfaceToVariableSwallowError(name_servers[:4]))
|
||||||
|
|
||||||
// More than 1 element in both
|
// More than 1 element in both
|
||||||
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.name_servers",
|
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.name_servers",
|
||||||
interfaceToVariableSwallowError(name_servers))
|
interfaceToVariableSwallowError([]interface{}{name_servers[:4], name_servers[4:]}))
|
||||||
|
|
||||||
// Exactly 1 element
|
// Exactly 1 element
|
||||||
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.listeners",
|
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.listeners",
|
||||||
@ -461,7 +461,7 @@ func TestInterpolator_resourceMultiAttributesWithResourceCount(t *testing.T) {
|
|||||||
|
|
||||||
// Exactly 1 element in both
|
// Exactly 1 element in both
|
||||||
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.listeners",
|
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.listeners",
|
||||||
interfaceToVariableSwallowError([]interface{}{"red", "blue"}))
|
interfaceToVariableSwallowError([]interface{}{[]interface{}{"red"}, []interface{}{"blue"}}))
|
||||||
|
|
||||||
// Zero elements
|
// Zero elements
|
||||||
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.nothing",
|
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.nothing",
|
||||||
@ -469,7 +469,7 @@ func TestInterpolator_resourceMultiAttributesWithResourceCount(t *testing.T) {
|
|||||||
|
|
||||||
// Zero + 1 element
|
// Zero + 1 element
|
||||||
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.special",
|
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.special",
|
||||||
interfaceToVariableSwallowError([]interface{}{"extra"}))
|
interfaceToVariableSwallowError([]interface{}{[]interface{}{"extra"}}))
|
||||||
|
|
||||||
// Maps still need to work
|
// Maps still need to work
|
||||||
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.tags.Name", ast.Variable{
|
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.tags.Name", ast.Variable{
|
||||||
|
Loading…
Reference in New Issue
Block a user