core: Legacy ApplyFn handling for MockProvisioner

This commit is contained in:
Martin Atkins 2018-09-14 11:38:53 -07:00
parent d48f3600fe
commit 859b384558
1 changed files with 39 additions and 5 deletions

View File

@ -1,11 +1,12 @@
package terraform
import (
"fmt"
"sync"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
"github.com/hashicorp/terraform/provisioners"
"github.com/hashicorp/terraform/tfdiags"
)
var _ provisioners.Interface = (*MockProvisioner)(nil)
@ -50,6 +51,12 @@ func (p *MockProvisioner) GetSchema() provisioners.GetSchemaResponse {
defer p.Unlock()
p.GetSchemaCalled = true
return p.getSchema()
}
// getSchema is the implementation of GetSchema, which can be called from other
// methods on MockProvisioner that may already be holding the lock.
func (p *MockProvisioner) getSchema() provisioners.GetSchemaResponse {
return p.GetSchemaResponse
}
@ -67,12 +74,40 @@ func (p *MockProvisioner) ValidateProvisionerConfig(r provisioners.ValidateProvi
func (p *MockProvisioner) ProvisionResource(r provisioners.ProvisionResourceRequest) provisioners.ProvisionResourceResponse {
p.Lock()
defer p.Unlock()
p.ProvisionResourceCalled = true
p.ProvisionResourceRequest = r
if p.ApplyFn != nil {
return provisioners.ProvisionResourceResponse{
Diagnostics: tfdiags.Diagnostics(nil).Append(fmt.Errorf("legacy ApplyFn handling in MockProvisioner not actually implemented yet")),
schema := p.getSchema()
rc := NewResourceConfigShimmed(r.Config, schema.Provisioner)
connVal := r.Connection
connMap := map[string]string{}
for it := connVal.ElementIterator(); it.Next(); {
ak, av := it.Element()
name := ak.AsString()
if !av.IsKnown() || av.IsNull() {
continue
}
av, _ = convert.Convert(av, cty.String)
connMap[name] = av.AsString()
}
// We no longer pass the full instance state to a provisioner, so we'll
// construct a partial one that should be good enough for what existing
// test mocks need.
is := &InstanceState{
Ephemeral: EphemeralState{
ConnInfo: connMap,
},
}
var resp provisioners.ProvisionResourceResponse
err := p.ApplyFn(is, rc)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
}
return resp
}
if p.ProvisionResourceFn != nil {
fn := p.ProvisionResourceFn
@ -80,7 +115,6 @@ func (p *MockProvisioner) ProvisionResource(r provisioners.ProvisionResourceRequ
return fn(r)
}
defer p.Unlock()
return p.ProvisionResourceResponse
}