Skip to content

Commit

Permalink
Add configurable timeouts for disks (#717)
Browse files Browse the repository at this point in the history
* add configurable timeouts for disks

* add test for disk timeout
  • Loading branch information
danawillow authored Nov 10, 2017
1 parent b3a22d0 commit b5faecd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
15 changes: 11 additions & 4 deletions google/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
Expand All @@ -29,6 +30,12 @@ func resourceComputeDisk() *schema.Resource {
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -199,7 +206,7 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error {
// It probably maybe worked, so store the ID now
d.SetId(disk.Name)

err = computeOperationWait(config.clientCompute, op, project, "Creating Disk")
err = computeOperationWaitTime(config.clientCompute, op, project, "Creating Disk", int(d.Timeout(schema.TimeoutCreate).Minutes()))
if err != nil {
return err
}
Expand All @@ -225,7 +232,7 @@ func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error {
}
d.SetPartial("size")

err = computeOperationWait(config.clientCompute, op, project, "Resizing Disk")
err = computeOperationWaitTime(config.clientCompute, op, project, "Resizing Disk", int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if err != nil {
return err
}
Expand All @@ -243,7 +250,7 @@ func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error {
}
d.SetPartial("labels")

err = computeOperationWait(config.clientCompute, op, project, "Setting labels on disk")
err = computeOperationWaitTime(config.clientCompute, op, project, "Setting labels on disk", int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if err != nil {
return err
}
Expand Down Expand Up @@ -378,7 +385,7 @@ func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error deleting disk: %s", err)
}

err = computeOperationWait(config.clientCompute, op, project, "Deleting Disk")
err = computeOperationWaitTime(config.clientCompute, op, project, "Deleting Disk", int(d.Timeout(schema.TimeoutDelete).Minutes()))
if err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions google/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"fmt"
"os"
"regexp"
"strconv"
"testing"

Expand Down Expand Up @@ -36,6 +37,21 @@ func TestAccComputeDisk_basic(t *testing.T) {
})
}

func TestAccComputeDisk_timeout(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeDisk_timeout,
ExpectError: regexp.MustCompile("timeout"),
},
},
})
}

func TestAccComputeDisk_update(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -298,6 +314,18 @@ resource "google_compute_disk" "foobar" {
}`, diskName)
}

var testAccComputeDisk_timeout = fmt.Sprintf(`
resource "google_compute_disk" "foobar" {
name = "%s"
image = "debian-8-jessie-v20160803"
type = "pd-ssd"
zone = "us-central1-a"
timeouts {
Create = "1s"
}
}`, acctest.RandString(10))

func testAccComputeDisk_updated(diskName string) string {
return fmt.Sprintf(`
resource "google_compute_disk" "foobar" {
Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/compute_disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ exported:

* `label_fingerprint` - The fingerprint of the assigned labels.

## Timeouts

`google_compute_disk` provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - (Default `5 minutes`) Used for creating disks.
- `update` - (Default `5 minutes`) Used for resizing a disk and setting labels on disks.
- `delete` - (Default `5 minutes`) Used for destroying disks (not including time to detach the disk from instances).

## Import

Disks can be imported using the `name`, e.g.
Expand Down

0 comments on commit b5faecd

Please sign in to comment.