diff --git a/terraform/context_test.go b/terraform/context_test.go index 6c0fa081c..1c658d4f2 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1205,6 +1205,50 @@ func TestContextApply_outputMultiIndex(t *testing.T) { } } +func TestContextApply_taint(t *testing.T) { + c := testConfig(t, "apply-taint") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + s := &State{ + Resources: map[string]*ResourceState{ + "aws_instance.bar": &ResourceState{ + ID: "baz", + Type: "aws_instance", + Attributes: map[string]string{ + "num": "2", + "type": "aws_instance", + }, + }, + }, + Tainted: map[string]struct{}{ + "aws_instance.bar": struct{}{}, + }, + } + ctx := testContext(t, &ContextOpts{ + Config: c, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + 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(testTerraformApplyTaintStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContextApply_unknownAttribute(t *testing.T) { c := testConfig(t, "apply-unknown") p := testProvider("aws") diff --git a/terraform/diff.go b/terraform/diff.go index 7f492e883..b2b120685 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -171,6 +171,10 @@ type ResourceAttrDiff struct { Type DiffAttrType } +func (d *ResourceAttrDiff) GoString() string { + return fmt.Sprintf("*%#v", *d) +} + // DiffAttrType is an enum type that says whether a resource attribute // diff is an input attribute (comes from the configuration) or an // output attribute (comes as a result of applying the configuration). An diff --git a/terraform/graph.go b/terraform/graph.go index 5c9dfdfa2..a3f2ca96a 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -347,6 +347,15 @@ func graphAddDiff(g *depgraph.Graph, d *Diff) error { Source: n, Target: newN, }) + + // If the resource is tainted, mark the state as nil so + // that a fresh create is done. + if rn.Resource.Tainted { + rn.Resource.State = &ResourceState{ + Type: rn.Resource.State.Type, + } + rn.Resource.Tainted = false + } } rn.Resource.Diff = rd diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 384006ab0..b852864cf 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -166,6 +166,13 @@ aws_instance.foo: num = 2 ` +const testTerraformApplyTaintStr = ` +aws_instance.bar: + ID = foo + num = 2 + type = aws_instance +` + const testTerraformApplyOutputStr = ` aws_instance.bar: ID = foo diff --git a/terraform/test-fixtures/apply-taint/main.tf b/terraform/test-fixtures/apply-taint/main.tf new file mode 100644 index 000000000..fb8e23f34 --- /dev/null +++ b/terraform/test-fixtures/apply-taint/main.tf @@ -0,0 +1,4 @@ +resource "aws_instance" "bar" { + id = "foo" + num = "2" +}