terraform: set state properly when tainted
This commit is contained in:
parent
2094326c10
commit
7564a0a21c
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
resource "aws_instance" "bar" {
|
||||
id = "foo"
|
||||
num = "2"
|
||||
}
|
Loading…
Reference in New Issue