2014-07-19 02:20:28 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-05-15 15:54:16 +02:00
|
|
|
"context"
|
2014-07-19 02:20:28 +02:00
|
|
|
"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,
|
2016-06-29 21:28:47 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2014-11-17 18:55:45 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-20 18:47:54 +01:00
|
|
|
"image": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"name": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"region": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2016-06-09 10:23:44 +02:00
|
|
|
StateFunc: func(val interface{}) string {
|
|
|
|
// DO API V2 region slug is always lowercase
|
|
|
|
return strings.ToLower(val.(string))
|
|
|
|
},
|
2014-11-17 18:55:45 +01:00
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"size": {
|
2014-11-17 18:55:45 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"disk": {
|
2016-10-25 02:00:04 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"vcpus": {
|
2016-10-25 02:00:04 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-04-18 13:52:40 +02:00
|
|
|
"price_hourly": {
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"price_monthly": {
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"resize_disk": {
|
2016-10-25 02:00:04 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"status": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"locked": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"backups": {
|
2015-02-26 18:37:05 +01:00
|
|
|
Type: schema.TypeBool,
|
2014-11-17 18:55:45 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"ipv6": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"ipv6_address": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
2016-08-15 16:52:48 +02:00
|
|
|
StateFunc: func(val interface{}) string {
|
|
|
|
return strings.ToLower(val.(string))
|
|
|
|
},
|
2014-11-17 18:55:45 +01:00
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"ipv6_address_private": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"private_networking": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"ipv4_address": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"ipv4_address_private": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"ssh_keys": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"tags": {
|
2016-07-11 13:09:06 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"user_data": {
|
2014-11-17 18:55:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-11-03 21:53:54 +01:00
|
|
|
ForceNew: true,
|
2014-11-17 18:55:45 +01:00
|
|
|
},
|
2016-07-13 16:36:37 +02:00
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"volume_ids": {
|
2016-07-13 16:36:37 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: 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)
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:36:37 +02:00
|
|
|
if attr, ok := d.GetOk("volume_ids"); ok {
|
|
|
|
for _, id := range attr.([]interface{}) {
|
|
|
|
opts.Volumes = append(opts.Volumes, godo.DropletCreateVolume{
|
|
|
|
ID: id.(string),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// 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)
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
droplet, _, err := client.Droplets.Create(context.Background(), 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)
|
|
|
|
}
|
|
|
|
|
2016-07-11 13:09:06 +02:00
|
|
|
// droplet needs to be active in order to set tags
|
|
|
|
err = setTags(client, d)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error setting tags: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
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
|
2017-05-15 15:54:16 +02:00
|
|
|
droplet, resp, err := client.Droplets.Get(context.Background(), 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.
|
2016-03-11 17:00:13 +01:00
|
|
|
if resp != nil && 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)
|
|
|
|
}
|
|
|
|
|
2017-04-27 19:48:58 +02:00
|
|
|
_, err = strconv.Atoi(d.Get("image").(string))
|
|
|
|
if err == nil || droplet.Image.Slug == "" {
|
|
|
|
// The image field is provided as an ID (number), or
|
|
|
|
// the image bash no slug. In both cases we store it as an ID.
|
2015-09-27 06:06:51 +02:00
|
|
|
d.Set("image", droplet.Image.ID)
|
2017-04-27 19:48:58 +02:00
|
|
|
} else {
|
|
|
|
d.Set("image", droplet.Image.Slug)
|
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)
|
2017-04-18 13:52:40 +02:00
|
|
|
d.Set("price_hourly", droplet.Size.PriceHourly)
|
|
|
|
d.Set("price_monthly", droplet.Size.PriceMonthly)
|
2016-10-25 02:00:04 +02:00
|
|
|
d.Set("disk", droplet.Disk)
|
|
|
|
d.Set("vcpus", droplet.Vcpus)
|
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
|
|
|
|
2016-07-13 16:36:37 +02:00
|
|
|
if len(droplet.VolumeIDs) > 0 {
|
|
|
|
vlms := make([]interface{}, 0, len(droplet.VolumeIDs))
|
|
|
|
for _, vid := range droplet.VolumeIDs {
|
|
|
|
vlms = append(vlms, vid)
|
|
|
|
}
|
|
|
|
d.Set("volume_ids", vlms)
|
|
|
|
}
|
|
|
|
|
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)
|
2016-08-15 16:52:48 +02:00
|
|
|
d.Set("ipv6_address", strings.ToLower(publicIPv6))
|
2015-09-27 06:06:51 +02:00
|
|
|
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
|
|
|
|
2016-07-11 13:09:06 +02:00
|
|
|
d.Set("tags", droplet.Tags)
|
|
|
|
|
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
|
|
|
|
2017-03-27 12:16:01 +02:00
|
|
|
resize_disk := d.Get("resize_disk").(bool)
|
|
|
|
if d.HasChange("size") || d.HasChange("resize_disk") && resize_disk {
|
|
|
|
newSize := d.Get("size")
|
2014-07-19 18:33:25 +02:00
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.DropletActions.PowerOff(context.Background(), 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
|
2017-05-15 15:54:16 +02:00
|
|
|
action, _, err := client.DropletActions.Resize(context.Background(), id, newSize.(string), resize_disk)
|
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
|
|
|
}
|
|
|
|
|
2017-03-27 12:16:01 +02:00
|
|
|
// Wait for the resize action to complete.
|
|
|
|
if err := waitForAction(client, action); 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
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.DropletActions.PowerOn(context.Background(), 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
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.DropletActions.Rename(context.Background(), 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) {
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.DropletActions.EnablePrivateNetworking(context.Background(), 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) {
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.DropletActions.EnableIPv6(context.Background(), 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
|
|
|
|
2016-07-11 13:09:06 +02:00
|
|
|
if d.HasChange("tags") {
|
|
|
|
err = setTags(client, d)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating tags: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:36:37 +02:00
|
|
|
if d.HasChange("volume_ids") {
|
|
|
|
oldIDs, newIDs := d.GetChange("volume_ids")
|
|
|
|
newSet := func(ids []interface{}) map[string]struct{} {
|
|
|
|
out := make(map[string]struct{}, len(ids))
|
|
|
|
for _, id := range ids {
|
|
|
|
out[id.(string)] = struct{}{}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
// leftDiff returns all elements in Left that are not in Right
|
|
|
|
leftDiff := func(left, right map[string]struct{}) map[string]struct{} {
|
|
|
|
out := make(map[string]struct{})
|
|
|
|
for l := range left {
|
|
|
|
if _, ok := right[l]; !ok {
|
|
|
|
out[l] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
oldIDSet := newSet(oldIDs.([]interface{}))
|
|
|
|
newIDSet := newSet(newIDs.([]interface{}))
|
|
|
|
for volumeID := range leftDiff(newIDSet, oldIDSet) {
|
2017-05-15 15:54:16 +02:00
|
|
|
action, _, err := client.StorageActions.Attach(context.Background(), volumeID, id)
|
2016-07-13 16:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error attaching volume %q to droplet (%s): %s", volumeID, d.Id(), err)
|
|
|
|
}
|
|
|
|
// can't fire >1 action at a time, so waiting for each is OK
|
|
|
|
if err := waitForAction(client, action); err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for volume %q to attach to droplet (%s): %s", volumeID, d.Id(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for volumeID := range leftDiff(oldIDSet, newIDSet) {
|
2017-05-15 15:54:16 +02:00
|
|
|
action, _, err := client.StorageActions.DetachByDropletID(context.Background(), volumeID, id)
|
2016-07-13 16:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error detaching volume %q from droplet (%s): %s", volumeID, d.Id(), err)
|
|
|
|
}
|
|
|
|
// can't fire >1 action at a time, so waiting for each is OK
|
|
|
|
if err := waitForAction(client, action); err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for volume %q to detach from droplet (%s): %s", volumeID, d.Id(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-05-15 15:54:16 +02:00
|
|
|
_, err = client.Droplets.Delete(context.Background(), 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,
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{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
|
2017-05-15 15:54:16 +02:00
|
|
|
droplet, _, err := client.Droplets.Get(context.Background(), 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)
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.DropletActions.PowerOn(context.Background(), 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
|
|
|
|
}
|