terraform: allow TF_VAR_name to be set to set variables

This commit is contained in:
Mitchell Hashimoto 2015-04-22 06:31:53 +02:00
parent 0f68f3b3d8
commit 5ae9ee4d27
4 changed files with 73 additions and 0 deletions

View File

@ -5988,6 +5988,46 @@ func TestContext2Apply_vars(t *testing.T) {
} }
} }
func TestContext2Apply_varsEnv(t *testing.T) {
m := testModule(t, "apply-vars-env")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
// 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)
}
if len(e) > 0 {
t.Fatalf("bad: %s", e)
}
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyVarsEnvStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}
func TestContext2Apply_createBefore_depends(t *testing.T) { func TestContext2Apply_createBefore_depends(t *testing.T) {
m := testModule(t, "apply-depends-create-before") m := testModule(t, "apply-depends-create-before")
h := new(HookRecordApplyOrder) h := new(HookRecordApplyOrder)

View File

@ -11,6 +11,12 @@ import (
"github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/config/module"
) )
const (
// VarEnvPrefix is the prefix of variables that are read from
// the environment to set variables here.
VarEnvPrefix = "TF_VAR_"
)
// Interpolater is the structure responsible for determining the values // Interpolater is the structure responsible for determining the values
// for interpolations such as `aws_instance.foo.bar`. // for interpolations such as `aws_instance.foo.bar`.
type Interpolater struct { type Interpolater struct {
@ -43,6 +49,11 @@ 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,

View File

@ -47,6 +47,14 @@ func tempDir(t *testing.T) string {
return dir return dir
} }
// tempEnv lets you temporarily set an environment variable. It returns
// the old value that should be set via a defer.
func tempEnv(t *testing.T, k string, v string) string {
old := os.Getenv(k)
os.Setenv(k, v)
return old
}
func testConfig(t *testing.T, name string) *config.Config { func testConfig(t *testing.T, name string) *config.Config {
c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf")) c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf"))
if err != nil { if err != nil {
@ -621,6 +629,13 @@ aws_instance.foo:
type = aws_instance type = aws_instance
` `
const testTerraformApplyVarsEnvStr = `
aws_instance.bar:
ID = foo
foo = baz
type = aws_instance
`
const testTerraformPlanStr = ` const testTerraformPlanStr = `
DIFF: DIFF:

View File

@ -0,0 +1,7 @@
variable "ami" {
default = "foo"
}
resource "aws_instance" "bar" {
foo = "${var.ami}"
}