terraform: allow TF_VAR_name to be set to set variables
This commit is contained in:
parent
0f68f3b3d8
commit
5ae9ee4d27
|
@ -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) {
|
||||
m := testModule(t, "apply-depends-create-before")
|
||||
h := new(HookRecordApplyOrder)
|
||||
|
|
|
@ -11,6 +11,12 @@ import (
|
|||
"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
|
||||
// for interpolations such as `aws_instance.foo.bar`.
|
||||
type Interpolater struct {
|
||||
|
@ -43,6 +49,11 @@ 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,
|
||||
|
|
|
@ -47,6 +47,14 @@ func tempDir(t *testing.T) string {
|
|||
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 {
|
||||
c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf"))
|
||||
if err != nil {
|
||||
|
@ -621,6 +629,13 @@ aws_instance.foo:
|
|||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyVarsEnvStr = `
|
||||
aws_instance.bar:
|
||||
ID = foo
|
||||
foo = baz
|
||||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformPlanStr = `
|
||||
DIFF:
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
variable "ami" {
|
||||
default = "foo"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "${var.ami}"
|
||||
}
|
Loading…
Reference in New Issue