0

List the Member Resources of a VPC

by
Published Dec 20, 2024

To list all of the resources that are members of a VPC, send a GET request to `/v2/vpcs/$VPC_ID/members`. To only list resources of a specific type that are members of the VPC, included a `resource_type` query parameter. For example, to only list Droplets in the VPC, send a GET request to `/v2/vpcs/$VPC_ID/members?resource_type=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
 * List the Member Resources of a VPC
7
 * To list all of the resources that are members of a VPC, send a GET request to
8
`/v2/vpcs/$VPC_ID/members`.
9

10
To only list resources of a specific type that are members of the VPC,
11
included a `resource_type` query parameter. For example, to only list Droplets
12
in the VPC, send a GET request to `/v2/vpcs/$VPC_ID/members?resource_type=droplet`.
13

14
 */
15
export async function main(
16
  auth: Digitalocean,
17
  vpc_id: string,
18
  resource_type: string | undefined,
19
  per_page: string | undefined,
20
  page: string | undefined,
21
) {
22
  const url = new URL(`https://api.digitalocean.com/v2/vpcs/${vpc_id}/members`);
23
  for (const [k, v] of [
24
    ["resource_type", resource_type],
25
    ["per_page", per_page],
26
    ["page", page],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45