forked from hashicorp/terraform
/
eval_variable.go
56 lines (47 loc) · 1.27 KB
/
eval_variable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package terraform
import (
"fmt"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/config"
"github.com/mitchellh/mapstructure"
)
// EvalSetVariables is an EvalNode implementation that sets the variables
// explicitly for interpolation later.
type EvalSetVariables struct {
Variables map[string]string
}
// TODO: test
func (n *EvalSetVariables) Eval(ctx EvalContext) (interface{}, error) {
ctx.SetVariables(n.Variables)
return nil, nil
}
// EvalVariableBlock is an EvalNode implementation that evaluates the
// given configuration, and uses the final values as a way to set the
// mapping.
type EvalVariableBlock struct {
Config **ResourceConfig
Variables map[string]string
}
// TODO: test
func (n *EvalVariableBlock) Eval(ctx EvalContext) (interface{}, error) {
// Clear out the existing mapping
for k, _ := range n.Variables {
delete(n.Variables, k)
}
// Get our configuration
rc := *n.Config
for k, v := range rc.Config {
var vStr string
if err := mapstructure.WeakDecode(v, &vStr); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf(
"%s: error reading value: {{err}}", k), err)
}
n.Variables[k] = vStr
}
for k, _ := range rc.Raw {
if _, ok := n.Variables[k]; !ok {
n.Variables[k] = config.UnknownVariableValue
}
}
return nil, nil
}