0

List All VPC Peerings

by
Published Dec 20, 2024

To list all of the VPC peerings on your account, send a GET request to `/v2/vpc_peerings`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * List All VPC Peerings
7
 * To list all of the VPC peerings on your account, send a GET request to `/v2/vpc_peerings`.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  per_page: string | undefined,
12
  page: string | undefined,
13
  region:
14
    | "ams1"
15
    | "ams2"
16
    | "ams3"
17
    | "blr1"
18
    | "fra1"
19
    | "lon1"
20
    | "nyc1"
21
    | "nyc2"
22
    | "nyc3"
23
    | "sfo1"
24
    | "sfo2"
25
    | "sfo3"
26
    | "sgp1"
27
    | "tor1"
28
    | "syd1"
29
    | undefined,
30
) {
31
  const url = new URL(`https://api.digitalocean.com/v2/vpc_peerings`);
32
  for (const [k, v] of [
33
    ["per_page", per_page],
34
    ["page", page],
35
    ["region", region],
36
  ]) {
37
    if (v !== undefined && v !== "" && k !== undefined) {
38
      url.searchParams.append(k, v);
39
    }
40
  }
41
  const response = await fetch(url, {
42
    method: "GET",
43
    headers: {
44
      Authorization: "Bearer " + auth.token,
45
    },
46
    body: undefined,
47
  });
48
  if (!response.ok) {
49
    const text = await response.text();
50
    throw new Error(`${response.status} ${text}`);
51
  }
52
  return await response.json();
53
}
54