diff --git a/configs/module.go b/configs/module.go index 21f021fb8..250f9d345 100644 --- a/configs/module.go +++ b/configs/module.go @@ -4,6 +4,8 @@ import ( "fmt" "github.com/hashicorp/hcl2/hcl" + + "github.com/hashicorp/terraform/addrs" ) // Module is a container for a set of configuration constructs that are @@ -99,6 +101,20 @@ func NewModule(primaryFiles, overrideFiles []*File) (*Module, hcl.Diagnostics) { return mod, diags } +// ResourceByAddr returns the configuration for the resource with the given +// address, or nil if there is no such resource. +func (m *Module) ResourceByAddr(addr addrs.Resource) *Resource { + key := addr.String() + switch addr.Mode { + case addrs.ManagedResourceMode: + return m.ManagedResources[key] + case addrs.DataResourceMode: + return m.DataResources[key] + default: + return nil + } +} + func (m *Module) appendFile(file *File) hcl.Diagnostics { var diags hcl.Diagnostics diff --git a/configs/resource.go b/configs/resource.go index 9909fa4cb..87d833315 100644 --- a/configs/resource.go +++ b/configs/resource.go @@ -47,14 +47,7 @@ type ManagedResource struct { } func (r *Resource) moduleUniqueKey() string { - switch r.Mode { - case addrs.ManagedResourceMode: - return fmt.Sprintf("%s.%s", r.Name, r.Type) - case addrs.DataResourceMode: - return fmt.Sprintf("data.%s.%s", r.Name, r.Type) - default: - panic(fmt.Errorf("Resource has invalid resource mode %s", r.Mode)) - } + return r.Addr().String() } // Addr returns a resource address for the receiver that is relative to the