From bf91bff2c896180b4e873576aa0d9ecd276feb2e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 12 Mar 2020 18:14:44 -0400 Subject: [PATCH] TargetContains improvements Simplify Module.TargetContains Handle the case of a keyed instance address in ModuleInstance.TargetContains. --- addrs/module.go | 12 +----------- addrs/module_instance.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/addrs/module.go b/addrs/module.go index fb41ded61..62271b298 100644 --- a/addrs/module.go +++ b/addrs/module.go @@ -71,17 +71,7 @@ func (m Module) TargetContains(other Targetable) bool { return true case ModuleInstance: - if len(to) < len(m) { - return false - } - for i, ourStep := range m { - otherStep := to[i] - // This is where ModuleInstance differs from Module - if ourStep != otherStep.Name { - return false - } - } - return true + return m.TargetContains(to.Module()) case AbsResource: return m.TargetContains(to.Module) diff --git a/addrs/module_instance.go b/addrs/module_instance.go index 9647e5e5c..40f765ebc 100644 --- a/addrs/module_instance.go +++ b/addrs/module_instance.go @@ -391,6 +391,16 @@ func (m ModuleInstance) TargetContains(other Targetable) bool { // Other is contained if its steps match for the length of our own path. for i, ourStep := range m { otherStep := to[i] + + // We can't contain an entire module if we have a specific instance + // key. The case of NoKey is OK because this address is either + // meant to address an unexpanded module, or a single instance of + // that module, and both of those are a covered in-full by the + // Module address. + if ourStep.InstanceKey != NoKey { + return false + } + if ourStep.Name != otherStep { return false }