From b861f5a4d721c7f70af90c182c8e63d05fba8406 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 9 Jun 2020 21:58:58 -0400 Subject: [PATCH] unkeyed target ModulesInstance can be Modules If the last step in a module target is an unkeyed instance, and it's being compared against keyed instances, we have to assume it was intended to be used as a Module rather than a ModuleInstance. --- addrs/module_instance.go | 19 ++++++++++++++++++- addrs/target_test.go | 12 +++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/addrs/module_instance.go b/addrs/module_instance.go index 49cbf7863..75c69254a 100644 --- a/addrs/module_instance.go +++ b/addrs/module_instance.go @@ -414,9 +414,26 @@ func (m ModuleInstance) TargetContains(other Targetable) bool { } for i, ourStep := range m { otherStep := to[i] - if ourStep != otherStep { + + if ourStep.Name != otherStep.Name { return false } + + // if this is our last step, because all targets are parsed as + // instances, this may be a ModuleInstance intended to be used as a + // Module. + if i == len(m)-1 { + if ourStep.InstanceKey == NoKey { + // If the other step is a keyed instance, then we contain that + // step, and if it isn't it's a match, which is true either way + return true + } + } + + if ourStep.InstanceKey != otherStep.InstanceKey { + return false + } + } return true diff --git a/addrs/target_test.go b/addrs/target_test.go index be54f9427..69792c974 100644 --- a/addrs/target_test.go +++ b/addrs/target_test.go @@ -20,13 +20,6 @@ func TestTargetContains(t *testing.T) { mustParseTarget("module.foo"), true, }, - { - // module.foo is an unkeyed module instance here, so it cannot - // contain another instance - mustParseTarget("module.foo"), - mustParseTarget("module.foo[0]"), - false, - }, { RootModuleInstance, mustParseTarget("module.foo"), @@ -99,6 +92,11 @@ func TestTargetContains(t *testing.T) { mustParseTarget("module.bar.test_resource.foo[0]"), true, }, + { + mustParseTarget("module.bax"), + mustParseTarget("module.bax[0].test_resource.foo[0]"), + true, + }, // Config paths, while never returned from parsing a target, must still // be targetable