diff --git a/configs/named_values.go b/configs/named_values.go index 9a463c214..0454a98ee 100644 --- a/configs/named_values.go +++ b/configs/named_values.go @@ -9,6 +9,8 @@ import ( "github.com/hashicorp/hcl2/hcl/hclsyntax" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/convert" + + "github.com/hashicorp/terraform/addrs" ) // A consistent detail message for all "not a valid identifier" diagnostics. @@ -311,6 +313,14 @@ func decodeLocalsBlock(block *hcl.Block) ([]*Local, hcl.Diagnostics) { return locals, diags } +// Addr returns the address of the local value declared by the receiver, +// relative to its containing module. +func (l *Local) Addr() addrs.LocalValue { + return addrs.LocalValue{ + Name: l.Name, + } +} + var variableBlockSchema = &hcl.BodySchema{ Attributes: []hcl.AttributeSchema{ { diff --git a/configs/provider.go b/configs/provider.go index 927490963..b12a95f49 100644 --- a/configs/provider.go +++ b/configs/provider.go @@ -6,6 +6,8 @@ import ( "github.com/hashicorp/hcl2/gohcl" "github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hcl2/hcl/hclsyntax" + + "github.com/hashicorp/terraform/addrs" ) // Provider represents a "provider" block in a module or file. A provider @@ -57,6 +59,15 @@ func decodeProviderBlock(block *hcl.Block) (*Provider, hcl.Diagnostics) { return provider, diags } +// Addr returns the address of the receiving provider configuration, relative +// to its containing module. +func (p *Provider) Addr() addrs.ProviderConfig { + return addrs.ProviderConfig{ + Type: p.Name, + Alias: p.Alias, + } +} + func (p *Provider) moduleUniqueKey() string { if p.Alias != "" { return fmt.Sprintf("%s.%s", p.Name, p.Alias) diff --git a/configs/resource.go b/configs/resource.go index 18eb774f8..9909fa4cb 100644 --- a/configs/resource.go +++ b/configs/resource.go @@ -2,13 +2,12 @@ package configs import ( "fmt" - "strings" - - "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/hcl2/gohcl" "github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hcl2/hcl/hclsyntax" + + "github.com/hashicorp/terraform/addrs" ) // Resource represents a "resource" or "data" block in a module or file. @@ -58,20 +57,29 @@ func (r *Resource) moduleUniqueKey() string { } } -// ProviderConfigKey returns a string key for the provider configuration +// Addr returns a resource address for the receiver that is relative to the +// resource's containing module. +func (r *Resource) Addr() addrs.Resource { + return addrs.Resource{ + Mode: r.Mode, + Type: r.Type, + Name: r.Name, + } +} + +// ProviderConfigAddr returns the address for the provider configuration // that should be used for this resource. This function implements the // default behavior of extracting the type from the resource type name if // an explicit "provider" argument was not provided. -func (r *Resource) ProviderConfigKey() string { +func (r *Resource) ProviderConfigAddr() addrs.ProviderConfig { if r.ProviderConfigRef == nil { - typeName := r.Type - if under := strings.Index(typeName, "_"); under != -1 { - return typeName[:under] - } - return typeName + return r.Addr().DefaultProviderConfig() } - return r.ProviderConfigRef.String() + return addrs.ProviderConfig{ + Type: r.ProviderConfigRef.Name, + Alias: r.ProviderConfigRef.Alias, + } } func decodeResourceBlock(block *hcl.Block) (*Resource, hcl.Diagnostics) { @@ -389,6 +397,18 @@ func decodeProviderConfigRef(expr hcl.Expression, argName string) (*ProviderConf return ret, diags } +// Addr returns the provider config address corresponding to the receiving +// config reference. +// +// This is a trivial conversion, essentially just discarding the source +// location information and keeping just the addressing information. +func (r *ProviderConfigRef) Addr() addrs.ProviderConfig { + return addrs.ProviderConfig{ + Type: r.Name, + Alias: r.Alias, + } +} + func (r *ProviderConfigRef) String() string { if r == nil { return ""