2014-06-12 18:40:53 -05:00
|
|
|
package config
|
|
|
|
|
2014-06-12 19:24:55 -05:00
|
|
|
import (
|
2014-06-20 12:33:26 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
|
2014-06-12 19:24:55 -05:00
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
"github.com/mitchellh/reflectwalk"
|
|
|
|
)
|
|
|
|
|
2014-06-12 18:40:53 -05:00
|
|
|
// UnknownVariableValue is a sentinel value that can be used
|
|
|
|
// to denote that the value of a variable is unknown at this time.
|
|
|
|
// RawConfig uses this information to build up data about
|
|
|
|
// unknown keys.
|
|
|
|
const UnknownVariableValue = "74D93920-ED26-11E3-AC10-0800200C9A66"
|
|
|
|
|
|
|
|
// RawConfig is a structure that holds a piece of configuration
|
|
|
|
// where te overall structure is unknown since it will be used
|
|
|
|
// 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 {
|
2014-10-02 13:14:50 -05:00
|
|
|
Key string
|
2014-08-22 10:46:03 -05:00
|
|
|
Raw map[string]interface{}
|
2014-08-21 17:05:56 -05:00
|
|
|
Interpolations []Interpolation
|
2014-08-22 10:46:03 -05:00
|
|
|
Variables map[string]InterpolatedVariable
|
2014-06-12 19:24:55 -05: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 12:33:26 -05:00
|
|
|
result := &RawConfig{Raw: raw}
|
|
|
|
if err := result.init(); err != nil {
|
2014-06-12 19:24:55 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-20 12:33:26 -05:00
|
|
|
return result, nil
|
2014-06-12 18:40:53 -05:00
|
|
|
}
|
|
|
|
|
2014-10-02 13:14:50 -05: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Raw[r.Key]
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:27:53 -05: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{} {
|
|
|
|
return r.config
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:40:53 -05: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.
|
2014-06-12 19:24:55 -05:00
|
|
|
func (r *RawConfig) Interpolate(vs map[string]string) error {
|
|
|
|
config, err := copystructure.Copy(r.Raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.config = config.(map[string]interface{})
|
2014-07-21 13:45:56 -05:00
|
|
|
|
|
|
|
fn := func(i Interpolation) (string, error) {
|
|
|
|
return i.Interpolate(vs)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &interpolationWalker{F: fn, Replace: true}
|
2014-06-12 19:24:55 -05:00
|
|
|
err = reflectwalk.Walk(r.config, w)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-21 13:45:56 -05:00
|
|
|
r.unknownKeys = w.unknownKeys
|
2014-06-12 19:24:55 -05:00
|
|
|
return nil
|
2014-06-12 18:40:53 -05:00
|
|
|
}
|
|
|
|
|
2014-06-20 12:33:26 -05:00
|
|
|
func (r *RawConfig) init() error {
|
2014-07-21 13:45:56 -05:00
|
|
|
r.config = r.Raw
|
2014-08-21 17:05:56 -05:00
|
|
|
r.Interpolations = nil
|
2014-07-21 13:45:56 -05:00
|
|
|
r.Variables = nil
|
|
|
|
|
|
|
|
fn := func(i Interpolation) (string, error) {
|
2014-08-21 17:05:56 -05:00
|
|
|
r.Interpolations = append(r.Interpolations, i)
|
|
|
|
|
2014-07-21 13:45:56 -05:00
|
|
|
for k, v := range i.Variables() {
|
|
|
|
if r.Variables == nil {
|
|
|
|
r.Variables = make(map[string]InterpolatedVariable)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Variables[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
walker := &interpolationWalker{F: fn}
|
2014-06-20 12:33:26 -05:00
|
|
|
if err := reflectwalk.Walk(r.Raw, walker); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-20 19:17:03 -05:00
|
|
|
func (r *RawConfig) merge(r2 *RawConfig) *RawConfig {
|
|
|
|
rawRaw, err := copystructure.Copy(r.Raw)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := rawRaw.(map[string]interface{})
|
|
|
|
for k, v := range r2.Raw {
|
|
|
|
raw[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:24:55 -05: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 {
|
|
|
|
return r.unknownKeys
|
2014-06-12 18:40:53 -05:00
|
|
|
}
|
2014-06-20 12:33:26 -05:00
|
|
|
|
|
|
|
// See GobEncode
|
|
|
|
func (r *RawConfig) GobDecode(b []byte) error {
|
2014-10-02 15:42:36 -05:00
|
|
|
var data gobRawConfig
|
|
|
|
err := gob.NewDecoder(bytes.NewReader(b)).Decode(&data)
|
2014-06-20 12:33:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-02 15:42:36 -05:00
|
|
|
r.Key = data.Key
|
|
|
|
r.Raw = data.Raw
|
|
|
|
|
2014-06-20 12:33:26 -05: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) {
|
2014-10-02 15:42:36 -05:00
|
|
|
data := gobRawConfig{
|
|
|
|
Key: r.Key,
|
|
|
|
Raw: r.Raw,
|
|
|
|
}
|
|
|
|
|
2014-06-20 12:33:26 -05:00
|
|
|
var buf bytes.Buffer
|
2014-10-02 15:42:36 -05:00
|
|
|
if err := gob.NewEncoder(&buf).Encode(data); err != nil {
|
2014-06-20 12:33:26 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
2014-10-02 15:42:36 -05:00
|
|
|
|
|
|
|
type gobRawConfig struct {
|
|
|
|
Key string
|
|
|
|
Raw map[string]interface{}
|
|
|
|
}
|