0

Initiate a Floating IP Action

by
Published Dec 20, 2024

To initiate an action on a floating IP send a POST request to `/v2/floating_ips/$FLOATING_IP/actions`. In the JSON body to the request, set the `type` attribute to on of the supported action types: | Action | Details |------------|-------- | `assign` | Assigns a floating IP to a Droplet | `unassign` | Unassign a floating IP from a Droplet

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Initiate a Floating IP Action
7
 * To initiate an action on a floating IP send a POST request to
8
`/v2/floating_ips/$FLOATING_IP/actions`. In the JSON body to the request,
9
set the `type` attribute to on of the supported action types:
10

11
| Action     | Details
12
|------------|--------
13
| `assign`   | Assigns a floating IP to a Droplet
14
| `unassign` | Unassign a floating IP from a Droplet
15

16
 */
17
export async function main(
18
  auth: Digitalocean,
19
  floating_ip: string,
20
  body:
21
    | ({ type: "assign" | "unassign" } & {})
22
    | ({ type: "assign" | "unassign" } & { droplet_id: number }),
23
) {
24
  const url = new URL(
25
    `https://api.digitalocean.com/v2/floating_ips/${floating_ip}/actions`,
26
  );
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      "Content-Type": "application/json",
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: JSON.stringify(body),
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42