0

Initiate A Block Storage Action By Volume Name

by
Published Dec 20, 2024

To initiate an action on a block storage volume by Name, send a POST request to `~/v2/volumes/actions`.

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 Block Storage Action By Volume Name
7
 * To initiate an action on a block storage volume by Name, send a POST request to
8
`~/v2/volumes/actions`.
9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  per_page: string | undefined,
13
  page: string | undefined,
14
  body:
15
    | ({
16
        type: "attach" | "detach" | "resize";
17
        region?:
18
          | "ams1"
19
          | "ams2"
20
          | "ams3"
21
          | "blr1"
22
          | "fra1"
23
          | "lon1"
24
          | "nyc1"
25
          | "nyc2"
26
          | "nyc3"
27
          | "sfo1"
28
          | "sfo2"
29
          | "sfo3"
30
          | "sgp1"
31
          | "tor1"
32
          | "syd1";
33
      } & { droplet_id: number; tags?: string[] })
34
    | ({
35
        type: "attach" | "detach" | "resize";
36
        region?:
37
          | "ams1"
38
          | "ams2"
39
          | "ams3"
40
          | "blr1"
41
          | "fra1"
42
          | "lon1"
43
          | "nyc1"
44
          | "nyc2"
45
          | "nyc3"
46
          | "sfo1"
47
          | "sfo2"
48
          | "sfo3"
49
          | "sgp1"
50
          | "tor1"
51
          | "syd1";
52
      } & { droplet_id: number }),
53
) {
54
  const url = new URL(`https://api.digitalocean.com/v2/volumes/actions`);
55
  for (const [k, v] of [
56
    ["per_page", per_page],
57
    ["page", page],
58
  ]) {
59
    if (v !== undefined && v !== "" && k !== undefined) {
60
      url.searchParams.append(k, v);
61
    }
62
  }
63
  const response = await fetch(url, {
64
    method: "POST",
65
    headers: {
66
      "Content-Type": "application/json",
67
      Authorization: "Bearer " + auth.token,
68
    },
69
    body: JSON.stringify(body),
70
  });
71
  if (!response.ok) {
72
    const text = await response.text();
73
    throw new Error(`${response.status} ${text}`);
74
  }
75
  return await response.json();
76
}
77