2014-08-26 01:23:28 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.google.com/p/google-api-go-client/compute/v1"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeDisk() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeDiskCreate,
|
|
|
|
Read: resourceComputeDiskRead,
|
|
|
|
Delete: resourceComputeDiskDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"image": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
// Build the disk parameter
|
|
|
|
disk := &compute.Disk{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
SizeGb: int64(d.Get("size").(int)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were given a source image, load that.
|
|
|
|
if v, ok := d.GetOk("image"); ok {
|
|
|
|
log.Printf("[DEBUG] Loading image: %s", v.(string))
|
|
|
|
image, err := readImage(config, v.(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading image '%s': %s",
|
|
|
|
v.(string), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
disk.SourceImage = image.SelfLink
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientCompute.Disks.Insert(
|
|
|
|
config.Project, d.Get("zone").(string), disk).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating disk: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(disk.Name)
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Zone: d.Get("zone").(string),
|
|
|
|
Type: OperationWaitZone,
|
|
|
|
}
|
|
|
|
state := w.Conf()
|
|
|
|
state.Timeout = 2 * time.Minute
|
|
|
|
state.MinTimeout = 1 * time.Second
|
2014-08-26 07:44:27 +02:00
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for disk to create: %s", err)
|
|
|
|
}
|
|
|
|
op = opRaw.(*compute.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
// The resource didn't actually create
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
// Return the error
|
|
|
|
return OperationError(*op.Error)
|
2014-08-26 01:23:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeDiskRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
_, err := config.clientCompute.Disks.Get(
|
|
|
|
config.Project, d.Get("zone").(string), d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading disk: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2014-08-26 07:44:27 +02:00
|
|
|
// Delete the disk
|
2014-08-26 01:23:28 +02:00
|
|
|
op, err := config.clientCompute.Disks.Delete(
|
|
|
|
config.Project, d.Get("zone").(string), d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting disk: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Zone: d.Get("zone").(string),
|
|
|
|
Type: OperationWaitZone,
|
|
|
|
}
|
|
|
|
state := w.Conf()
|
|
|
|
state.Timeout = 2 * time.Minute
|
|
|
|
state.MinTimeout = 1 * time.Second
|
2014-08-26 07:44:27 +02:00
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for disk to delete: %s", err)
|
|
|
|
}
|
|
|
|
op = opRaw.(*compute.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
// Return the error
|
|
|
|
return OperationError(*op.Error)
|
2014-08-26 01:23:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|