mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-12 08:26:30 -06:00
terraform: do the deposed check within EvalDiff
There is never any reason to separate the two.
This commit is contained in:
parent
8701434b4d
commit
80457b689c
@ -11,6 +11,31 @@ func TestCountHook_impl(t *testing.T) {
|
|||||||
var _ terraform.Hook = new(CountHook)
|
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) {
|
func TestCountHookPostDiff_DestroyOnly(t *testing.T) {
|
||||||
h := new(CountHook)
|
h := new(CountHook)
|
||||||
|
|
||||||
|
@ -534,6 +534,13 @@ func (d *InstanceDiff) GetDestroyDeposed() bool {
|
|||||||
return d.DestroyDeposed
|
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
|
// These methods are properly locked, for use outside other InstanceDiff
|
||||||
// methods but everywhere else within in the terraform package.
|
// methods but everywhere else within in the terraform package.
|
||||||
// TODO refactor the locking scheme
|
// TODO refactor the locking scheme
|
||||||
|
@ -69,6 +69,7 @@ func (n *EvalCompareDiff) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
// EvalDiff is an EvalNode implementation that does a refresh for
|
// EvalDiff is an EvalNode implementation that does a refresh for
|
||||||
// a resource.
|
// a resource.
|
||||||
type EvalDiff struct {
|
type EvalDiff struct {
|
||||||
|
Name string
|
||||||
Info *InstanceInfo
|
Info *InstanceInfo
|
||||||
Config **ResourceConfig
|
Config **ResourceConfig
|
||||||
Provider *ResourceProvider
|
Provider *ResourceProvider
|
||||||
@ -112,6 +113,18 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
diff = new(InstanceDiff)
|
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
|
// Preserve the DestroyTainted flag
|
||||||
if n.Diff != nil {
|
if n.Diff != nil {
|
||||||
diff.SetTainted((*n.Diff).GetDestroyTainted())
|
diff.SetTainted((*n.Diff).GetDestroyTainted())
|
||||||
|
@ -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
|
|
||||||
}
|
|
@ -169,6 +169,7 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(
|
|||||||
Output: &state,
|
Output: &state,
|
||||||
},
|
},
|
||||||
&EvalDiff{
|
&EvalDiff{
|
||||||
|
Name: stateId,
|
||||||
Info: info,
|
Info: info,
|
||||||
Config: &resourceConfig,
|
Config: &resourceConfig,
|
||||||
Resource: n.Config,
|
Resource: n.Config,
|
||||||
@ -177,10 +178,6 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(
|
|||||||
OutputDiff: &diff,
|
OutputDiff: &diff,
|
||||||
OutputState: &state,
|
OutputState: &state,
|
||||||
},
|
},
|
||||||
&EvalDiffDeposed{
|
|
||||||
Name: stateId,
|
|
||||||
Diff: &diff,
|
|
||||||
},
|
|
||||||
&EvalCheckPreventDestroy{
|
&EvalCheckPreventDestroy{
|
||||||
Resource: n.Config,
|
Resource: n.Config,
|
||||||
Diff: &diff,
|
Diff: &diff,
|
||||||
|
Loading…
Reference in New Issue
Block a user