From ddfdfebcc5c3fbd13edb8a6c8747881ec9a8de87 Mon Sep 17 00:00:00 2001 From: Jeff Goldschrafe Date: Tue, 7 Oct 2014 00:59:09 -0400 Subject: [PATCH] Configurable disk types for GCE Supports configuring the disk type for Google Compute Engine disk resources. Both `google_compute_disk` and `google_compute_instance` disk types are supported. Resolves #351. --- builtin/providers/google/disk_type.go | 15 +++++++++++ .../providers/google/resource_compute_disk.go | 27 +++++++++++++++++++ .../google/resource_compute_disk_test.go | 1 + .../google/resource_compute_instance.go | 19 +++++++++++++ .../google/resource_compute_instance_test.go | 1 + .../google/r/compute_disk.html.markdown | 3 +++ .../google/r/compute_instance.html.markdown | 2 ++ 7 files changed, 68 insertions(+) create mode 100644 builtin/providers/google/disk_type.go diff --git a/builtin/providers/google/disk_type.go b/builtin/providers/google/disk_type.go new file mode 100644 index 000000000..dfea866db --- /dev/null +++ b/builtin/providers/google/disk_type.go @@ -0,0 +1,15 @@ +package google + +import ( + "code.google.com/p/google-api-go-client/compute/v1" +) + +// readDiskType finds the disk type with the given name. +func readDiskType(c *Config, zone *compute.Zone, name string) (*compute.DiskType, error) { + diskType, err := c.clientCompute.DiskTypes.Get(c.Project, zone.Name, name).Do() + if err == nil && diskType != nil && diskType.SelfLink != "" { + return diskType, nil + } else { + return nil, err + } +} diff --git a/builtin/providers/google/resource_compute_disk.go b/builtin/providers/google/resource_compute_disk.go index e69cf9a16..5daf304cd 100644 --- a/builtin/providers/google/resource_compute_disk.go +++ b/builtin/providers/google/resource_compute_disk.go @@ -40,6 +40,12 @@ func resourceComputeDisk() *schema.Resource { Optional: true, ForceNew: true, }, + + "type": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -47,6 +53,15 @@ func resourceComputeDisk() *schema.Resource { func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + // Get the zone + log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string)) + zone, err := config.clientCompute.Zones.Get( + config.Project, d.Get("zone").(string)).Do() + if err != nil { + return fmt.Errorf( + "Error loading zone '%s': %s", d.Get("zone").(string), err) + } + // Build the disk parameter disk := &compute.Disk{ Name: d.Get("name").(string), @@ -66,6 +81,18 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { disk.SourceImage = image.SelfLink } + if v, ok := d.GetOk("type"); ok { + log.Printf("[DEBUG] Loading disk type: %s", v.(string)) + diskType, err := readDiskType(config, zone, v.(string)) + if err != nil { + return fmt.Errorf( + "Error loading disk type '%s': %s", + v.(string), err) + } + + disk.Type = diskType.SelfLink + } + op, err := config.clientCompute.Disks.Insert( config.Project, d.Get("zone").(string), disk).Do() if err != nil { diff --git a/builtin/providers/google/resource_compute_disk_test.go b/builtin/providers/google/resource_compute_disk_test.go index 04853e4e8..f99d9ed62 100644 --- a/builtin/providers/google/resource_compute_disk_test.go +++ b/builtin/providers/google/resource_compute_disk_test.go @@ -80,5 +80,6 @@ resource "google_compute_disk" "foobar" { name = "terraform-test" image = "debian-7-wheezy-v20140814" size = 50 + type = "pd-ssd" zone = "us-central1-a" }` diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 52b97c014..7ce009ceb 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -60,6 +60,13 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Optional: true, }, + + "type": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "auto_delete": &schema.Schema{ Type: schema.TypeBool, Optional: true, @@ -197,6 +204,18 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err } } + if v, ok := d.GetOk(prefix + ".type"); ok { + diskTypeName := v.(string) + diskType, err := readDiskType(config, zone, diskTypeName) + if err != nil { + return fmt.Errorf( + "Error loading disk type '%s': %s", + diskTypeName, err) + } + + disk.InitializeParams.DiskType = diskType.SelfLink + } + disks = append(disks, &disk) } diff --git a/builtin/providers/google/resource_compute_instance_test.go b/builtin/providers/google/resource_compute_instance_test.go index 8759cf94f..ec4f1be71 100644 --- a/builtin/providers/google/resource_compute_instance_test.go +++ b/builtin/providers/google/resource_compute_instance_test.go @@ -298,6 +298,7 @@ resource "google_compute_instance" "foobar" { disk { disk = "terraform-test-disk" auto_delete = false + type = "pd-ssd" } network { diff --git a/website/source/docs/providers/google/r/compute_disk.html.markdown b/website/source/docs/providers/google/r/compute_disk.html.markdown index 96e43efec..f52120058 100644 --- a/website/source/docs/providers/google/r/compute_disk.html.markdown +++ b/website/source/docs/providers/google/r/compute_disk.html.markdown @@ -13,6 +13,7 @@ Creates a new persistent disk within GCE, based on another disk. ``` resource "google_compute_disk" "default" { name = "test-disk" + type = "pd-ssd" zone = "us-central1-a" image = "debian7-wheezy" } @@ -32,6 +33,8 @@ The following arguments are supported: * `size` - (Optional) The size of the image in gigabytes. If not specified, it will inherit the size of its base image. +* `type` - (Optional) The GCE disk type. + ## Attributes Reference The following attributes are exported: diff --git a/website/source/docs/providers/google/r/compute_instance.html.markdown b/website/source/docs/providers/google/r/compute_instance.html.markdown index c9f0bd896..cb39d7121 100644 --- a/website/source/docs/providers/google/r/compute_instance.html.markdown +++ b/website/source/docs/providers/google/r/compute_instance.html.markdown @@ -67,6 +67,8 @@ The `disk` block supports: * `auto_delete` - (Optional) Whether or not the disk should be auto-deleted. This defaults to true. +* `type` - (Optional) The GCE disk type. + The `network` block supports: * `source` - (Required) The name of the network to attach this interface to.