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:
James Nugent 2016-07-11 11:40:21 -06:00
parent a0f8e7bd04
commit d955c5191c
2 changed files with 12 additions and 17 deletions

View File

@ -493,7 +493,8 @@ func (i *Interpolater) computeResourceMultiVariable(
if module == nil || len(module.Resources) == 0 {
return &unknownVariable, nil
}
var values []string
var values []interface{}
for j := 0; j < count; j++ {
id := fmt.Sprintf("%s.%d", v.ResourceId(), j)
@ -521,9 +522,10 @@ func (i *Interpolater) computeResourceMultiVariable(
continue
}
// computed list attribute
_, ok = r.Primary.Attributes[v.Field+".#"]
if !ok {
// computed list or map attribute
_, isList := r.Primary.Attributes[v.Field+".#"]
_, isMap := r.Primary.Attributes[v.Field+".%"]
if !(isList || isMap) {
continue
}
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
}
for _, element := range multiAttr.Value.([]ast.Variable) {
strVal := element.Value.(string)
if strVal == config.UnknownVariableValue {
return &unknownVariable, nil
}
values = append(values, strVal)
}
values = append(values, multiAttr)
}
if len(values) == 0 {
@ -595,7 +590,7 @@ func (i *Interpolater) interpolateComplexTypeAttribute(
keys := make([]string, 0)
listElementKey := regexp.MustCompile("^" + resourceID + "\\.[0-9]+$")
for id, _ := range attributes {
for id := range attributes {
if listElementKey.MatchString(id) {
keys = append(keys, id)
}

View File

@ -449,11 +449,11 @@ func TestInterpolator_resourceMultiAttributesWithResourceCount(t *testing.T) {
// More than 1 element
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
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.name_servers",
interfaceToVariableSwallowError(name_servers))
interfaceToVariableSwallowError([]interface{}{name_servers[:4], name_servers[4:]}))
// Exactly 1 element
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
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.listeners",
interfaceToVariableSwallowError([]interface{}{"red", "blue"}))
interfaceToVariableSwallowError([]interface{}{[]interface{}{"red"}, []interface{}{"blue"}}))
// Zero elements
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.nothing",
@ -469,7 +469,7 @@ func TestInterpolator_resourceMultiAttributesWithResourceCount(t *testing.T) {
// Zero + 1 element
testInterpolate(t, i, scope, "aws_route53_zone.terra.*.special",
interfaceToVariableSwallowError([]interface{}{"extra"}))
interfaceToVariableSwallowError([]interface{}{[]interface{}{"extra"}}))
// Maps still need to work
testInterpolate(t, i, scope, "aws_route53_zone.terra.0.tags.Name", ast.Variable{