Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure destroy edges from data sources #32209

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions internal/terraform/node_resource_abstract_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,21 @@ func (n *NodeAbstractResourceInstance) References() []*addrs.Reference {
return nil
}

// StateDependencies returns the dependencies saved in the state.
// StateDependencies returns the dependencies which will be saved in the state
// for managed resources, or the most current dependencies for data resources.
func (n *NodeAbstractResourceInstance) StateDependencies() []addrs.ConfigResource {
// Managed resources prefer the stored dependencies, to avoid possible
// conflicts in ordering when refactoring configuration.
if s := n.instanceState; s != nil {
if s.Current != nil {
return s.Current.Dependencies
}
}

return nil
// If there are no stored dependencies, this is either a newly created
// managed resource, or a data source, and we can use the most recently
// calculated dependencies.
return n.Dependencies
}

// GraphNodeProviderConsumer
Expand Down
48 changes: 48 additions & 0 deletions internal/terraform/transform_destroy_edge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,54 @@ test_object.C (destroy)`)
}
}

func TestDestroyEdgeTransformer_dataDependsOn(t *testing.T) {
g := Graph{Path: addrs.RootModuleInstance}

addrA := mustResourceInstanceAddr("test_object.A")
instA := NewNodeAbstractResourceInstance(addrA)
a := &NodeDestroyResourceInstance{NodeAbstractResourceInstance: instA}
g.Add(a)

// B here represents a data sources, which is effectively an update during
// apply, but won't have dependencies stored in the state.
addrB := mustResourceInstanceAddr("test_object.B")
instB := NewNodeAbstractResourceInstance(addrB)
instB.Dependencies = append(instB.Dependencies, addrA.ConfigResource())
b := &NodeApplyableResourceInstance{NodeAbstractResourceInstance: instB}

g.Add(b)

state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.A").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"A"}`),
},
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
)

if err := (&AttachStateTransformer{State: state}).Transform(&g); err != nil {
t.Fatal(err)
}

tf := &DestroyEdgeTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}

actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(`
test_object.A (destroy)
test_object.B
test_object.A (destroy)
`)
if actual != expected {
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
}
}

func testDestroyNode(addrString string) GraphNodeDestroyer {
instAddr := mustResourceInstanceAddr(addrString)
inst := NewNodeAbstractResourceInstance(instAddr)
Expand Down