terraform: set state properly when tainted

This commit is contained in:
Mitchell Hashimoto 2014-07-26 21:02:34 -07:00
parent 2094326c10
commit 7564a0a21c
5 changed files with 68 additions and 0 deletions

View File

@ -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) { func TestContextApply_unknownAttribute(t *testing.T) {
c := testConfig(t, "apply-unknown") c := testConfig(t, "apply-unknown")
p := testProvider("aws") p := testProvider("aws")

View File

@ -171,6 +171,10 @@ type ResourceAttrDiff struct {
Type DiffAttrType Type DiffAttrType
} }
func (d *ResourceAttrDiff) GoString() string {
return fmt.Sprintf("*%#v", *d)
}
// DiffAttrType is an enum type that says whether a resource attribute // DiffAttrType is an enum type that says whether a resource attribute
// diff is an input attribute (comes from the configuration) or an // diff is an input attribute (comes from the configuration) or an
// output attribute (comes as a result of applying the configuration). An // output attribute (comes as a result of applying the configuration). An

View File

@ -347,6 +347,15 @@ func graphAddDiff(g *depgraph.Graph, d *Diff) error {
Source: n, Source: n,
Target: newN, 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 rn.Resource.Diff = rd

View File

@ -166,6 +166,13 @@ aws_instance.foo:
num = 2 num = 2
` `
const testTerraformApplyTaintStr = `
aws_instance.bar:
ID = foo
num = 2
type = aws_instance
`
const testTerraformApplyOutputStr = ` const testTerraformApplyOutputStr = `
aws_instance.bar: aws_instance.bar:
ID = foo ID = foo

View File

@ -0,0 +1,4 @@
resource "aws_instance" "bar" {
id = "foo"
num = "2"
}