Skip to content

Commit

Permalink
Read more default envvars for GCP
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored and chrislovecnm committed Apr 16, 2016
1 parent de146d1 commit be7a9c9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
21 changes: 15 additions & 6 deletions builtin/providers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},

Expand Down
37 changes: 30 additions & 7 deletions builtin/providers/google/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -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, ", "))
}
}

Expand Down
27 changes: 19 additions & 8 deletions website/source/docs/providers/google/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit be7a9c9

Please sign in to comment.