Skip to content

Commit 3fc9107

Browse files
committed
Using the laterst version of the DO API to get the Assign of an IP working without a time.Sleep
1 parent 74c93d3 commit 3fc9107

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

builtin/providers/digitalocean/resource_digitalocean_floating_ip.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
4241
func 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

6876
func 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

118125
func 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

138145
func 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
}

builtin/providers/digitalocean/resource_digitalocean_floating_ip_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ resource "digitalocean_droplet" "foobar" {
117117
118118
resource "digitalocean_floating_ip" "foobar" {
119119
droplet_id = "${digitalocean_droplet.foobar.id}"
120+
region = "${digitalocean_droplet.foobar.region}"
120121
}`

website/source/docs/providers/do/r/floating_ip.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ resource "digitalocean_droplet" "foobar" {
2424
2525
resource "digitalocean_floating_ip" "foobar" {
2626
droplet_id = "${digitalocean_droplet.foobar.id}"
27+
region = "${digitalocean_droplet.foobar.region}"
2728
}
2829
```
2930

3031
## Argument Reference
3132

3233
The following arguments are supported:
3334

34-
* `region` - (Optional) The region that the Floating IP is reserved to.
35+
* `region` - (Required) The region that the Floating IP is reserved to.
3536
* `droplet_id` - (Optional) The ID of Droplet that the Floating IP will be assigned to.
3637

3738
~> **NOTE:** A Floating IP can be assigned to a region OR a droplet_id. If both region AND droplet_id are specified, then the Floating IP will be assigned to the droplet and use that region

0 commit comments

Comments
 (0)