Allow resizing of Google Cloud persistent disks (#15077)

This commit is contained in:
Sam Bashton 2017-06-05 18:19:57 +01:00 committed by Paul Stack
parent 06562df1e6
commit b0f815cc58
2 changed files with 62 additions and 1 deletions

View File

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

View File

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