terraform: fix more create-before-destroy state juggling

This commit is contained in:
Mitchell Hashimoto 2015-02-13 16:27:23 -08:00
parent 33cad6d207
commit c8091aa338
5 changed files with 22 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package terraform package terraform
import ( import (
"fmt"
"sync" "sync"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
@ -112,6 +113,7 @@ func (c *Context2) Apply() (*State, error) {
// Clean out any unused things // Clean out any unused things
c.state.prune() c.state.prune()
println(fmt.Sprintf("%#v", c.state))
return c.state, err return c.state, err
} }

View File

@ -3473,8 +3473,7 @@ func TestContext2Apply_error_createBeforeDestroy(t *testing.T) {
} }
} }
/* func TestContext2Apply_errorDestroy_createBeforeDestroy(t *testing.T) {
func TestContextApply_errorDestroy_createBeforeDestroy(t *testing.T) {
m := testModule(t, "apply-error-create-before") m := testModule(t, "apply-error-create-before")
p := testProvider("aws") p := testProvider("aws")
state := &State{ state := &State{
@ -3495,7 +3494,7 @@ func TestContextApply_errorDestroy_createBeforeDestroy(t *testing.T) {
}, },
}, },
} }
ctx := testContext(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
Module: m, Module: m,
Providers: map[string]ResourceProviderFactory{ Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p), "aws": testProviderFuncFixed(p),
@ -3531,7 +3530,6 @@ func TestContextApply_errorDestroy_createBeforeDestroy(t *testing.T) {
t.Fatalf("bad: actual:\n%s\n\nexpected:\n%s", actual, expected) t.Fatalf("bad: actual:\n%s\n\nexpected:\n%s", actual, expected)
} }
} }
*/
func TestContext2Apply_provisionerResourceRef(t *testing.T) { func TestContext2Apply_provisionerResourceRef(t *testing.T) {
m := testModule(t, "apply-provisioner-resource-ref") m := testModule(t, "apply-provisioner-resource-ref")

View File

@ -43,8 +43,16 @@ func (n *EvalReadState) Eval(
// Return the primary // Return the primary
result = rs.Primary result = rs.Primary
} else { } else {
// Return the proper tainted resource // Get the index. If it is negative, then we get the last one
result = rs.Tainted[n.TaintedIndex] idx := n.TaintedIndex
if idx < 0 {
idx = len(rs.Tainted) - 1
}
if idx < len(rs.Tainted) {
// Return the proper tainted resource
result = rs.Tainted[n.TaintedIndex]
}
} }
// Write the result to the output pointer // Write the result to the output pointer
@ -103,8 +111,6 @@ func (n *EvalWriteState) Eval(
rs.Type = n.ResourceType rs.Type = n.ResourceType
rs.Dependencies = n.Dependencies rs.Dependencies = n.Dependencies
println(fmt.Sprintf("%#v", rs))
println(fmt.Sprintf("%#v", *n.State))
if n.Tainted != nil && *n.Tainted { if n.Tainted != nil && *n.Tainted {
if n.TaintedIndex != -1 { if n.TaintedIndex != -1 {
rs.Tainted[n.TaintedIndex] = *n.State rs.Tainted[n.TaintedIndex] = *n.State
@ -119,9 +125,8 @@ func (n *EvalWriteState) Eval(
// Set the primary state // Set the primary state
rs.Primary = *n.State rs.Primary = *n.State
} }
println(fmt.Sprintf("%#v", rs))
// Prune because why not, we can clear out old useless entries now
rs.prune()
return nil, nil return nil, nil
} }
@ -216,7 +221,6 @@ func (n *EvalUndeposeState) Eval(
idx := len(rs.Tainted) - 1 idx := len(rs.Tainted) - 1
rs.Primary = rs.Tainted[idx] rs.Primary = rs.Tainted[idx]
rs.Tainted[idx] = nil rs.Tainted[idx] = nil
rs.Tainted = rs.Tainted[:idx]
return nil, nil return nil, nil
} }

View File

@ -373,6 +373,7 @@ func (m *ModuleState) deepcopy() *ModuleState {
func (m *ModuleState) prune() { func (m *ModuleState) prune() {
for k, v := range m.Resources { for k, v := range m.Resources {
v.prune() v.prune()
if (v.Primary == nil || v.Primary.ID == "") && len(v.Tainted) == 0 { if (v.Primary == nil || v.Primary.ID == "") && len(v.Tainted) == 0 {
delete(m.Resources, k) delete(m.Resources, k)
} }
@ -554,8 +555,10 @@ func (r *ResourceState) prune() {
copy(r.Tainted[i:], r.Tainted[i+1:]) copy(r.Tainted[i:], r.Tainted[i+1:])
r.Tainted[n-1] = nil r.Tainted[n-1] = nil
n-- n--
i--
} }
} }
r.Tainted = r.Tainted[:n] r.Tainted = r.Tainted[:n]
} }

View File

@ -315,8 +315,11 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
}, },
&EvalIf{ &EvalIf{
If: func(ctx EvalContext) (bool, error) { If: func(ctx EvalContext) (bool, error) {
if n.Resource.Lifecycle.CreateBeforeDestroy {
tainted = err != nil
}
failure := tainted || err != nil failure := tainted || err != nil
tainted = n.Resource.Lifecycle.CreateBeforeDestroy
return n.Resource.Lifecycle.CreateBeforeDestroy && failure, nil return n.Resource.Lifecycle.CreateBeforeDestroy && failure, nil
}, },
Node: &EvalUndeposeState{ Node: &EvalUndeposeState{