8 "github.com/hashicorp/hil"
9 "github.com/hashicorp/hil/ast"
10 "github.com/mitchellh/copystructure"
11 "github.com/mitchellh/reflectwalk"
14 // UnknownVariableValue is a sentinel value that can be used
15 // to denote that the value of a variable is unknown at this time.
16 // RawConfig uses this information to build up data about
18 const UnknownVariableValue = "74D93920-ED26-11E3-AC10-0800200C9A66"
20 // RawConfig is a structure that holds a piece of configuration
21 // where the overall structure is unknown since it will be used
22 // to configure a plugin or some other similar external component.
24 // RawConfigs can be interpolated with variables that come from
25 // other resources, user variables, etc.
27 // RawConfig supports a query-like interface to request
28 // information from deep within the structure.
29 type RawConfig struct {
31 Raw map[string]interface{}
32 Interpolations []ast.Node
33 Variables map[string]InterpolatedVariable
36 config map[string]interface{}
40 // NewRawConfig creates a new RawConfig structure and populates the
41 // publicly readable struct fields.
42 func NewRawConfig(raw map[string]interface{}) (*RawConfig, error) {
43 result := &RawConfig{Raw: raw}
44 if err := result.init(); err != nil {
51 // RawMap returns a copy of the RawConfig.Raw map.
52 func (r *RawConfig) RawMap() map[string]interface{} {
56 m := make(map[string]interface{})
57 for k, v := range r.Raw {
63 // Copy returns a copy of this RawConfig, uninterpolated.
64 func (r *RawConfig) Copy() *RawConfig {
72 newRaw := make(map[string]interface{})
73 for k, v := range r.Raw {
77 result, err := NewRawConfig(newRaw)
79 panic("copy failed: " + err.Error())
86 // Value returns the value of the configuration if this configuration
87 // has a Key set. If this does not have a Key set, nil will be returned.
88 func (r *RawConfig) Value() interface{} {
89 if c := r.Config(); c != nil {
90 if v, ok := c[r.Key]; ok {
100 // Config returns the entire configuration with the variables
101 // interpolated from any call to Interpolate.
103 // If any interpolated variables are unknown (value set to
104 // UnknownVariableValue), the first non-container (map, slice, etc.) element
105 // will be removed from the config. The keys of unknown variables
106 // can be found using the UnknownKeys function.
108 // By pruning out unknown keys from the configuration, the raw
109 // structure will always successfully decode into its ultimate
110 // structure using something like mapstructure.
111 func (r *RawConfig) Config() map[string]interface{} {
113 defer r.lock.Unlock()
117 // Interpolate uses the given mapping of variable values and uses
118 // those as the values to replace any variables in this raw
121 // Any prior calls to Interpolate are replaced with this one.
123 // If a variable key is missing, this will panic.
124 func (r *RawConfig) Interpolate(vs map[string]ast.Variable) error {
126 defer r.lock.Unlock()
128 config := langEvalConfig(vs)
129 return r.interpolate(func(root ast.Node) (interface{}, error) {
130 // None of the variables we need are computed, meaning we should
131 // be able to properly evaluate.
132 result, err := hil.Eval(root, config)
137 return result.Value, nil
141 // Merge merges another RawConfig into this one (overriding any conflicting
142 // values in this config) and returns a new config. The original config
144 func (r *RawConfig) Merge(other *RawConfig) *RawConfig {
146 defer r.lock.Unlock()
148 // Merge the raw configurations
149 raw := make(map[string]interface{})
150 for k, v := range r.Raw {
153 for k, v := range other.Raw {
158 result, err := NewRawConfig(raw)
163 // Merge the interpolated results
164 result.config = make(map[string]interface{})
165 for k, v := range r.config {
168 for k, v := range other.config {
172 // Build the unknown keys
173 if len(r.unknownKeys) > 0 || len(other.unknownKeys) > 0 {
174 unknownKeys := make(map[string]struct{})
175 for _, k := range r.unknownKeys {
176 unknownKeys[k] = struct{}{}
178 for _, k := range other.unknownKeys {
179 unknownKeys[k] = struct{}{}
182 result.unknownKeys = make([]string, 0, len(unknownKeys))
183 for k, _ := range unknownKeys {
184 result.unknownKeys = append(result.unknownKeys, k)
191 func (r *RawConfig) init() error {
193 defer r.lock.Unlock()
196 r.Interpolations = nil
199 fn := func(node ast.Node) (interface{}, error) {
200 r.Interpolations = append(r.Interpolations, node)
201 vars, err := DetectVariables(node)
206 for _, v := range vars {
207 if r.Variables == nil {
208 r.Variables = make(map[string]InterpolatedVariable)
211 r.Variables[v.FullKey()] = v
217 walker := &interpolationWalker{F: fn}
218 if err := reflectwalk.Walk(r.Raw, walker); err != nil {
225 func (r *RawConfig) interpolate(fn interpolationWalkerFunc) error {
226 config, err := copystructure.Copy(r.Raw)
230 r.config = config.(map[string]interface{})
232 w := &interpolationWalker{F: fn, Replace: true}
233 err = reflectwalk.Walk(r.config, w)
238 r.unknownKeys = w.unknownKeys
242 func (r *RawConfig) merge(r2 *RawConfig) *RawConfig {
243 if r == nil && r2 == nil {
251 rawRaw, err := copystructure.Copy(r.Raw)
256 raw := rawRaw.(map[string]interface{})
258 for k, v := range r2.Raw {
263 result, err := NewRawConfig(raw)
271 // UnknownKeys returns the keys of the configuration that are unknown
272 // because they had interpolated variables that must be computed.
273 func (r *RawConfig) UnknownKeys() []string {
275 defer r.lock.Unlock()
280 func (r *RawConfig) GobDecode(b []byte) error {
281 var data gobRawConfig
282 err := gob.NewDecoder(bytes.NewReader(b)).Decode(&data)
293 // GobEncode is a custom Gob encoder to use so that we only include the
294 // raw configuration. Interpolated variables and such are lost and the
295 // tree of interpolated variables is recomputed on decode, since it is
296 // referentially transparent.
297 func (r *RawConfig) GobEncode() ([]byte, error) {
299 defer r.lock.Unlock()
301 data := gobRawConfig{
307 if err := gob.NewEncoder(&buf).Encode(data); err != nil {
311 return buf.Bytes(), nil
314 type gobRawConfig struct {
316 Raw map[string]interface{}
319 // langEvalConfig returns the evaluation configuration we use to execute.
320 func langEvalConfig(vs map[string]ast.Variable) *hil.EvalConfig {
321 funcMap := make(map[string]ast.Function)
322 for k, v := range Funcs() {
325 funcMap["lookup"] = interpolationFuncLookup(vs)
326 funcMap["keys"] = interpolationFuncKeys(vs)
327 funcMap["values"] = interpolationFuncValues(vs)
329 return &hil.EvalConfig{
330 GlobalScope: &ast.BasicScope{