@@ -25,8 +25,7 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
2525
2626 "region" : & schema.Schema {
2727 Type : schema .TypeString ,
28- Optional : true ,
29- Computed : true ,
28+ Required : true ,
3029 ForceNew : true ,
3130 },
3231
@@ -42,33 +41,41 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
4241func resourceDigitalOceanFloatingIpCreate (d * schema.ResourceData , meta interface {}) error {
4342 client := meta .(* godo.Client )
4443
45- // Build up our creation options
46- opts := & godo.FloatingIPCreateRequest {}
47-
48- if v , ok := d .GetOk ("region" ); ok {
49- log .Printf ("[INFO] Create a FloatingIP for a region" )
50- opts .Region = v .(string )
51- }
52-
53- if v , ok := d .GetOk ("droplet_id" ); ok {
54- log .Printf ("[INFO] Found a droplet_id to try and attach to the FloatingIP" )
55- opts .DropletID = v .(int )
44+ log .Printf ("[INFO] Create a FloatingIP In a Region" )
45+ regionOpts := & godo.FloatingIPCreateRequest {
46+ Region : d .Get ("region" ).(string ),
5647 }
5748
58- log .Printf ("[DEBUG] FloatingIP Create: %#v" , opts )
59- floatingIp , _ , err := client .FloatingIPs .Create (opts )
49+ log .Printf ("[DEBUG] FloatingIP Create: %#v" , regionOpts )
50+ floatingIp , _ , err := client .FloatingIPs .Create (regionOpts )
6051 if err != nil {
6152 return fmt .Errorf ("Error creating FloatingIP: %s" , err )
6253 }
54+
6355 d .SetId (floatingIp .IP )
6456
57+ if v , ok := d .GetOk ("droplet_id" ); ok {
58+
59+ log .Printf ("[INFO] Assigning the Floating IP to the Droplet %s" , v .(int ))
60+ action , _ , err := client .FloatingIPActions .Assign (d .Id (), v .(int ))
61+ if err != nil {
62+ return fmt .Errorf (
63+ "Error Assigning FloatingIP (%s) to the droplet: %s" , d .Id (), err )
64+ }
65+
66+ _ , unassignedErr := waitForFloatingIPReady (d , "completed" , []string {"new" , "in-progress" }, "status" , meta , action .ID )
67+ if unassignedErr != nil {
68+ return fmt .Errorf (
69+ "Error waiting for FloatingIP (%s) to be Assigned: %s" , d .Id (), unassignedErr )
70+ }
71+ }
72+
6573 return resourceDigitalOceanFloatingIpRead (d , meta )
6674}
6775
6876func resourceDigitalOceanFloatingIpRead (d * schema.ResourceData , meta interface {}) error {
6977 client := meta .(* godo.Client )
7078
71- time .Sleep (7 * time .Second )
7279 log .Printf ("[INFO] Reading the details of the FloatingIP %s" , d .Id ())
7380 floatingIp , _ , err := client .FloatingIPs .Get (d .Id ())
7481 if err != nil {
@@ -98,7 +105,7 @@ func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface
98105 "Error Unassigning FloatingIP (%s) from the droplet: %s" , d .Id (), err )
99106 }
100107
101- _ , unassignedErr := waitForFloatingIPReady (d , "completed" , []string {"new" }, "status" , meta , action .ID )
108+ _ , unassignedErr := waitForFloatingIPReady (d , "completed" , []string {"new" , "in-progress" }, "status" , meta , action .ID )
102109 if unassignedErr != nil {
103110 return fmt .Errorf (
104111 "Error waiting for FloatingIP (%s) to be unassigned: %s" , d .Id (), unassignedErr )
@@ -116,15 +123,15 @@ func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface
116123}
117124
118125func waitForFloatingIPReady (
119- d * schema.ResourceData , target string , pending []string , attribute string , meta interface {}, action int ) (interface {}, error ) {
126+ d * schema.ResourceData , target string , pending []string , attribute string , meta interface {}, actionId int ) (interface {}, error ) {
120127 log .Printf (
121128 "[INFO] Waiting for FloatingIP (%s) to have %s of %s" ,
122129 d .Id (), attribute , target )
123130
124131 stateConf := & resource.StateChangeConf {
125132 Pending : pending ,
126133 Target : target ,
127- Refresh : newFloatingIPStateRefreshFunc (d , attribute , meta , action ),
134+ Refresh : newFloatingIPStateRefreshFunc (d , attribute , meta , actionId ),
128135 Timeout : 60 * time .Minute ,
129136 Delay : 10 * time .Second ,
130137 MinTimeout : 3 * time .Second ,
@@ -136,15 +143,17 @@ func waitForFloatingIPReady(
136143}
137144
138145func newFloatingIPStateRefreshFunc (
139- d * schema.ResourceData , attribute string , meta interface {}, action int ) resource.StateRefreshFunc {
146+ d * schema.ResourceData , attribute string , meta interface {}, actionId int ) resource.StateRefreshFunc {
140147 client := meta .(* godo.Client )
141148 return func () (interface {}, string , error ) {
142- floatingIP , _ , err := client .FloatingIPActions .Get (d .Id (), action )
149+
150+ log .Printf ("[INFO] Assigning the Floating IP to the Droplet" )
151+ action , _ , err := client .FloatingIPActions .Get (d .Id (), actionId )
143152 if err != nil {
144- return nil , "" , fmt .Errorf ("Error retrieving FloatingIP Action : %s" , err )
153+ return nil , "" , fmt .Errorf ("Error retrieving FloatingIP (%s) ActionId (%d) : %s" , d . Id (), actionId , err )
145154 }
146155
147- log .Printf ("[INFO] The FloatingIP Assigned Status is %s" , floatingIP .Status )
148- return & floatingIP , floatingIP .Status , nil
156+ log .Printf ("[INFO] The FloatingIP Action Status is %s" , action .Status )
157+ return & action , action .Status , nil
149158 }
150159}
0 commit comments