terraform: guard output types. Fixes #593

This commit is contained in:
Armon Dadgar 2014-11-24 16:49:38 -08:00
parent ccf7f40711
commit 5b025c89f9
5 changed files with 104 additions and 1 deletions

View File

@ -550,7 +550,14 @@ func (c *walkContext) Walk() error {
}
}
if vraw != nil {
outputs[o.Name] = vraw.(string)
if list, ok := vraw.([]interface{}); ok {
vraw = list[0]
}
if s, ok := vraw.(string); ok {
outputs[o.Name] = s
} else {
return fmt.Errorf("Type of output '%s' is not a string: %#v", o.Name, vraw)
}
}
}

View File

@ -2416,6 +2416,55 @@ func TestContextApply_output(t *testing.T) {
}
}
func TestContextApply_outputInvalid(t *testing.T) {
m := testModule(t, "apply-output-invalid")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
_, err := ctx.Plan(nil)
if err == nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(err.Error(), "is not a string") {
t.Fatalf("err: %s", err)
}
}
func TestContextApply_outputList(t *testing.T) {
m := testModule(t, "apply-output-list")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext(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(testTerraformApplyOutputListStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}
func TestContextApply_outputMulti(t *testing.T) {
m := testModule(t, "apply-output-multi")
p := testProvider("aws")

View File

@ -377,6 +377,29 @@ Outputs:
foo_num = 2
`
const testTerraformApplyOutputListStr = `
aws_instance.bar.0:
ID = foo
foo = bar
type = aws_instance
aws_instance.bar.1:
ID = foo
foo = bar
type = aws_instance
aws_instance.bar.2:
ID = foo
foo = bar
type = aws_instance
aws_instance.foo:
ID = foo
num = 2
type = aws_instance
Outputs:
foo_num = bar,bar,bar
`
const testTerraformApplyOutputMultiStr = `
aws_instance.bar.0:
ID = foo

View File

@ -0,0 +1,12 @@
resource "aws_instance" "foo" {
num = "2"
}
resource "aws_instance" "bar" {
foo = "bar"
count = 3
}
output "foo_num" {
value = 42
}

View File

@ -0,0 +1,12 @@
resource "aws_instance" "foo" {
num = "2"
}
resource "aws_instance" "bar" {
foo = "bar"
count = 3
}
output "foo_num" {
value = ["${join(",", aws_instance.bar.*.foo)}"]
}