diff --git a/addrs/module.go b/addrs/module.go index 62271b2984..2a3ffe3fcc 100644 --- a/addrs/module.go +++ b/addrs/module.go @@ -73,6 +73,9 @@ func (m Module) TargetContains(other Targetable) bool { case ModuleInstance: return m.TargetContains(to.Module()) + case ConfigResource: + 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 40f765ebca..49cbf78630 100644 --- a/addrs/module_instance.go +++ b/addrs/module_instance.go @@ -420,6 +420,9 @@ func (m ModuleInstance) TargetContains(other Targetable) bool { } return true + case ConfigResource: + return m.TargetContains(to.Module) + case AbsResource: return m.TargetContains(to.Module) diff --git a/addrs/resource.go b/addrs/resource.go index 4bad01d150..56b2e51190 100644 --- a/addrs/resource.go +++ b/addrs/resource.go @@ -145,6 +145,11 @@ func (r AbsResource) TargetContains(other Targetable) bool { // We'll use our stringification as a cheat-ish way to test for equality. return to.String() == r.String() + case ConfigResource: + // if an absolute resource from parsing a target address contains a + // ConfigResource, the string representation will match + return to.String() == r.String() + case AbsResourceInstance: return r.TargetContains(to.ContainingResource()) @@ -203,9 +208,15 @@ func (r AbsResourceInstance) ContainingResource() AbsResource { func (r AbsResourceInstance) TargetContains(other Targetable) bool { switch to := other.(type) { + // while we currently don't start with an AbsResourceInstance as a target + // address, check all resource types for consistency. case AbsResourceInstance: // We'll use our stringification as a cheat-ish way to test for equality. return to.String() == r.String() + case ConfigResource: + return to.String() == r.String() + case AbsResource: + return to.String() == r.String() default: return false diff --git a/addrs/target_test.go b/addrs/target_test.go index 8d5c9cc9c7..be54f94276 100644 --- a/addrs/target_test.go +++ b/addrs/target_test.go @@ -94,6 +94,11 @@ func TestTargetContains(t *testing.T) { mustParseTarget("module.bar[0].test_resource.foo[2]"), false, }, + { + mustParseTarget("module.bar.test_resource.foo"), + mustParseTarget("module.bar.test_resource.foo[0]"), + true, + }, // Config paths, while never returned from parsing a target, must still // be targetable @@ -109,6 +114,30 @@ func TestTargetContains(t *testing.T) { mustParseTarget("module.bar.test_resource.foo[2]"), true, }, + { + mustParseTarget("module.bar"), + ConfigResource{ + Module: []string{"bar"}, + Resource: Resource{ + Mode: ManagedResourceMode, + Type: "test_resource", + Name: "foo", + }, + }, + true, + }, + { + mustParseTarget("module.bar.test_resource.foo"), + ConfigResource{ + Module: []string{"bar"}, + Resource: Resource{ + Mode: ManagedResourceMode, + Type: "test_resource", + Name: "foo", + }, + }, + true, + }, { ConfigResource{ Resource: Resource{