Merge pull request #851 from hashicorp/sethvargo/aws_envvars

Use standard AWS environment variables
This commit is contained in:
Seth Vargo 2015-01-25 13:29:32 -05:00
commit 918ba4c3be
6 changed files with 129 additions and 45 deletions

View File

@ -13,23 +13,32 @@ func Provider() terraform.ResourceProvider {
return &schema.Provider{ return &schema.Provider{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"access_key": &schema.Schema{ "access_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.EnvDefaultFunc("AWS_ACCESS_KEY", nil), DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_ACCESS_KEY",
"AWS_ACCESS_KEY_ID",
}, nil),
Description: descriptions["access_key"], Description: descriptions["access_key"],
}, },
"secret_key": &schema.Schema{ "secret_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.EnvDefaultFunc("AWS_SECRET_KEY", nil), DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_SECRET_KEY",
"AWS_SECRET_ACCESS_KEY",
}, nil),
Description: descriptions["secret_key"], Description: descriptions["secret_key"],
}, },
"region": &schema.Schema{ "region": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.EnvDefaultFunc("AWS_REGION", nil), DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_REGION",
"AWS_DEFAULT_REGION",
}, nil),
Description: descriptions["region"], Description: descriptions["region"],
InputDefault: "us-east-1", InputDefault: "us-east-1",
}, },

View File

@ -30,14 +30,14 @@ func TestProvider_impl(t *testing.T) {
} }
func testAccPreCheck(t *testing.T) { func testAccPreCheck(t *testing.T) {
if v := os.Getenv("AWS_ACCESS_KEY"); v == "" { if v := os.Getenv("AWS_ACCESS_KEY_ID"); v == "" {
t.Fatal("AWS_ACCESS_KEY must be set for acceptance tests") t.Fatal("AWS_ACCESS_KEY_ID must be set for acceptance tests")
} }
if v := os.Getenv("AWS_SECRET_KEY"); v == "" { if v := os.Getenv("AWS_SECRET_ACCESS_KEY"); v == "" {
t.Fatal("AWS_SECRET_KEY must be set for acceptance tests") 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") 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 // 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. // element. This unique ID is used to store the element in a hash.
type SchemaSetFunc func(interface{}) int type SchemaSetFunc func(interface{}) int

View File

@ -40,6 +40,63 @@ 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)
}
}()
// Test that the first key is returned first
f := MultiEnvDefaultFunc(keys, "42")
if err := os.Setenv(keys[0], "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[0]); err != nil {
t.Fatalf("err: %s", err)
}
// Test that the second key is returned if the first one is empty
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)
}
// Test that the default value is returned when no keys are set
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) { func TestValueType_Zero(t *testing.T) {
cases := []struct { cases := []struct {
Type ValueType Type ValueType

View File

@ -1,6 +1,6 @@
GIT GIT
remote: git://github.com/hashicorp/middleman-hashicorp.git remote: git://github.com/hashicorp/middleman-hashicorp.git
revision: b82c2c2fdc244cd0bd529ff27cfab24e43f07708 revision: 30c15f93fb501041cff97c490b60ddc96c8314c9
specs: specs:
middleman-hashicorp (0.1.0) middleman-hashicorp (0.1.0)
bootstrap-sass (~> 3.2) bootstrap-sass (~> 3.2)
@ -20,14 +20,18 @@ GIT
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
activesupport (4.1.8) activesupport (4.1.9)
i18n (~> 0.6, >= 0.6.9) i18n (~> 0.6, >= 0.6.9)
json (~> 1.7, >= 1.7.7) json (~> 1.7, >= 1.7.7)
minitest (~> 5.1) minitest (~> 5.1)
thread_safe (~> 0.1) thread_safe (~> 0.1)
tzinfo (~> 1.1) tzinfo (~> 1.1)
bootstrap-sass (3.3.1.0) autoprefixer-rails (5.0.0.2)
sass (~> 3.2) execjs
json
bootstrap-sass (3.3.3)
autoprefixer-rails (>= 5.0.0.1)
sass (>= 3.2.19)
builder (3.2.2) builder (3.2.2)
celluloid (0.16.0) celluloid (0.16.0)
timers (~> 4.0.0) timers (~> 4.0.0)
@ -37,14 +41,14 @@ GEM
execjs execjs
coffee-script-source (1.8.0) coffee-script-source (1.8.0)
commonjs (0.2.7) commonjs (0.2.7)
compass (1.0.1) compass (1.0.3)
chunky_png (~> 1.2) chunky_png (~> 1.2)
compass-core (~> 1.0.1) compass-core (~> 1.0.2)
compass-import-once (~> 1.0.5) compass-import-once (~> 1.0.5)
rb-fsevent (>= 0.9.3) rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9) rb-inotify (>= 0.9)
sass (>= 3.3.13, < 3.5) sass (>= 3.3.13, < 3.5)
compass-core (1.0.1) compass-core (1.0.3)
multi_json (~> 1.0) multi_json (~> 1.0)
sass (>= 3.3.0, < 3.5) sass (>= 3.3.0, < 3.5)
compass-import-once (1.0.5) compass-import-once (1.0.5)
@ -54,10 +58,10 @@ GEM
eventmachine (>= 0.12.9) eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0) http_parser.rb (~> 0.6.0)
erubis (2.7.0) erubis (2.7.0)
eventmachine (1.0.3) eventmachine (1.0.4)
execjs (2.2.2) execjs (2.2.2)
ffi (1.9.6) ffi (1.9.6)
haml (4.0.5) haml (4.0.6)
tilt tilt
hike (1.2.3) hike (1.2.3)
hitimes (1.2.2) hitimes (1.2.2)
@ -66,12 +70,12 @@ GEM
htmlcompressor (0.1.2) htmlcompressor (0.1.2)
http_parser.rb (0.6.0) http_parser.rb (0.6.0)
i18n (0.6.11) i18n (0.6.11)
json (1.8.1) json (1.8.2)
kramdown (1.5.0) kramdown (1.5.0)
less (2.6.0) less (2.6.0)
commonjs (~> 0.2.7) commonjs (~> 0.2.7)
libv8 (3.16.14.7) libv8 (3.16.14.7)
listen (2.8.0) listen (2.8.5)
celluloid (>= 0.15.2) celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3) rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9) rb-inotify (>= 0.9)
@ -98,22 +102,22 @@ GEM
rack-test (~> 0.6.2) rack-test (~> 0.6.2)
thor (>= 0.15.2, < 2.0) thor (>= 0.15.2, < 2.0)
tilt (~> 1.4.1, < 2.0) tilt (~> 1.4.1, < 2.0)
middleman-livereload (3.3.4) middleman-livereload (3.4.2)
em-websocket (~> 0.5.1) em-websocket (~> 0.5.1)
middleman-core (~> 3.2) middleman-core (>= 3.3)
rack-livereload (~> 0.3.15) rack-livereload (~> 0.3.15)
middleman-minify-html (3.4.0) middleman-minify-html (3.4.0)
htmlcompressor (~> 0.1.0) htmlcompressor (~> 0.1.0)
middleman-core (>= 3.2) middleman-core (>= 3.2)
middleman-sprockets (3.3.10) middleman-sprockets (3.4.1)
middleman-core (~> 3.3) middleman-core (>= 3.3)
sprockets (~> 2.12.1) sprockets (~> 2.12.1)
sprockets-helpers (~> 1.1.0) sprockets-helpers (~> 1.1.0)
sprockets-sass (~> 1.2.0) sprockets-sass (~> 1.3.0)
middleman-syntax (2.0.0) middleman-syntax (2.0.0)
middleman-core (~> 3.2) middleman-core (~> 3.2)
rouge (~> 1.0) rouge (~> 1.0)
minitest (5.4.3) minitest (5.5.1)
multi_json (1.10.1) multi_json (1.10.1)
padrino-helpers (0.12.4) padrino-helpers (0.12.4)
i18n (~> 0.6, >= 0.6.7) i18n (~> 0.6, >= 0.6.7)
@ -121,22 +125,22 @@ GEM
tilt (~> 1.4.1) tilt (~> 1.4.1)
padrino-support (0.12.4) padrino-support (0.12.4)
activesupport (>= 3.1) activesupport (>= 3.1)
rack (1.5.2) rack (1.6.0)
rack-contrib (1.2.0) rack-contrib (1.2.0)
rack (>= 0.9.1) rack (>= 0.9.1)
rack-livereload (0.3.15) rack-livereload (0.3.15)
rack rack
rack-rewrite (1.5.0) rack-rewrite (1.5.1)
rack-ssl-enforcer (0.2.8) rack-ssl-enforcer (0.2.8)
rack-test (0.6.2) rack-test (0.6.3)
rack (>= 1.0) rack (>= 1.0)
rb-fsevent (0.9.4) rb-fsevent (0.9.4)
rb-inotify (0.9.5) rb-inotify (0.9.5)
ffi (>= 0.5.0) ffi (>= 0.5.0)
redcarpet (3.2.0) redcarpet (3.2.2)
ref (1.0.5) ref (1.0.5)
rouge (1.7.3) rouge (1.7.7)
sass (3.4.8) sass (3.4.10)
sprockets (2.12.3) sprockets (2.12.3)
hike (~> 1.2) hike (~> 1.2)
multi_json (~> 1.0) multi_json (~> 1.0)
@ -144,7 +148,7 @@ GEM
tilt (~> 1.1, != 1.3.0) tilt (~> 1.1, != 1.3.0)
sprockets-helpers (1.1.0) sprockets-helpers (1.1.0)
sprockets (~> 2.0) sprockets (~> 2.0)
sprockets-sass (1.2.0) sprockets-sass (1.3.1)
sprockets (~> 2.0) sprockets (~> 2.0)
tilt (~> 1.1) tilt (~> 1.1)
therubyracer (0.12.1) therubyracer (0.12.1)
@ -161,8 +165,8 @@ GEM
hitimes hitimes
tzinfo (1.2.2) tzinfo (1.2.2)
thread_safe (~> 0.1) thread_safe (~> 0.1)
uber (0.0.11) uber (0.0.13)
uglifier (2.5.3) uglifier (2.7.0)
execjs (>= 0.3.0) execjs (>= 0.3.0)
json (>= 1.8.0) json (>= 1.8.0)

View File

@ -35,11 +35,10 @@ resource "aws_instance" "web" {
The following arguments are supported: The following arguments are supported:
* `access_key` - (Required) This is the AWS access key. It must be provided, but * `access_key` - (Required) This is the AWS access key. It must be provided, but
it can also be sourced from the `AWS_ACCESS_KEY` environment variable. it can also be sourced from the `AWS_ACCESS_KEY_ID` environment variable.
* `secret_key` - (Required) This is the AWS secret key. It must be provided, but * `secret_key` - (Required) This is the AWS secret key. It must be provided, but
it can also be sourced from the `AWS_SECRET_KEY` environment variable. it can also be sourced from the `AWS_SECRET_ACCESS_KEY` environment variable.
* `region` - (Required) This is the AWS region. It must be provided, but * `region` - (Required) This is the AWS region. It must be provided, but
it can also be sourced from the `AWS_REGION` environment variables. it can also be sourced from the `AWS_DEFAULT_REGION` environment variables.