Skip to content

Commit 80457b6

Browse files
committed
terraform: do the deposed check within EvalDiff
There is never any reason to separate the two.
1 parent 8701434 commit 80457b6

5 files changed

Lines changed: 46 additions & 41 deletions

File tree

‎command/hook_count_test.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ func TestCountHook_impl(t *testing.T) {
1111
var _ terraform.Hook = new(CountHook)
1212
}
1313

14+
func TestCountHookPostDiff_DestroyDeposed(t *testing.T) {
15+
h := new(CountHook)
16+
17+
resources := map[string]*terraform.InstanceDiff{
18+
"lorem": &terraform.InstanceDiff{DestroyDeposed: true},
19+
}
20+
21+
n := &terraform.InstanceInfo{} // TODO
22+
23+
for _, d := range resources {
24+
h.PostDiff(n, d)
25+
}
26+
27+
expected := new(CountHook)
28+
expected.ToAdd = 0
29+
expected.ToChange = 0
30+
expected.ToRemoveAndAdd = 0
31+
expected.ToRemove = 1
32+
33+
if !reflect.DeepEqual(expected, h) {
34+
t.Fatalf("Expected %#v, got %#v instead.",
35+
expected, h)
36+
}
37+
}
38+
1439
func TestCountHookPostDiff_DestroyOnly(t *testing.T) {
1540
h := new(CountHook)
1641

‎terraform/diff.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@ func (d *InstanceDiff) GetDestroyDeposed() bool {
534534
return d.DestroyDeposed
535535
}
536536

537+
func (d *InstanceDiff) SetDestroyDeposed(b bool) {
538+
d.mu.Lock()
539+
defer d.mu.Unlock()
540+
541+
d.DestroyDeposed = b
542+
}
543+
537544
// These methods are properly locked, for use outside other InstanceDiff
538545
// methods but everywhere else within in the terraform package.
539546
// TODO refactor the locking scheme

‎terraform/eval_diff.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (n *EvalCompareDiff) Eval(ctx EvalContext) (interface{}, error) {
6969
// EvalDiff is an EvalNode implementation that does a refresh for
7070
// a resource.
7171
type EvalDiff struct {
72+
Name string
7273
Info *InstanceInfo
7374
Config **ResourceConfig
7475
Provider *ResourceProvider
@@ -112,6 +113,18 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
112113
diff = new(InstanceDiff)
113114
}
114115

116+
// Set DestroyDeposed if we have deposed instances
117+
_, err = readInstanceFromState(ctx, n.Name, nil, func(rs *ResourceState) (*InstanceState, error) {
118+
if len(rs.Deposed) > 0 {
119+
diff.DestroyDeposed = true
120+
}
121+
122+
return nil, nil
123+
})
124+
if err != nil {
125+
return nil, err
126+
}
127+
115128
// Preserve the DestroyTainted flag
116129
if n.Diff != nil {
117130
diff.SetTainted((*n.Diff).GetDestroyTainted())

‎terraform/eval_diff_deposed.go‎

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎terraform/node_resource_plan_instance.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(
169169
Output: &state,
170170
},
171171
&EvalDiff{
172+
Name: stateId,
172173
Info: info,
173174
Config: &resourceConfig,
174175
Resource: n.Config,
@@ -177,10 +178,6 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(
177178
OutputDiff: &diff,
178179
OutputState: &state,
179180
},
180-
&EvalDiffDeposed{
181-
Name: stateId,
182-
Diff: &diff,
183-
},
184181
&EvalCheckPreventDestroy{
185182
Resource: n.Config,
186183
Diff: &diff,

0 commit comments

Comments
 (0)