Read the "standard" AWS environment variables

This is 100% backwards-compatible
This commit is contained in:
Seth Vargo 2015-01-22 15:30:27 -05:00
parent 4980838802
commit 072a1cf353
4 changed files with 76 additions and 15 deletions

View File

@ -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",
},

View File

@ -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")
}
}

View File

@ -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

View File

@ -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