2018-03-30 21:58:57 -05:00
|
|
|
package addrs
|
|
|
|
|
2018-04-06 20:45:16 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-23 18:58:01 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
2018-04-06 20:45:16 -05:00
|
|
|
)
|
|
|
|
|
2018-03-30 21:58:57 -05:00
|
|
|
// ProviderConfig is the address of a provider configuration.
|
|
|
|
type ProviderConfig struct {
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// If not empty, Alias identifies which non-default (aliased) provider
|
|
|
|
// configuration this address refers to.
|
|
|
|
Alias string
|
|
|
|
}
|
2018-04-06 20:45:16 -05:00
|
|
|
|
2018-04-30 12:06:05 -05:00
|
|
|
// NewDefaultProviderConfig returns the address of the default (un-aliased)
|
|
|
|
// configuration for the provider with the given type name.
|
|
|
|
func NewDefaultProviderConfig(typeName string) ProviderConfig {
|
|
|
|
return ProviderConfig{
|
|
|
|
Type: typeName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseProviderConfigCompact parses the given absolute traversal as a relative
|
|
|
|
// provider address in compact form. The following are examples of traversals
|
|
|
|
// that can be successfully parsed as compact relative provider configuration
|
|
|
|
// addresses:
|
|
|
|
//
|
|
|
|
// aws
|
|
|
|
// aws.foo
|
|
|
|
//
|
|
|
|
// This function will panic if given a relative traversal.
|
|
|
|
//
|
|
|
|
// If the returned diagnostics contains errors then the result value is invalid
|
|
|
|
// and must not be used.
|
|
|
|
func ParseProviderConfigCompact(traversal hcl.Traversal) (ProviderConfig, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
ret := ProviderConfig{
|
|
|
|
Type: traversal.RootName(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(traversal) < 2 {
|
|
|
|
// Just a type name, then.
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasStep := traversal[1]
|
|
|
|
switch ts := aliasStep.(type) {
|
|
|
|
case hcl.TraverseAttr:
|
|
|
|
ret.Alias = ts.Name
|
|
|
|
return ret, diags
|
|
|
|
default:
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid provider configuration address",
|
|
|
|
Detail: "The provider type name must either stand alone or be followed by an alias name separated with a dot.",
|
|
|
|
Subject: aliasStep.SourceRange().Ptr(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(traversal) > 2 {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid provider configuration address",
|
|
|
|
Detail: "Extraneous extra operators after provider configuration address.",
|
|
|
|
Subject: traversal[2:].SourceRange().Ptr(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:45:16 -05:00
|
|
|
// Absolute returns an AbsProviderConfig from the receiver and the given module
|
|
|
|
// instance address.
|
|
|
|
func (pc ProviderConfig) Absolute(module ModuleInstance) AbsProviderConfig {
|
|
|
|
return AbsProviderConfig{
|
|
|
|
Module: module,
|
|
|
|
ProviderConfig: pc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc ProviderConfig) String() string {
|
|
|
|
if pc.Alias != "" {
|
|
|
|
return fmt.Sprintf("provider.%s.%s", pc.Type, pc.Alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "provider." + pc.Type
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:06:05 -05:00
|
|
|
// StringCompact is an alternative to String that returns the form that can
|
|
|
|
// be parsed by ParseProviderConfigCompact, without the "provider." prefix.
|
|
|
|
func (pc ProviderConfig) StringCompact() string {
|
|
|
|
if pc.Alias != "" {
|
|
|
|
return fmt.Sprintf("%s.%s", pc.Type, pc.Alias)
|
|
|
|
}
|
|
|
|
return pc.Type
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:45:16 -05:00
|
|
|
// AbsProviderConfig is the absolute address of a provider configuration
|
|
|
|
// within a particular module instance.
|
|
|
|
type AbsProviderConfig struct {
|
|
|
|
Module ModuleInstance
|
|
|
|
ProviderConfig ProviderConfig
|
|
|
|
}
|
|
|
|
|
2018-04-23 18:58:01 -05:00
|
|
|
// ParseAbsProviderConfig parses the given traversal as an absolute provider
|
|
|
|
// address. The following are examples of traversals that can be successfully
|
|
|
|
// parsed as absolute provider configuration addresses:
|
|
|
|
//
|
|
|
|
// provider.aws
|
|
|
|
// provider.aws.foo
|
|
|
|
// module.bar.provider.aws
|
|
|
|
// module.bar.module.baz.provider.aws.foo
|
|
|
|
// module.foo[1].provider.aws.foo
|
|
|
|
//
|
|
|
|
// This type of address is used, for example, to record the relationships
|
|
|
|
// between resources and provider configurations in the state structure.
|
|
|
|
// This type of address is not generally used in the UI, except in error
|
|
|
|
// messages that refer to provider configurations.
|
|
|
|
func ParseAbsProviderConfig(traversal hcl.Traversal) (AbsProviderConfig, tfdiags.Diagnostics) {
|
|
|
|
modInst, remain, diags := parseModuleInstancePrefix(traversal)
|
|
|
|
ret := AbsProviderConfig{
|
|
|
|
Module: modInst,
|
|
|
|
}
|
|
|
|
if len(remain) < 2 || remain.RootName() != "provider" {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid provider configuration address",
|
|
|
|
Detail: "Provider address must begin with \"provider.\", followed by a provider type name.",
|
|
|
|
Subject: remain.SourceRange().Ptr(),
|
|
|
|
})
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
if len(remain) > 3 {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid provider configuration address",
|
|
|
|
Detail: "Extraneous operators after provider configuration alias.",
|
|
|
|
Subject: hcl.Traversal(remain[3:]).SourceRange().Ptr(),
|
|
|
|
})
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt, ok := remain[1].(hcl.TraverseAttr); ok {
|
|
|
|
ret.ProviderConfig.Type = tt.Name
|
|
|
|
} else {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid provider configuration address",
|
|
|
|
Detail: "The prefix \"provider.\" must be followed by a provider type name.",
|
|
|
|
Subject: remain[1].SourceRange().Ptr(),
|
|
|
|
})
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(remain) == 3 {
|
|
|
|
if tt, ok := remain[2].(hcl.TraverseAttr); ok {
|
|
|
|
ret.ProviderConfig.Alias = tt.Name
|
|
|
|
} else {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid provider configuration address",
|
|
|
|
Detail: "Provider type name must be followed by a configuration alias name.",
|
|
|
|
Subject: remain[2].SourceRange().Ptr(),
|
|
|
|
})
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:45:16 -05:00
|
|
|
func (pc AbsProviderConfig) String() string {
|
2018-04-23 18:58:01 -05:00
|
|
|
if len(pc.Module) == 0 {
|
|
|
|
return pc.ProviderConfig.String()
|
|
|
|
}
|
2018-04-06 20:45:16 -05:00
|
|
|
return fmt.Sprintf("%s.%s", pc.Module.String(), pc.ProviderConfig.String())
|
|
|
|
}
|