2016-07-13 16:36:37 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-05-15 15:54:16 +02:00
|
|
|
"context"
|
2016-07-13 16:36:37 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/digitalocean/godo"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceDigitalOceanVolume() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceDigitalOceanVolumeCreate,
|
|
|
|
Read: resourceDigitalOceanVolumeRead,
|
|
|
|
Delete: resourceDigitalOceanVolumeDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourceDigitalOceanVolumeImport,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-20 18:47:54 +01:00
|
|
|
"region": {
|
2016-07-13 16:36:37 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"id": {
|
2016-07-13 16:36:37 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"name": {
|
2016-07-13 16:36:37 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"droplet_ids": {
|
2016-07-13 16:36:37 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeInt},
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"size": {
|
2016-07-13 16:36:37 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true, // Update-ability Coming Soon ™
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"description": {
|
2016-07-13 16:36:37 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true, // Update-ability Coming Soon ™
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanVolumeCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
opts := &godo.VolumeCreateRequest{
|
|
|
|
Region: d.Get("region").(string),
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
SizeGigaBytes: int64(d.Get("size").(int)),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Volume create configuration: %#v", opts)
|
2017-05-15 15:54:16 +02:00
|
|
|
volume, _, err := client.Storage.CreateVolume(context.Background(), opts)
|
2016-07-13 16:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Volume: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(volume.ID)
|
|
|
|
log.Printf("[INFO] Volume name: %s", volume.Name)
|
|
|
|
|
|
|
|
return resourceDigitalOceanVolumeRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanVolumeRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
volume, resp, err := client.Storage.GetVolume(context.Background(), d.Id())
|
2016-07-13 16:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
// If the volume is somehow already destroyed, mark as
|
|
|
|
// successfully gone
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error retrieving volume: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("id", volume.ID)
|
|
|
|
|
|
|
|
dids := make([]interface{}, 0, len(volume.DropletIDs))
|
|
|
|
for _, did := range volume.DropletIDs {
|
|
|
|
dids = append(dids, did)
|
|
|
|
}
|
|
|
|
d.Set("droplet_ids", schema.NewSet(
|
|
|
|
func(dropletID interface{}) int { return dropletID.(int) },
|
|
|
|
dids,
|
|
|
|
))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanVolumeDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting volume: %s", d.Id())
|
2017-05-15 15:54:16 +02:00
|
|
|
_, err := client.Storage.DeleteVolume(context.Background(), d.Id())
|
2016-07-13 16:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting volume: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanVolumeImport(rs *schema.ResourceData, v interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
client := v.(*godo.Client)
|
2017-05-15 15:54:16 +02:00
|
|
|
volume, _, err := client.Storage.GetVolume(context.Background(), rs.Id())
|
2016-07-13 16:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rs.Set("id", volume.ID)
|
|
|
|
rs.Set("name", volume.Name)
|
|
|
|
rs.Set("region", volume.Region.Slug)
|
|
|
|
rs.Set("description", volume.Description)
|
|
|
|
rs.Set("size", int(volume.SizeGigaBytes))
|
|
|
|
|
|
|
|
dids := make([]interface{}, 0, len(volume.DropletIDs))
|
|
|
|
for _, did := range volume.DropletIDs {
|
|
|
|
dids = append(dids, did)
|
|
|
|
}
|
|
|
|
rs.Set("droplet_ids", schema.NewSet(
|
|
|
|
func(dropletID interface{}) int { return dropletID.(int) },
|
|
|
|
dids,
|
|
|
|
))
|
|
|
|
|
|
|
|
return []*schema.ResourceData{rs}, nil
|
|
|
|
}
|