10 "github.com/zclconf/go-cty/cty"
11 "github.com/zclconf/go-cty/cty/convert"
13 hcl2 "github.com/hashicorp/hcl2/hcl"
14 "github.com/hashicorp/hil"
15 "github.com/hashicorp/hil/ast"
16 "github.com/mitchellh/copystructure"
17 "github.com/mitchellh/reflectwalk"
20 // UnknownVariableValue is a sentinel value that can be used
21 // to denote that the value of a variable is unknown at this time.
22 // RawConfig uses this information to build up data about
24 const UnknownVariableValue = "74D93920-ED26-11E3-AC10-0800200C9A66"
26 // RawConfig is a structure that holds a piece of configuration
27 // where the overall structure is unknown since it will be used
28 // to configure a plugin or some other similar external component.
30 // RawConfigs can be interpolated with variables that come from
31 // other resources, user variables, etc.
33 // RawConfig supports a query-like interface to request
34 // information from deep within the structure.
35 type RawConfig struct {
38 // Only _one_ of Raw and Body may be populated at a time.
40 // In the normal case, Raw is populated and Body is nil.
42 // When the experimental HCL2 parsing mode is enabled, "Body"
43 // is populated and RawConfig serves only to transport the hcl2.Body
44 // through the rest of Terraform core so we can ultimately decode it
45 // once its schema is known.
47 // Once we transition to HCL2 as the primary representation, RawConfig
48 // should be removed altogether and the hcl2.Body should be passed
51 Raw map[string]interface{}
54 Interpolations []ast.Node
55 Variables map[string]InterpolatedVariable
58 config map[string]interface{}
62 // NewRawConfig creates a new RawConfig structure and populates the
63 // publicly readable struct fields.
64 func NewRawConfig(raw map[string]interface{}) (*RawConfig, error) {
65 result := &RawConfig{Raw: raw}
66 if err := result.init(); err != nil {
73 // NewRawConfigHCL2 creates a new RawConfig that is serving as a capsule
74 // to transport a hcl2.Body. In this mode, the publicly-readable struct
75 // fields are not populated since all operations should instead be diverted
78 // For a RawConfig object constructed with this function, the only valid use
79 // is to later retrieve the Body value and call its own methods. Callers
80 // may choose to set and then later handle the Key field, in a manner
81 // consistent with how it is handled by the Value method, but the Value
82 // method itself must not be used.
84 // This is an experimental codepath to be used only by the HCL2 config loader.
85 // Non-experimental parsing should _always_ use NewRawConfig to produce a
86 // fully-functional RawConfig object.
87 func NewRawConfigHCL2(body hcl2.Body) *RawConfig {
93 // RawMap returns a copy of the RawConfig.Raw map.
94 func (r *RawConfig) RawMap() map[string]interface{} {
98 m := make(map[string]interface{})
99 for k, v := range r.Raw {
105 // Copy returns a copy of this RawConfig, uninterpolated.
106 func (r *RawConfig) Copy() *RawConfig {
112 defer r.lock.Unlock()
115 return NewRawConfigHCL2(r.Body)
118 newRaw := make(map[string]interface{})
119 for k, v := range r.Raw {
123 result, err := NewRawConfig(newRaw)
125 panic("copy failed: " + err.Error())
132 // Value returns the value of the configuration if this configuration
133 // has a Key set. If this does not have a Key set, nil will be returned.
134 func (r *RawConfig) Value() interface{} {
135 if c := r.Config(); c != nil {
136 if v, ok := c[r.Key]; ok {
142 defer r.lock.Unlock()
146 // Config returns the entire configuration with the variables
147 // interpolated from any call to Interpolate.
149 // If any interpolated variables are unknown (value set to
150 // UnknownVariableValue), the first non-container (map, slice, etc.) element
151 // will be removed from the config. The keys of unknown variables
152 // can be found using the UnknownKeys function.
154 // By pruning out unknown keys from the configuration, the raw
155 // structure will always successfully decode into its ultimate
156 // structure using something like mapstructure.
157 func (r *RawConfig) Config() map[string]interface{} {
159 defer r.lock.Unlock()
163 // Interpolate uses the given mapping of variable values and uses
164 // those as the values to replace any variables in this raw
167 // Any prior calls to Interpolate are replaced with this one.
169 // If a variable key is missing, this will panic.
170 func (r *RawConfig) Interpolate(vs map[string]ast.Variable) error {
172 defer r.lock.Unlock()
174 config := langEvalConfig(vs)
175 return r.interpolate(func(root ast.Node) (interface{}, error) {
176 // None of the variables we need are computed, meaning we should
177 // be able to properly evaluate.
178 result, err := hil.Eval(root, config)
183 return result.Value, nil
187 // Merge merges another RawConfig into this one (overriding any conflicting
188 // values in this config) and returns a new config. The original config
190 func (r *RawConfig) Merge(other *RawConfig) *RawConfig {
192 defer r.lock.Unlock()
194 // Merge the raw configurations
195 raw := make(map[string]interface{})
196 for k, v := range r.Raw {
199 for k, v := range other.Raw {
204 result, err := NewRawConfig(raw)
209 // Merge the interpolated results
210 result.config = make(map[string]interface{})
211 for k, v := range r.config {
214 for k, v := range other.config {
218 // Build the unknown keys
219 if len(r.unknownKeys) > 0 || len(other.unknownKeys) > 0 {
220 unknownKeys := make(map[string]struct{})
221 for _, k := range r.unknownKeys {
222 unknownKeys[k] = struct{}{}
224 for _, k := range other.unknownKeys {
225 unknownKeys[k] = struct{}{}
228 result.unknownKeys = make([]string, 0, len(unknownKeys))
229 for k, _ := range unknownKeys {
230 result.unknownKeys = append(result.unknownKeys, k)
237 func (r *RawConfig) init() error {
239 defer r.lock.Unlock()
242 r.Interpolations = nil
245 fn := func(node ast.Node) (interface{}, error) {
246 r.Interpolations = append(r.Interpolations, node)
247 vars, err := DetectVariables(node)
252 for _, v := range vars {
253 if r.Variables == nil {
254 r.Variables = make(map[string]InterpolatedVariable)
257 r.Variables[v.FullKey()] = v
263 walker := &interpolationWalker{F: fn}
264 if err := reflectwalk.Walk(r.Raw, walker); err != nil {
271 func (r *RawConfig) interpolate(fn interpolationWalkerFunc) error {
273 // For RawConfigs created for the HCL2 experiement, callers must
274 // use the HCL2 Body API directly rather than interpolating via
276 return errors.New("this feature is not yet supported under the HCL2 experiment")
279 config, err := copystructure.Copy(r.Raw)
283 r.config = config.(map[string]interface{})
285 w := &interpolationWalker{F: fn, Replace: true}
286 err = reflectwalk.Walk(r.config, w)
291 r.unknownKeys = w.unknownKeys
295 func (r *RawConfig) merge(r2 *RawConfig) *RawConfig {
296 if r == nil && r2 == nil {
304 rawRaw, err := copystructure.Copy(r.Raw)
309 raw := rawRaw.(map[string]interface{})
311 for k, v := range r2.Raw {
316 result, err := NewRawConfig(raw)
324 // couldBeInteger is a helper that determines if the represented value could
325 // result in an integer.
327 // This function only works for RawConfigs that have "Key" set, meaning that
328 // a single result can be produced. Calling this function will overwrite
329 // the Config and Value results to be a test value.
331 // This function is conservative. If there is some doubt about whether the
332 // result could be an integer -- for example, if it depends on a variable
333 // whose type we don't know yet -- it will still return true.
334 func (r *RawConfig) couldBeInteger() bool {
336 // un-keyed RawConfigs can never produce numbers
340 // Normal path: using the interpolator in this package
341 // Interpolate with a fixed number to verify that its a number.
342 r.interpolate(func(root ast.Node) (interface{}, error) {
343 // Execute the node but transform the AST so that it returns
344 // a fixed value of "5" for all interpolations.
345 result, err := hil.Eval(
346 hil.FixedValueTransform(
347 root, &ast.LiteralNode{Value: "5", Typex: ast.TypeString}),
353 return result.Value, nil
355 _, err := strconv.ParseInt(r.Value().(string), 0, 0)
358 // HCL2 experiment path: using the HCL2 API via shims
360 // This path catches fewer situations because we have to assume all
361 // variables are entirely unknown in HCL2, rather than the assumption
362 // above that all variables can be numbers because names like "var.foo"
363 // are considered a single variable rather than an attribute access.
364 // This is fine in practice, because we get a definitive answer
365 // during the graph walk when we have real values to work with.
366 attrs, diags := r.Body.JustAttributes()
367 if diags.HasErrors() {
368 // This body is not just a single attribute with a value, so
369 // this can't be a number.
372 attr, hasAttr := attrs[r.Key]
376 result, diags := hcl2EvalWithUnknownVars(attr.Expr)
377 if diags.HasErrors() {
378 // We'll conservatively assume that this error is a result of
379 // us not being ready to fully-populate the scope, and catch
380 // any further problems during the main graph walk.
384 // If the result is convertable to number then we'll allow it.
385 // We do this because an unknown string is optimistically convertable
386 // to number (might be "5") but a _known_ string "hello" is not.
387 _, err := convert.Convert(result, cty.Number)
392 // UnknownKeys returns the keys of the configuration that are unknown
393 // because they had interpolated variables that must be computed.
394 func (r *RawConfig) UnknownKeys() []string {
396 defer r.lock.Unlock()
401 func (r *RawConfig) GobDecode(b []byte) error {
402 var data gobRawConfig
403 err := gob.NewDecoder(bytes.NewReader(b)).Decode(&data)
414 // GobEncode is a custom Gob encoder to use so that we only include the
415 // raw configuration. Interpolated variables and such are lost and the
416 // tree of interpolated variables is recomputed on decode, since it is
417 // referentially transparent.
418 func (r *RawConfig) GobEncode() ([]byte, error) {
420 defer r.lock.Unlock()
422 data := gobRawConfig{
428 if err := gob.NewEncoder(&buf).Encode(data); err != nil {
432 return buf.Bytes(), nil
435 type gobRawConfig struct {
437 Raw map[string]interface{}
440 // langEvalConfig returns the evaluation configuration we use to execute.
441 func langEvalConfig(vs map[string]ast.Variable) *hil.EvalConfig {
442 funcMap := make(map[string]ast.Function)
443 for k, v := range Funcs() {
446 funcMap["lookup"] = interpolationFuncLookup(vs)
447 funcMap["keys"] = interpolationFuncKeys(vs)
448 funcMap["values"] = interpolationFuncValues(vs)
450 return &hil.EvalConfig{
451 GlobalScope: &ast.BasicScope{