From 072a1cf353d603680ac3d547acef802a62cfb760 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 22 Jan 2015 15:30:27 -0500 Subject: [PATCH] Read the "standard" AWS environment variables This is 100% backwards-compatible --- builtin/providers/aws/provider.go | 27 ++++++++++++------- builtin/providers/aws/provider_test.go | 12 ++++----- helper/schema/schema.go | 15 +++++++++++ helper/schema/schema_test.go | 37 ++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 54b542781..3c417d32d 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -13,23 +13,32 @@ func Provider() terraform.ResourceProvider { return &schema.Provider{ Schema: map[string]*schema.Schema{ "access_key": &schema.Schema{ - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.EnvDefaultFunc("AWS_ACCESS_KEY", nil), + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "AWS_ACCESS_KEY", + "AWS_ACCESS_KEY_ID", + }, nil), Description: descriptions["access_key"], }, "secret_key": &schema.Schema{ - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.EnvDefaultFunc("AWS_SECRET_KEY", nil), + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "AWS_SECRET_KEY", + "AWS_SECRET_ACCESS_KEY", + }, nil), Description: descriptions["secret_key"], }, "region": &schema.Schema{ - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.EnvDefaultFunc("AWS_REGION", nil), + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "AWS_REGION", + "AWS_DEFAULT_REGION", + }, nil), Description: descriptions["region"], InputDefault: "us-east-1", }, diff --git a/builtin/providers/aws/provider_test.go b/builtin/providers/aws/provider_test.go index ea214f7ee..77cd93107 100644 --- a/builtin/providers/aws/provider_test.go +++ b/builtin/providers/aws/provider_test.go @@ -30,14 +30,14 @@ func TestProvider_impl(t *testing.T) { } func testAccPreCheck(t *testing.T) { - if v := os.Getenv("AWS_ACCESS_KEY"); v == "" { - t.Fatal("AWS_ACCESS_KEY must be set for acceptance tests") + if v := os.Getenv("AWS_ACCESS_KEY_ID"); v == "" { + t.Fatal("AWS_ACCESS_KEY_ID must be set for acceptance tests") } - if v := os.Getenv("AWS_SECRET_KEY"); v == "" { - t.Fatal("AWS_SECRET_KEY must be set for acceptance tests") + if v := os.Getenv("AWS_SECRET_ACCESS_KEY"); v == "" { + t.Fatal("AWS_SECRET_ACCESS_KEY must be set for acceptance tests") } - if v := os.Getenv("AWS_REGION"); v == "" { + if v := os.Getenv("AWS_DEFAULT_REGION"); v == "" { log.Println("[INFO] Test: Using us-west-2 as test region") - os.Setenv("AWS_REGION", "us-west-2") + os.Setenv("AWS_DEFAULT_REGION", "us-west-2") } } diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 2bb23168c..b41e826a2 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -174,6 +174,21 @@ func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc { } } +// MultiEnvDefaultFunc is a helper function that returns the value of the first +// environment variable in the given list that returns a non-empty value. If +// none of the environment variables return a value, the default value is +// returned. +func MultiEnvDefaultFunc(ks []string, dv interface{}) SchemaDefaultFunc { + return func() (interface{}, error) { + for _, k := range ks { + if v := os.Getenv(k); v != "" { + return v, nil + } + } + return dv, nil + } +} + // SchemaSetFunc is a function that must return a unique ID for the given // element. This unique ID is used to store the element in a hash. type SchemaSetFunc func(interface{}) int diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 6d20256d1..e61aa1152 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -40,6 +40,43 @@ func TestEnvDefaultFunc(t *testing.T) { } } +func TestMultiEnvDefaultFunc(t *testing.T) { + keys := []string{ + "TF_TEST_MULTI_ENV_DEFAULT_FUNC1", + "TF_TEST_MULTI_ENV_DEFAULT_FUNC2", + } + defer func() { + for _, k := range keys { + os.Unsetenv(k) + } + }() + + f := MultiEnvDefaultFunc(keys, "42") + if err := os.Setenv(keys[1], "foo"); err != nil { + t.Fatalf("err: %s", err) + } + + actual, err := f() + if err != nil { + t.Fatalf("err: %s", err) + } + if actual != "foo" { + t.Fatalf("bad: %#v", actual) + } + + if err := os.Unsetenv(keys[1]); err != nil { + t.Fatalf("err: %s", err) + } + + actual, err = f() + if err != nil { + t.Fatalf("err: %s", err) + } + if actual != "42" { + t.Fatalf("bad: %#v", actual) + } +} + func TestValueType_Zero(t *testing.T) { cases := []struct { Type ValueType