2018-03-31 04:58:57 +02:00
|
|
|
package addrs
|
|
|
|
|
2018-04-04 03:03:47 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-30 19:06:05 +02:00
|
|
|
"strings"
|
2018-04-04 03:03:47 +02:00
|
|
|
)
|
|
|
|
|
2018-03-31 04:58:57 +02:00
|
|
|
// Resource is an address for a resource block within configuration, which
|
|
|
|
// contains potentially-multiple resource instances if that configuration
|
|
|
|
// block uses "count" or "for_each".
|
|
|
|
type Resource struct {
|
|
|
|
referenceable
|
|
|
|
Mode ResourceMode
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2018-04-04 03:03:47 +02:00
|
|
|
func (r Resource) String() string {
|
|
|
|
switch r.Mode {
|
|
|
|
case ManagedResourceMode:
|
|
|
|
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
|
|
|
case DataResourceMode:
|
|
|
|
return fmt.Sprintf("data.%s.%s", r.Type, r.Name)
|
|
|
|
default:
|
2018-05-05 04:42:08 +02:00
|
|
|
// Should never happen, but we'll return a string here rather than
|
|
|
|
// crashing just in case it does.
|
|
|
|
return fmt.Sprintf("<invalid>.%s.%s", r.Type, r.Name)
|
2018-04-04 03:03:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
func (r Resource) Equal(o Resource) bool {
|
2021-03-05 18:07:31 +01:00
|
|
|
return r.Mode == o.Mode && r.Name == o.Name && r.Type == o.Type
|
2018-09-07 02:17:13 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 00:18:19 +02:00
|
|
|
func (r Resource) UniqueKey() UniqueKey {
|
|
|
|
return r // A Resource is its own UniqueKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) uniqueKeySigil() {}
|
|
|
|
|
2018-03-31 04:58:57 +02:00
|
|
|
// Instance produces the address for a specific instance of the receiver
|
|
|
|
// that is idenfied by the given key.
|
|
|
|
func (r Resource) Instance(key InstanceKey) ResourceInstance {
|
|
|
|
return ResourceInstance{
|
|
|
|
Resource: r,
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absolute returns an AbsResource from the receiver and the given module
|
|
|
|
// instance address.
|
|
|
|
func (r Resource) Absolute(module ModuleInstance) AbsResource {
|
|
|
|
return AbsResource{
|
|
|
|
Module: module,
|
|
|
|
Resource: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 02:27:54 +02:00
|
|
|
// InModule returns a ConfigResource from the receiver and the given module
|
|
|
|
// address.
|
|
|
|
func (r Resource) InModule(module Module) ConfigResource {
|
|
|
|
return ConfigResource{
|
|
|
|
Module: module,
|
|
|
|
Resource: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 13:58:20 +01:00
|
|
|
// ImpliedProvider returns the implied provider type name, for e.g. the "aws" in
|
|
|
|
// "aws_instance"
|
|
|
|
func (r Resource) ImpliedProvider() string {
|
2018-04-30 19:06:05 +02:00
|
|
|
typeName := r.Type
|
|
|
|
if under := strings.Index(typeName, "_"); under != -1 {
|
|
|
|
typeName = typeName[:under]
|
|
|
|
}
|
2019-12-06 14:00:18 +01:00
|
|
|
|
2020-03-18 13:58:20 +01:00
|
|
|
return typeName
|
2018-04-30 19:06:05 +02:00
|
|
|
}
|
|
|
|
|
2018-03-31 04:58:57 +02:00
|
|
|
// ResourceInstance is an address for a specific instance of a resource.
|
|
|
|
// When a resource is defined in configuration with "count" or "for_each" it
|
|
|
|
// produces zero or more instances, which can be addressed using this type.
|
|
|
|
type ResourceInstance struct {
|
|
|
|
referenceable
|
|
|
|
Resource Resource
|
|
|
|
Key InstanceKey
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:06:05 +02:00
|
|
|
func (r ResourceInstance) ContainingResource() Resource {
|
|
|
|
return r.Resource
|
|
|
|
}
|
|
|
|
|
2018-04-04 03:03:47 +02:00
|
|
|
func (r ResourceInstance) String() string {
|
|
|
|
if r.Key == NoKey {
|
|
|
|
return r.Resource.String()
|
|
|
|
}
|
|
|
|
return r.Resource.String() + r.Key.String()
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
func (r ResourceInstance) Equal(o ResourceInstance) bool {
|
2021-03-05 18:07:31 +01:00
|
|
|
return r.Key == o.Key && r.Resource.Equal(o.Resource)
|
2018-09-07 02:17:13 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 00:18:19 +02:00
|
|
|
func (r ResourceInstance) UniqueKey() UniqueKey {
|
|
|
|
return r // A ResourceInstance is its own UniqueKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ResourceInstance) uniqueKeySigil() {}
|
|
|
|
|
2018-03-31 04:58:57 +02:00
|
|
|
// Absolute returns an AbsResourceInstance from the receiver and the given module
|
|
|
|
// instance address.
|
|
|
|
func (r ResourceInstance) Absolute(module ModuleInstance) AbsResourceInstance {
|
|
|
|
return AbsResourceInstance{
|
|
|
|
Module: module,
|
|
|
|
Resource: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 01:56:16 +02:00
|
|
|
// AbsResource is an absolute address for a resource under a given module path.
|
|
|
|
type AbsResource struct {
|
2018-04-30 19:06:05 +02:00
|
|
|
targetable
|
2018-04-24 01:56:16 +02:00
|
|
|
Module ModuleInstance
|
|
|
|
Resource Resource
|
|
|
|
}
|
|
|
|
|
2018-03-31 04:58:57 +02:00
|
|
|
// Resource returns the address of a particular resource within the receiver.
|
|
|
|
func (m ModuleInstance) Resource(mode ResourceMode, typeName string, name string) AbsResource {
|
|
|
|
return AbsResource{
|
|
|
|
Module: m,
|
|
|
|
Resource: Resource{
|
|
|
|
Mode: mode,
|
|
|
|
Type: typeName,
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:06:05 +02:00
|
|
|
// Instance produces the address for a specific instance of the receiver
|
|
|
|
// that is idenfied by the given key.
|
|
|
|
func (r AbsResource) Instance(key InstanceKey) AbsResourceInstance {
|
|
|
|
return AbsResourceInstance{
|
|
|
|
Module: r.Module,
|
|
|
|
Resource: r.Resource.Instance(key),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 18:04:37 +01:00
|
|
|
// Config returns the unexpanded ConfigResource for this AbsResource.
|
|
|
|
func (r AbsResource) Config() ConfigResource {
|
|
|
|
return ConfigResource{
|
|
|
|
Module: r.Module.Module(),
|
|
|
|
Resource: r.Resource,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:06:05 +02:00
|
|
|
// TargetContains implements Targetable by returning true if the given other
|
|
|
|
// address is either equal to the receiver or is an instance of the
|
|
|
|
// receiver.
|
|
|
|
func (r AbsResource) TargetContains(other Targetable) bool {
|
|
|
|
switch to := other.(type) {
|
|
|
|
|
|
|
|
case AbsResource:
|
|
|
|
// We'll use our stringification as a cheat-ish way to test for equality.
|
|
|
|
return to.String() == r.String()
|
|
|
|
|
2020-03-14 00:01:23 +01:00
|
|
|
case ConfigResource:
|
|
|
|
// if an absolute resource from parsing a target address contains a
|
|
|
|
// ConfigResource, the string representation will match
|
|
|
|
return to.String() == r.String()
|
|
|
|
|
2018-04-30 19:06:05 +02:00
|
|
|
case AbsResourceInstance:
|
|
|
|
return r.TargetContains(to.ContainingResource())
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:53:16 +02:00
|
|
|
func (r AbsResource) AddrType() TargetableAddrType {
|
|
|
|
return AbsResourceAddrType
|
|
|
|
}
|
|
|
|
|
2018-04-24 01:56:16 +02:00
|
|
|
func (r AbsResource) String() string {
|
|
|
|
if len(r.Module) == 0 {
|
|
|
|
return r.Resource.String()
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s.%s", r.Module.String(), r.Resource.String())
|
2018-03-31 04:58:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
func (r AbsResource) Equal(o AbsResource) bool {
|
2021-03-05 18:07:31 +01:00
|
|
|
return r.Module.Equal(o.Module) && r.Resource.Equal(o.Resource)
|
2018-09-07 02:17:13 +02:00
|
|
|
}
|
|
|
|
|
2021-06-30 04:03:59 +02:00
|
|
|
func (r AbsResource) absMoveableSigil() {
|
|
|
|
// AbsResource is moveable
|
|
|
|
}
|
|
|
|
|
2021-07-29 03:25:00 +02:00
|
|
|
type absResourceKey string
|
|
|
|
|
|
|
|
func (r AbsResource) UniqueKey() UniqueKey {
|
|
|
|
return absResourceInstanceKey(r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rk absResourceKey) uniqueKeySigil() {}
|
|
|
|
|
2018-03-31 04:58:57 +02:00
|
|
|
// AbsResourceInstance is an absolute address for a resource instance under a
|
|
|
|
// given module path.
|
|
|
|
type AbsResourceInstance struct {
|
2018-04-30 19:06:05 +02:00
|
|
|
targetable
|
2018-03-31 04:58:57 +02:00
|
|
|
Module ModuleInstance
|
|
|
|
Resource ResourceInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceInstance returns the address of a particular resource instance within the receiver.
|
|
|
|
func (m ModuleInstance) ResourceInstance(mode ResourceMode, typeName string, name string, key InstanceKey) AbsResourceInstance {
|
|
|
|
return AbsResourceInstance{
|
|
|
|
Module: m,
|
|
|
|
Resource: ResourceInstance{
|
|
|
|
Resource: Resource{
|
|
|
|
Mode: mode,
|
|
|
|
Type: typeName,
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Key: key,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:06:05 +02:00
|
|
|
// ContainingResource returns the address of the resource that contains the
|
|
|
|
// receving resource instance. In other words, it discards the key portion
|
|
|
|
// of the address to produce an AbsResource value.
|
|
|
|
func (r AbsResourceInstance) ContainingResource() AbsResource {
|
|
|
|
return AbsResource{
|
|
|
|
Module: r.Module,
|
|
|
|
Resource: r.Resource.ContainingResource(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetContains implements Targetable by returning true if the given other
|
|
|
|
// address is equal to the receiver.
|
|
|
|
func (r AbsResourceInstance) TargetContains(other Targetable) bool {
|
|
|
|
switch to := other.(type) {
|
|
|
|
|
2020-03-14 00:01:23 +01:00
|
|
|
// while we currently don't start with an AbsResourceInstance as a target
|
|
|
|
// address, check all resource types for consistency.
|
2018-04-30 19:06:05 +02:00
|
|
|
case AbsResourceInstance:
|
|
|
|
// We'll use our stringification as a cheat-ish way to test for equality.
|
|
|
|
return to.String() == r.String()
|
2020-03-14 00:01:23 +01:00
|
|
|
case ConfigResource:
|
|
|
|
return to.String() == r.String()
|
|
|
|
case AbsResource:
|
|
|
|
return to.String() == r.String()
|
2018-04-30 19:06:05 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:53:16 +02:00
|
|
|
func (r AbsResourceInstance) AddrType() TargetableAddrType {
|
|
|
|
return AbsResourceInstanceAddrType
|
|
|
|
}
|
|
|
|
|
2018-04-24 01:56:16 +02:00
|
|
|
func (r AbsResourceInstance) String() string {
|
|
|
|
if len(r.Module) == 0 {
|
|
|
|
return r.Resource.String()
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s.%s", r.Module.String(), r.Resource.String())
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
func (r AbsResourceInstance) Equal(o AbsResourceInstance) bool {
|
2021-03-05 18:07:31 +01:00
|
|
|
return r.Module.Equal(o.Module) && r.Resource.Equal(o.Resource)
|
2018-09-07 02:17:13 +02:00
|
|
|
}
|
|
|
|
|
2018-06-08 02:17:47 +02:00
|
|
|
// Less returns true if the receiver should sort before the given other value
|
|
|
|
// in a sorted list of addresses.
|
|
|
|
func (r AbsResourceInstance) Less(o AbsResourceInstance) bool {
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case len(r.Module) != len(o.Module):
|
|
|
|
return len(r.Module) < len(o.Module)
|
|
|
|
|
|
|
|
case r.Module.String() != o.Module.String():
|
|
|
|
return r.Module.Less(o.Module)
|
|
|
|
|
|
|
|
case r.Resource.Resource.Mode != o.Resource.Resource.Mode:
|
|
|
|
return r.Resource.Resource.Mode == DataResourceMode
|
|
|
|
|
|
|
|
case r.Resource.Resource.Type != o.Resource.Resource.Type:
|
|
|
|
return r.Resource.Resource.Type < o.Resource.Resource.Type
|
|
|
|
|
|
|
|
case r.Resource.Resource.Name != o.Resource.Resource.Name:
|
|
|
|
return r.Resource.Resource.Name < o.Resource.Resource.Name
|
|
|
|
|
|
|
|
case r.Resource.Key != o.Resource.Key:
|
|
|
|
return InstanceKeyLess(r.Resource.Key, o.Resource.Key)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 01:30:31 +02:00
|
|
|
type absResourceInstanceKey string
|
|
|
|
|
|
|
|
func (r AbsResourceInstance) UniqueKey() UniqueKey {
|
|
|
|
return absResourceInstanceKey(r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r absResourceInstanceKey) uniqueKeySigil() {}
|
|
|
|
|
2021-06-30 04:03:59 +02:00
|
|
|
func (r AbsResourceInstance) absMoveableSigil() {
|
|
|
|
// AbsResourceInstance is moveable
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:59:53 +01:00
|
|
|
// ConfigResource is an address for a resource within a configuration.
|
|
|
|
type ConfigResource struct {
|
|
|
|
targetable
|
|
|
|
Module Module
|
|
|
|
Resource Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource returns the address of a particular resource within the module.
|
|
|
|
func (m Module) Resource(mode ResourceMode, typeName string, name string) ConfigResource {
|
|
|
|
return ConfigResource{
|
|
|
|
Module: m,
|
|
|
|
Resource: Resource{
|
|
|
|
Mode: mode,
|
|
|
|
Type: typeName,
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absolute produces the address for the receiver within a specific module instance.
|
|
|
|
func (r ConfigResource) Absolute(module ModuleInstance) AbsResource {
|
|
|
|
return AbsResource{
|
|
|
|
Module: module,
|
|
|
|
Resource: r.Resource,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetContains implements Targetable by returning true if the given other
|
|
|
|
// address is either equal to the receiver or is an instance of the
|
|
|
|
// receiver.
|
|
|
|
func (r ConfigResource) TargetContains(other Targetable) bool {
|
|
|
|
switch to := other.(type) {
|
|
|
|
case ConfigResource:
|
|
|
|
// We'll use our stringification as a cheat-ish way to test for equality.
|
|
|
|
return to.String() == r.String()
|
|
|
|
case AbsResource:
|
2020-03-13 18:04:37 +01:00
|
|
|
return r.TargetContains(to.Config())
|
2020-03-12 18:59:53 +01:00
|
|
|
case AbsResourceInstance:
|
|
|
|
return r.TargetContains(to.ContainingResource())
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:53:16 +02:00
|
|
|
func (r ConfigResource) AddrType() TargetableAddrType {
|
|
|
|
return ConfigResourceAddrType
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:59:53 +01:00
|
|
|
func (r ConfigResource) String() string {
|
|
|
|
if len(r.Module) == 0 {
|
|
|
|
return r.Resource.String()
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s.%s", r.Module.String(), r.Resource.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ConfigResource) Equal(o ConfigResource) bool {
|
2021-03-05 18:07:31 +01:00
|
|
|
return r.Module.Equal(o.Module) && r.Resource.Equal(o.Resource)
|
2020-03-12 18:59:53 +01:00
|
|
|
}
|
|
|
|
|
2021-06-30 04:03:59 +02:00
|
|
|
func (r ConfigResource) configMoveableSigil() {
|
|
|
|
// AbsResource is moveable
|
|
|
|
}
|
|
|
|
|
2018-03-31 04:58:57 +02:00
|
|
|
// ResourceMode defines which lifecycle applies to a given resource. Each
|
|
|
|
// resource lifecycle has a slightly different address format.
|
|
|
|
type ResourceMode rune
|
|
|
|
|
2019-10-17 22:17:23 +02:00
|
|
|
//go:generate go run golang.org/x/tools/cmd/stringer -type ResourceMode
|
2018-03-31 04:58:57 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
// InvalidResourceMode is the zero value of ResourceMode and is not
|
|
|
|
// a valid resource mode.
|
|
|
|
InvalidResourceMode ResourceMode = 0
|
|
|
|
|
|
|
|
// ManagedResourceMode indicates a managed resource, as defined by
|
|
|
|
// "resource" blocks in configuration.
|
|
|
|
ManagedResourceMode ResourceMode = 'M'
|
|
|
|
|
|
|
|
// DataResourceMode indicates a data resource, as defined by
|
|
|
|
// "data" blocks in configuration.
|
|
|
|
DataResourceMode ResourceMode = 'D'
|
|
|
|
)
|