Merge pull request #11042 from hashicorp/GH-10920

fix flatmap.Expand to work with the schema.Set representation
This commit is contained in:
James Bardin 2017-01-09 09:48:11 -05:00 committed by GitHub
commit 97d1244d1a
3 changed files with 86 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package flatmap
import (
"fmt"
"sort"
"strconv"
"strings"
)
@ -42,9 +43,43 @@ func expandArray(m map[string]string, prefix string) []interface{} {
panic(err)
}
// The Schema "Set" type stores its values in an array format, but using
// numeric hash values instead of ordinal keys. Take the set of keys
// regardless of value, and expand them in numeric order.
// See GH-11042 for more details.
keySet := map[int]bool{}
for k := range m {
if !strings.HasPrefix(k, prefix+".") {
continue
}
key := k[len(prefix)+1:]
idx := strings.Index(key, ".")
if idx != -1 {
key = key[:idx]
}
// skip the count value
if key == "#" {
continue
}
k, err := strconv.Atoi(key)
if err != nil {
panic(err)
}
keySet[int(k)] = true
}
keysList := make([]int, 0, num)
for key := range keySet {
keysList = append(keysList, key)
}
sort.Ints(keysList)
result := make([]interface{}, num)
for i := 0; i < int(num); i++ {
result[i] = Expand(m, fmt.Sprintf("%s.%d", prefix, i))
for i, key := range keysList {
result[i] = Expand(m, fmt.Sprintf("%s.%d", prefix, key))
}
return result

View File

@ -106,6 +106,17 @@ func TestExpand(t *testing.T) {
"list2": []interface{}{"c"},
},
},
{
Map: map[string]string{
"set.#": "3",
"set.1234": "a",
"set.1235": "b",
"set.1236": "c",
},
Key: "set",
Output: []interface{}{"a", "b", "c"},
},
}
for _, tc := range cases {

View File

@ -838,6 +838,44 @@ func TestInterpolator_nestedMapsAndLists(t *testing.T) {
interfaceToVariableSwallowError(mapOfList))
}
func TestInterpolator_sets(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_network_interface.set": &ResourceState{
Type: "aws_network_interface",
Dependencies: []string{},
Primary: &InstanceState{
ID: "null",
Attributes: map[string]string{
"private_ips.#": "1",
"private_ips.3977356764": "10.42.16.179",
},
},
},
},
},
},
}
i := &Interpolater{
Module: testModule(t, "interpolate-multi-vars"),
StateLock: new(sync.RWMutex),
State: state,
}
scope := &InterpolationScope{
Path: rootModulePath,
}
set := []interface{}{"10.42.16.179"}
testInterpolate(t, i, scope, "aws_network_interface.set.private_ips",
interfaceToVariableSwallowError(set))
}
func testInterpolate(
t *testing.T, i *Interpolater,
scope *InterpolationScope,