mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Using the laterst version of the DO API to get the Assign of an IP working without a time.Sleep
This commit is contained in:
parent
74c93d3a46
commit
3fc9107333
@ -25,8 +25,7 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
|
||||
|
||||
"region": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
@ -42,33 +41,41 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
|
||||
func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*godo.Client)
|
||||
|
||||
// Build up our creation options
|
||||
opts := &godo.FloatingIPCreateRequest{}
|
||||
|
||||
if v, ok := d.GetOk("region"); ok {
|
||||
log.Printf("[INFO] Create a FloatingIP for a region")
|
||||
opts.Region = v.(string)
|
||||
log.Printf("[INFO] Create a FloatingIP In a Region")
|
||||
regionOpts := &godo.FloatingIPCreateRequest{
|
||||
Region: d.Get("region").(string),
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("droplet_id"); ok {
|
||||
log.Printf("[INFO] Found a droplet_id to try and attach to the FloatingIP")
|
||||
opts.DropletID = v.(int)
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] FloatingIP Create: %#v", opts)
|
||||
floatingIp, _, err := client.FloatingIPs.Create(opts)
|
||||
log.Printf("[DEBUG] FloatingIP Create: %#v", regionOpts)
|
||||
floatingIp, _, err := client.FloatingIPs.Create(regionOpts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating FloatingIP: %s", err)
|
||||
}
|
||||
|
||||
d.SetId(floatingIp.IP)
|
||||
|
||||
if v, ok := d.GetOk("droplet_id"); ok {
|
||||
|
||||
log.Printf("[INFO] Assigning the Floating IP to the Droplet %s", 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)
|
||||
}
|
||||
}
|
||||
|
||||
return resourceDigitalOceanFloatingIpRead(d, meta)
|
||||
}
|
||||
|
||||
func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*godo.Client)
|
||||
|
||||
time.Sleep(7 * time.Second)
|
||||
log.Printf("[INFO] Reading the details of the FloatingIP %s", d.Id())
|
||||
floatingIp, _, err := client.FloatingIPs.Get(d.Id())
|
||||
if err != nil {
|
||||
@ -98,7 +105,7 @@ func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface
|
||||
"Error Unassigning FloatingIP (%s) from the droplet: %s", d.Id(), err)
|
||||
}
|
||||
|
||||
_, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new"}, "status", meta, action.ID)
|
||||
_, 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)
|
||||
@ -116,7 +123,7 @@ func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface
|
||||
}
|
||||
|
||||
func waitForFloatingIPReady(
|
||||
d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}, action int) (interface{}, error) {
|
||||
d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}, actionId int) (interface{}, error) {
|
||||
log.Printf(
|
||||
"[INFO] Waiting for FloatingIP (%s) to have %s of %s",
|
||||
d.Id(), attribute, target)
|
||||
@ -124,7 +131,7 @@ func waitForFloatingIPReady(
|
||||
stateConf := &resource.StateChangeConf{
|
||||
Pending: pending,
|
||||
Target: target,
|
||||
Refresh: newFloatingIPStateRefreshFunc(d, attribute, meta, action),
|
||||
Refresh: newFloatingIPStateRefreshFunc(d, attribute, meta, actionId),
|
||||
Timeout: 60 * time.Minute,
|
||||
Delay: 10 * time.Second,
|
||||
MinTimeout: 3 * time.Second,
|
||||
@ -136,15 +143,17 @@ func waitForFloatingIPReady(
|
||||
}
|
||||
|
||||
func newFloatingIPStateRefreshFunc(
|
||||
d *schema.ResourceData, attribute string, meta interface{}, action int) resource.StateRefreshFunc {
|
||||
d *schema.ResourceData, attribute string, meta interface{}, actionId int) resource.StateRefreshFunc {
|
||||
client := meta.(*godo.Client)
|
||||
return func() (interface{}, string, error) {
|
||||
floatingIP, _, err := client.FloatingIPActions.Get(d.Id(), action)
|
||||
|
||||
log.Printf("[INFO] Assigning the Floating IP to the Droplet")
|
||||
action, _, err := client.FloatingIPActions.Get(d.Id(), actionId)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("Error retrieving FloatingIP Action: %s", err)
|
||||
return nil, "", fmt.Errorf("Error retrieving FloatingIP (%s) ActionId (%d): %s", d.Id(), actionId, err)
|
||||
}
|
||||
|
||||
log.Printf("[INFO] The FloatingIP Assigned Status is %s", floatingIP.Status)
|
||||
return &floatingIP, floatingIP.Status, nil
|
||||
log.Printf("[INFO] The FloatingIP Action Status is %s", action.Status)
|
||||
return &action, action.Status, nil
|
||||
}
|
||||
}
|
||||
|
@ -117,4 +117,5 @@ resource "digitalocean_droplet" "foobar" {
|
||||
|
||||
resource "digitalocean_floating_ip" "foobar" {
|
||||
droplet_id = "${digitalocean_droplet.foobar.id}"
|
||||
region = "${digitalocean_droplet.foobar.region}"
|
||||
}`
|
||||
|
@ -24,6 +24,7 @@ resource "digitalocean_droplet" "foobar" {
|
||||
|
||||
resource "digitalocean_floating_ip" "foobar" {
|
||||
droplet_id = "${digitalocean_droplet.foobar.id}"
|
||||
region = "${digitalocean_droplet.foobar.region}"
|
||||
}
|
||||
```
|
||||
|
||||
@ -31,7 +32,7 @@ resource "digitalocean_floating_ip" "foobar" {
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `region` - (Optional) The region that the Floating IP is reserved to.
|
||||
* `region` - (Required) The region that the Floating IP is reserved to.
|
||||
* `droplet_id` - (Optional) The ID of Droplet that the Floating IP will be assigned to.
|
||||
|
||||
~> **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
|
||||
|
Loading…
Reference in New Issue
Block a user