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

core: fix crash when computed nested map given in module block #13343

Merged
merged 2 commits into from
Apr 4, 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
14 changes: 9 additions & 5 deletions terraform/eval_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ type EvalVariableBlock struct {
VariableValues map[string]interface{}
}

// TODO: test
Copy link
Member

Choose a reason for hiding this comment

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

I love seeing these go away

func (n *EvalVariableBlock) Eval(ctx EvalContext) (interface{}, error) {
// Clear out the existing mapping
for k, _ := range n.VariableValues {
Expand Down Expand Up @@ -174,17 +173,22 @@ func (n *EvalVariableBlock) setUnknownVariableValueForPath(path string) error {
// Otherwise find the correct point in the tree and then set to unknown
var current interface{} = n.VariableValues[pathComponents[0]]
for i := 1; i < len(pathComponents); i++ {
switch current.(type) {
case []interface{}, []map[string]interface{}:
tCurrent := current.([]interface{})
switch tCurrent := current.(type) {
case []interface{}:
index, err := strconv.Atoi(pathComponents[i])
if err != nil {
return fmt.Errorf("Cannot convert %s to slice index in path %s",
pathComponents[i], path)
}
current = tCurrent[index]
case []map[string]interface{}:
index, err := strconv.Atoi(pathComponents[i])
if err != nil {
return fmt.Errorf("Cannot convert %s to slice index in path %s",
pathComponents[i], path)
}
current = tCurrent[index]
case map[string]interface{}:
tCurrent := current.(map[string]interface{})
if val, hasVal := tCurrent[pathComponents[i]]; hasVal {
current = val
continue
Expand Down
77 changes: 77 additions & 0 deletions terraform/eval_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package terraform
import (
"reflect"
"testing"

"github.com/hashicorp/terraform/config"
)

func TestCoerceMapVariable(t *testing.T) {
Expand Down Expand Up @@ -140,3 +142,78 @@ func TestCoerceMapVariable(t *testing.T) {
}
}
}

func TestEvalVariableBlock(t *testing.T) {
rc, err := config.NewRawConfig(map[string]interface{}{
"known": "foo",
"known_list": []interface{}{"foo"},
"known_map": map[string]interface{}{
"foo": "foo",
},
"known_list_of_maps": []map[string]interface{}{
map[string]interface{}{
"foo": "foo",
},
},
"computed_map": map[string]interface{}{},
"computed_list_of_maps": []map[string]interface{}{
map[string]interface{}{},
},
// No computed_list right now, because that isn't currently supported:
// EvalVariableBlock assumes the final step of the path will always
// be a map.
})
if err != nil {
t.Fatalf("config.NewRawConfig failed: %s", err)
}

cfg := NewResourceConfig(rc)
cfg.ComputedKeys = []string{
"computed",
"computed_map.foo",
"computed_list_of_maps.0.foo",
}

n := &EvalVariableBlock{
VariableValues: map[string]interface{}{
// Should be cleared out on Eval
"should_be_deleted": true,
},
Config: &cfg,
}

ctx := &MockEvalContext{}
val, err := n.Eval(ctx)
if err != nil {
t.Fatalf("n.Eval failed: %s", err)
}
if val != nil {
t.Fatalf("n.Eval returned non-nil result: %#v", val)
}

got := n.VariableValues
want := map[string]interface{}{
"known": "foo",
"known_list": []interface{}{"foo"},
"known_map": map[string]interface{}{
"foo": "foo",
},
"known_list_of_maps": []interface{}{
map[string]interface{}{
"foo": "foo",
},
},
"computed": config.UnknownVariableValue,
"computed_map": map[string]interface{}{
"foo": config.UnknownVariableValue,
},
"computed_list_of_maps": []interface{}{
map[string]interface{}{
"foo": config.UnknownVariableValue,
},
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Incorrect variables\ngot: %#v\nwant: %#v", got, want)
}
}