terraform: validation should succeed if env var set [GH-1930]

This commit is contained in:
Mitchell Hashimoto 2015-05-13 20:09:05 -07:00
parent 442376c8e4
commit fe74d69852
3 changed files with 27 additions and 10 deletions

View File

@ -3,7 +3,9 @@ package terraform
import (
"fmt"
"log"
"os"
"sort"
"strings"
"sync"
"github.com/hashicorp/go-multierror"
@ -98,6 +100,26 @@ func NewContext(opts *ContextOpts) *Context {
par = 10
}
// Setup the variables. We first take the variables given to us.
// We then merge in the variables set in the environment.
variables := make(map[string]string)
for k, v := range opts.Variables {
variables[k] = v
}
for _, v := range os.Environ() {
if !strings.HasPrefix(v, VarEnvPrefix) {
continue
}
// Strip off the prefix and get the value after the first "="
idx := strings.Index(v, "=")
k := v[len(VarEnvPrefix):idx]
v = v[idx+1:]
// Override the command-line set variable
variables[k] = v
}
return &Context{
destroy: opts.Destroy,
diff: opts.Diff,
@ -108,7 +130,7 @@ func NewContext(opts *ContextOpts) *Context {
state: state,
targets: opts.Targets,
uiInput: opts.UIInput,
variables: opts.Variables,
variables: variables,
parallelSem: NewSemaphore(par),
providerInputConfig: make(map[string]map[string]interface{}),

View File

@ -6417,6 +6417,10 @@ func TestContext2Apply_vars(t *testing.T) {
}
func TestContext2Apply_varsEnv(t *testing.T) {
// Set the env var
old := tempEnv(t, "TF_VAR_ami", "baz")
defer os.Setenv("TF_VAR_ami", old)
m := testModule(t, "apply-vars-env")
p := testProvider("aws")
p.ApplyFn = testApplyFn
@ -6428,10 +6432,6 @@ func TestContext2Apply_varsEnv(t *testing.T) {
},
})
// Set the env var
old := tempEnv(t, "TF_VAR_ami", "baz")
defer os.Setenv("TF_VAR_ami", old)
w, e := ctx.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)

View File

@ -49,11 +49,6 @@ func (i *Interpolater) Values(
}
for _, v := range mod.Config().Variables {
for k, val := range v.DefaultsMap() {
envKey := VarEnvPrefix + strings.TrimPrefix(k, "var.")
if v := os.Getenv(envKey); v != "" {
val = v
}
result[k] = ast.Variable{
Value: val,
Type: ast.TypeString,