2014-07-19 02:20:28 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/pearkes/digitalocean"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_digitalocean_droplet_create(
|
2014-09-17 02:28:22 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff,
|
2014-09-17 02:28:22 +02:00
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-19 02:20:28 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
// Build up our creation options
|
|
|
|
opts := digitalocean.CreateDroplet{
|
|
|
|
Backups: rs.Attributes["backups"],
|
2014-07-19 02:41:02 +02:00
|
|
|
Image: rs.Attributes["image"],
|
2014-07-19 02:20:28 +02:00
|
|
|
IPV6: rs.Attributes["ipv6"],
|
2014-07-19 02:41:02 +02:00
|
|
|
Name: rs.Attributes["name"],
|
2014-07-19 02:20:28 +02:00
|
|
|
PrivateNetworking: rs.Attributes["private_networking"],
|
2014-07-19 02:41:02 +02:00
|
|
|
Region: rs.Attributes["region"],
|
|
|
|
Size: rs.Attributes["size"],
|
2014-09-05 17:16:12 +02:00
|
|
|
UserData: rs.Attributes["user_data"],
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only expand ssh_keys if we have them
|
|
|
|
if _, ok := rs.Attributes["ssh_keys.#"]; ok {
|
|
|
|
v := flatmap.Expand(rs.Attributes, "ssh_keys").([]interface{})
|
|
|
|
if len(v) > 0 {
|
|
|
|
vs := make([]string, 0, len(v))
|
|
|
|
|
|
|
|
// here we special case the * expanded lists. For example:
|
|
|
|
//
|
|
|
|
// ssh_keys = ["${digitalocean_key.foo.*.id}"]
|
|
|
|
//
|
|
|
|
if len(v) == 1 && strings.Contains(v[0].(string), ",") {
|
|
|
|
vs = strings.Split(v[0].(string), ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range v {
|
|
|
|
vs = append(vs, v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.SSHKeys = vs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Droplet create configuration: %#v", opts)
|
|
|
|
|
|
|
|
id, err := client.CreateDroplet(&opts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating Droplet: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the droplets id
|
|
|
|
rs.ID = id
|
|
|
|
|
|
|
|
log.Printf("[INFO] Droplet ID: %s", id)
|
|
|
|
|
2014-07-19 15:45:42 +02:00
|
|
|
dropletRaw, err := WaitForDropletAttribute(id, "active", []string{"new"}, "status", client)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return rs, fmt.Errorf(
|
|
|
|
"Error waiting for droplet (%s) to become ready: %s",
|
|
|
|
id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
droplet := dropletRaw.(*digitalocean.Droplet)
|
|
|
|
|
2014-07-24 19:50:40 +02:00
|
|
|
// Initialize the connection info
|
2014-09-17 02:28:22 +02:00
|
|
|
rs.Ephemeral.ConnInfo["type"] = "ssh"
|
|
|
|
rs.Ephemeral.ConnInfo["host"] = droplet.IPV4Address("public")
|
2014-07-24 19:50:40 +02:00
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
return resource_digitalocean_droplet_update_state(rs, droplet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_digitalocean_droplet_update(
|
2014-09-17 02:28:22 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff,
|
2014-09-17 02:28:22 +02:00
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-19 18:33:25 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if attr, ok := d.Attributes["size"]; ok {
|
|
|
|
err = client.PowerOff(rs.ID)
|
|
|
|
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "Droplet is already powered off") {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for power off
|
|
|
|
_, err = WaitForDropletAttribute(
|
|
|
|
rs.ID, "off", []string{"active"}, "status", client)
|
|
|
|
|
|
|
|
err = client.Resize(rs.ID, attr.New)
|
|
|
|
|
|
|
|
if err != nil {
|
2014-08-01 16:10:52 +02:00
|
|
|
newErr := power_on_and_wait(rs.ID, client)
|
|
|
|
if newErr != nil {
|
|
|
|
return rs, newErr
|
|
|
|
}
|
2014-07-19 18:33:25 +02:00
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the size to change
|
|
|
|
_, err = WaitForDropletAttribute(
|
|
|
|
rs.ID, attr.New, []string{"", attr.Old}, "size", client)
|
|
|
|
|
|
|
|
if err != nil {
|
2014-08-01 16:10:52 +02:00
|
|
|
newErr := power_on_and_wait(rs.ID, client)
|
|
|
|
if newErr != nil {
|
|
|
|
return rs, newErr
|
|
|
|
}
|
2014-07-19 18:33:25 +02:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.PowerOn(rs.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for power off
|
|
|
|
_, err = WaitForDropletAttribute(
|
|
|
|
rs.ID, "active", []string{"off"}, "status", client)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.Attributes["name"]; ok {
|
|
|
|
err = client.Rename(rs.ID, attr.New)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the name to change
|
|
|
|
_, err = WaitForDropletAttribute(
|
|
|
|
rs.ID, attr.New, []string{"", attr.Old}, "name", client)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.Attributes["private_networking"]; ok {
|
|
|
|
err = client.Rename(rs.ID, attr.New)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the private_networking to turn on/off
|
|
|
|
_, err = WaitForDropletAttribute(
|
|
|
|
rs.ID, attr.New, []string{"", attr.Old}, "private_networking", client)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.Attributes["ipv6"]; ok {
|
|
|
|
err = client.Rename(rs.ID, attr.New)
|
2014-07-19 15:45:42 +02:00
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for ipv6 to turn on/off
|
|
|
|
_, err = WaitForDropletAttribute(
|
|
|
|
rs.ID, attr.New, []string{"", attr.Old}, "ipv6", client)
|
|
|
|
}
|
2014-07-19 15:45:42 +02:00
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
droplet, err := resource_digitalocean_droplet_retrieve(rs.ID, client)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
return resource_digitalocean_droplet_update_state(rs, droplet)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_digitalocean_droplet_destroy(
|
2014-09-17 02:28:22 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-19 02:20:28 +02:00
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting Droplet: %s", s.ID)
|
|
|
|
|
|
|
|
// Destroy the droplet
|
|
|
|
err := client.DestroyDroplet(s.ID)
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Error deleting Droplet: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_digitalocean_droplet_refresh(
|
2014-09-17 02:28:22 +02:00
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-19 02:20:28 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
client := p.client
|
|
|
|
|
|
|
|
droplet, err := resource_digitalocean_droplet_retrieve(s.ID, client)
|
2014-07-24 23:31:23 +02:00
|
|
|
|
|
|
|
// Handle remotely destroyed droplets
|
|
|
|
if err != nil && strings.Contains(err.Error(), "404 Not Found") {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource_digitalocean_droplet_update_state(s, droplet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_digitalocean_droplet_diff(
|
2014-09-17 02:28:22 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-19 02:20:28 +02:00
|
|
|
c *terraform.ResourceConfig,
|
2014-09-18 01:33:24 +02:00
|
|
|
meta interface{}) (*terraform.InstanceDiff, error) {
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
Attrs: map[string]diff.AttrType{
|
|
|
|
"backups": diff.AttrTypeUpdate,
|
2014-07-19 02:41:02 +02:00
|
|
|
"image": diff.AttrTypeCreate,
|
2014-07-19 02:20:28 +02:00
|
|
|
"ipv6": diff.AttrTypeUpdate,
|
2014-07-19 02:41:02 +02:00
|
|
|
"name": diff.AttrTypeUpdate,
|
2014-07-19 02:20:28 +02:00
|
|
|
"private_networking": diff.AttrTypeUpdate,
|
|
|
|
"region": diff.AttrTypeCreate,
|
2014-07-19 18:33:25 +02:00
|
|
|
"size": diff.AttrTypeUpdate,
|
2014-07-19 02:20:28 +02:00
|
|
|
"ssh_keys": diff.AttrTypeCreate,
|
2014-09-05 17:16:12 +02:00
|
|
|
"user_data": diff.AttrTypeCreate,
|
2014-07-19 02:20:28 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ComputedAttrs: []string{
|
2014-07-19 02:41:02 +02:00
|
|
|
"backups",
|
2014-07-19 02:20:28 +02:00
|
|
|
"ipv4_address",
|
2014-07-30 00:58:48 +02:00
|
|
|
"ipv4_address_private",
|
2014-07-19 02:41:02 +02:00
|
|
|
"ipv6",
|
2014-07-19 02:20:28 +02:00
|
|
|
"ipv6_address",
|
2014-07-30 00:58:48 +02:00
|
|
|
"ipv6_address_private",
|
2014-07-28 19:04:17 +02:00
|
|
|
"locked",
|
2014-07-19 02:20:28 +02:00
|
|
|
"private_networking",
|
2014-07-28 19:04:17 +02:00
|
|
|
"status",
|
2014-07-19 02:20:28 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_digitalocean_droplet_update_state(
|
2014-09-17 02:28:22 +02:00
|
|
|
s *terraform.InstanceState,
|
|
|
|
droplet *digitalocean.Droplet) (*terraform.InstanceState, error) {
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
s.Attributes["name"] = droplet.Name
|
|
|
|
s.Attributes["region"] = droplet.RegionSlug()
|
|
|
|
|
|
|
|
if droplet.ImageSlug() == "" && droplet.ImageId() != "" {
|
|
|
|
s.Attributes["image"] = droplet.ImageId()
|
|
|
|
} else {
|
|
|
|
s.Attributes["image"] = droplet.ImageSlug()
|
|
|
|
}
|
|
|
|
|
2014-07-30 00:58:48 +02:00
|
|
|
if droplet.IPV6Address("public") != "" {
|
2014-07-19 18:33:25 +02:00
|
|
|
s.Attributes["ipv6"] = "true"
|
2014-07-30 00:58:48 +02:00
|
|
|
s.Attributes["ipv6_address"] = droplet.IPV6Address("public")
|
|
|
|
s.Attributes["ipv6_address_private"] = droplet.IPV6Address("private")
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
2014-07-30 00:58:48 +02:00
|
|
|
s.Attributes["ipv4_address"] = droplet.IPV4Address("public")
|
2014-07-28 19:04:17 +02:00
|
|
|
s.Attributes["locked"] = droplet.IsLocked()
|
2014-07-30 00:58:48 +02:00
|
|
|
|
|
|
|
if droplet.NetworkingType() == "private" {
|
|
|
|
s.Attributes["private_networking"] = "true"
|
2014-08-01 16:12:29 +02:00
|
|
|
s.Attributes["ipv4_address_private"] = droplet.IPV4Address("private")
|
2014-07-30 00:58:48 +02:00
|
|
|
}
|
|
|
|
|
2014-07-19 02:41:02 +02:00
|
|
|
s.Attributes["size"] = droplet.SizeSlug()
|
2014-07-28 19:04:17 +02:00
|
|
|
s.Attributes["status"] = droplet.Status
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:19:56 +02:00
|
|
|
// retrieves an ELB by its ID
|
2014-07-19 02:20:28 +02:00
|
|
|
func resource_digitalocean_droplet_retrieve(id string, client *digitalocean.Client) (*digitalocean.Droplet, error) {
|
|
|
|
// Retrieve the ELB properties for updating the state
|
|
|
|
droplet, err := client.RetrieveDroplet(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving droplet: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &droplet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_digitalocean_droplet_validation() *config.Validator {
|
|
|
|
return &config.Validator{
|
|
|
|
Required: []string{
|
2014-07-19 02:41:02 +02:00
|
|
|
"image",
|
2014-07-19 02:20:28 +02:00
|
|
|
"name",
|
|
|
|
"region",
|
|
|
|
"size",
|
|
|
|
},
|
|
|
|
Optional: []string{
|
|
|
|
"backups",
|
2014-09-05 17:16:12 +02:00
|
|
|
"user_data",
|
2014-07-19 02:20:28 +02:00
|
|
|
"ipv6",
|
|
|
|
"private_networking",
|
2014-07-19 02:41:02 +02:00
|
|
|
"ssh_keys.*",
|
2014-07-19 02:20:28 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 15:45:42 +02:00
|
|
|
func WaitForDropletAttribute(id string, target string, pending []string, attribute string, client *digitalocean.Client) (interface{}, error) {
|
|
|
|
// Wait for the droplet so we can get the networking attributes
|
|
|
|
// that show up after a while
|
|
|
|
log.Printf(
|
2014-07-19 18:33:25 +02:00
|
|
|
"[INFO] Waiting for Droplet (%s) to have %s of %s",
|
2014-07-19 15:45:42 +02:00
|
|
|
id, attribute, target)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
2014-07-28 18:58:19 +02:00
|
|
|
Pending: pending,
|
|
|
|
Target: target,
|
|
|
|
Refresh: new_droplet_state_refresh_func(id, attribute, client),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
2014-07-19 15:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return stateConf.WaitForState()
|
|
|
|
}
|
|
|
|
|
|
|
|
func new_droplet_state_refresh_func(id string, attribute string, client *digitalocean.Client) resource.StateRefreshFunc {
|
2014-07-19 02:20:28 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2014-07-19 15:45:42 +02:00
|
|
|
// Retrieve the ELB properties for updating the state
|
|
|
|
droplet, err := client.RetrieveDroplet(id)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-07-19 15:45:42 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error on retrieving droplet when waiting: %s", err)
|
|
|
|
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
|
|
|
|
if droplet.IsLocked() == "true" {
|
|
|
|
log.Println("[DEBUG] Droplet is locked, skipping status check and retrying")
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
2014-07-19 15:45:42 +02:00
|
|
|
// Use our mapping to get back a map of the
|
|
|
|
// droplet properties
|
|
|
|
resourceMap, err := resource_digitalocean_droplet_update_state(
|
2014-09-17 02:28:22 +02:00
|
|
|
&terraform.InstanceState{Attributes: map[string]string{}}, &droplet)
|
2014-07-19 15:45:42 +02:00
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
if err != nil {
|
2014-07-19 15:45:42 +02:00
|
|
|
log.Printf("Error creating map from droplet: %s", err)
|
2014-07-19 02:20:28 +02:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2014-07-19 15:45:42 +02:00
|
|
|
// See if we can access our attribute
|
|
|
|
if attr, ok := resourceMap.Attributes[attribute]; ok {
|
|
|
|
return &droplet, attr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
func power_on_and_wait(id string, client *digitalocean.Client) error {
|
|
|
|
err := client.PowerOn(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-01 17:05:32 +02:00
|
|
|
// Wait for power on
|
2014-08-01 16:10:52 +02:00
|
|
|
_, err = WaitForDropletAttribute(
|
|
|
|
id, "active", []string{"off"}, "status", client)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|