2016-12-22 22:57:42 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2018-07-05 19:33:29 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2016-12-22 22:57:42 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provisioner represents a resource provisioner in Terraform and properly
|
|
|
|
// implements all of the ResourceProvisioner API.
|
|
|
|
//
|
|
|
|
// This higher level structure makes it much easier to implement a new or
|
|
|
|
// custom provisioner for Terraform.
|
|
|
|
//
|
|
|
|
// The function callbacks for this structure are all passed a context object.
|
|
|
|
// This context object has a number of pre-defined values that can be accessed
|
|
|
|
// via the global functions defined in context.go.
|
|
|
|
type Provisioner struct {
|
|
|
|
// ConnSchema is the schema for the connection settings for this
|
|
|
|
// provisioner.
|
|
|
|
//
|
|
|
|
// The keys of this map are the configuration keys, and the value is
|
|
|
|
// the schema describing the value of the configuration.
|
|
|
|
//
|
|
|
|
// NOTE: The value of connection keys can only be strings for now.
|
|
|
|
ConnSchema map[string]*Schema
|
|
|
|
|
|
|
|
// Schema is the schema for the usage of this provisioner.
|
|
|
|
//
|
|
|
|
// The keys of this map are the configuration keys, and the value is
|
|
|
|
// the schema describing the value of the configuration.
|
|
|
|
Schema map[string]*Schema
|
|
|
|
|
|
|
|
// ApplyFunc is the function for executing the provisioner. This is required.
|
|
|
|
// It is given a context. See the Provisioner struct docs for more
|
|
|
|
// information.
|
|
|
|
ApplyFunc func(ctx context.Context) error
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
// ValidateFunc is a function for extended validation. This is optional
|
|
|
|
// and should be used when individual field validation is not enough.
|
2017-06-15 19:57:04 +02:00
|
|
|
ValidateFunc func(*terraform.ResourceConfig) ([]string, []error)
|
2016-11-29 21:00:26 +01:00
|
|
|
|
2016-12-22 22:57:42 +01:00
|
|
|
stopCtx context.Context
|
|
|
|
stopCtxCancel context.CancelFunc
|
|
|
|
stopOnce sync.Once
|
|
|
|
}
|
|
|
|
|
2017-04-06 16:39:59 +02:00
|
|
|
// Keys that can be used to access data in the context parameters for
|
|
|
|
// Provisioners.
|
|
|
|
var (
|
|
|
|
connDataInvalid = contextKey("data invalid")
|
2016-12-22 22:57:42 +01:00
|
|
|
|
|
|
|
// This returns a *ResourceData for the connection information.
|
|
|
|
// Guaranteed to never be nil.
|
2017-04-06 16:39:59 +02:00
|
|
|
ProvConnDataKey = contextKey("provider conn data")
|
2016-12-22 22:57:42 +01:00
|
|
|
|
|
|
|
// This returns a *ResourceData for the config information.
|
|
|
|
// Guaranteed to never be nil.
|
2017-04-06 16:39:59 +02:00
|
|
|
ProvConfigDataKey = contextKey("provider config data")
|
2016-12-22 22:57:42 +01:00
|
|
|
|
|
|
|
// This returns a terraform.UIOutput. Guaranteed to never be nil.
|
2017-04-06 16:39:59 +02:00
|
|
|
ProvOutputKey = contextKey("provider output")
|
2016-12-23 01:43:52 +01:00
|
|
|
|
|
|
|
// This returns the raw InstanceState passed to Apply. Guaranteed to
|
|
|
|
// be set, but may be nil.
|
2017-04-06 16:39:59 +02:00
|
|
|
ProvRawStateKey = contextKey("provider raw state")
|
2016-12-22 22:57:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// InternalValidate should be called to validate the structure
|
|
|
|
// of the provisioner.
|
|
|
|
//
|
|
|
|
// This should be called in a unit test to verify before release that this
|
|
|
|
// structure is properly configured for use.
|
|
|
|
func (p *Provisioner) InternalValidate() error {
|
|
|
|
if p == nil {
|
|
|
|
return errors.New("provisioner is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
var validationErrors error
|
|
|
|
{
|
|
|
|
sm := schemaMap(p.ConnSchema)
|
|
|
|
if err := sm.InternalValidate(sm); err != nil {
|
|
|
|
validationErrors = multierror.Append(validationErrors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
sm := schemaMap(p.Schema)
|
|
|
|
if err := sm.InternalValidate(sm); err != nil {
|
|
|
|
validationErrors = multierror.Append(validationErrors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.ApplyFunc == nil {
|
|
|
|
validationErrors = multierror.Append(validationErrors, fmt.Errorf(
|
|
|
|
"ApplyFunc must not be nil"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return validationErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopContext returns a context that checks whether a provisioner is stopped.
|
|
|
|
func (p *Provisioner) StopContext() context.Context {
|
|
|
|
p.stopOnce.Do(p.stopInit)
|
|
|
|
return p.stopCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) stopInit() {
|
|
|
|
p.stopCtx, p.stopCtxCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop implementation of terraform.ResourceProvisioner interface.
|
|
|
|
func (p *Provisioner) Stop() error {
|
|
|
|
p.stopOnce.Do(p.stopInit)
|
|
|
|
p.stopCtxCancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// GetConfigSchema implementation of terraform.ResourceProvisioner interface.
|
|
|
|
func (p *Provisioner) GetConfigSchema() (*configschema.Block, error) {
|
|
|
|
return schemaMap(p.Schema).CoreConfigSchema(), nil
|
|
|
|
}
|
|
|
|
|
2016-12-22 22:57:42 +01:00
|
|
|
// Apply implementation of terraform.ResourceProvisioner interface.
|
|
|
|
func (p *Provisioner) Apply(
|
|
|
|
o terraform.UIOutput,
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
c *terraform.ResourceConfig) error {
|
|
|
|
var connData, configData *ResourceData
|
|
|
|
|
|
|
|
{
|
|
|
|
// We first need to turn the connection information into a
|
|
|
|
// terraform.ResourceConfig so that we can use that type to more
|
|
|
|
// easily build a ResourceData structure. We do this by simply treating
|
|
|
|
// the conn info as configuration input.
|
|
|
|
raw := make(map[string]interface{})
|
2016-12-22 23:11:28 +01:00
|
|
|
if s != nil {
|
|
|
|
for k, v := range s.Ephemeral.ConnInfo {
|
|
|
|
raw[k] = v
|
|
|
|
}
|
2016-12-22 22:57:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := config.NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sm := schemaMap(p.ConnSchema)
|
2017-05-28 07:58:44 +02:00
|
|
|
diff, err := sm.Diff(nil, terraform.NewResourceConfig(c), nil, nil)
|
2016-12-22 22:57:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
connData, err = sm.Data(nil, diff)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Build the configuration data. Doing this requires making a "diff"
|
|
|
|
// even though that's never used. We use that just to get the correct types.
|
|
|
|
configMap := schemaMap(p.Schema)
|
2017-05-28 07:58:44 +02:00
|
|
|
diff, err := configMap.Diff(nil, c, nil, nil)
|
2016-12-22 22:57:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
configData, err = configMap.Data(nil, diff)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the context and call the function
|
|
|
|
ctx := p.StopContext()
|
|
|
|
ctx = context.WithValue(ctx, ProvConnDataKey, connData)
|
|
|
|
ctx = context.WithValue(ctx, ProvConfigDataKey, configData)
|
|
|
|
ctx = context.WithValue(ctx, ProvOutputKey, o)
|
2016-12-23 01:43:52 +01:00
|
|
|
ctx = context.WithValue(ctx, ProvRawStateKey, s)
|
2016-12-22 22:57:42 +01:00
|
|
|
return p.ApplyFunc(ctx)
|
|
|
|
}
|
2017-06-15 19:57:04 +02:00
|
|
|
|
|
|
|
// Validate implements the terraform.ResourceProvisioner interface.
|
|
|
|
func (p *Provisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) {
|
|
|
|
if err := p.InternalValidate(); err != nil {
|
|
|
|
return nil, []error{fmt.Errorf(
|
|
|
|
"Internal validation of the provisioner failed! This is always a bug\n"+
|
|
|
|
"with the provisioner itself, and not a user issue. Please report\n"+
|
|
|
|
"this bug:\n\n%s", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Schema != nil {
|
|
|
|
w, e := schemaMap(p.Schema).Validate(c)
|
|
|
|
ws = append(ws, w...)
|
|
|
|
es = append(es, e...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.ValidateFunc != nil {
|
|
|
|
w, e := p.ValidateFunc(c)
|
|
|
|
ws = append(ws, w...)
|
|
|
|
es = append(es, e...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ws, es
|
|
|
|
}
|