2017-01-19 05:49:31 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2018-07-05 19:33:29 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/config/hcl2shim"
|
2017-01-19 05:49:31 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Backend represents a partial backend.Backend implementation and simplifies
|
|
|
|
// the creation of configuration loading and validation.
|
|
|
|
//
|
|
|
|
// Unlike other schema structs such as Provider, this struct is meant to be
|
|
|
|
// embedded within your actual implementation. It provides implementations
|
|
|
|
// only for Input and Configure and gives you a method for accessing the
|
|
|
|
// configuration in the form of a ResourceData that you're expected to call
|
|
|
|
// from the other implementation funcs.
|
|
|
|
type Backend struct {
|
|
|
|
// Schema is the schema for the configuration of this backend. If this
|
|
|
|
// Backend has no configuration this can be omitted.
|
|
|
|
Schema map[string]*Schema
|
|
|
|
|
|
|
|
// ConfigureFunc is called to configure the backend. Use the
|
|
|
|
// FromContext* methods to extract information from the context.
|
|
|
|
// This can be nil, in which case nothing will be called but the
|
|
|
|
// config will still be stored.
|
|
|
|
ConfigureFunc func(context.Context) error
|
|
|
|
|
|
|
|
config *ResourceData
|
|
|
|
}
|
|
|
|
|
2017-04-06 16:31:27 +02:00
|
|
|
var (
|
2017-04-06 16:39:59 +02:00
|
|
|
backendConfigKey = contextKey("backend config")
|
2017-01-19 05:49:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// FromContextBackendConfig extracts a ResourceData with the configuration
|
|
|
|
// from the context. This should only be called by Backend functions.
|
|
|
|
func FromContextBackendConfig(ctx context.Context) *ResourceData {
|
|
|
|
return ctx.Value(backendConfigKey).(*ResourceData)
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func (b *Backend) ConfigSchema() *configschema.Block {
|
|
|
|
// This is an alias of CoreConfigSchema just to implement the
|
|
|
|
// backend.Backend interface.
|
|
|
|
return b.CoreConfigSchema()
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func (b *Backend) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
|
2017-01-19 05:49:31 +01:00
|
|
|
if b == nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
return nil
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
shimRC := b.shimConfig(obj)
|
|
|
|
warns, errs := schemaMap(b.Schema).Validate(shimRC)
|
|
|
|
for _, warn := range warns {
|
|
|
|
diags = diags.Append(tfdiags.SimpleWarning(warn))
|
|
|
|
}
|
|
|
|
for _, err := range errs {
|
|
|
|
diags = diags.Append(err)
|
|
|
|
}
|
|
|
|
return diags
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics {
|
2017-01-19 05:49:31 +01:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-01-19 05:49:31 +01:00
|
|
|
sm := schemaMap(b.Schema)
|
2018-03-21 02:43:02 +01:00
|
|
|
shimRC := b.shimConfig(obj)
|
2017-01-19 05:49:31 +01:00
|
|
|
|
|
|
|
// Get a ResourceData for this configuration. To do this, we actually
|
|
|
|
// generate an intermediary "diff" although that is never exposed.
|
2018-03-21 02:43:02 +01:00
|
|
|
diff, err := sm.Diff(nil, shimRC, nil, nil)
|
2017-01-19 05:49:31 +01:00
|
|
|
if err != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(err)
|
|
|
|
return diags
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := sm.Data(nil, diff)
|
|
|
|
if err != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(err)
|
|
|
|
return diags
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
b.config = data
|
|
|
|
|
|
|
|
if b.ConfigureFunc != nil {
|
|
|
|
err = b.ConfigureFunc(context.WithValue(
|
|
|
|
context.Background(), backendConfigKey, data))
|
|
|
|
if err != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(err)
|
|
|
|
return diags
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// shimConfig turns a new-style cty.Value configuration (which must be of
|
|
|
|
// an object type) into a minimal old-style *terraform.ResourceConfig object
|
|
|
|
// that should be populated enough to appease the not-yet-updated functionality
|
|
|
|
// in this package. This should be removed once everything is updated.
|
|
|
|
func (b *Backend) shimConfig(obj cty.Value) *terraform.ResourceConfig {
|
|
|
|
shimMap := hcl2shim.ConfigValueFromHCL2(obj).(map[string]interface{})
|
|
|
|
return &terraform.ResourceConfig{
|
|
|
|
Config: shimMap,
|
|
|
|
Raw: shimMap,
|
|
|
|
}
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config returns the configuration. This is available after Configure is
|
|
|
|
// called.
|
|
|
|
func (b *Backend) Config() *ResourceData {
|
|
|
|
return b.config
|
|
|
|
}
|