Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flatmap.Expand to work with the schema.Set representation #11042

Merged
merged 4 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions flatmap/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flatmap

import (
"fmt"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -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{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your PR explanation is great, but I'd also add a short comment above here to explain why we're doing this versus the normal incremental range. Changing it would of course fail a test, but in the future I don't want a maintainer to think "but why did jbardin do this?!" Yes, you could always git blame, which is why I said "short comment" to explain the gist and then say something like "see GH-11042 for full details"

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
Expand Down
11 changes: 11 additions & 0 deletions flatmap/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions terraform/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down