github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/terraform/variables.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/hashicorp/terraform/config" 9 "github.com/hashicorp/terraform/config/module" 10 "github.com/hashicorp/terraform/helper/hilmapstructure" 11 ) 12 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. 16 // 17 // The given module tree doesn't need to be loaded. 18 func Variables( 19 m *module.Tree, 20 override map[string]interface{}) (map[string]interface{}, error) { 21 result := make(map[string]interface{}) 22 23 // Variables are loaded in the following sequence. Each additional step 24 // will override conflicting variable keys from prior steps: 25 // 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. 30 // 31 32 // First load from the config 33 for _, v := range m.Config().Variables { 34 // If the var has no default, ignore 35 if v.Default == nil { 36 continue 37 } 38 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 42 continue 43 } 44 45 // v.Default has already been parsed as HCL but it may be an int type 46 switch typedDefault := v.Default.(type) { 47 case string: 48 if typedDefault == "" { 49 continue 50 } 51 result[v.Name] = typedDefault 52 case int, int64: 53 result[v.Name] = fmt.Sprintf("%d", typedDefault) 54 case float32, float64: 55 result[v.Name] = fmt.Sprintf("%f", typedDefault) 56 case bool: 57 result[v.Name] = fmt.Sprintf("%t", typedDefault) 58 default: 59 panic(fmt.Sprintf( 60 "Unknown default var type: %T\n\n"+ 61 "THIS IS A BUG. Please report it.", 62 v.Default)) 63 } 64 } 65 66 // Load from env vars 67 for _, v := range os.Environ() { 68 if !strings.HasPrefix(v, VarEnvPrefix) { 69 continue 70 } 71 72 // Strip off the prefix and get the value after the first "=" 73 idx := strings.Index(v, "=") 74 k := v[len(VarEnvPrefix):idx] 75 v = v[idx+1:] 76 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 { 81 if schema.Name != k { 82 continue 83 } 84 85 varType := schema.Type() 86 varVal, err := parseVariableAsHCL(k, v, varType) 87 if err != nil { 88 return nil, err 89 } 90 91 switch varType { 92 case config.VariableTypeMap: 93 varSetMap(result, k, varVal) 94 default: 95 result[k] = varVal 96 } 97 } 98 } 99 100 // Load from overrides 101 for k, v := range override { 102 for _, schema := range m.Config().Variables { 103 if schema.Name != k { 104 continue 105 } 106 107 switch schema.Type() { 108 case config.VariableTypeList: 109 result[k] = v 110 case config.VariableTypeMap: 111 varSetMap(result, k, v) 112 case config.VariableTypeString: 113 // Convert to a string and set. We don't catch any errors 114 // here because the validation step later should catch 115 // any type errors. 116 var strVal string 117 if err := hilmapstructure.WeakDecode(v, &strVal); err == nil { 118 result[k] = strVal 119 } else { 120 result[k] = v 121 } 122 default: 123 panic(fmt.Sprintf( 124 "Unhandled var type: %T\n\n"+ 125 "THIS IS A BUG. Please report it.", 126 schema.Type())) 127 } 128 } 129 } 130 131 return result, nil 132 } 133 134 // varSetMap sets or merges the map in "v" with the key "k" in the 135 // "current" set of variables. This is just a private function to remove 136 // duplicate logic in Variables 137 func varSetMap(current map[string]interface{}, k string, v interface{}) { 138 existing, ok := current[k] 139 if !ok { 140 current[k] = v 141 return 142 } 143 144 existingMap, ok := existing.(map[string]interface{}) 145 if !ok { 146 panic(fmt.Sprintf("%s is not a map, this is a bug in Terraform.", k)) 147 } 148 149 switch typedV := v.(type) { 150 case []map[string]interface{}: 151 for newKey, newVal := range typedV[0] { 152 existingMap[newKey] = newVal 153 } 154 case map[string]interface{}: 155 for newKey, newVal := range typedV { 156 existingMap[newKey] = newVal 157 } 158 default: 159 panic(fmt.Sprintf("%s is not a map, this is a bug in Terraform.", k)) 160 } 161 }