0

Acting on Tagged Droplets

by
Published Dec 20, 2024

Some actions can be performed in bulk on tagged Droplets. The actions can be initiated by sending a POST to `/v2/droplets/actions?tag_name=$TAG_NAME` with the action arguments. Only a sub-set of action types are supported: - `power_cycle` - `power_on` - `power_off` - `shutdown` - `enable_ipv6` - `enable_backups` - `disable_backups` - `snapshot`

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Acting on Tagged Droplets
7
 * Some actions can be performed in bulk on tagged Droplets. The actions can be
8
initiated by sending a POST to `/v2/droplets/actions?tag_name=$TAG_NAME` with
9
the action arguments.
10

11
Only a sub-set of action types are supported:
12

13
- `power_cycle`
14
- `power_on`
15
- `power_off`
16
- `shutdown`
17
- `enable_ipv6`
18
- `enable_backups`
19
- `disable_backups`
20
- `snapshot`
21

22
 */
23
export async function main(
24
  auth: Digitalocean,
25
  tag_name: string | undefined,
26
  body:
27
    | {
28
        type:
29
          | "enable_backups"
30
          | "disable_backups"
31
          | "reboot"
32
          | "power_cycle"
33
          | "shutdown"
34
          | "power_off"
35
          | "power_on"
36
          | "restore"
37
          | "password_reset"
38
          | "resize"
39
          | "rebuild"
40
          | "rename"
41
          | "change_kernel"
42
          | "enable_ipv6"
43
          | "snapshot";
44
      }
45
    | ({
46
        type:
47
          | "enable_backups"
48
          | "disable_backups"
49
          | "reboot"
50
          | "power_cycle"
51
          | "shutdown"
52
          | "power_off"
53
          | "power_on"
54
          | "restore"
55
          | "password_reset"
56
          | "resize"
57
          | "rebuild"
58
          | "rename"
59
          | "change_kernel"
60
          | "enable_ipv6"
61
          | "snapshot";
62
      } & { name?: string }),
63
) {
64
  const url = new URL(`https://api.digitalocean.com/v2/droplets/actions`);
65
  for (const [k, v] of [["tag_name", tag_name]]) {
66
    if (v !== undefined && v !== "" && k !== undefined) {
67
      url.searchParams.append(k, v);
68
    }
69
  }
70
  const response = await fetch(url, {
71
    method: "POST",
72
    headers: {
73
      "Content-Type": "application/json",
74
      Authorization: "Bearer " + auth.token,
75
    },
76
    body: JSON.stringify(body),
77
  });
78
  if (!response.ok) {
79
    const text = await response.text();
80
    throw new Error(`${response.status} ${text}`);
81
  }
82
  return await response.json();
83
}
84