terraform: remove no-longer-necessary type strings

EvalContext.InitProvider no longer needs the redundant typ String
terraform.contextComponentFactory refactored to take an addrs.Provider
instead of a string.
This commit is contained in:
Kristin Laemmert 2020-02-14 09:53:55 -05:00 committed by Martin Atkins
parent 6e2618d9be
commit 228d881722
9 changed files with 20 additions and 27 deletions

View File

@ -14,7 +14,7 @@ import (
// a Context. This information is used for debugging.
type contextComponentFactory interface {
// ResourceProvider creates a new ResourceProvider with the given type.
ResourceProvider(typ string) (providers.Interface, error)
ResourceProvider(typ addrs.Provider) (providers.Interface, error)
ResourceProviders() []string
// ResourceProvisioner creates a new ResourceProvisioner with the given
@ -46,10 +46,10 @@ func (c *basicComponentFactory) ResourceProvisioners() []string {
return result
}
func (c *basicComponentFactory) ResourceProvider(typ string) (providers.Interface, error) {
f, ok := c.providers[addrs.NewLegacyProvider(typ)]
func (c *basicComponentFactory) ResourceProvider(typ addrs.Provider) (providers.Interface, error) {
f, ok := c.providers[typ]
if !ok {
return nil, fmt.Errorf("unknown provider %q", typ)
return nil, fmt.Errorf("unknown provider %q", typ.LegacyString())
}
return f()

View File

@ -30,13 +30,13 @@ type EvalContext interface {
// Input is the UIInput object for interacting with the UI.
Input() UIInput
// InitProvider initializes the provider with the given type and address, and
// returns the implementation of the resource provider or an error.
// InitProvider initializes the provider with the given address, and returns
// the implementation of the resource provider or an error.
//
// It is an error to initialize the same provider more than once. This
// method will panic if the module instance address of the given provider
// configuration does not match the Path() of the EvalContext.
InitProvider(typ string, addr addrs.AbsProviderConfig) (providers.Interface, error)
InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error)
// Provider gets the provider instance with the given address (already
// initialized) or returns nil if the provider isn't initialized.

View File

@ -105,7 +105,7 @@ func (ctx *BuiltinEvalContext) Input() UIInput {
return ctx.InputValue
}
func (ctx *BuiltinEvalContext) InitProvider(typeName string, addr addrs.AbsProviderConfig) (providers.Interface, error) {
func (ctx *BuiltinEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) {
ctx.once.Do(ctx.init)
absAddr := addr
if !absAddr.Module.Equal(ctx.Path()) {
@ -126,12 +126,12 @@ func (ctx *BuiltinEvalContext) InitProvider(typeName string, addr addrs.AbsProvi
key := absAddr.String()
p, err := ctx.Components.ResourceProvider(typeName)
p, err := ctx.Components.ResourceProvider(addr.Provider)
if err != nil {
return nil, err
}
log.Printf("[TRACE] BuiltinEvalContext: Initialized %q provider for %s", typeName, absAddr)
log.Printf("[TRACE] BuiltinEvalContext: Initialized %q provider for %s", addr.LegacyString(), absAddr)
ctx.ProviderCache[key] = p
return p, nil

View File

@ -78,11 +78,11 @@ func TestBuildingEvalContextInitProvider(t *testing.T) {
Alias: "foo",
}
_, err := ctx.InitProvider("test", providerAddrDefault)
_, err := ctx.InitProvider(providerAddrDefault)
if err != nil {
t.Fatalf("error initializing provider test: %s", err)
}
_, err = ctx.InitProvider("test", providerAddrAlias)
_, err = ctx.InitProvider(providerAddrAlias)
if err != nil {
t.Fatalf("error initializing provider test.foo: %s", err)
}

View File

@ -154,9 +154,9 @@ func (c *MockEvalContext) Input() UIInput {
return c.InputInput
}
func (c *MockEvalContext) InitProvider(t string, addr addrs.AbsProviderConfig) (providers.Interface, error) {
func (c *MockEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) {
c.InitProviderCalled = true
c.InitProviderType = t
c.InitProviderType = addr.LegacyString()
c.InitProviderAddr = addr
return c.InitProviderProvider, c.InitProviderError
}

View File

@ -88,12 +88,11 @@ func (n *EvalConfigProvider) Eval(ctx EvalContext) (interface{}, error) {
// and returns nothing. The provider can be retrieved again with the
// EvalGetProvider node.
type EvalInitProvider struct {
TypeName string
Addr addrs.AbsProviderConfig
Addr addrs.AbsProviderConfig
}
func (n *EvalInitProvider) Eval(ctx EvalContext) (interface{}, error) {
return ctx.InitProvider(n.TypeName, n.Addr)
return ctx.InitProvider(n.Addr)
}
// EvalCloseProvider is an EvalNode implementation that closes provider

View File

@ -15,10 +15,7 @@ func ProviderEvalTree(n *NodeApplyableProvider, config *configs.Provider) EvalNo
seq := make([]EvalNode, 0, 5)
seq = append(seq, &EvalInitProvider{
// FIXME: type is now in the AbsProviderConfig, EvalInitProvider doen't
// need this field anymore
TypeName: addr.Provider.Type,
Addr: addr,
Addr: addr,
})
// Input stuff

View File

@ -13,9 +13,6 @@ func (n *NodeEvalableProvider) EvalTree() EvalNode {
addr := n.Addr
return &EvalInitProvider{
// FIXME: type is now in the AbsProviderConfig, EvalInitProvider doen't
// need this field anymore
TypeName: addr.Provider.Type,
Addr: addr,
Addr: addr,
}
}

View File

@ -100,8 +100,8 @@ func loadProviderSchemas(schemas map[addrs.Provider]*ProviderSchema, config *con
return
}
log.Printf("[TRACE] LoadSchemas: retrieving schema for provider type %q", typeName)
provider, err := components.ResourceProvider(typeName)
log.Printf("[TRACE] LoadSchemas: retrieving schema for provider type %q", fqn.LegacyString())
provider, err := components.ResourceProvider(fqn)
if err != nil {
// We'll put a stub in the map so we won't re-attempt this on
// future calls.