diff --git a/plans/changes.go b/plans/changes.go index d7c86e6c2..04d070410 100644 --- a/plans/changes.go +++ b/plans/changes.go @@ -52,6 +52,22 @@ func (c *Changes) ResourceInstance(addr addrs.AbsResourceInstance) *ResourceInst } return nil + +} + +// ConfigResourceInstances returns the planned change for the current objects +// of the resource instances of the given address, if any. Returns nil if no +// changes are planned. +func (c *Changes) ConfigResourceInstances(addr addrs.ConfigResource) []*ResourceInstanceChangeSrc { + var changes []*ResourceInstanceChangeSrc + for _, rc := range c.Resources { + resAddr := rc.Addr.ContainingResource().Config() + if resAddr.Equal(addr) && rc.DeposedKey == states.NotDeposed { + changes = append(changes, rc) + } + } + + return changes } // ResourceInstanceDeposed returns the plan change of a deposed object of diff --git a/plans/changes_sync.go b/plans/changes_sync.go index 56323e0e9..97cac918e 100644 --- a/plans/changes_sync.go +++ b/plans/changes_sync.go @@ -62,6 +62,29 @@ func (cs *ChangesSync) GetResourceInstanceChange(addr addrs.AbsResourceInstance, panic(fmt.Sprintf("unsupported generation value %#v", gen)) } +// GetConfigResourceChanges searched the set of resource instance +// changes and returns all changes related to a given configuration address. +// This is be used to find possible changes related to a configuration +// reference. +// +// If no such changes exist, nil is returned. +// +// The returned objects are a deep copy of the change recorded in the plan, so +// callers may mutate them although it's generally better (less confusing) to +// treat planned changes as immutable after they've been initially constructed. +func (cs *ChangesSync) GetConfigResourceChanges(addr addrs.ConfigResource) []*ResourceInstanceChangeSrc { + if cs == nil { + panic("GetConfigResourceChanges on nil ChangesSync") + } + cs.lock.Lock() + defer cs.lock.Unlock() + var changes []*ResourceInstanceChangeSrc + for _, c := range cs.changes.ConfigResourceInstances(addr) { + changes = append(changes, c.DeepCopy()) + } + return changes +} + // RemoveResourceInstanceChange searches the set of resource instance changes // for one matching the given address and generation, and removes it from the // set if it exists.