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 ( import (
"fmt" "fmt"
"log" "log"
"os"
"sort" "sort"
"strings"
"sync" "sync"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
@ -98,6 +100,26 @@ func NewContext(opts *ContextOpts) *Context {
par = 10 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{ return &Context{
destroy: opts.Destroy, destroy: opts.Destroy,
diff: opts.Diff, diff: opts.Diff,
@ -108,7 +130,7 @@ func NewContext(opts *ContextOpts) *Context {
state: state, state: state,
targets: opts.Targets, targets: opts.Targets,
uiInput: opts.UIInput, uiInput: opts.UIInput,
variables: opts.Variables, variables: variables,
parallelSem: NewSemaphore(par), parallelSem: NewSemaphore(par),
providerInputConfig: make(map[string]map[string]interface{}), 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) { 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") m := testModule(t, "apply-vars-env")
p := testProvider("aws") p := testProvider("aws")
p.ApplyFn = testApplyFn 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() w, e := ctx.Validate()
if len(w) > 0 { if len(w) > 0 {
t.Fatalf("bad: %#v", w) t.Fatalf("bad: %#v", w)

View File

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