8 "github.com/hashicorp/terraform/config"
9 "github.com/hashicorp/terraform/config/module"
10 "github.com/hashicorp/terraform/helper/hilmapstructure"
13 // Variables returns the fully loaded set of variables to use with
14 // ContextOpts and NewContext, loading any additional variables from
15 // the environment or any other sources.
17 // The given module tree doesn't need to be loaded.
20 override map[string]interface{}) (map[string]interface{}, error) {
21 result := make(map[string]interface{})
23 // Variables are loaded in the following sequence. Each additional step
24 // will override conflicting variable keys from prior steps:
26 // * Take default values from config
27 // * Take values from TF_VAR_x env vars
28 // * Take values specified in the "override" param which is usually
29 // from -var, -var-file, etc.
32 // First load from the config
33 for _, v := range m.Config().Variables {
34 // If the var has no default, ignore
39 // If the type isn't a string, we use it as-is since it is a rich type
40 if v.Type() != config.VariableTypeString {
41 result[v.Name] = v.Default
45 // v.Default has already been parsed as HCL but it may be an int type
46 switch typedDefault := v.Default.(type) {
48 if typedDefault == "" {
51 result[v.Name] = typedDefault
53 result[v.Name] = fmt.Sprintf("%d", typedDefault)
54 case float32, float64:
55 result[v.Name] = fmt.Sprintf("%f", typedDefault)
57 result[v.Name] = fmt.Sprintf("%t", typedDefault)
60 "Unknown default var type: %T\n\n"+
61 "THIS IS A BUG. Please report it.",
67 for _, v := range os.Environ() {
68 if !strings.HasPrefix(v, VarEnvPrefix) {
72 // Strip off the prefix and get the value after the first "="
73 idx := strings.Index(v, "=")
74 k := v[len(VarEnvPrefix):idx]
77 // Override the configuration-default values. Note that *not* finding the variable
78 // in configuration is OK, as we don't want to preclude people from having multiple
79 // sets of TF_VAR_whatever in their environment even if it is a little weird.
80 for _, schema := range m.Config().Variables {
85 varType := schema.Type()
86 varVal, err := parseVariableAsHCL(k, v, varType)
92 case config.VariableTypeMap:
93 if err := varSetMap(result, k, varVal); err != nil {
102 // Load from overrides
103 for k, v := range override {
104 for _, schema := range m.Config().Variables {
105 if schema.Name != k {
109 switch schema.Type() {
110 case config.VariableTypeList:
112 case config.VariableTypeMap:
113 if err := varSetMap(result, k, v); err != nil {
116 case config.VariableTypeString:
117 // Convert to a string and set. We don't catch any errors
118 // here because the validation step later should catch
121 if err := hilmapstructure.WeakDecode(v, &strVal); err == nil {
128 "Unhandled var type: %T\n\n"+
129 "THIS IS A BUG. Please report it.",
138 // varSetMap sets or merges the map in "v" with the key "k" in the
139 // "current" set of variables. This is just a private function to remove
140 // duplicate logic in Variables
141 func varSetMap(current map[string]interface{}, k string, v interface{}) error {
142 existing, ok := current[k]
148 existingMap, ok := existing.(map[string]interface{})
150 panic(fmt.Sprintf("%q is not a map, this is a bug in Terraform.", k))
153 switch typedV := v.(type) {
154 case []map[string]interface{}:
155 for newKey, newVal := range typedV[0] {
156 existingMap[newKey] = newVal
158 case map[string]interface{}:
159 for newKey, newVal := range typedV {
160 existingMap[newKey] = newVal
163 return fmt.Errorf("variable %q should be type map, got %s", k, hclTypeName(v))