terraform: only depose state if we're doing the destroy side
This commit is contained in:
parent
fa222a44c3
commit
4b6f258f1f
|
@ -2787,6 +2787,60 @@ func TestContext2Apply_createBeforeDestroy(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_createBeforeDestroyUpdate(t *testing.T) {
|
||||
m := testModule(t, "apply-good-create-before-update")
|
||||
p := testProvider("aws")
|
||||
p.ApplyFn = testApplyFn
|
||||
p.DiffFn = testDiffFn
|
||||
state := &State{
|
||||
Modules: []*ModuleState{
|
||||
&ModuleState{
|
||||
Path: rootModulePath,
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.bar": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Primary: &InstanceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
State: state,
|
||||
})
|
||||
|
||||
if p, err := ctx.Plan(nil); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
} else {
|
||||
t.Logf(p.String())
|
||||
}
|
||||
|
||||
state, err := ctx.Apply()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
mod := state.RootModule()
|
||||
if len(mod.Resources) != 1 {
|
||||
t.Fatalf("bad: %s", state)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(state.String())
|
||||
expected := strings.TrimSpace(testTerraformApplyCreateBeforeUpdateStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: \n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_minimal(t *testing.T) {
|
||||
m := testModule(t, "apply-minimal")
|
||||
p := testProvider("aws")
|
||||
|
|
|
@ -200,6 +200,13 @@ aws_instance.bar:
|
|||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyCreateBeforeUpdateStr = `
|
||||
aws_instance.bar:
|
||||
ID = foo
|
||||
foo = baz
|
||||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyCancelStr = `
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
resource "aws_instance" "bar" {
|
||||
foo = "baz"
|
||||
|
||||
lifecycle {
|
||||
create_before_destroy = true
|
||||
}
|
||||
}
|
|
@ -256,6 +256,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
|||
var diffApply *InstanceDiff
|
||||
var err error
|
||||
var createNew, tainted bool
|
||||
var createBeforeDestroyEnabled bool
|
||||
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
||||
Ops: []walkOperation{walkApply},
|
||||
Node: &EvalSequence{
|
||||
|
@ -285,7 +286,16 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
|||
|
||||
&EvalIf{
|
||||
If: func(ctx EvalContext) (bool, error) {
|
||||
return n.Resource.Lifecycle.CreateBeforeDestroy, nil
|
||||
destroy := false
|
||||
if diffApply != nil {
|
||||
destroy = diffApply.Destroy || diffApply.RequiresNew()
|
||||
}
|
||||
|
||||
createBeforeDestroyEnabled =
|
||||
n.Resource.Lifecycle.CreateBeforeDestroy &&
|
||||
destroy
|
||||
|
||||
return createBeforeDestroyEnabled, nil
|
||||
},
|
||||
Node: &EvalDeposeState{
|
||||
Name: n.stateId(),
|
||||
|
@ -361,12 +371,12 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
|||
},
|
||||
&EvalIf{
|
||||
If: func(ctx EvalContext) (bool, error) {
|
||||
if n.Resource.Lifecycle.CreateBeforeDestroy {
|
||||
if createBeforeDestroyEnabled {
|
||||
tainted = err != nil
|
||||
}
|
||||
|
||||
failure := tainted || err != nil
|
||||
return n.Resource.Lifecycle.CreateBeforeDestroy && failure, nil
|
||||
return createBeforeDestroyEnabled && failure, nil
|
||||
},
|
||||
Node: &EvalUndeposeState{
|
||||
Name: n.stateId(),
|
||||
|
|
Loading…
Reference in New Issue