Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit f6d80f2

Browse files
ColinHebertjen20
authored andcommitted
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.
1 parent 587ce29 commit f6d80f2

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

builtin/providers/digitalocean/resource_digitalocean_floating_ip.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func resourceDigitalOceanFloatingIp() *schema.Resource {
1414
return &schema.Resource{
1515
Create: resourceDigitalOceanFloatingIpCreate,
16+
Update: resourceDigitalOceanFloatingIpUpdate,
1617
Read: resourceDigitalOceanFloatingIpRead,
1718
Delete: resourceDigitalOceanFloatingIpDelete,
1819

@@ -32,7 +33,6 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
3233
"droplet_id": &schema.Schema{
3334
Type: schema.TypeInt,
3435
Optional: true,
35-
ForceNew: true,
3636
},
3737
},
3838
}
@@ -73,6 +73,42 @@ func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface
7373
return resourceDigitalOceanFloatingIpRead(d, meta)
7474
}
7575

76+
func resourceDigitalOceanFloatingIpUpdate(d *schema.ResourceData, meta interface{}) error {
77+
client := meta.(*godo.Client)
78+
79+
if d.HasChange("droplet_id") {
80+
if v, ok := d.GetOk("droplet_id"); ok {
81+
log.Printf("[INFO] Assigning the Floating IP %s to the Droplet %d", d.Id(), v.(int))
82+
action, _, err := client.FloatingIPActions.Assign(d.Id(), v.(int))
83+
if err != nil {
84+
return fmt.Errorf(
85+
"Error Assigning FloatingIP (%s) to the droplet: %s", d.Id(), err)
86+
}
87+
88+
_, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID)
89+
if unassignedErr != nil {
90+
return fmt.Errorf(
91+
"Error waiting for FloatingIP (%s) to be Assigned: %s", d.Id(), unassignedErr)
92+
}
93+
} else {
94+
log.Printf("[INFO] Unassigning the Floating IP %s", d.Id())
95+
action, _, err := client.FloatingIPActions.Unassign(d.Id())
96+
if err != nil {
97+
return fmt.Errorf(
98+
"Error Unassigning FloatingIP (%s): %s", d.Id(), err)
99+
}
100+
101+
_, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID)
102+
if unassignedErr != nil {
103+
return fmt.Errorf(
104+
"Error waiting for FloatingIP (%s) to be Unassigned: %s", d.Id(), unassignedErr)
105+
}
106+
}
107+
}
108+
109+
return resourceDigitalOceanFloatingIpRead(d, meta)
110+
}
111+
76112
func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error {
77113
client := meta.(*godo.Client)
78114

0 commit comments

Comments
 (0)