From 0e2c4da3e27d5f3a54bf1cc93b416da4e05b681b Mon Sep 17 00:00:00 2001 From: Paddy Date: Mon, 7 Nov 2016 16:00:42 -0800 Subject: [PATCH] provider/google: throw an error for invalid disks When configuring an instance's attached disk, if the attached disk has both the disk and type attributes set, it would previously cause terraform to crash with a nil pointer exception. The root cause was that we only instantiate the InitializeParams property of the disk if its disk attribute isn't set, and we try to write to the InitializeParams property when the type attribute is set. So setting both caused the InitializeParams property to not be initialized, then written to. Now we throw an error explaining that the configuration can't have both the disk and the type set. Fixes #6495. --- resource_compute_instance.go | 7 +++++ resource_compute_instance_test.go | 49 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/resource_compute_instance.go b/resource_compute_instance.go index dd413440..3b54708f 100644 --- a/resource_compute_instance.go +++ b/resource_compute_instance.go @@ -361,6 +361,13 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err disk.Boot = i == 0 disk.AutoDelete = d.Get(prefix + ".auto_delete").(bool) + if _, ok := d.GetOk(prefix + ".disk"); ok { + if _, ok := d.GetOk(prefix + ".type"); ok { + return fmt.Errorf( + "Error: cannot define both disk and type.") + } + } + // Load up the disk for this disk if specified if v, ok := d.GetOk(prefix + ".disk"); ok { diskName := v.(string) diff --git a/resource_compute_instance_test.go b/resource_compute_instance_test.go index 7ea120e2..33b82aa7 100644 --- a/resource_compute_instance_test.go +++ b/resource_compute_instance_test.go @@ -2,6 +2,7 @@ package google import ( "fmt" + "regexp" "strings" "testing" @@ -482,6 +483,23 @@ func TestAccComputeInstance_private_image_family(t *testing.T) { }) } +func TestAccComputeInstance_invalid_disk(t *testing.T) { + var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) + var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeInstance_invalid_disk(diskName, instanceName), + ExpectError: regexp.MustCompile("Error: cannot define both disk and type."), + }, + }, + }) +} + func testAccCheckComputeInstanceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -1152,3 +1170,34 @@ func testAccComputeInstance_private_image_family(disk, image, family, instance s } }`, disk, image, family, instance) } + +func testAccComputeInstance_invalid_disk(disk, instance string) string { + return fmt.Sprintf(` + resource "google_compute_instance" "foobar" { + name = "%s" + machine_type = "f1-micro" + zone = "us-central1-a" + + disk { + image = "ubuntu-os-cloud/ubuntu-1604-lts" + type = "pd-standard" + } + + disk { + disk = "${google_compute_disk.foobar.name}" + type = "pd-standard" + device_name = "xvdb" + } + + network_interface { + network = "default" + } + } + + resource "google_compute_disk" "foobar" { + name = "%s" + zone = "us-central1-a" + type = "pd-standard" + size = "1" + }`, instance, disk) +}