@@ -13,6 +13,7 @@ import (
1313func 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+
76112func resourceDigitalOceanFloatingIpRead (d * schema.ResourceData , meta interface {}) error {
77113 client := meta .(* godo.Client )
78114
0 commit comments