From 5b025c89f92fffb7792ff1e350c610b8eac1c767 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 24 Nov 2014 16:49:38 -0800 Subject: [PATCH] terraform: guard output types. Fixes #593 --- terraform/context.go | 9 +++- terraform/context_test.go | 49 +++++++++++++++++++ terraform/terraform_test.go | 23 +++++++++ .../apply-output-invalid/main.tf | 12 +++++ .../test-fixtures/apply-output-list/main.tf | 12 +++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/apply-output-invalid/main.tf create mode 100644 terraform/test-fixtures/apply-output-list/main.tf diff --git a/terraform/context.go b/terraform/context.go index 81735063f..7546e9dae 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -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) + } } } diff --git a/terraform/context_test.go b/terraform/context_test.go index dcfc69d3a..99b031853 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -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") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 543862950..72568d267 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -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 diff --git a/terraform/test-fixtures/apply-output-invalid/main.tf b/terraform/test-fixtures/apply-output-invalid/main.tf new file mode 100644 index 000000000..ee7a9048a --- /dev/null +++ b/terraform/test-fixtures/apply-output-invalid/main.tf @@ -0,0 +1,12 @@ +resource "aws_instance" "foo" { + num = "2" +} + +resource "aws_instance" "bar" { + foo = "bar" + count = 3 +} + +output "foo_num" { + value = 42 +} diff --git a/terraform/test-fixtures/apply-output-list/main.tf b/terraform/test-fixtures/apply-output-list/main.tf new file mode 100644 index 000000000..11b8107df --- /dev/null +++ b/terraform/test-fixtures/apply-output-list/main.tf @@ -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)}"] +}