2014-07-19 02:20:28 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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
|
|
|
"github.com/pearkes/digitalocean"
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDropletCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*digitalocean.Client)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
// Build up our creation options
|
2014-11-17 18:55:45 +01:00
|
|
|
opts := &digitalocean.CreateDroplet{
|
|
|
|
Image: d.Get("image").(string),
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
ssh_keys := d.Get("ssh_keys.#").(int)
|
|
|
|
if ssh_keys > 0 {
|
|
|
|
opts.SSHKeys = make([]string, 0, ssh_keys)
|
|
|
|
for i := 0; i < ssh_keys; i++ {
|
|
|
|
key := fmt.Sprintf("ssh_keys.%d", i)
|
|
|
|
opts.SSHKeys = append(opts.SSHKeys, d.Get(key).(string))
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Droplet create configuration: %#v", opts)
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
id, err := client.CreateDroplet(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
|
2014-11-17 18:55:45 +01:00
|
|
|
d.SetId(id)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
client := meta.(*digitalocean.Client)
|
|
|
|
|
|
|
|
// Retrieve the droplet properties for updating the state
|
|
|
|
droplet, err := client.RetrieveDroplet(d.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.
|
|
|
|
if err.Error() == "Error retrieving droplet: API Error: 404 Not Found" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
return fmt.Errorf("Error retrieving droplet: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:36:05 +01:00
|
|
|
if droplet.ImageSlug() != "" {
|
2014-11-17 18:55:45 +01:00
|
|
|
d.Set("image", droplet.ImageSlug())
|
2014-12-01 20:36:05 +01:00
|
|
|
} else {
|
|
|
|
d.Set("image", droplet.ImageId())
|
2014-11-17 18:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", droplet.Name)
|
|
|
|
d.Set("region", droplet.RegionSlug())
|
|
|
|
d.Set("size", droplet.SizeSlug)
|
|
|
|
d.Set("status", droplet.Status)
|
|
|
|
d.Set("locked", droplet.IsLocked())
|
|
|
|
|
|
|
|
if droplet.IPV6Address("public") != "" {
|
|
|
|
d.Set("ipv6", true)
|
|
|
|
d.Set("ipv6_address", droplet.IPV6Address("public"))
|
|
|
|
d.Set("ipv6_address_private", droplet.IPV6Address("private"))
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
d.Set("ipv4_address", droplet.IPV4Address("public"))
|
|
|
|
|
|
|
|
if droplet.NetworkingType() == "private" {
|
|
|
|
d.Set("private_networking", true)
|
|
|
|
d.Set("ipv4_address_private", droplet.IPV4Address("private"))
|
|
|
|
}
|
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",
|
|
|
|
"host": droplet.IPV4Address("public"),
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDropletUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*digitalocean.Client)
|
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
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
err := client.PowerOff(d.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
|
|
|
|
err = client.Resize(d.Id(), newSize.(string))
|
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
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
err = client.PowerOn(d.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
|
|
|
|
err := client.Rename(d.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) {
|
|
|
|
err := client.EnablePrivateNetworking(d.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) {
|
|
|
|
err := client.EnableIPV6s(d.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 {
|
|
|
|
client := meta.(*digitalocean.Client)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2015-02-26 01:54:39 +01:00
|
|
|
_, err := WaitForDropletAttribute(
|
|
|
|
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-02-26 01:54:39 +01:00
|
|
|
err = client.DestroyDroplet(d.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 {
|
|
|
|
client := meta.(*digitalocean.Client)
|
2014-07-19 02:20:28 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2014-11-17 18:55:45 +01:00
|
|
|
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
|
|
|
|
droplet, err := client.RetrieveDroplet(d.Id())
|
|
|
|
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 {
|
2014-11-17 18:55:45 +01:00
|
|
|
client := meta.(*digitalocean.Client)
|
|
|
|
err := client.PowerOn(d.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
|
|
|
|
}
|