mirror of https://github.com/hashicorp/terraform
Merge pull request #7493 from hashicorp/b-pass-map-to-module
terraform: allow literal maps to be passed to modulespull/7557/head
commit
1401a52a5c
@ -0,0 +1,142 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCoerceMapVariable(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
Input *EvalCoerceMapVariable
|
||||
ExpectVars map[string]interface{}
|
||||
}{
|
||||
"a valid map is untouched": {
|
||||
Input: &EvalCoerceMapVariable{
|
||||
Variables: map[string]interface{}{
|
||||
"amap": map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
ModulePath: []string{"root"},
|
||||
ModuleTree: testModuleInline(t, map[string]string{
|
||||
"main.tf": `
|
||||
variable "amap" {
|
||||
type = "map"
|
||||
}
|
||||
`,
|
||||
}),
|
||||
},
|
||||
ExpectVars: map[string]interface{}{
|
||||
"amap": map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
"a list w/ a single map element is coerced": {
|
||||
Input: &EvalCoerceMapVariable{
|
||||
Variables: map[string]interface{}{
|
||||
"amap": []interface{}{
|
||||
map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
ModulePath: []string{"root"},
|
||||
ModuleTree: testModuleInline(t, map[string]string{
|
||||
"main.tf": `
|
||||
variable "amap" {
|
||||
type = "map"
|
||||
}
|
||||
`,
|
||||
}),
|
||||
},
|
||||
ExpectVars: map[string]interface{}{
|
||||
"amap": map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
"a list w/ more than one map element is untouched": {
|
||||
Input: &EvalCoerceMapVariable{
|
||||
Variables: map[string]interface{}{
|
||||
"amap": []interface{}{
|
||||
map[string]interface{}{"foo": "bar"},
|
||||
map[string]interface{}{"baz": "qux"},
|
||||
},
|
||||
},
|
||||
ModulePath: []string{"root"},
|
||||
ModuleTree: testModuleInline(t, map[string]string{
|
||||
"main.tf": `
|
||||
variable "amap" {
|
||||
type = "map"
|
||||
}
|
||||
`,
|
||||
}),
|
||||
},
|
||||
ExpectVars: map[string]interface{}{
|
||||
"amap": []interface{}{
|
||||
map[string]interface{}{"foo": "bar"},
|
||||
map[string]interface{}{"baz": "qux"},
|
||||
},
|
||||
},
|
||||
},
|
||||
"list coercion also works in a module": {
|
||||
Input: &EvalCoerceMapVariable{
|
||||
Variables: map[string]interface{}{
|
||||
"amap": []interface{}{
|
||||
map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
ModulePath: []string{"root", "middle", "bottom"},
|
||||
ModuleTree: testModuleInline(t, map[string]string{
|
||||
"top.tf": `
|
||||
module "middle" {
|
||||
source = "./middle"
|
||||
}
|
||||
`,
|
||||
"middle/mid.tf": `
|
||||
module "bottom" {
|
||||
source = "./bottom"
|
||||
amap {
|
||||
foo = "bar"
|
||||
}
|
||||
}
|
||||
`,
|
||||
"middle/bottom/bot.tf": `
|
||||
variable "amap" {
|
||||
type = "map"
|
||||
}
|
||||
`,
|
||||
}),
|
||||
},
|
||||
ExpectVars: map[string]interface{}{
|
||||
"amap": map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
"coercion only occurs when target var is a map": {
|
||||
Input: &EvalCoerceMapVariable{
|
||||
Variables: map[string]interface{}{
|
||||
"alist": []interface{}{
|
||||
map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
ModulePath: []string{"root"},
|
||||
ModuleTree: testModuleInline(t, map[string]string{
|
||||
"main.tf": `
|
||||
variable "alist" {
|
||||
type = "list"
|
||||
}
|
||||
`,
|
||||
}),
|
||||
},
|
||||
ExpectVars: map[string]interface{}{
|
||||
"alist": []interface{}{
|
||||
map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for tn, tc := range cases {
|
||||
_, err := tc.Input.Eval(&MockEvalContext{})
|
||||
if err != nil {
|
||||
t.Fatalf("%s: Unexpected err: %s", tn, err)
|
||||
}
|
||||
if !reflect.DeepEqual(tc.Input.Variables, tc.ExpectVars) {
|
||||
t.Fatalf("%s: Expected variables:\n\n%#v\n\nGot:\n\n%#v",
|
||||
tn, tc.ExpectVars, tc.Input.Variables)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
variable "amap" {
|
||||
type = "map"
|
||||
}
|
||||
|
||||
variable "othermap" {
|
||||
type = "map"
|
||||
}
|
||||
|
||||
resource "aws_instance" "foo" {
|
||||
tags = "${var.amap}"
|
||||
meta = "${var.othermap}"
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
module "child" {
|
||||
source = "./child"
|
||||
amap {
|
||||
foo = "bar"
|
||||
}
|
||||
othermap {}
|
||||
}
|
||||
Loading…
Reference in new issue