Merge pull request #11042 from hashicorp/GH-10920
fix flatmap.Expand to work with the schema.Set representation
This commit is contained in:
commit
97d1244d1a
|
@ -2,6 +2,7 @@ package flatmap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -42,9 +43,43 @@ func expandArray(m map[string]string, prefix string) []interface{} {
|
||||||
panic(err)
|
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)
|
result := make([]interface{}, num)
|
||||||
for i := 0; i < int(num); i++ {
|
for i, key := range keysList {
|
||||||
result[i] = Expand(m, fmt.Sprintf("%s.%d", prefix, i))
|
result[i] = Expand(m, fmt.Sprintf("%s.%d", prefix, key))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -106,6 +106,17 @@ func TestExpand(t *testing.T) {
|
||||||
"list2": []interface{}{"c"},
|
"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 {
|
for _, tc := range cases {
|
||||||
|
|
|
@ -838,6 +838,44 @@ func TestInterpolator_nestedMapsAndLists(t *testing.T) {
|
||||||
interfaceToVariableSwallowError(mapOfList))
|
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(
|
func testInterpolate(
|
||||||
t *testing.T, i *Interpolater,
|
t *testing.T, i *Interpolater,
|
||||||
scope *InterpolationScope,
|
scope *InterpolationScope,
|
||||||
|
|
Loading…
Reference in New Issue