terraform: module inputs/vars can be non-strings [GH-819]

This commit is contained in:
Mitchell Hashimoto 2015-02-20 09:40:41 -08:00
parent 9c9bc9b9ca
commit 022967acdb
6 changed files with 77 additions and 2 deletions

View File

@ -3298,6 +3298,35 @@ func TestContext2Apply_moduleVarResourceCount(t *testing.T) {
}
}
// GH-819
func TestContext2Apply_moduleBool(t *testing.T) {
m := testModule(t, "apply-module-bool")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(nil); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyModuleBoolStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}
func TestContext2Apply_multiProvider(t *testing.T) {
m := testModule(t, "apply-multi-provider")
p := testProvider("aws")

View File

@ -1,7 +1,11 @@
package terraform
import (
"fmt"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/config"
"github.com/mitchellh/mapstructure"
)
// EvalSetVariables is an EvalNode implementation that sets the variables
@ -34,7 +38,13 @@ func (n *EvalVariableBlock) Eval(ctx EvalContext) (interface{}, error) {
// Get our configuration
rc := *n.Config
for k, v := range rc.Config {
n.Variables[k] = v.(string)
var vStr string
if err := mapstructure.WeakDecode(v, &vStr); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf(
"%s: error reading value: {{err}}", k), err)
}
n.Variables[k] = vStr
}
for k, _ := range rc.Raw {
if _, ok := n.Variables[k]; !ok {

View File

@ -231,7 +231,12 @@ func (s *State) String() string {
s := bufio.NewScanner(strings.NewReader(mStr))
for s.Scan() {
buf.WriteString(fmt.Sprintf(" %s\n", s.Text()))
text := s.Text()
if text != "" {
text = " " + text
}
buf.WriteString(fmt.Sprintf("%s\n", text))
}
}

View File

@ -285,6 +285,22 @@ module.child:
type = aws_instance
`
const testTerraformApplyModuleBoolStr = `
aws_instance.bar:
ID = foo
foo = 1
type = aws_instance
Dependencies:
module.child
module.child:
<no state>
Outputs:
leader = 1
`
const testTerraformApplyMultiProviderStr = `
aws_instance.bar:
ID = foo

View File

@ -0,0 +1,7 @@
variable "leader" {
default = false
}
output "leader" {
value = "${var.leader}"
}

View File

@ -0,0 +1,8 @@
module "child" {
source = "./child"
leader = true
}
resource "aws_instance" "bar" {
foo = "${module.child.leader}"
}