From 144ceb800362c7ff31b732970409fe8fbc3670cf Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Thu, 20 Nov 2014 11:25:23 +0100 Subject: [PATCH] providers/google: update schema to use a DefaultFunc This makes testing easier and gives you a way to configure the provider using env variables. It also makes the provider more inline following the TF 0.2 approach. --- builtin/providers/google/config.go | 14 ---------- builtin/providers/google/provider.go | 32 +++++++++++++++++------ builtin/providers/google/provider_test.go | 4 +++ 3 files changed, 28 insertions(+), 22 deletions(-) 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") + } }