mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-10 23:55:34 -06:00
provider/digitalocean: allow reassignment of floating IPs
Do not force the creation of a new IP when the droplet_id changes, and unassignment of the the floating IP without destroying it.
This commit is contained in:
parent
587ce292f0
commit
f6d80f2215
@ -13,6 +13,7 @@ import (
|
||||
func resourceDigitalOceanFloatingIp() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceDigitalOceanFloatingIpCreate,
|
||||
Update: resourceDigitalOceanFloatingIpUpdate,
|
||||
Read: resourceDigitalOceanFloatingIpRead,
|
||||
Delete: resourceDigitalOceanFloatingIpDelete,
|
||||
|
||||
@ -32,7 +33,6 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
|
||||
"droplet_id": &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -73,6 +73,42 @@ func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface
|
||||
return resourceDigitalOceanFloatingIpRead(d, meta)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*godo.Client)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user