From b0f815cc58b9dcd50da51a9f6b3da5b9d00f30e2 Mon Sep 17 00:00:00 2001 From: Sam Bashton Date: Mon, 5 Jun 2017 18:19:57 +0100 Subject: [PATCH] Allow resizing of Google Cloud persistent disks (#15077) --- .../providers/google/resource_compute_disk.go | 24 +++++++++++- .../google/resource_compute_disk_test.go | 39 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/builtin/providers/google/resource_compute_disk.go b/builtin/providers/google/resource_compute_disk.go index bb83a3dc8..294f65479 100644 --- a/builtin/providers/google/resource_compute_disk.go +++ b/builtin/providers/google/resource_compute_disk.go @@ -23,6 +23,7 @@ func resourceComputeDisk() *schema.Resource { return &schema.Resource{ Create: resourceComputeDiskCreate, Read: resourceComputeDiskRead, + Update: resourceComputeDiskUpdate, Delete: resourceComputeDiskDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, @@ -68,7 +69,6 @@ func resourceComputeDisk() *schema.Resource { "size": &schema.Schema{ Type: schema.TypeInt, Optional: true, - ForceNew: true, }, "self_link": &schema.Schema{ @@ -185,6 +185,28 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { return resourceComputeDiskRead(d, meta) } +func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + + if d.HasChange("size") { + rb := &compute.DisksResizeRequest{ + SizeGb: int64(d.Get("size").(int)), + } + _, err := config.clientCompute.Disks.Resize( + project, d.Get("zone").(string), d.Id(), rb).Do() + if err != nil { + return fmt.Errorf("Error resizing disk: %s", err) + } + } + + return nil +} + func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) diff --git a/builtin/providers/google/resource_compute_disk_test.go b/builtin/providers/google/resource_compute_disk_test.go index adbd10e12..28288a811 100644 --- a/builtin/providers/google/resource_compute_disk_test.go +++ b/builtin/providers/google/resource_compute_disk_test.go @@ -32,6 +32,34 @@ func TestAccComputeDisk_basic(t *testing.T) { }) } +func TestAccComputeDisk_updateSize(t *testing.T) { + diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + var disk compute.Disk + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeDisk_basic(diskName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeDiskExists( + "google_compute_disk.foobar", &disk), + resource.TestCheckResourceAttr("google_compute_disk.foobar", "size", "50"), + ), + }, + { + Config: testAccComputeDisk_resized(diskName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeDiskExists( + "google_compute_disk.foobar", &disk), + resource.TestCheckResourceAttr("google_compute_disk.foobar", "size", "100"), + ), + }, + }, + }) +} + func TestAccComputeDisk_fromSnapshotURI(t *testing.T) { diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) firstDiskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) @@ -212,6 +240,17 @@ resource "google_compute_disk" "foobar" { }`, diskName) } +func testAccComputeDisk_resized(diskName string) string { + return fmt.Sprintf(` +resource "google_compute_disk" "foobar" { + name = "%s" + image = "debian-8-jessie-v20160803" + size = 100 + type = "pd-ssd" + zone = "us-central1-a" +}`, diskName) +} + func testAccComputeDisk_fromSnapshotURI(firstDiskName, snapshotName, diskName, xpn_host string) string { return fmt.Sprintf(` resource "google_compute_disk" "foobar" {