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.
This commit is contained in:
Jeff Goldschrafe 2014-10-07 00:59:09 -04:00
parent 28cd738edc
commit ddfdfebcc5
7 changed files with 68 additions and 0 deletions

View File

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

View File

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

View File

@ -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"
}`

View File

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

View File

@ -298,6 +298,7 @@ resource "google_compute_instance" "foobar" {
disk {
disk = "terraform-test-disk"
auto_delete = false
type = "pd-ssd"
}
network {

View File

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

View File

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