From 8e421caba16d566dd46ff96dd9b0b0957b7400f7 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Fri, 19 Sep 2014 15:04:21 -0700 Subject: [PATCH] terraform: show each tainted ID --- terraform/context_test.go | 51 +++++++++++++++++++++++++++++++++++++ terraform/state.go | 10 ++++---- terraform/terraform_test.go | 24 +++++++++++++++-- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/terraform/context_test.go b/terraform/context_test.go index 780815bbb..8319f0da3 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1939,6 +1939,57 @@ func TestContextPlan_taint(t *testing.T) { } } +func TestContextPlan_multiple_taint(t *testing.T) { + c := testConfig(t, "plan-taint") + p := testProvider("aws") + p.DiffFn = testDiffFn + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{"num": "2"}, + }, + }, + "aws_instance.bar": &ResourceState{ + Type: "aws_instance", + Tainted: []*InstanceState{ + &InstanceState{ + ID: "baz", + }, + &InstanceState{ + ID: "zip", + }, + }, + }, + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Config: c, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + plan, err := ctx.Plan(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanMultipleTaintStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContextPlan_varMultiCountOne(t *testing.T) { c := testConfig(t, "plan-var-multi-count-one") p := testProvider("aws") diff --git a/terraform/state.go b/terraform/state.go index 7122d3763..d018362b8 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -125,11 +125,7 @@ func (s *State) String() string { id = rs.Primary.ID } if id == "" { - if len(rs.Tainted) > 0 { - id = rs.Tainted[0].ID - } else { - id = "" - } + id = "" } taintStr := "" @@ -159,6 +155,10 @@ func (s *State) String() string { buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av)) } + for idx, t := range rs.Tainted { + buf.WriteString(fmt.Sprintf(" Tainted ID %d = %s\n", idx+1, t.ID)) + } + if len(rs.Dependencies) > 0 { buf.WriteString(fmt.Sprintf("\n Dependencies:\n")) for _, dep := range rs.Dependencies { diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index e86c3f5e1..d2228f884 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -138,7 +138,8 @@ aws_instance.foo: const testTerraformApplyProvisionerFailStr = ` aws_instance.bar: (1 tainted) - ID = foo + ID = + Tainted ID 1 = foo aws_instance.foo: ID = foo num = 2 @@ -501,7 +502,26 @@ DESTROY/CREATE: aws_instance.bar STATE: aws_instance.bar: (1 tainted) - ID = baz + ID = + Tainted ID 1 = baz +aws_instance.foo: + ID = bar + num = 2 +` + +const testTerraformPlanMultipleTaintStr = ` +DIFF: + +DESTROY/CREATE: aws_instance.bar + foo: "" => "2" + type: "" => "aws_instance" + +STATE: + +aws_instance.bar: (2 tainted) + ID = + Tainted ID 1 = baz + Tainted ID 2 = zip aws_instance.foo: ID = bar num = 2