addrs: ProviderConfig fixups (#24115)

* fix outdated syntax in comments
* test for non-strings in ParseAbsProviderConfig
* ProviderConfigDefault and ProviderConfigAliased now take Providers
instead of strings
This commit is contained in:
Kristin Laemmert 2020-02-14 09:06:50 -05:00 committed by GitHub
parent 72ec579807
commit add134298a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 45 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/hashicorp/terraform/tfdiags" "github.com/hashicorp/terraform/tfdiags"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/hcl/v2/hclsyntax"
@ -96,11 +97,11 @@ var _ ProviderConfig = AbsProviderConfig{}
// address. The following are examples of traversals that can be successfully // address. The following are examples of traversals that can be successfully
// parsed as absolute provider configuration addresses: // parsed as absolute provider configuration addresses:
// //
// provider.["registry.terraform.io/hashicorp/aws"] // provider["registry.terraform.io/hashicorp/aws"]
// provider.["registry.terraform.io/hashicorp/aws"].foo // provider["registry.terraform.io/hashicorp/aws"].foo
// module.bar.provider.["registry.terraform.io/hashicorp/aws"] // module.bar.provider["registry.terraform.io/hashicorp/aws"]
// module.bar.module.baz.provider.["registry.terraform.io/hashicorp/aws"].foo // module.bar.module.baz.provider["registry.terraform.io/hashicorp/aws"].foo
// module.foo[1].provider.["registry.terraform.io/hashicorp/aws"].foo // module.foo[1].provider["registry.terraform.io/hashicorp/aws"].foo
// //
// This type of address is used, for example, to record the relationships // This type of address is used, for example, to record the relationships
// between resources and provider configurations in the state structure. // between resources and provider configurations in the state structure.
@ -131,6 +132,15 @@ func ParseAbsProviderConfig(traversal hcl.Traversal) (AbsProviderConfig, tfdiags
} }
if tt, ok := remain[1].(hcl.TraverseIndex); ok { if tt, ok := remain[1].(hcl.TraverseIndex); ok {
if !tt.Key.Type().Equals(cty.String) {
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
}
p, sourceDiags := ParseProviderSourceString(tt.Key.AsString()) p, sourceDiags := ParseProviderSourceString(tt.Key.AsString())
ret.Provider = p ret.Provider = p
if sourceDiags.HasErrors() { if sourceDiags.HasErrors() {
@ -273,29 +283,21 @@ func ParseLegacyAbsProviderConfig(traversal hcl.Traversal) (AbsProviderConfig, t
return ret, diags return ret, diags
} }
// ProviderConfigDefault returns the address of the default provider config // ProviderConfigDefault returns the address of the default provider config of
// of the given type inside the recieving module instance. // the given type inside the recieving module instance.
// func (m ModuleInstance) ProviderConfigDefault(provider Provider) AbsProviderConfig {
// TODO: The signature of this should change to accept a Provider address
// instead of a bare name once AbsProviderConfig starts having its own Provider
// and Alias fields rather than embedding LocalProviderConfig.
func (m ModuleInstance) ProviderConfigDefault(name string) AbsProviderConfig {
return AbsProviderConfig{ return AbsProviderConfig{
Module: m, Module: m,
Provider: NewLegacyProvider(name), Provider: provider,
} }
} }
// ProviderConfigAliased returns the address of an aliased provider config // ProviderConfigAliased returns the address of an aliased provider config of
// of with given type and alias inside the recieving module instance. // the given type and alias inside the recieving module instance.
// func (m ModuleInstance) ProviderConfigAliased(provider Provider, alias string) AbsProviderConfig {
// TODO: The signature of this should change to accept a Provider address
// instead of a bare name once AbsProviderConfig starts having its own Provider
// and Alias fields rather than embedding LocalProviderConfig.
func (m ModuleInstance) ProviderConfigAliased(name, alias string) AbsProviderConfig {
return AbsProviderConfig{ return AbsProviderConfig{
Module: m, Module: m,
Provider: NewLegacyProvider(name), Provider: provider,
Alias: alias, Alias: alias,
} }
} }
@ -311,9 +313,9 @@ func (pc AbsProviderConfig) providerConfig() {}
// other than the root module. Even if a valid address is returned, inheritence // other than the root module. Even if a valid address is returned, inheritence
// may not be performed for other reasons, such as if the calling module // may not be performed for other reasons, such as if the calling module
// provided explicit provider configurations within the call for this module. // provided explicit provider configurations within the call for this module.
// The ProviderTransformer graph transform in the main terraform module has // The ProviderTransformer graph transform in the main terraform module has the
// the authoritative logic for provider inheritance, and this method is here // authoritative logic for provider inheritance, and this method is here mainly
// mainly just for its benefit. // just for its benefit.
func (pc AbsProviderConfig) Inherited() (AbsProviderConfig, bool) { func (pc AbsProviderConfig) Inherited() (AbsProviderConfig, bool) {
// Can't inherit if we're already in the root. // Can't inherit if we're already in the root.
if len(pc.Module) == 0 { if len(pc.Module) == 0 {

View File

@ -157,6 +157,11 @@ func TestParseAbsProviderConfig(t *testing.T) {
AbsProviderConfig{}, AbsProviderConfig{},
`Provider address must begin with "provider.", followed by a provider type name.`, `Provider address must begin with "provider.", followed by a provider type name.`,
}, },
{
`provider[0]`,
AbsProviderConfig{},
`The prefix "provider." must be followed by a provider type name.`,
},
} }
for _, test := range tests { for _, test := range tests {

View File

@ -85,7 +85,7 @@ func TestShow_aliasedProvider(t *testing.T) {
Dependencies: []addrs.AbsResource{}, Dependencies: []addrs.AbsResource{},
DependsOn: []addrs.Referenceable{}, DependsOn: []addrs.Referenceable{},
}, },
addrs.RootModuleInstance.ProviderConfigAliased("test", "alias"), addrs.RootModuleInstance.ProviderConfigAliased(addrs.NewLegacyProvider("test"), "alias"),
) )
}) })

View File

@ -392,7 +392,7 @@ func TestContextImport_providerNonVarConfig(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -436,7 +436,7 @@ func TestContextImport_refresh(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -483,7 +483,7 @@ func TestContextImport_refreshNil(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -524,7 +524,7 @@ func TestContextImport_module(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -565,7 +565,7 @@ func TestContextImport_moduleDepth2(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -626,7 +626,7 @@ func TestContextImport_moduleDiff(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -687,7 +687,7 @@ func TestContextImport_moduleExisting(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -753,7 +753,7 @@ func TestContextImport_multiState(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -823,7 +823,7 @@ func TestContextImport_multiStateSame(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
}, },
}, },
}) })
@ -865,7 +865,7 @@ func TestContextImport_customProviderMissing(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased(addrs.NewLegacyProvider("aws"), "alias"),
}, },
}, },
}) })
@ -900,7 +900,7 @@ func TestContextImport_customProvider(t *testing.T) {
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased(addrs.NewLegacyProvider("aws"), "alias"),
}, },
}, },
}) })

View File

@ -152,7 +152,7 @@ func TestEvalGetProvider_impl(t *testing.T) {
func TestEvalGetProvider(t *testing.T) { func TestEvalGetProvider(t *testing.T) {
var actual providers.Interface var actual providers.Interface
n := &EvalGetProvider{ n := &EvalGetProvider{
Addr: addrs.RootModuleInstance.ProviderConfigDefault("foo"), Addr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("foo")),
Output: &actual, Output: &actual,
} }
provider := &MockProvider{} provider := &MockProvider{}

View File

@ -214,7 +214,7 @@ func TestEvalWriteState(t *testing.T) {
State: &obj, State: &obj,
ProviderSchema: &providerSchema, ProviderSchema: &providerSchema,
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
} }
_, err := node.Eval(ctx) _, err := node.Eval(ctx)
if err != nil { if err != nil {
@ -261,7 +261,7 @@ func TestEvalWriteStateDeposed(t *testing.T) {
State: &obj, State: &obj,
ProviderSchema: &providerSchema, ProviderSchema: &providerSchema,
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"), ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault(addrs.NewLegacyProvider("aws")),
} }
_, err := node.Eval(ctx) _, err := node.Eval(ctx)
if err != nil { if err != nil {

View File

@ -312,7 +312,7 @@ func (t *MissingProviderTransformer) Transform(g *Graph) error {
// We're going to create an implicit _default_ configuration for the // We're going to create an implicit _default_ configuration for the
// referenced provider type in the _root_ module, ignoring all other // referenced provider type in the _root_ module, ignoring all other
// aspects of the resource's declared provider address. // aspects of the resource's declared provider address.
defaultAddr := addrs.RootModuleInstance.ProviderConfigDefault(p.Provider.LegacyString()) defaultAddr := addrs.RootModuleInstance.ProviderConfigDefault(p.Provider)
key := defaultAddr.String() key := defaultAddr.String()
provider := m[key] provider := m[key]

View File

@ -62,7 +62,7 @@ func TestProviderTransformer_moduleChild(t *testing.T) {
), ),
ProviderAddr: addrs.RootModuleInstance. ProviderAddr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey). Child("moo", addrs.NoKey).
ProviderConfigDefault("foo"), ProviderConfigDefault(addrs.NewLegacyProvider("foo")),
ID: "bar", ID: "bar",
}, },
}, },
@ -279,7 +279,7 @@ func TestMissingProviderTransformer_moduleChild(t *testing.T) {
), ),
ProviderAddr: addrs.RootModuleInstance. ProviderAddr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey). Child("moo", addrs.NoKey).
ProviderConfigDefault("foo"), ProviderConfigDefault(addrs.NewLegacyProvider("foo")),
ID: "bar", ID: "bar",
}, },
}, },
@ -324,7 +324,7 @@ func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) {
), ),
ProviderAddr: addrs.RootModuleInstance. ProviderAddr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey). Child("moo", addrs.NoKey).
ProviderConfigDefault("foo"), ProviderConfigDefault(addrs.NewLegacyProvider("foo")),
ID: "bar", ID: "bar",
}, },
}, },
@ -366,7 +366,7 @@ func TestParentProviderTransformer(t *testing.T) {
), ),
ProviderAddr: addrs.RootModuleInstance. ProviderAddr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey). Child("moo", addrs.NoKey).
ProviderConfigDefault("foo"), ProviderConfigDefault(addrs.NewLegacyProvider("foo")),
ID: "bar", ID: "bar",
}, },
}, },
@ -420,7 +420,7 @@ func TestParentProviderTransformer_moduleGrandchild(t *testing.T) {
), ),
ProviderAddr: addrs.RootModuleInstance. ProviderAddr: addrs.RootModuleInstance.
Child("moo", addrs.NoKey). Child("moo", addrs.NoKey).
ProviderConfigDefault("foo"), ProviderConfigDefault(addrs.NewLegacyProvider("foo")),
ID: "bar", ID: "bar",
}, },
}, },