2014-06-13 01:40:53 +02:00
|
|
|
package config
|
|
|
|
|
2014-06-13 02:24:55 +02:00
|
|
|
import (
|
2014-06-20 19:33:26 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2017-09-29 02:07:37 +02:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
2015-04-09 18:21:38 +02:00
|
|
|
"sync"
|
2014-06-20 19:33:26 +02:00
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
hcl2 "github.com/hashicorp/hcl/v2"
|
2016-01-31 08:38:37 +01:00
|
|
|
"github.com/hashicorp/hil"
|
|
|
|
"github.com/hashicorp/hil/ast"
|
2014-06-13 02:24:55 +02:00
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
"github.com/mitchellh/reflectwalk"
|
|
|
|
)
|
|
|
|
|
2014-06-13 01:40:53 +02:00
|
|
|
// RawConfig is a structure that holds a piece of configuration
|
2016-10-29 17:18:56 +02:00
|
|
|
// where the overall structure is unknown since it will be used
|
2014-06-13 01:40:53 +02:00
|
|
|
// to configure a plugin or some other similar external component.
|
|
|
|
//
|
|
|
|
// RawConfigs can be interpolated with variables that come from
|
|
|
|
// other resources, user variables, etc.
|
|
|
|
//
|
|
|
|
// RawConfig supports a query-like interface to request
|
|
|
|
// information from deep within the structure.
|
|
|
|
type RawConfig struct {
|
2017-09-21 00:02:22 +02:00
|
|
|
Key string
|
|
|
|
|
|
|
|
// Only _one_ of Raw and Body may be populated at a time.
|
|
|
|
//
|
|
|
|
// In the normal case, Raw is populated and Body is nil.
|
|
|
|
//
|
|
|
|
// When the experimental HCL2 parsing mode is enabled, "Body"
|
|
|
|
// is populated and RawConfig serves only to transport the hcl2.Body
|
|
|
|
// through the rest of Terraform core so we can ultimately decode it
|
|
|
|
// once its schema is known.
|
|
|
|
//
|
|
|
|
// Once we transition to HCL2 as the primary representation, RawConfig
|
|
|
|
// should be removed altogether and the hcl2.Body should be passed
|
|
|
|
// around directly.
|
|
|
|
|
|
|
|
Raw map[string]interface{}
|
|
|
|
Body hcl2.Body
|
|
|
|
|
2015-01-13 19:27:57 +01:00
|
|
|
Interpolations []ast.Node
|
2014-08-22 17:46:03 +02:00
|
|
|
Variables map[string]InterpolatedVariable
|
2014-06-13 02:24:55 +02:00
|
|
|
|
2015-04-09 18:21:38 +02:00
|
|
|
lock sync.Mutex
|
2014-06-13 02:24:55 +02:00
|
|
|
config map[string]interface{}
|
|
|
|
unknownKeys []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRawConfig creates a new RawConfig structure and populates the
|
|
|
|
// publicly readable struct fields.
|
|
|
|
func NewRawConfig(raw map[string]interface{}) (*RawConfig, error) {
|
2014-06-20 19:33:26 +02:00
|
|
|
result := &RawConfig{Raw: raw}
|
|
|
|
if err := result.init(); err != nil {
|
2014-06-13 02:24:55 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-20 19:33:26 +02:00
|
|
|
return result, nil
|
2014-06-13 01:40:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 00:02:22 +02:00
|
|
|
// NewRawConfigHCL2 creates a new RawConfig that is serving as a capsule
|
|
|
|
// to transport a hcl2.Body. In this mode, the publicly-readable struct
|
|
|
|
// fields are not populated since all operations should instead be diverted
|
|
|
|
// to the HCL2 body.
|
|
|
|
//
|
|
|
|
// For a RawConfig object constructed with this function, the only valid use
|
|
|
|
// is to later retrieve the Body value and call its own methods. Callers
|
|
|
|
// may choose to set and then later handle the Key field, in a manner
|
|
|
|
// consistent with how it is handled by the Value method, but the Value
|
|
|
|
// method itself must not be used.
|
|
|
|
//
|
|
|
|
// This is an experimental codepath to be used only by the HCL2 config loader.
|
|
|
|
// Non-experimental parsing should _always_ use NewRawConfig to produce a
|
|
|
|
// fully-functional RawConfig object.
|
|
|
|
func NewRawConfigHCL2(body hcl2.Body) *RawConfig {
|
|
|
|
return &RawConfig{
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:16:37 +02:00
|
|
|
// RawMap returns a copy of the RawConfig.Raw map.
|
|
|
|
func (r *RawConfig) RawMap() map[string]interface{} {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
for k, v := range r.Raw {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:21:38 +02:00
|
|
|
// Copy returns a copy of this RawConfig, uninterpolated.
|
|
|
|
func (r *RawConfig) Copy() *RawConfig {
|
2016-10-09 04:05:58 +02:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:21:38 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2017-10-14 03:28:03 +02:00
|
|
|
if r.Body != nil {
|
|
|
|
return NewRawConfigHCL2(r.Body)
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:40:35 +01:00
|
|
|
newRaw := make(map[string]interface{})
|
|
|
|
for k, v := range r.Raw {
|
|
|
|
newRaw[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := NewRawConfig(newRaw)
|
2015-04-09 18:21:38 +02:00
|
|
|
if err != nil {
|
|
|
|
panic("copy failed: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:31:04 +02:00
|
|
|
result.Key = r.Key
|
2015-04-09 18:21:38 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-10-02 20:14:50 +02:00
|
|
|
// Value returns the value of the configuration if this configuration
|
|
|
|
// has a Key set. If this does not have a Key set, nil will be returned.
|
|
|
|
func (r *RawConfig) Value() interface{} {
|
|
|
|
if c := r.Config(); c != nil {
|
|
|
|
if v, ok := c[r.Key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:21:38 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
2014-10-02 20:14:50 +02:00
|
|
|
return r.Raw[r.Key]
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:27:53 +02:00
|
|
|
// Config returns the entire configuration with the variables
|
|
|
|
// interpolated from any call to Interpolate.
|
|
|
|
//
|
|
|
|
// If any interpolated variables are unknown (value set to
|
|
|
|
// UnknownVariableValue), the first non-container (map, slice, etc.) element
|
|
|
|
// will be removed from the config. The keys of unknown variables
|
|
|
|
// can be found using the UnknownKeys function.
|
|
|
|
//
|
|
|
|
// By pruning out unknown keys from the configuration, the raw
|
|
|
|
// structure will always successfully decode into its ultimate
|
|
|
|
// structure using something like mapstructure.
|
|
|
|
func (r *RawConfig) Config() map[string]interface{} {
|
2016-07-29 19:17:48 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
2014-06-13 02:27:53 +02:00
|
|
|
return r.config
|
|
|
|
}
|
|
|
|
|
2014-06-13 01:40:53 +02:00
|
|
|
// Interpolate uses the given mapping of variable values and uses
|
|
|
|
// those as the values to replace any variables in this raw
|
|
|
|
// configuration.
|
|
|
|
//
|
|
|
|
// Any prior calls to Interpolate are replaced with this one.
|
|
|
|
//
|
|
|
|
// If a variable key is missing, this will panic.
|
2015-01-15 07:01:42 +01:00
|
|
|
func (r *RawConfig) Interpolate(vs map[string]ast.Variable) error {
|
2015-04-09 18:21:38 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2015-01-15 07:01:42 +01:00
|
|
|
config := langEvalConfig(vs)
|
2016-04-11 19:40:06 +02:00
|
|
|
return r.interpolate(func(root ast.Node) (interface{}, error) {
|
2015-02-28 07:47:43 +01:00
|
|
|
// None of the variables we need are computed, meaning we should
|
|
|
|
// be able to properly evaluate.
|
2016-04-09 03:23:36 +02:00
|
|
|
result, err := hil.Eval(root, config)
|
2015-01-13 19:27:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-04-11 19:40:06 +02:00
|
|
|
return result.Value, nil
|
2014-10-03 01:51:20 +02:00
|
|
|
})
|
2014-06-13 01:40:53 +02:00
|
|
|
}
|
|
|
|
|
2015-02-10 08:01:47 +01:00
|
|
|
// Merge merges another RawConfig into this one (overriding any conflicting
|
|
|
|
// values in this config) and returns a new config. The original config
|
|
|
|
// is not modified.
|
|
|
|
func (r *RawConfig) Merge(other *RawConfig) *RawConfig {
|
2015-04-09 18:21:38 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2015-02-10 08:01:47 +01:00
|
|
|
// Merge the raw configurations
|
|
|
|
raw := make(map[string]interface{})
|
|
|
|
for k, v := range r.Raw {
|
|
|
|
raw[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range other.Raw {
|
|
|
|
raw[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the result
|
|
|
|
result, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the interpolated results
|
|
|
|
result.config = make(map[string]interface{})
|
|
|
|
for k, v := range r.config {
|
|
|
|
result.config[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range other.config {
|
|
|
|
result.config[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the unknown keys
|
2016-10-20 00:21:09 +02:00
|
|
|
if len(r.unknownKeys) > 0 || len(other.unknownKeys) > 0 {
|
|
|
|
unknownKeys := make(map[string]struct{})
|
|
|
|
for _, k := range r.unknownKeys {
|
|
|
|
unknownKeys[k] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, k := range other.unknownKeys {
|
|
|
|
unknownKeys[k] = struct{}{}
|
|
|
|
}
|
2015-02-10 08:01:47 +01:00
|
|
|
|
2016-10-20 00:21:09 +02:00
|
|
|
result.unknownKeys = make([]string, 0, len(unknownKeys))
|
|
|
|
for k, _ := range unknownKeys {
|
|
|
|
result.unknownKeys = append(result.unknownKeys, k)
|
|
|
|
}
|
2015-02-10 08:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-06-20 19:33:26 +02:00
|
|
|
func (r *RawConfig) init() error {
|
2016-08-30 20:16:37 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2014-07-21 20:45:56 +02:00
|
|
|
r.config = r.Raw
|
2014-08-22 00:05:56 +02:00
|
|
|
r.Interpolations = nil
|
2014-07-21 20:45:56 +02:00
|
|
|
r.Variables = nil
|
|
|
|
|
2016-04-11 19:40:06 +02:00
|
|
|
fn := func(node ast.Node) (interface{}, error) {
|
2015-01-13 19:27:57 +01:00
|
|
|
r.Interpolations = append(r.Interpolations, node)
|
|
|
|
vars, err := DetectVariables(node)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-08-22 00:05:56 +02:00
|
|
|
|
2015-01-13 19:27:57 +01:00
|
|
|
for _, v := range vars {
|
2014-07-21 20:45:56 +02:00
|
|
|
if r.Variables == nil {
|
|
|
|
r.Variables = make(map[string]InterpolatedVariable)
|
|
|
|
}
|
|
|
|
|
2015-01-13 19:27:57 +01:00
|
|
|
r.Variables[v.FullKey()] = v
|
2014-07-21 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
walker := &interpolationWalker{F: fn}
|
2014-06-20 19:33:26 +02:00
|
|
|
if err := reflectwalk.Walk(r.Raw, walker); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-03 01:51:20 +02:00
|
|
|
func (r *RawConfig) interpolate(fn interpolationWalkerFunc) error {
|
2017-09-29 02:07:37 +02:00
|
|
|
if r.Body != nil {
|
|
|
|
// For RawConfigs created for the HCL2 experiement, callers must
|
|
|
|
// use the HCL2 Body API directly rather than interpolating via
|
|
|
|
// the RawConfig.
|
|
|
|
return errors.New("this feature is not yet supported under the HCL2 experiment")
|
|
|
|
}
|
|
|
|
|
2014-10-03 01:51:20 +02:00
|
|
|
config, err := copystructure.Copy(r.Raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.config = config.(map[string]interface{})
|
|
|
|
|
|
|
|
w := &interpolationWalker{F: fn, Replace: true}
|
|
|
|
err = reflectwalk.Walk(r.config, w)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.unknownKeys = w.unknownKeys
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-21 02:17:03 +02:00
|
|
|
func (r *RawConfig) merge(r2 *RawConfig) *RawConfig {
|
2016-12-14 06:48:59 +01:00
|
|
|
if r == nil && r2 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r == nil {
|
|
|
|
r = &RawConfig{}
|
|
|
|
}
|
|
|
|
|
2014-07-21 02:17:03 +02:00
|
|
|
rawRaw, err := copystructure.Copy(r.Raw)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := rawRaw.(map[string]interface{})
|
2016-12-14 06:48:59 +01:00
|
|
|
if r2 != nil {
|
|
|
|
for k, v := range r2.Raw {
|
|
|
|
raw[k] = v
|
|
|
|
}
|
2014-07-21 02:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-09-29 02:07:37 +02:00
|
|
|
// couldBeInteger is a helper that determines if the represented value could
|
|
|
|
// result in an integer.
|
|
|
|
//
|
|
|
|
// This function only works for RawConfigs that have "Key" set, meaning that
|
|
|
|
// a single result can be produced. Calling this function will overwrite
|
|
|
|
// the Config and Value results to be a test value.
|
|
|
|
//
|
|
|
|
// This function is conservative. If there is some doubt about whether the
|
|
|
|
// result could be an integer -- for example, if it depends on a variable
|
|
|
|
// whose type we don't know yet -- it will still return true.
|
|
|
|
func (r *RawConfig) couldBeInteger() bool {
|
|
|
|
if r.Key == "" {
|
|
|
|
// un-keyed RawConfigs can never produce numbers
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.Body == nil {
|
|
|
|
// Normal path: using the interpolator in this package
|
|
|
|
// Interpolate with a fixed number to verify that its a number.
|
|
|
|
r.interpolate(func(root ast.Node) (interface{}, error) {
|
|
|
|
// Execute the node but transform the AST so that it returns
|
|
|
|
// a fixed value of "5" for all interpolations.
|
|
|
|
result, err := hil.Eval(
|
|
|
|
hil.FixedValueTransform(
|
|
|
|
root, &ast.LiteralNode{Value: "5", Typex: ast.TypeString}),
|
|
|
|
nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.Value, nil
|
|
|
|
})
|
|
|
|
_, err := strconv.ParseInt(r.Value().(string), 0, 0)
|
|
|
|
return err == nil
|
|
|
|
} else {
|
2019-10-29 00:26:40 +01:00
|
|
|
// We briefly tried to gradually implement HCL2 support by adding a
|
|
|
|
// branch here, but that experiment was not successful.
|
|
|
|
panic("HCL2 experimental path no longer supported")
|
2017-09-29 02:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:55 +02:00
|
|
|
// UnknownKeys returns the keys of the configuration that are unknown
|
|
|
|
// because they had interpolated variables that must be computed.
|
|
|
|
func (r *RawConfig) UnknownKeys() []string {
|
2016-08-30 20:16:37 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
2014-06-13 02:24:55 +02:00
|
|
|
return r.unknownKeys
|
2014-06-13 01:40:53 +02:00
|
|
|
}
|
2014-06-20 19:33:26 +02:00
|
|
|
|
|
|
|
// See GobEncode
|
|
|
|
func (r *RawConfig) GobDecode(b []byte) error {
|
2014-10-02 22:42:36 +02:00
|
|
|
var data gobRawConfig
|
|
|
|
err := gob.NewDecoder(bytes.NewReader(b)).Decode(&data)
|
2014-06-20 19:33:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:42:36 +02:00
|
|
|
r.Key = data.Key
|
|
|
|
r.Raw = data.Raw
|
|
|
|
|
2014-06-20 19:33:26 +02:00
|
|
|
return r.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GobEncode is a custom Gob encoder to use so that we only include the
|
|
|
|
// raw configuration. Interpolated variables and such are lost and the
|
|
|
|
// tree of interpolated variables is recomputed on decode, since it is
|
|
|
|
// referentially transparent.
|
|
|
|
func (r *RawConfig) GobEncode() ([]byte, error) {
|
2015-04-09 18:21:38 +02:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2014-10-02 22:42:36 +02:00
|
|
|
data := gobRawConfig{
|
|
|
|
Key: r.Key,
|
|
|
|
Raw: r.Raw,
|
|
|
|
}
|
|
|
|
|
2014-06-20 19:33:26 +02:00
|
|
|
var buf bytes.Buffer
|
2014-10-02 22:42:36 +02:00
|
|
|
if err := gob.NewEncoder(&buf).Encode(data); err != nil {
|
2014-06-20 19:33:26 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
2014-10-02 22:42:36 +02:00
|
|
|
|
|
|
|
type gobRawConfig struct {
|
|
|
|
Key string
|
|
|
|
Raw map[string]interface{}
|
|
|
|
}
|
2015-01-13 20:50:44 +01:00
|
|
|
|
2015-01-15 07:01:42 +01:00
|
|
|
// langEvalConfig returns the evaluation configuration we use to execute.
|
2019-10-29 00:26:40 +01:00
|
|
|
//
|
|
|
|
// The interpolation functions are no longer available here, because this
|
|
|
|
// codepath is no longer used. Instead, see ../lang/functions.go .
|
2016-01-31 08:38:37 +01:00
|
|
|
func langEvalConfig(vs map[string]ast.Variable) *hil.EvalConfig {
|
2015-01-15 07:01:42 +01:00
|
|
|
funcMap := make(map[string]ast.Function)
|
2016-01-15 22:28:47 +01:00
|
|
|
for k, v := range Funcs() {
|
2015-01-13 21:06:04 +01:00
|
|
|
funcMap[k] = v
|
|
|
|
}
|
|
|
|
|
2016-01-31 08:38:37 +01:00
|
|
|
return &hil.EvalConfig{
|
2015-01-15 07:01:42 +01:00
|
|
|
GlobalScope: &ast.BasicScope{
|
2015-01-14 19:40:43 +01:00
|
|
|
VarMap: vs,
|
2015-01-13 21:06:04 +01:00
|
|
|
FuncMap: funcMap,
|
2015-01-13 20:50:44 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|