From 337895b51e2d11a04324adb708e02fe0bd394c03 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 10 Apr 2016 19:31:40 -0400 Subject: [PATCH] Read more default envvars for GCP - Closes #5874 - Fixes #5872 --- builtin/providers/google/provider.go | 21 ++++++++--- builtin/providers/google/provider_test.go | 37 +++++++++++++++---- .../docs/providers/google/index.html.markdown | 27 ++++++++++---- 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index 8fd5339f5..89e176979 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -27,20 +27,29 @@ func Provider() terraform.ResourceProvider { DefaultFunc: schema.MultiEnvDefaultFunc([]string{ "GOOGLE_CREDENTIALS", "GOOGLE_CLOUD_KEYFILE_JSON", + "GCLOUD_KEYFILE_JSON", }, nil), ValidateFunc: validateCredentials, }, "project": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", ""), + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "GOOGLE_PROJECT", + "GCLOUD_PROJECT", + "CLOUDSDK_CORE_PROJECT", + }, nil), }, "region": &schema.Schema{ - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.EnvDefaultFunc("GOOGLE_REGION", nil), + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "GOOGLE_REGION", + "GCLOUD_REGION", + "CLOUDSDK_COMPUTE_REGION", + }, nil), }, }, diff --git a/builtin/providers/google/provider_test.go b/builtin/providers/google/provider_test.go index 9bf5414b7..40bf1654e 100644 --- a/builtin/providers/google/provider_test.go +++ b/builtin/providers/google/provider_test.go @@ -3,6 +3,7 @@ package google import ( "io/ioutil" "os" + "strings" "testing" "github.com/hashicorp/terraform/helper/schema" @@ -38,18 +39,40 @@ func testAccPreCheck(t *testing.T) { os.Setenv("GOOGLE_CREDENTIALS", string(creds)) } - if v := os.Getenv("GOOGLE_CREDENTIALS"); v == "" { - if w := os.Getenv("GOOGLE_CLOUD_KEYFILE_JSON"); w == "" { - t.Fatal("GOOGLE_CREDENTIALS or GOOGLE_CLOUD_KEYFILE_JSON must be set for acceptance tests") + multiEnvSearch := func(ks []string) string { + for _, k := range ks { + if v := os.Getenv(k); v != "" { + return v + } } + return "" } - if v := os.Getenv("GOOGLE_PROJECT"); v == "" { - t.Fatal("GOOGLE_PROJECT must be set for acceptance tests") + creds := []string{ + "GOOGLE_CREDENTIALS", + "GOOGLE_CLOUD_KEYFILE_JSON", + "GCLOUD_KEYFILE_JSON", + } + if v := multiEnvSearch(creds); v == "" { + t.Fatalf("One of %s must be set for acceptance tests", strings.Join(creds, ", ")) } - if v := os.Getenv("GOOGLE_REGION"); v != "us-central1" { - t.Fatal("GOOGLE_REGION must be set to us-central1 for acceptance tests") + projs := []string{ + "GOOGLE_PROJECT", + "GCLOUD_PROJECT", + "CLOUDSDK_CORE_PROJECT", + } + if v := multiEnvSearch(projs); v == "" { + t.Fatalf("One of %s must be set for acceptance tests", strings.Join(creds, ", ")) + } + + regs := []string{ + "GOOGLE_REGION", + "GCLOUD_REGION", + "CLOUDSDK_COMPUTE_REGION", + } + if v := multiEnvSearch(regs); v != "us-central-1" { + t.Fatalf("One of %s must be set to us-central-1 for acceptance tests", strings.Join(creds, ", ")) } } diff --git a/website/source/docs/providers/google/index.html.markdown b/website/source/docs/providers/google/index.html.markdown index 641e2b419..936cc2612 100644 --- a/website/source/docs/providers/google/index.html.markdown +++ b/website/source/docs/providers/google/index.html.markdown @@ -39,17 +39,28 @@ The following keys can be used to configure the provider. retrieving this file are below. Credentials may be blank if you are running Terraform from a GCE instance with a properly-configured [Compute Engine Service Account](https://cloud.google.com/compute/docs/authentication). This - can also be specified with the `GOOGLE_CREDENTIALS` or `GOOGLE_CLOUD_KEYFILE_JSON` - shell environment variable, containing the contents of the credentials file. + can also be specified using any of the following environment variables + (listed in order of precedence): + + * `GOOGLE_CREDENTIALS` + * `GOOGLE_CLOUD_KEYFILE_JSON` + * `GCLOUD_KEYFILE_JSON` + +* `project` - (Required) The ID of the project to apply any resources to. This + can be specified using any of the following environment variables (listed in + order of precedence): + + * `GOOGLE_PROJECT` + * `GCLOUD_PROJECT` + * `CLOUDSDK_CORE_PROJECT` * `region` - (Required) The region to operate under. This can also be specified - with the `GOOGLE_REGION` shell environment variable. + using any of the following environment variables (listed in order of + precedence): -* `project` - (Optional) The ID of the project to apply resources in. This - can also be specified with the `GOOGLE_PROJECT` shell environment variable. - If unspecified, users will need to specify the `project` attribute for - all resources. If specified, resources which do not depend on a project will - ignore this value. + * `GOOGLE_REGION` + * `GCLOUD_REGION` + * `CLOUDSDK_COMPUTE_REGION` The following keys are supported for backwards compatibility, and may be removed in a future version: