Skip to content

Commit

Permalink
Merge pull request #23696 from hashicorp/jbardin/orphan-resource-prov…
Browse files Browse the repository at this point in the history
…ider

NodeDestroyResource does not need a provider
  • Loading branch information
jbardin authored Dec 17, 2019
2 parents d2fc7aa + 414cbbe commit 37d2202
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
42 changes: 42 additions & 0 deletions terraform/graph_builder_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,48 @@ test_object.b
}
}

// The orphan clean up node should not be connected to a provider
func TestApplyGraphBuilder_orphanedWithProvider(t *testing.T) {
changes := &plans.Changes{
Resources: []*plans.ResourceInstanceChangeSrc{
{
Addr: mustResourceInstanceAddr("test_object.A"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Delete,
},
},
},
}

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.test.foo"),
)

b := &ApplyGraphBuilder{
Config: testModule(t, "graph-builder-orphan-alias"),
Changes: changes,
Components: simpleMockComponentFactory(),
Schemas: simpleTestSchemas(),
State: state,
}

g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatal(err)
}

// The cleanup node has no state or config of its own, so would create a
// default provider which we don't want.
testGraphNotContains(t, g, "provider.test")
}

const testApplyGraphBuilderStr = `
meta.count-boundary (EachMode fixup)
module.child.test_object.other
Expand Down
29 changes: 4 additions & 25 deletions terraform/node_resource_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,36 +277,15 @@ func (n *NodeDestroyResourceInstance) EvalTree() EvalNode {
// leaving skeleton resource objects in state after their instances have
// all been destroyed.
type NodeDestroyResource struct {
*NodeAbstractResource
NodeAbstractResource *NodeAbstractResource
}

var (
_ GraphNodeResource = (*NodeDestroyResource)(nil)
_ GraphNodeReferenceable = (*NodeDestroyResource)(nil)
_ GraphNodeReferencer = (*NodeDestroyResource)(nil)
_ GraphNodeEvalable = (*NodeDestroyResource)(nil)
_ GraphNodeEvalable = (*NodeDestroyResource)(nil)
)

func (n *NodeDestroyResource) Name() string {
return n.ResourceAddr().String() + " (clean up state)"
}

// GraphNodeReferenceable, overriding NodeAbstractResource
func (n *NodeDestroyResource) ReferenceableAddrs() []addrs.Referenceable {
// NodeDestroyResource doesn't participate in references: the graph
// builder that created it should ensure directly that it already depends
// on every other node related to its resource, without relying on
// references.
return nil
}

// GraphNodeReferencer, overriding NodeAbstractResource
func (n *NodeDestroyResource) References() []*addrs.Reference {
// NodeDestroyResource doesn't participate in references: the graph
// builder that created it should ensure directly that it already depends
// on every other node related to its resource, without relying on
// references.
return nil
return n.NodeAbstractResource.ResourceAddr().String() + " (clean up state)"
}

// GraphNodeEvalable
Expand All @@ -316,6 +295,6 @@ func (n *NodeDestroyResource) EvalTree() EvalNode {
// leftover husk of a resource in state after all of the child instances
// and their objects were destroyed.
return &EvalForgetResourceState{
Addr: n.ResourceAddr().Resource,
Addr: n.NodeAbstractResource.ResourceAddr().Resource,
}
}
3 changes: 3 additions & 0 deletions terraform/testdata/graph-builder-orphan-alias/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "test" {
alias = "foo"
}

0 comments on commit 37d2202

Please sign in to comment.