2015-11-04 11:38:26 +01:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-11-04 16:32:26 +01:00
|
|
|
"time"
|
2015-11-04 11:38:26 +01:00
|
|
|
|
|
|
|
"github.com/digitalocean/godo"
|
2015-11-04 16:32:26 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-11-04 11:38:26 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceDigitalOceanFloatingIp() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceDigitalOceanFloatingIpCreate,
|
2016-01-01 10:02:46 +01:00
|
|
|
Update: resourceDigitalOceanFloatingIpUpdate,
|
2015-11-04 11:38:26 +01:00
|
|
|
Read: resourceDigitalOceanFloatingIpRead,
|
|
|
|
Delete: resourceDigitalOceanFloatingIpDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-11-04 12:37:33 +01:00
|
|
|
"ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-11-04 11:38:26 +01:00
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-11-23 13:50:35 +01:00
|
|
|
Required: true,
|
2015-11-04 11:38:26 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-11-04 12:37:33 +01:00
|
|
|
"droplet_id": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2015-11-04 11:38:26 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
2015-11-23 13:50:35 +01:00
|
|
|
log.Printf("[INFO] Create a FloatingIP In a Region")
|
|
|
|
regionOpts := &godo.FloatingIPCreateRequest{
|
|
|
|
Region: d.Get("region").(string),
|
2015-11-04 11:38:26 +01:00
|
|
|
}
|
|
|
|
|
2015-11-23 13:50:35 +01:00
|
|
|
log.Printf("[DEBUG] FloatingIP Create: %#v", regionOpts)
|
|
|
|
floatingIp, _, err := client.FloatingIPs.Create(regionOpts)
|
2015-11-04 11:38:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating FloatingIP: %s", err)
|
|
|
|
}
|
2015-11-23 13:50:35 +01:00
|
|
|
|
2015-11-04 11:38:26 +01:00
|
|
|
d.SetId(floatingIp.IP)
|
|
|
|
|
2015-11-23 13:50:35 +01:00
|
|
|
if v, ok := d.GetOk("droplet_id"); ok {
|
|
|
|
|
2015-11-25 08:59:46 +01:00
|
|
|
log.Printf("[INFO] Assigning the Floating IP to the Droplet %d", v.(int))
|
2015-11-23 13:50:35 +01:00
|
|
|
action, _, err := client.FloatingIPActions.Assign(d.Id(), v.(int))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error Assigning FloatingIP (%s) to the droplet: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID)
|
|
|
|
if unassignedErr != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for FloatingIP (%s) to be Assigned: %s", d.Id(), unassignedErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 11:38:26 +01:00
|
|
|
return resourceDigitalOceanFloatingIpRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2016-01-01 10:02:46 +01:00
|
|
|
func resourceDigitalOceanFloatingIpUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
if d.HasChange("droplet_id") {
|
|
|
|
if v, ok := d.GetOk("droplet_id"); ok {
|
|
|
|
log.Printf("[INFO] Assigning the Floating IP %s to the Droplet %d", d.Id(), v.(int))
|
|
|
|
action, _, err := client.FloatingIPActions.Assign(d.Id(), v.(int))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error Assigning FloatingIP (%s) to the droplet: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID)
|
|
|
|
if unassignedErr != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for FloatingIP (%s) to be Assigned: %s", d.Id(), unassignedErr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[INFO] Unassigning the Floating IP %s", d.Id())
|
|
|
|
action, _, err := client.FloatingIPActions.Unassign(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error Unassigning FloatingIP (%s): %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID)
|
|
|
|
if unassignedErr != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for FloatingIP (%s) to be Unassigned: %s", d.Id(), unassignedErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceDigitalOceanFloatingIpRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-11-04 11:38:26 +01:00
|
|
|
func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
2015-11-04 16:32:26 +01:00
|
|
|
log.Printf("[INFO] Reading the details of the FloatingIP %s", d.Id())
|
2015-11-04 11:38:26 +01:00
|
|
|
floatingIp, _, err := client.FloatingIPs.Get(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving FloatingIP: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-12-08 17:52:41 +01:00
|
|
|
if floatingIp.Droplet != nil {
|
|
|
|
log.Printf("[INFO] A droplet was detected on the FloatingIP so setting the Region based on the Droplet")
|
|
|
|
log.Printf("[INFO] The region of the Droplet is %s", floatingIp.Droplet.Region.Slug)
|
|
|
|
d.Set("region", floatingIp.Droplet.Region.Slug)
|
2015-11-04 16:32:26 +01:00
|
|
|
} else {
|
|
|
|
d.Set("region", floatingIp.Region.Slug)
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:37:33 +01:00
|
|
|
d.Set("ip_address", floatingIp.IP)
|
2015-11-04 11:38:26 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
2015-11-04 16:32:26 +01:00
|
|
|
if _, ok := d.GetOk("droplet_id"); ok {
|
|
|
|
log.Printf("[INFO] Unassigning the Floating IP from the Droplet")
|
|
|
|
action, _, err := client.FloatingIPActions.Unassign(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error Unassigning FloatingIP (%s) from the droplet: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
2015-11-23 13:50:35 +01:00
|
|
|
_, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID)
|
2015-11-04 16:32:26 +01:00
|
|
|
if unassignedErr != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for FloatingIP (%s) to be unassigned: %s", d.Id(), unassignedErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 11:38:26 +01:00
|
|
|
log.Printf("[INFO] Deleting FloatingIP: %s", d.Id())
|
|
|
|
_, err := client.FloatingIPs.Delete(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting FloatingIP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-04 16:32:26 +01:00
|
|
|
|
|
|
|
func waitForFloatingIPReady(
|
2015-11-23 13:50:35 +01:00
|
|
|
d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}, actionId int) (interface{}, error) {
|
2015-11-04 16:32:26 +01:00
|
|
|
log.Printf(
|
|
|
|
"[INFO] Waiting for FloatingIP (%s) to have %s of %s",
|
|
|
|
d.Id(), attribute, target)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: pending,
|
|
|
|
Target: target,
|
2015-11-23 13:50:35 +01:00
|
|
|
Refresh: newFloatingIPStateRefreshFunc(d, attribute, meta, actionId),
|
2015-11-04 16:32:26 +01:00
|
|
|
Timeout: 60 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
|
|
|
|
NotFoundChecks: 60,
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateConf.WaitForState()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFloatingIPStateRefreshFunc(
|
2015-11-23 13:50:35 +01:00
|
|
|
d *schema.ResourceData, attribute string, meta interface{}, actionId int) resource.StateRefreshFunc {
|
2015-11-04 16:32:26 +01:00
|
|
|
client := meta.(*godo.Client)
|
|
|
|
return func() (interface{}, string, error) {
|
2015-11-23 13:50:35 +01:00
|
|
|
|
|
|
|
log.Printf("[INFO] Assigning the Floating IP to the Droplet")
|
|
|
|
action, _, err := client.FloatingIPActions.Get(d.Id(), actionId)
|
2015-11-04 16:32:26 +01:00
|
|
|
if err != nil {
|
2015-11-23 13:50:35 +01:00
|
|
|
return nil, "", fmt.Errorf("Error retrieving FloatingIP (%s) ActionId (%d): %s", d.Id(), actionId, err)
|
2015-11-04 16:32:26 +01:00
|
|
|
}
|
|
|
|
|
2015-11-23 13:50:35 +01:00
|
|
|
log.Printf("[INFO] The FloatingIP Action Status is %s", action.Status)
|
|
|
|
return &action, action.Status, nil
|
2015-11-04 16:32:26 +01:00
|
|
|
}
|
|
|
|
}
|