mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
configs: Allow looking up resources by resource addresses.
Throughout the main "terraform" package we identify resources using the address types, and so this helper is useful to make concise transitions between the address types and the configuration types. As part of this, we use the address types to produce the keys used in our resource maps. This has no visible change in behavior since the prior implementation produced an equal result, but this change ensures that ResourceByAddr cannot be broken by hypothetical future changes to the key serialization.
This commit is contained in:
parent
ccc1b6990f
commit
5cf791861f
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user