minor cleanup

Remove unused variables, sync.Once, and init in BuiltinEvalContext.
Replace some shim calls.
GraphNodeAttachProvider doesn't need to be a NodeModuleInstance.
This commit is contained in:
James Bardin 2020-03-11 17:54:46 -04:00
parent 4a1ec05092
commit 482ae66e18
5 changed files with 15 additions and 36 deletions

View File

@ -597,7 +597,7 @@ func TestContextImport_moduleDiff(t *testing.T) {
Mode: addrs.ManagedResourceMode, Mode: addrs.ManagedResourceMode,
Type: "aws_instance", Type: "aws_instance",
Name: "bar", Name: "bar",
}.Instance(addrs.NoKey).Absolute(addrs.Module{"bar"}.UnkeyedInstanceShim()), }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("bar", addrs.NoKey)),
&states.ResourceInstanceObjectSrc{ &states.ResourceInstanceObjectSrc{
AttrsFlat: map[string]string{ AttrsFlat: map[string]string{
"id": "bar", "id": "bar",
@ -658,7 +658,7 @@ func TestContextImport_moduleExisting(t *testing.T) {
Mode: addrs.ManagedResourceMode, Mode: addrs.ManagedResourceMode,
Type: "aws_instance", Type: "aws_instance",
Name: "bar", Name: "bar",
}.Instance(addrs.NoKey).Absolute(addrs.Module{"foo"}.UnkeyedInstanceShim()), }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("foo", addrs.NoKey)),
&states.ResourceInstanceObjectSrc{ &states.ResourceInstanceObjectSrc{
AttrsFlat: map[string]string{ AttrsFlat: map[string]string{
"id": "bar", "id": "bar",

View File

@ -65,8 +65,6 @@ type BuiltinEvalContext struct {
ChangesValue *plans.ChangesSync ChangesValue *plans.ChangesSync
StateValue *states.SyncState StateValue *states.SyncState
InstanceExpanderValue *instances.Expander InstanceExpanderValue *instances.Expander
once sync.Once
} }
// BuiltinEvalContext implements EvalContext // BuiltinEvalContext implements EvalContext
@ -106,16 +104,14 @@ func (ctx *BuiltinEvalContext) Input() UIInput {
} }
func (ctx *BuiltinEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) { func (ctx *BuiltinEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) {
ctx.once.Do(ctx.init) if !addr.Module.Equal(ctx.Path().Module()) {
absAddr := addr
if !absAddr.Module.Equal(ctx.Path().Module()) {
// This indicates incorrect use of InitProvider: it should be used // This indicates incorrect use of InitProvider: it should be used
// only from the module that the provider configuration belongs to. // only from the module that the provider configuration belongs to.
panic(fmt.Sprintf("%s initialized by wrong module %s", absAddr, ctx.Path())) panic(fmt.Sprintf("%s initialized by wrong module %s", addr, ctx.Path()))
} }
// If we already initialized, it is an error // If we already initialized, it is an error
if p := ctx.Provider(absAddr); p != nil { if p := ctx.Provider(addr); p != nil {
return nil, fmt.Errorf("%s is already initialized", addr) return nil, fmt.Errorf("%s is already initialized", addr)
} }
@ -124,22 +120,20 @@ func (ctx *BuiltinEvalContext) InitProvider(addr addrs.AbsProviderConfig) (provi
ctx.ProviderLock.Lock() ctx.ProviderLock.Lock()
defer ctx.ProviderLock.Unlock() defer ctx.ProviderLock.Unlock()
key := absAddr.String() key := addr.String()
p, err := ctx.Components.ResourceProvider(addr.Provider) p, err := ctx.Components.ResourceProvider(addr.Provider)
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("[TRACE] BuiltinEvalContext: Initialized %q provider for %s", addr.LegacyString(), absAddr) log.Printf("[TRACE] BuiltinEvalContext: Initialized %q provider for %s", addr.LegacyString(), addr)
ctx.ProviderCache[key] = p ctx.ProviderCache[key] = p
return p, nil return p, nil
} }
func (ctx *BuiltinEvalContext) Provider(addr addrs.AbsProviderConfig) providers.Interface { func (ctx *BuiltinEvalContext) Provider(addr addrs.AbsProviderConfig) providers.Interface {
ctx.once.Do(ctx.init)
ctx.ProviderLock.Lock() ctx.ProviderLock.Lock()
defer ctx.ProviderLock.Unlock() defer ctx.ProviderLock.Unlock()
@ -147,12 +141,10 @@ func (ctx *BuiltinEvalContext) Provider(addr addrs.AbsProviderConfig) providers.
} }
func (ctx *BuiltinEvalContext) ProviderSchema(addr addrs.AbsProviderConfig) *ProviderSchema { func (ctx *BuiltinEvalContext) ProviderSchema(addr addrs.AbsProviderConfig) *ProviderSchema {
ctx.once.Do(ctx.init)
return ctx.Schemas.ProviderSchema(addr.Provider) return ctx.Schemas.ProviderSchema(addr.Provider)
} }
func (ctx *BuiltinEvalContext) CloseProvider(addr addrs.AbsProviderConfig) error { func (ctx *BuiltinEvalContext) CloseProvider(addr addrs.AbsProviderConfig) error {
ctx.once.Do(ctx.init)
if !addr.Module.Equal(ctx.Path().Module()) { if !addr.Module.Equal(ctx.Path().Module()) {
// This indicates incorrect use of CloseProvider: it should be used // This indicates incorrect use of CloseProvider: it should be used
// only from the module that the provider configuration belongs to. // only from the module that the provider configuration belongs to.
@ -174,22 +166,21 @@ func (ctx *BuiltinEvalContext) CloseProvider(addr addrs.AbsProviderConfig) error
func (ctx *BuiltinEvalContext) ConfigureProvider(addr addrs.AbsProviderConfig, cfg cty.Value) tfdiags.Diagnostics { func (ctx *BuiltinEvalContext) ConfigureProvider(addr addrs.AbsProviderConfig, cfg cty.Value) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
absAddr := addr if !addr.Module.Equal(ctx.Path().Module()) {
if !absAddr.Module.Equal(ctx.Path().Module()) {
// This indicates incorrect use of ConfigureProvider: it should be used // This indicates incorrect use of ConfigureProvider: it should be used
// only from the module that the provider configuration belongs to. // only from the module that the provider configuration belongs to.
panic(fmt.Sprintf("%s configured by wrong module %s", absAddr, ctx.Path())) panic(fmt.Sprintf("%s configured by wrong module %s", addr, ctx.Path()))
} }
p := ctx.Provider(absAddr) p := ctx.Provider(addr)
if p == nil { if p == nil {
diags = diags.Append(fmt.Errorf("%s not initialized", addr)) diags = diags.Append(fmt.Errorf("%s not initialized", addr))
return diags return diags
} }
providerSchema := ctx.ProviderSchema(absAddr) providerSchema := ctx.ProviderSchema(addr)
if providerSchema == nil { if providerSchema == nil {
diags = diags.Append(fmt.Errorf("schema for %s is not available", absAddr)) diags = diags.Append(fmt.Errorf("schema for %s is not available", addr))
return diags return diags
} }
@ -241,8 +232,6 @@ func (ctx *BuiltinEvalContext) SetProviderInput(pc addrs.AbsProviderConfig, c ma
} }
func (ctx *BuiltinEvalContext) InitProvisioner(n string) (provisioners.Interface, error) { func (ctx *BuiltinEvalContext) InitProvisioner(n string) (provisioners.Interface, error) {
ctx.once.Do(ctx.init)
// If we already initialized, it is an error // If we already initialized, it is an error
if p := ctx.Provisioner(n); p != nil { if p := ctx.Provisioner(n); p != nil {
return nil, fmt.Errorf("Provisioner '%s' already initialized", n) return nil, fmt.Errorf("Provisioner '%s' already initialized", n)
@ -264,8 +253,6 @@ func (ctx *BuiltinEvalContext) InitProvisioner(n string) (provisioners.Interface
} }
func (ctx *BuiltinEvalContext) Provisioner(n string) provisioners.Interface { func (ctx *BuiltinEvalContext) Provisioner(n string) provisioners.Interface {
ctx.once.Do(ctx.init)
ctx.ProvisionerLock.Lock() ctx.ProvisionerLock.Lock()
defer ctx.ProvisionerLock.Unlock() defer ctx.ProvisionerLock.Unlock()
@ -273,14 +260,10 @@ func (ctx *BuiltinEvalContext) Provisioner(n string) provisioners.Interface {
} }
func (ctx *BuiltinEvalContext) ProvisionerSchema(n string) *configschema.Block { func (ctx *BuiltinEvalContext) ProvisionerSchema(n string) *configschema.Block {
ctx.once.Do(ctx.init)
return ctx.Schemas.ProvisionerConfig(n) return ctx.Schemas.ProvisionerConfig(n)
} }
func (ctx *BuiltinEvalContext) CloseProvisioner(n string) error { func (ctx *BuiltinEvalContext) CloseProvisioner(n string) error {
ctx.once.Do(ctx.init)
ctx.ProvisionerLock.Lock() ctx.ProvisionerLock.Lock()
defer ctx.ProvisionerLock.Unlock() defer ctx.ProvisionerLock.Unlock()
@ -361,6 +344,3 @@ func (ctx *BuiltinEvalContext) State() *states.SyncState {
func (ctx *BuiltinEvalContext) InstanceExpander() *instances.Expander { func (ctx *BuiltinEvalContext) InstanceExpander() *instances.Expander {
return ctx.InstanceExpanderValue return ctx.InstanceExpanderValue
} }
func (ctx *BuiltinEvalContext) init() {
}

View File

@ -41,6 +41,8 @@ func (n *NodeAbstractProvider) Name() string {
// GraphNodeModuleInstance // GraphNodeModuleInstance
func (n *NodeAbstractProvider) Path() addrs.ModuleInstance { func (n *NodeAbstractProvider) Path() addrs.ModuleInstance {
// Providers cannot be contained inside an expanded module, so this shim
// converts our module path to the correct ModuleInstance.
return n.Addr.Module.UnkeyedInstanceShim() return n.Addr.Module.UnkeyedInstanceShim()
} }

View File

@ -14,7 +14,7 @@ type NodeDisabledProvider struct {
} }
var ( var (
_ GraphNodeModuleInstance = (*NodeDisabledProvider)(nil) _ GraphNodeModulePath = (*NodeDisabledProvider)(nil)
_ RemovableIfNotTargeted = (*NodeDisabledProvider)(nil) _ RemovableIfNotTargeted = (*NodeDisabledProvider)(nil)
_ GraphNodeReferencer = (*NodeDisabledProvider)(nil) _ GraphNodeReferencer = (*NodeDisabledProvider)(nil)
_ GraphNodeProvider = (*NodeDisabledProvider)(nil) _ GraphNodeProvider = (*NodeDisabledProvider)(nil)

View File

@ -8,9 +8,6 @@ import (
// GraphNodeAttachProvider is an interface that must be implemented by nodes // GraphNodeAttachProvider is an interface that must be implemented by nodes
// that want provider configurations attached. // that want provider configurations attached.
type GraphNodeAttachProvider interface { type GraphNodeAttachProvider interface {
// Must be implemented to determine the path for the configuration
GraphNodeModuleInstance
// ProviderName with no module prefix. Example: "aws". // ProviderName with no module prefix. Example: "aws".
ProviderAddr() addrs.AbsProviderConfig ProviderAddr() addrs.AbsProviderConfig