Read more default envvars for GCP

- Closes #5874
- Fixes #5872
This commit is contained in:
Seth Vargo 2016-04-10 19:31:40 -04:00
parent e9a918f83d
commit 337895b51e
3 changed files with 64 additions and 21 deletions

View File

@ -27,20 +27,29 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.MultiEnvDefaultFunc([]string{ DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_CREDENTIALS", "GOOGLE_CREDENTIALS",
"GOOGLE_CLOUD_KEYFILE_JSON", "GOOGLE_CLOUD_KEYFILE_JSON",
"GCLOUD_KEYFILE_JSON",
}, nil), }, nil),
ValidateFunc: validateCredentials, ValidateFunc: validateCredentials,
}, },
"project": &schema.Schema{ "project": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", ""), DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_PROJECT",
"GCLOUD_PROJECT",
"CLOUDSDK_CORE_PROJECT",
}, nil),
}, },
"region": &schema.Schema{ "region": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_REGION", nil), DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_REGION",
"GCLOUD_REGION",
"CLOUDSDK_COMPUTE_REGION",
}, nil),
}, },
}, },

View File

@ -3,6 +3,7 @@ package google
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"testing" "testing"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
@ -38,18 +39,40 @@ func testAccPreCheck(t *testing.T) {
os.Setenv("GOOGLE_CREDENTIALS", string(creds)) os.Setenv("GOOGLE_CREDENTIALS", string(creds))
} }
if v := os.Getenv("GOOGLE_CREDENTIALS"); v == "" { multiEnvSearch := func(ks []string) string {
if w := os.Getenv("GOOGLE_CLOUD_KEYFILE_JSON"); w == "" { for _, k := range ks {
t.Fatal("GOOGLE_CREDENTIALS or GOOGLE_CLOUD_KEYFILE_JSON must be set for acceptance tests") if v := os.Getenv(k); v != "" {
return v
}
} }
return ""
} }
if v := os.Getenv("GOOGLE_PROJECT"); v == "" { creds := []string{
t.Fatal("GOOGLE_PROJECT must be set for acceptance tests") "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" { projs := []string{
t.Fatal("GOOGLE_REGION must be set to us-central1 for acceptance tests") "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, ", "))
} }
} }

View File

@ -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 retrieving this file are below. Credentials may be blank if you are running
Terraform from a GCE instance with a properly-configured [Compute Engine Terraform from a GCE instance with a properly-configured [Compute Engine
Service Account](https://cloud.google.com/compute/docs/authentication). This Service Account](https://cloud.google.com/compute/docs/authentication). This
can also be specified with the `GOOGLE_CREDENTIALS` or `GOOGLE_CLOUD_KEYFILE_JSON` can also be specified using any of the following environment variables
shell environment variable, containing the contents of the credentials file. (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 * `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 * `GOOGLE_REGION`
can also be specified with the `GOOGLE_PROJECT` shell environment variable. * `GCLOUD_REGION`
If unspecified, users will need to specify the `project` attribute for * `CLOUDSDK_COMPUTE_REGION`
all resources. If specified, resources which do not depend on a project will
ignore this value.
The following keys are supported for backwards compatibility, and may be The following keys are supported for backwards compatibility, and may be
removed in a future version: removed in a future version: