Skip to content

Commit

Permalink
Add labels to google_compute_disk (hashicorp#344)
Browse files Browse the repository at this point in the history
* Remove redundant '(Computed)' text

* Add support for setting labels on compute_disk

* Fix minor spacing issue in test
  • Loading branch information
selmanj authored Aug 18, 2017
1 parent e94f800 commit 5ec8fce
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
42 changes: 41 additions & 1 deletion google/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,24 @@ func resourceComputeDisk() *schema.Resource {
Default: "pd-standard",
ForceNew: true,
},

"users": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"label_fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -173,6 +186,10 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error {
disk.DiskEncryptionKey.RawKey = v.(string)
}

if _, ok := d.GetOk("labels"); ok {
disk.Labels = expandLabels(d)
}

op, err := config.clientCompute.Disks.Insert(
project, d.Get("zone").(string), disk).Do()
if err != nil {
Expand All @@ -196,7 +213,7 @@ func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}

d.Partial(true)
if d.HasChange("size") {
rb := &compute.DisksResizeRequest{
SizeGb: int64(d.Get("size").(int)),
Expand All @@ -206,12 +223,33 @@ func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf("Error resizing disk: %s", err)
}
d.SetPartial("size")

err = computeOperationWait(config, op, project, "Resizing Disk")
if err != nil {
return err
}
}

if d.HasChange("labels") {
zslr := compute.ZoneSetLabelsRequest{
Labels: expandLabels(d),
LabelFingerprint: d.Get("label_fingerprint").(string),
}
op, err := config.clientCompute.Disks.SetLabels(
project, d.Get("zone").(string), d.Id(), &zslr).Do()
if err != nil {
return fmt.Errorf("Error when setting labels: %s", err)
}
d.SetPartial("labels")

err = computeOperationWait(config, op, project, "Setting labels on disk")
if err != nil {
return err
}
}
d.Partial(false)

return resourceComputeDiskRead(d, meta)
}

Expand Down Expand Up @@ -266,6 +304,8 @@ func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error {

d.Set("image", disk.SourceImage)
d.Set("snapshot", disk.SourceSnapshot)
d.Set("labels", disk.Labels)
d.Set("label_fingerprint", disk.LabelFingerprint)

return nil
}
Expand Down
51 changes: 48 additions & 3 deletions google/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func TestAccComputeDisk_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeDiskExists(
"google_compute_disk.foobar", &disk),
testAccCheckComputeDiskHasLabel(&disk, "my-label", "my-label-value"),
testAccCheckComputeDiskHasLabelFingerprint(&disk, "google_compute_disk.foobar"),
),
},
},
})
}

func TestAccComputeDisk_updateSize(t *testing.T) {
func TestAccComputeDisk_update(t *testing.T) {
diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
var disk compute.Disk

Expand All @@ -46,14 +48,19 @@ func TestAccComputeDisk_updateSize(t *testing.T) {
testAccCheckComputeDiskExists(
"google_compute_disk.foobar", &disk),
resource.TestCheckResourceAttr("google_compute_disk.foobar", "size", "50"),
testAccCheckComputeDiskHasLabel(&disk, "my-label", "my-label-value"),
testAccCheckComputeDiskHasLabelFingerprint(&disk, "google_compute_disk.foobar"),
),
},
{
Config: testAccComputeDisk_resized(diskName),
Config: testAccComputeDisk_updated(diskName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeDiskExists(
"google_compute_disk.foobar", &disk),
resource.TestCheckResourceAttr("google_compute_disk.foobar", "size", "100"),
testAccCheckComputeDiskHasLabel(&disk, "my-label", "my-updated-label-value"),
testAccCheckComputeDiskHasLabel(&disk, "a-new-label", "a-new-label-value"),
testAccCheckComputeDiskHasLabelFingerprint(&disk, "google_compute_disk.foobar"),
),
},
},
Expand Down Expand Up @@ -194,6 +201,37 @@ func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCh
}
}

func testAccCheckComputeDiskHasLabel(disk *compute.Disk, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
val, ok := disk.Labels[key]
if !ok {
return fmt.Errorf("Label with key %s not found", key)
}

if val != value {
return fmt.Errorf("Label value did not match for key %s: expected %s but found %s", key, value, val)
}
return nil
}
}

func testAccCheckComputeDiskHasLabelFingerprint(disk *compute.Disk, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
state := s.RootModule().Resources[resourceName]
if state == nil {
return fmt.Errorf("Unable to find resource named %s", resourceName)
}

labelFingerprint := state.Primary.Attributes["label_fingerprint"]
if labelFingerprint != disk.LabelFingerprint {
return fmt.Errorf("Label fingerprints do not match: api returned %s but state has %s",
disk.LabelFingerprint, labelFingerprint)
}

return nil
}
}

func testAccCheckEncryptionKey(n string, disk *compute.Disk) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -244,17 +282,24 @@ resource "google_compute_disk" "foobar" {
size = 50
type = "pd-ssd"
zone = "us-central1-a"
labels {
my-label = "my-label-value"
}
}`, diskName)
}

func testAccComputeDisk_resized(diskName string) string {
func testAccComputeDisk_updated(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"
labels {
my-label = "my-updated-label-value"
a-new-label = "a-new-label-value"
}
}`, diskName)
}

Expand Down
9 changes: 8 additions & 1 deletion website/docs/r/compute_disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ resource "google_compute_disk" "default" {
type = "pd-ssd"
zone = "us-central1-a"
image = "debian-8-jessie-v20170523"
labels {
environment = "dev"
}
}
```

Expand Down Expand Up @@ -57,6 +60,8 @@ The following arguments are supported:

* `type` - (Optional) The GCE disk type.

* `labels` - (Optional) A set of key/value label pairs to assign to the image.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand All @@ -69,7 +74,9 @@ exported:

* `self_link` - The URI of the created resource.

* `users` - (Computed) The Users of the created resource.
* `users` - The Users of the created resource.

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

## Import

Expand Down

0 comments on commit 5ec8fce

Please sign in to comment.