terraform: do the deposed check within EvalDiff

There is never any reason to separate the two.
This commit is contained in:
Mitchell Hashimoto 2016-11-28 13:58:12 -08:00
parent 8701434b4d
commit 80457b689c
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 46 additions and 41 deletions

View File

@ -11,6 +11,31 @@ func TestCountHook_impl(t *testing.T) {
var _ terraform.Hook = new(CountHook)
}
func TestCountHookPostDiff_DestroyDeposed(t *testing.T) {
h := new(CountHook)
resources := map[string]*terraform.InstanceDiff{
"lorem": &terraform.InstanceDiff{DestroyDeposed: true},
}
n := &terraform.InstanceInfo{} // TODO
for _, d := range resources {
h.PostDiff(n, d)
}
expected := new(CountHook)
expected.ToAdd = 0
expected.ToChange = 0
expected.ToRemoveAndAdd = 0
expected.ToRemove = 1
if !reflect.DeepEqual(expected, h) {
t.Fatalf("Expected %#v, got %#v instead.",
expected, h)
}
}
func TestCountHookPostDiff_DestroyOnly(t *testing.T) {
h := new(CountHook)

View File

@ -534,6 +534,13 @@ func (d *InstanceDiff) GetDestroyDeposed() bool {
return d.DestroyDeposed
}
func (d *InstanceDiff) SetDestroyDeposed(b bool) {
d.mu.Lock()
defer d.mu.Unlock()
d.DestroyDeposed = b
}
// These methods are properly locked, for use outside other InstanceDiff
// methods but everywhere else within in the terraform package.
// TODO refactor the locking scheme

View File

@ -69,6 +69,7 @@ func (n *EvalCompareDiff) Eval(ctx EvalContext) (interface{}, error) {
// EvalDiff is an EvalNode implementation that does a refresh for
// a resource.
type EvalDiff struct {
Name string
Info *InstanceInfo
Config **ResourceConfig
Provider *ResourceProvider
@ -112,6 +113,18 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
diff = new(InstanceDiff)
}
// Set DestroyDeposed if we have deposed instances
_, err = readInstanceFromState(ctx, n.Name, nil, func(rs *ResourceState) (*InstanceState, error) {
if len(rs.Deposed) > 0 {
diff.DestroyDeposed = true
}
return nil, nil
})
if err != nil {
return nil, err
}
// Preserve the DestroyTainted flag
if n.Diff != nil {
diff.SetTainted((*n.Diff).GetDestroyTainted())

View File

@ -1,37 +0,0 @@
package terraform
// EvalDiffDeposed is an EvalNode implementation that marks DestroyDeposed
// in a diff if a resource has deposed instances that need destruction.
type EvalDiffDeposed struct {
Name string
Diff **InstanceDiff
}
// TODO: test
func (n *EvalDiffDeposed) Eval(ctx EvalContext) (interface{}, error) {
// Check if there are any deposed items in the state
deposed := false
_, err := readInstanceFromState(ctx, n.Name, nil, func(rs *ResourceState) (*InstanceState, error) {
if len(rs.Deposed) > 0 {
deposed = true
}
return nil, nil
})
if err != nil {
return nil, err
}
// If no deposed items, just return
if !deposed {
return nil, nil
}
// Set the flag to true
if *n.Diff == nil {
*n.Diff = new(InstanceDiff)
}
(*n.Diff).DestroyDeposed = true
return nil, nil
}

View File

@ -169,6 +169,7 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(
Output: &state,
},
&EvalDiff{
Name: stateId,
Info: info,
Config: &resourceConfig,
Resource: n.Config,
@ -177,10 +178,6 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(
OutputDiff: &diff,
OutputState: &state,
},
&EvalDiffDeposed{
Name: stateId,
Diff: &diff,
},
&EvalCheckPreventDestroy{
Resource: n.Config,
Diff: &diff,