remove InitProvisioner and close all at once

InitProvisioner only stored the provisioner in the cache and provided a
way to return an error. We can push this back further into the cli init
process, but for now we can move the error into the Provisioner call and
drop InitProvisioner.

This also changes CloseProvisioner to CloseProvisioners, in preparation
for the removal of the graph nodes.
This commit is contained in:
James Bardin 2021-01-19 12:11:43 -05:00
parent 1c7c53adbb
commit 44daf76624
3 changed files with 27 additions and 53 deletions

View File

@ -77,22 +77,17 @@ type EvalContext interface {
ProviderInput(addrs.AbsProviderConfig) map[string]cty.Value ProviderInput(addrs.AbsProviderConfig) map[string]cty.Value
SetProviderInput(addrs.AbsProviderConfig, map[string]cty.Value) SetProviderInput(addrs.AbsProviderConfig, map[string]cty.Value)
// InitProvisioner initializes the provisioner with the given name.
// It is an error to initialize the same provisioner more than once.
InitProvisioner(string) error
// Provisioner gets the provisioner instance with the given name (already // Provisioner gets the provisioner instance with the given name (already
// initialized) or returns nil if the provisioner isn't initialized. // initialized) or returns nil if the provisioner isn't initialized.
Provisioner(string) provisioners.Interface Provisioner(string) (provisioners.Interface, error)
// ProvisionerSchema retrieves the main configuration schema for a // ProvisionerSchema retrieves the main configuration schema for a
// particular provisioner, which must have already been initialized with // particular provisioner, which must have already been initialized with
// InitProvisioner. // InitProvisioner.
ProvisionerSchema(string) *configschema.Block ProvisionerSchema(string) *configschema.Block
// CloseProvisioner closes provisioner connections that aren't needed // CloseProvisioner closes all provisioner plugins.
// anymore. CloseProvisioners() error
CloseProvisioner(string) error
// EvaluateBlock takes the given raw configuration block and associated // EvaluateBlock takes the given raw configuration block and associated
// schema and evaluates it to produce a value of an object type that // schema and evaluates it to produce a value of an object type that

View File

@ -228,48 +228,41 @@ func (ctx *BuiltinEvalContext) SetProviderInput(pc addrs.AbsProviderConfig, c ma
ctx.ProviderLock.Unlock() ctx.ProviderLock.Unlock()
} }
func (ctx *BuiltinEvalContext) InitProvisioner(n string) error { func (ctx *BuiltinEvalContext) Provisioner(n string) (provisioners.Interface, error) {
// If we already initialized, it is an error
if p := ctx.Provisioner(n); p != nil {
return fmt.Errorf("Provisioner '%s' already initialized", n)
}
// Warning: make sure to acquire these locks AFTER the call to Provisioner
// above, since it also acquires locks.
ctx.ProvisionerLock.Lock() ctx.ProvisionerLock.Lock()
defer ctx.ProvisionerLock.Unlock() defer ctx.ProvisionerLock.Unlock()
p, err := ctx.Components.ResourceProvisioner(n) p, ok := ctx.ProvisionerCache[n]
if !ok {
var err error
p, err = ctx.Components.ResourceProvisioner(n)
if err != nil { if err != nil {
return err return nil, err
} }
ctx.ProvisionerCache[n] = p ctx.ProvisionerCache[n] = p
}
return nil return p, nil
}
func (ctx *BuiltinEvalContext) Provisioner(n string) provisioners.Interface {
ctx.ProvisionerLock.Lock()
defer ctx.ProvisionerLock.Unlock()
return ctx.ProvisionerCache[n]
} }
func (ctx *BuiltinEvalContext) ProvisionerSchema(n string) *configschema.Block { func (ctx *BuiltinEvalContext) ProvisionerSchema(n string) *configschema.Block {
return ctx.Schemas.ProvisionerConfig(n) return ctx.Schemas.ProvisionerConfig(n)
} }
func (ctx *BuiltinEvalContext) CloseProvisioner(n string) error { func (ctx *BuiltinEvalContext) CloseProvisioners() error {
var diags tfdiags.Diagnostics
ctx.ProvisionerLock.Lock() ctx.ProvisionerLock.Lock()
defer ctx.ProvisionerLock.Unlock() defer ctx.ProvisionerLock.Unlock()
prov := ctx.ProvisionerCache[n] for name, prov := range ctx.ProvisionerCache {
if prov != nil { err := prov.Close()
return prov.Close() if err != nil {
diags = diags.Append(fmt.Errorf("provisioner.Close %s: %s", name, err))
}
} }
return nil return diags.Err()
} }
func (ctx *BuiltinEvalContext) EvaluateBlock(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, keyData InstanceKeyEvalData) (cty.Value, hcl.Body, tfdiags.Diagnostics) { func (ctx *BuiltinEvalContext) EvaluateBlock(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, keyData InstanceKeyEvalData) (cty.Value, hcl.Body, tfdiags.Diagnostics) {

View File

@ -63,11 +63,6 @@ type MockEvalContext struct {
ConfigureProviderConfig cty.Value ConfigureProviderConfig cty.Value
ConfigureProviderDiags tfdiags.Diagnostics ConfigureProviderDiags tfdiags.Diagnostics
InitProvisionerCalled bool
InitProvisionerName string
InitProvisionerProvisioner provisioners.Interface
InitProvisionerError error
ProvisionerCalled bool ProvisionerCalled bool
ProvisionerName string ProvisionerName string
ProvisionerProvisioner provisioners.Interface ProvisionerProvisioner provisioners.Interface
@ -76,9 +71,7 @@ type MockEvalContext struct {
ProvisionerSchemaName string ProvisionerSchemaName string
ProvisionerSchemaSchema *configschema.Block ProvisionerSchemaSchema *configschema.Block
CloseProvisionerCalled bool CloseProvisionersCalled bool
CloseProvisionerName string
CloseProvisionerProvisioner provisioners.Interface
EvaluateBlockCalled bool EvaluateBlockCalled bool
EvaluateBlockBody hcl.Body EvaluateBlockBody hcl.Body
@ -208,16 +201,10 @@ func (c *MockEvalContext) SetProviderInput(addr addrs.AbsProviderConfig, vals ma
c.SetProviderInputValues = vals c.SetProviderInputValues = vals
} }
func (c *MockEvalContext) InitProvisioner(n string) error { func (c *MockEvalContext) Provisioner(n string) (provisioners.Interface, error) {
c.InitProvisionerCalled = true
c.InitProvisionerName = n
return c.InitProvisionerError
}
func (c *MockEvalContext) Provisioner(n string) provisioners.Interface {
c.ProvisionerCalled = true c.ProvisionerCalled = true
c.ProvisionerName = n c.ProvisionerName = n
return c.ProvisionerProvisioner return c.ProvisionerProvisioner, nil
} }
func (c *MockEvalContext) ProvisionerSchema(n string) *configschema.Block { func (c *MockEvalContext) ProvisionerSchema(n string) *configschema.Block {
@ -226,9 +213,8 @@ func (c *MockEvalContext) ProvisionerSchema(n string) *configschema.Block {
return c.ProvisionerSchemaSchema return c.ProvisionerSchemaSchema
} }
func (c *MockEvalContext) CloseProvisioner(n string) error { func (c *MockEvalContext) CloseProvisioners() error {
c.CloseProvisionerCalled = true c.CloseProvisionersCalled = true
c.CloseProvisionerName = n
return nil return nil
} }