From e6f9f50183d45fc2ec37d1e3c285262e1981f3c8 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Fri, 1 Jan 2016 10:02:46 +0100 Subject: [PATCH 1/3] Do not force the creation of a new IP when the droplet_id changes --- .../providers/digitalocean/resource_digitalocean_floating_ip.go | 1 - 1 file changed, 1 deletion(-) diff --git a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go b/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go index 06a3760f8d58..24ea37152af8 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go +++ b/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go @@ -32,7 +32,6 @@ func resourceDigitalOceanFloatingIp() *schema.Resource { "droplet_id": &schema.Schema{ Type: schema.TypeInt, Optional: true, - ForceNew: true, }, }, } From b629b4dbd227956fec136f0647d9d76b80d5c2d8 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Fri, 1 Jan 2016 10:24:43 +0100 Subject: [PATCH 2/3] Add update floating ip action --- .../resource_digitalocean_floating_ip.go | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go b/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go index 24ea37152af8..bda7938fdfe3 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go +++ b/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go @@ -13,6 +13,7 @@ import ( func resourceDigitalOceanFloatingIp() *schema.Resource { return &schema.Resource{ Create: resourceDigitalOceanFloatingIpCreate, + Update: resourceDigitalOceanFloatingIpUpdate, Read: resourceDigitalOceanFloatingIpRead, Delete: resourceDigitalOceanFloatingIpDelete, @@ -72,6 +73,40 @@ func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface return resourceDigitalOceanFloatingIpRead(d, meta) } +func resourceDigitalOceanFloatingIpUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*godo.Client) + + 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) +} + func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*godo.Client) From bf8eb753accb444c01e661acc277eefb2bd4dd46 Mon Sep 17 00:00:00 2001 From: Makkhdyn Date: Wed, 20 Jan 2016 04:05:43 +1100 Subject: [PATCH 3/3] Only update floating IP when droplet_id changes --- .../resource_digitalocean_floating_ip.go | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go b/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go index bda7938fdfe3..34feea29862c 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go +++ b/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go @@ -76,31 +76,33 @@ func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface func resourceDigitalOceanFloatingIpUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*godo.Client) - 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) + 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) + } } }