orphan resources needs to use AbsResource

The expand logic was separated into
nodeExpandRefreshableManagedResource, but the orphan logic wasn't
updated.
This commit is contained in:
James Bardin 2020-03-25 15:07:35 -04:00
parent 7f0199bab0
commit 8eb3f2cf52
4 changed files with 24 additions and 26 deletions

View File

@ -147,7 +147,7 @@ func (n *NodeRefreshableDataResource) DynamicExpand(ctx EvalContext) (*Graph, er
// directly as NodeDestroyableDataResource. // directly as NodeDestroyableDataResource.
&OrphanResourceCountTransformer{ &OrphanResourceCountTransformer{
Concrete: concreteResourceDestroyable, Concrete: concreteResourceDestroyable,
Addr: n.ResourceAddr(), Addr: n.Addr,
InstanceAddrs: instanceAddrs, InstanceAddrs: instanceAddrs,
State: state, State: state,
}, },

View File

@ -209,7 +209,7 @@ func (n *NodePlannableResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
// Add the count/for_each orphans // Add the count/for_each orphans
&OrphanResourceCountTransformer{ &OrphanResourceCountTransformer{
Concrete: concreteResourceOrphan, Concrete: concreteResourceOrphan,
Addr: n.ResourceAddr(), Addr: n.Addr,
InstanceAddrs: instanceAddrs, InstanceAddrs: instanceAddrs,
State: state, State: state,
}, },

View File

@ -144,7 +144,7 @@ func (n *NodeRefreshableManagedResource) DynamicExpand(ctx EvalContext) (*Graph,
// during a scale in. // during a scale in.
&OrphanResourceCountTransformer{ &OrphanResourceCountTransformer{
Concrete: concreteResource, Concrete: concreteResource,
Addr: n.Addr.Config(), Addr: n.Addr,
InstanceAddrs: instanceAddrs, InstanceAddrs: instanceAddrs,
State: state, State: state,
}, },

View File

@ -18,39 +18,37 @@ import (
type OrphanResourceCountTransformer struct { type OrphanResourceCountTransformer struct {
Concrete ConcreteResourceInstanceNodeFunc Concrete ConcreteResourceInstanceNodeFunc
Addr addrs.ConfigResource // Addr of the resource to look for orphans Addr addrs.AbsResource // Addr of the resource to look for orphans
InstanceAddrs []addrs.AbsResourceInstance // Addresses that currently exist in config InstanceAddrs []addrs.AbsResourceInstance // Addresses that currently exist in config
State *states.State // Full global state State *states.State // Full global state
} }
func (t *OrphanResourceCountTransformer) Transform(g *Graph) error { func (t *OrphanResourceCountTransformer) Transform(g *Graph) error {
resources := t.State.Resources(t.Addr) rs := t.State.Resource(t.Addr)
if len(resources) == 0 { if rs == nil {
return nil // Resource doesn't exist in state, so nothing to do! return nil // Resource doesn't exist in state, so nothing to do!
} }
for _, rs := range resources { // This is an O(n*m) analysis, which we accept for now because the
// This is an O(n*m) analysis, which we accept for now because the // number of instances of a single resource ought to always be small in any
// number of instances of a single resource ought to always be small in any // reasonable Terraform configuration.
// reasonable Terraform configuration. Have:
Have: for key := range rs.Instances {
for key := range rs.Instances { thisAddr := rs.Addr.Instance(key)
thisAddr := rs.Addr.Instance(key) for _, wantAddr := range t.InstanceAddrs {
for _, wantAddr := range t.InstanceAddrs { if wantAddr.Equal(thisAddr) {
if wantAddr.Equal(thisAddr) { continue Have
continue Have
}
} }
// If thisAddr is not in t.InstanceAddrs then we've found an "orphan"
abstract := NewNodeAbstractResourceInstance(thisAddr)
var node dag.Vertex = abstract
if f := t.Concrete; f != nil {
node = f(abstract)
}
log.Printf("[TRACE] OrphanResourceCountTransformer: adding %s as %T", thisAddr, node)
g.Add(node)
} }
// If thisAddr is not in t.InstanceAddrs then we've found an "orphan"
abstract := NewNodeAbstractResourceInstance(thisAddr)
var node dag.Vertex = abstract
if f := t.Concrete; f != nil {
node = f(abstract)
}
log.Printf("[TRACE] OrphanResourceCountTransformer: adding %s as %T", thisAddr, node)
g.Add(node)
} }
return nil return nil