2014-07-19 02:20:28 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-09-27 06:06:51 +02:00
|
|
|
"strconv"
|
2014-07-19 02:20:28 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
2014-07-19 02:20:28 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-11-17 18:55:45 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-19 02:20:28 +02:00
|
|
|
)
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDroplet() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceDigitalOceanDropletCreate,
|
|
|
|
Read: resourceDigitalOceanDropletRead,
|
|
|
|
Update: resourceDigitalOceanDropletUpdate,
|
|
|
|
Delete: resourceDigitalOceanDropletDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"image": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"size": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-09-18 20:42:37 +02:00
|
|
|
StateFunc: func(val interface{}) string {
|
|
|
|
// DO API V2 size slug is always lowercase
|
|
|
|
return strings.ToLower(val.(string))
|
|
|
|
},
|
2014-11-17 18:55:45 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"status": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"locked": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"backups": &schema.Schema{
|
2015-02-26 18:37:05 +01:00
|
|
|
Type: schema.TypeBool,
|
2014-11-17 18:55:45 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ipv6": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ipv6_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ipv6_address_private": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"private_networking": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ipv4_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ipv4_address_private": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ssh_keys": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
|
|
|
"user_data": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-11-03 21:53:54 +01:00
|
|
|
ForceNew: true,
|
2014-11-17 18:55:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDropletCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
// Build up our creation options
|
2015-09-27 06:06:51 +02:00
|
|
|
opts := &godo.DropletCreateRequest{
|
|
|
|
Image: godo.DropletCreateImage{
|
|
|
|
Slug: d.Get("image").(string),
|
|
|
|
},
|
2014-11-17 18:55:45 +01:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Region: d.Get("region").(string),
|
|
|
|
Size: d.Get("size").(string),
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
if attr, ok := d.GetOk("backups"); ok {
|
2015-02-26 18:37:05 +01:00
|
|
|
opts.Backups = attr.(bool)
|
2014-11-17 18:55:45 +01:00
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2015-02-26 18:37:05 +01:00
|
|
|
if attr, ok := d.GetOk("ipv6"); ok {
|
2015-09-27 06:06:51 +02:00
|
|
|
opts.IPv6 = attr.(bool)
|
2014-11-17 18:55:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-26 18:37:05 +01:00
|
|
|
if attr, ok := d.GetOk("private_networking"); ok {
|
|
|
|
opts.PrivateNetworking = attr.(bool)
|
2014-11-17 18:55:45 +01:00
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
if attr, ok := d.GetOk("user_data"); ok {
|
|
|
|
opts.UserData = attr.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get configured ssh_keys
|
2015-09-27 06:06:51 +02:00
|
|
|
sshKeys := d.Get("ssh_keys.#").(int)
|
|
|
|
if sshKeys > 0 {
|
|
|
|
opts.SSHKeys = make([]godo.DropletCreateSSHKey, 0, sshKeys)
|
|
|
|
for i := 0; i < sshKeys; i++ {
|
2014-11-17 18:55:45 +01:00
|
|
|
key := fmt.Sprintf("ssh_keys.%d", i)
|
2015-10-25 21:29:01 +01:00
|
|
|
sshKeyRef := d.Get(key).(string)
|
|
|
|
|
|
|
|
var sshKey godo.DropletCreateSSHKey
|
|
|
|
// sshKeyRef can be either an ID or a fingerprint
|
|
|
|
if id, err := strconv.Atoi(sshKeyRef); err == nil {
|
|
|
|
sshKey.ID = id
|
|
|
|
} else {
|
|
|
|
sshKey.Fingerprint = sshKeyRef
|
2015-09-27 06:06:51 +02:00
|
|
|
}
|
|
|
|
|
2015-10-25 21:29:01 +01:00
|
|
|
opts.SSHKeys = append(opts.SSHKeys, sshKey)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Droplet create configuration: %#v", opts)
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
droplet, _, err := client.Droplets.Create(opts)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf("Error creating droplet: %s", err)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the droplets id
|
2015-09-27 06:06:51 +02:00
|
|
|
d.SetId(strconv.Itoa(droplet.ID))
|
2014-11-17 18:55:45 +01:00
|
|
|
|
|
|
|
log.Printf("[INFO] Droplet ID: %s", d.Id())
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
_, err = WaitForDropletAttribute(d, "active", []string{"new"}, "status", meta)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for droplet (%s) to become ready: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceDigitalOceanDropletRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanDropletRead(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid droplet id: %v", err)
|
|
|
|
}
|
2014-11-17 18:55:45 +01:00
|
|
|
|
|
|
|
// Retrieve the droplet properties for updating the state
|
2015-11-05 17:33:29 +01:00
|
|
|
droplet, resp, err := client.Droplets.Get(id)
|
2014-07-19 02:20:28 +02:00
|
|
|
if err != nil {
|
2015-06-25 23:01:48 +02:00
|
|
|
// check if the droplet no longer exists.
|
2015-11-05 17:33:29 +01:00
|
|
|
if resp.StatusCode == 404 {
|
2015-11-05 18:32:57 +01:00
|
|
|
log.Printf("[WARN] DigitalOcean Droplet (%s) not found", d.Id())
|
2015-06-25 23:01:48 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf("Error retrieving droplet: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
if droplet.Image.Slug != "" {
|
|
|
|
d.Set("image", droplet.Image.Slug)
|
2014-12-01 20:36:05 +01:00
|
|
|
} else {
|
2015-09-27 06:06:51 +02:00
|
|
|
d.Set("image", droplet.Image.ID)
|
2014-11-17 18:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", droplet.Name)
|
2015-09-27 06:06:51 +02:00
|
|
|
d.Set("region", droplet.Region.Slug)
|
|
|
|
d.Set("size", droplet.Size.Slug)
|
2014-11-17 18:55:45 +01:00
|
|
|
d.Set("status", droplet.Status)
|
2015-09-27 06:06:51 +02:00
|
|
|
d.Set("locked", strconv.FormatBool(droplet.Locked))
|
2014-11-17 18:55:45 +01:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
if publicIPv6 := findIPv6AddrByType(droplet, "public"); publicIPv6 != "" {
|
2014-11-17 18:55:45 +01:00
|
|
|
d.Set("ipv6", true)
|
2015-09-27 06:06:51 +02:00
|
|
|
d.Set("ipv6_address", publicIPv6)
|
|
|
|
d.Set("ipv6_address_private", findIPv6AddrByType(droplet, "private"))
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
d.Set("ipv4_address", findIPv4AddrByType(droplet, "public"))
|
2014-11-17 18:55:45 +01:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
if privateIPv4 := findIPv4AddrByType(droplet, "private"); privateIPv4 != "" {
|
2014-11-17 18:55:45 +01:00
|
|
|
d.Set("private_networking", true)
|
2015-09-27 06:06:51 +02:00
|
|
|
d.Set("ipv4_address_private", privateIPv4)
|
2014-11-17 18:55:45 +01:00
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-07-24 19:50:40 +02:00
|
|
|
// Initialize the connection info
|
2014-11-17 18:55:45 +01:00
|
|
|
d.SetConnInfo(map[string]string{
|
|
|
|
"type": "ssh",
|
2015-09-27 06:06:51 +02:00
|
|
|
"host": findIPv4AddrByType(droplet, "public"),
|
2014-11-17 18:55:45 +01:00
|
|
|
})
|
2014-07-24 19:50:40 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
return nil
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
func findIPv6AddrByType(d *godo.Droplet, addrType string) string {
|
|
|
|
for _, addr := range d.Networks.V6 {
|
|
|
|
if addr.Type == addrType {
|
|
|
|
return addr.IPAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func findIPv4AddrByType(d *godo.Droplet, addrType string) string {
|
|
|
|
for _, addr := range d.Networks.V4 {
|
|
|
|
if addr.Type == addrType {
|
|
|
|
return addr.IPAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDropletUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid droplet id: %v", err)
|
|
|
|
}
|
2014-07-19 18:33:25 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
if d.HasChange("size") {
|
|
|
|
oldSize, newSize := d.GetChange("size")
|
2014-07-19 18:33:25 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.DropletActions.PowerOff(id)
|
2014-07-19 18:33:25 +02:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "Droplet is already powered off") {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error powering off droplet (%s): %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for power off
|
2014-11-17 18:55:45 +01:00
|
|
|
_, err = WaitForDropletAttribute(d, "off", []string{"active"}, "status", client)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for droplet (%s) to become powered off: %s", d.Id(), err)
|
|
|
|
}
|
2014-07-19 18:33:25 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// Resize the droplet
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.DropletActions.Resize(id, newSize.(string), true)
|
2014-07-19 18:33:25 +02:00
|
|
|
if err != nil {
|
2014-12-15 15:26:17 +01:00
|
|
|
newErr := powerOnAndWait(d, meta)
|
2014-08-01 16:10:52 +02:00
|
|
|
if newErr != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error powering on droplet (%s) after failed resize: %s", d.Id(), err)
|
2014-08-01 16:10:52 +02:00
|
|
|
}
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error resizing droplet (%s): %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the size to change
|
|
|
|
_, err = WaitForDropletAttribute(
|
2014-11-17 18:55:45 +01:00
|
|
|
d, newSize.(string), []string{"", oldSize.(string)}, "size", meta)
|
2014-07-19 18:33:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2014-12-15 15:26:17 +01:00
|
|
|
newErr := powerOnAndWait(d, meta)
|
2014-08-01 16:10:52 +02:00
|
|
|
if newErr != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error powering on droplet (%s) after waiting for resize to finish: %s", d.Id(), err)
|
2014-08-01 16:10:52 +02:00
|
|
|
}
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for resize droplet (%s) to finish: %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.DropletActions.PowerOn(id)
|
2014-07-19 18:33:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error powering on droplet (%s) after resize: %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for power off
|
2014-11-17 18:55:45 +01:00
|
|
|
_, err = WaitForDropletAttribute(d, "active", []string{"off"}, "status", meta)
|
2014-07-19 18:33:25 +02:00
|
|
|
if err != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return err
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
if d.HasChange("name") {
|
|
|
|
oldName, newName := d.GetChange("name")
|
|
|
|
|
|
|
|
// Rename the droplet
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.DropletActions.Rename(id, newName.(string))
|
2014-07-19 18:33:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error renaming droplet (%s): %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the name to change
|
|
|
|
_, err = WaitForDropletAttribute(
|
2014-11-17 18:55:45 +01:00
|
|
|
d, newName.(string), []string{"", oldName.(string)}, "name", meta)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for rename droplet (%s) to finish: %s", d.Id(), err)
|
|
|
|
}
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// As there is no way to disable private networking,
|
|
|
|
// we only check if it needs to be enabled
|
|
|
|
if d.HasChange("private_networking") && d.Get("private_networking").(bool) {
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.DropletActions.EnablePrivateNetworking(id)
|
2014-07-19 18:33:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error enabling private networking for droplet (%s): %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// Wait for the private_networking to turn on
|
2014-07-19 18:33:25 +02:00
|
|
|
_, err = WaitForDropletAttribute(
|
2014-11-17 18:55:45 +01:00
|
|
|
d, "true", []string{"", "false"}, "private_networking", meta)
|
|
|
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for private networking to be enabled on for droplet (%s): %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// As there is no way to disable IPv6, we only check if it needs to be enabled
|
|
|
|
if d.HasChange("ipv6") && d.Get("ipv6").(bool) {
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.DropletActions.EnableIPv6(id)
|
2014-07-19 15:45:42 +02:00
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
if err != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error turning on ipv6 for droplet (%s): %s", d.Id(), err)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// Wait for ipv6 to turn on
|
2014-07-19 18:33:25 +02:00
|
|
|
_, err = WaitForDropletAttribute(
|
2014-11-17 18:55:45 +01:00
|
|
|
d, "true", []string{"", "false"}, "ipv6", meta)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for ipv6 to be turned on for droplet (%s): %s", d.Id(), err)
|
|
|
|
}
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
return resourceDigitalOceanDropletRead(d, meta)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDropletDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid droplet id: %v", err)
|
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
_, err = WaitForDropletAttribute(
|
2015-02-26 01:54:39 +01:00
|
|
|
d, "false", []string{"", "true"}, "locked", meta)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for droplet to be unlocked for destroy (%s): %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
log.Printf("[INFO] Deleting droplet: %s", d.Id())
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
// Destroy the droplet
|
2015-09-27 06:06:51 +02:00
|
|
|
_, err = client.Droplets.Delete(id)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-07-24 23:31:23 +02:00
|
|
|
// Handle remotely destroyed droplets
|
2014-07-24 23:57:23 +02:00
|
|
|
if err != nil && strings.Contains(err.Error(), "404 Not Found") {
|
2014-07-24 23:31:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
if err != nil {
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf("Error deleting droplet: %s", err)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func WaitForDropletAttribute(
|
|
|
|
d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}) (interface{}, error) {
|
2014-07-19 15:45:42 +02:00
|
|
|
// Wait for the droplet so we can get the networking attributes
|
|
|
|
// that show up after a while
|
|
|
|
log.Printf(
|
2014-11-20 15:09:22 +01:00
|
|
|
"[INFO] Waiting for droplet (%s) to have %s of %s",
|
2014-11-17 18:55:45 +01:00
|
|
|
d.Id(), attribute, target)
|
2014-07-19 15:45:42 +02:00
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
2014-07-28 18:58:19 +02:00
|
|
|
Pending: pending,
|
|
|
|
Target: target,
|
2014-12-15 15:26:17 +01:00
|
|
|
Refresh: newDropletStateRefreshFunc(d, attribute, meta),
|
2015-02-26 01:08:35 +01:00
|
|
|
Timeout: 60 * time.Minute,
|
2014-07-28 18:58:19 +02:00
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
2015-02-26 19:00:25 +01:00
|
|
|
|
|
|
|
// This is a hack around DO API strangeness.
|
|
|
|
// https://github.com/hashicorp/terraform/issues/481
|
|
|
|
//
|
|
|
|
NotFoundChecks: 60,
|
2014-07-19 15:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return stateConf.WaitForState()
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// TODO This function still needs a little more refactoring to make it
|
|
|
|
// cleaner and more efficient
|
2014-12-15 15:26:17 +01:00
|
|
|
func newDropletStateRefreshFunc(
|
2014-11-17 18:55:45 +01:00
|
|
|
d *schema.ResourceData, attribute string, meta interface{}) resource.StateRefreshFunc {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
2014-07-19 02:20:28 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2015-09-27 06:06:51 +02:00
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resourceDigitalOceanDropletRead(d, meta)
|
2014-07-19 15:45:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2014-07-21 17:05:24 +02:00
|
|
|
// If the droplet is locked, continue waiting. We can
|
|
|
|
// only perform actions on unlocked droplets, so it's
|
|
|
|
// pointless to look at that status
|
2014-11-17 18:55:45 +01:00
|
|
|
if d.Get("locked").(string) == "true" {
|
2014-07-21 17:05:24 +02:00
|
|
|
log.Println("[DEBUG] Droplet is locked, skipping status check and retrying")
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// See if we can access our attribute
|
|
|
|
if attr, ok := d.GetOk(attribute); ok {
|
|
|
|
// Retrieve the droplet properties
|
2015-09-27 06:06:51 +02:00
|
|
|
droplet, _, err := client.Droplets.Get(id)
|
2014-11-17 18:55:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("Error retrieving droplet: %s", err)
|
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
return &droplet, attr.(string), nil
|
2014-07-19 15:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, "", nil
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-01 16:10:52 +02:00
|
|
|
|
|
|
|
// Powers on the droplet and waits for it to be active
|
2014-12-15 15:26:17 +01:00
|
|
|
func powerOnAndWait(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid droplet id: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
_, _, err = client.DropletActions.PowerOn(id)
|
2014-08-01 16:10:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-01 17:05:32 +02:00
|
|
|
// Wait for power on
|
2014-11-17 18:55:45 +01:00
|
|
|
_, err = WaitForDropletAttribute(d, "active", []string{"off"}, "status", client)
|
2014-08-01 16:10:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|