diff --git a/builtin/providers/google/config.go b/builtin/providers/google/config.go index 91f8992a07..54c115b4b5 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 593b8559b5..ea630bbfef 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 f4903bd272..d5a32be33e 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") + } } diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index f6b0fde7a1..92065fb687 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -514,10 +514,10 @@ func resourceInstanceTags(d *schema.ResourceData) *compute.Tags { // Calculate the tags var tags *compute.Tags if v := d.Get("tags"); v != nil { - vs := v.(*schema.Set).List() + vs := v.(*schema.Set) tags = new(compute.Tags) - tags.Items = make([]string, len(vs)) - for i, v := range v.(*schema.Set).List() { + tags.Items = make([]string, vs.Len()) + for i, v := range vs.List() { tags.Items[i] = v.(string) } diff --git a/builtin/providers/google/resource_compute_instance_test.go b/builtin/providers/google/resource_compute_instance_test.go index 78c01e04e0..f765a44c44 100644 --- a/builtin/providers/google/resource_compute_instance_test.go +++ b/builtin/providers/google/resource_compute_instance_test.go @@ -52,9 +52,6 @@ func TestAccComputeInstance_IP(t *testing.T) { }) } -//!NB requires that disk with name terraform-test-disk is present in gce, -//if created as dependency then it tries to remove it while it is still attached -//to instance and that fails with an error func TestAccComputeInstance_disks(t *testing.T) { var instance compute.Instance @@ -66,6 +63,8 @@ func TestAccComputeInstance_disks(t *testing.T) { resource.TestStep{ Config: testAccComputeInstance_disks, Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + "google_compute_instance.foobar", &instance), testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true), testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false), ), @@ -287,6 +286,13 @@ resource "google_compute_instance" "foobar" { }` const testAccComputeInstance_disks = ` +resource "google_compute_disk" "foobar" { + name = "terraform-test-disk" + size = 10 + type = "pd-ssd" + zone = "us-central1-a" +} + resource "google_compute_instance" "foobar" { name = "terraform-test" machine_type = "n1-standard-1" @@ -297,9 +303,8 @@ resource "google_compute_instance" "foobar" { } disk { - disk = "terraform-test-disk" + disk = "${google_compute_disk.foobar.name}" auto_delete = false - type = "pd-ssd" } network {