diff --git a/builtin/providers/google/config.go b/builtin/providers/google/config.go index 91f8992a0..54c115b4b 100644 --- a/builtin/providers/google/config.go +++ b/builtin/providers/google/config.go @@ -29,20 +29,6 @@ func (c *Config) loadAndValidate() error { var account accountFile var secrets clientSecretsFile - // TODO: validation that it isn't blank - if c.AccountFile == "" { - c.AccountFile = os.Getenv("GOOGLE_ACCOUNT_FILE") - } - if c.ClientSecretsFile == "" { - c.ClientSecretsFile = os.Getenv("GOOGLE_CLIENT_FILE") - } - if c.Project == "" { - c.Project = os.Getenv("GOOGLE_PROJECT") - } - if c.Region == "" { - c.Region = os.Getenv("GOOGLE_REGION") - } - if err := loadJSON(&account, c.AccountFile); err != nil { return fmt.Errorf( "Error loading account file '%s': %s", diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index 593b8559b..ea630bbfe 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -1,6 +1,8 @@ package google import ( + "os" + "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -10,23 +12,27 @@ func Provider() terraform.ResourceProvider { return &schema.Provider{ Schema: map[string]*schema.Schema{ "account_file": &schema.Schema{ - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + DefaultFunc: envDefaultFunc("GOOGLE_ACCOUNT_FILE"), }, "client_secrets_file": &schema.Schema{ - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + DefaultFunc: envDefaultFunc("GOOGLE_CLIENT_FILE"), }, "project": &schema.Schema{ - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + DefaultFunc: envDefaultFunc("GOOGLE_PROJECT"), }, "region": &schema.Schema{ - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + DefaultFunc: envDefaultFunc("GOOGLE_REGION"), }, }, @@ -43,6 +49,16 @@ func Provider() terraform.ResourceProvider { } } +func envDefaultFunc(k string) schema.SchemaDefaultFunc { + return func() (interface{}, error) { + if v := os.Getenv(k); v != "" { + return v, nil + } + + return nil, nil + } +} + func providerConfigure(d *schema.ResourceData) (interface{}, error) { config := Config{ AccountFile: d.Get("account_file").(string), diff --git a/builtin/providers/google/provider_test.go b/builtin/providers/google/provider_test.go index f4903bd27..d5a32be33 100644 --- a/builtin/providers/google/provider_test.go +++ b/builtin/providers/google/provider_test.go @@ -40,4 +40,8 @@ func testAccPreCheck(t *testing.T) { if v := os.Getenv("GOOGLE_PROJECT"); v == "" { t.Fatal("GOOGLE_PROJECT must be set for acceptance tests") } + + if v := os.Getenv("GOOGLE_REGION"); v != "us-central1" { + t.Fatal("GOOGLE_REGION must be set to us-central1 for acceptance tests") + } }