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