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.
This commit is contained in:
Sander van Harmelen 2014-11-20 11:25:23 +01:00
parent e53bf23c0c
commit 144ceb8003
3 changed files with 28 additions and 22 deletions

View File

@ -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",

View File

@ -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),

View File

@ -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")
}
}