0

Add Tags to a Firewall

by
Published Dec 20, 2024

To assign a tag representing a group of Droplets to a firewall, send a POST request to `/v2/firewalls/$FIREWALL_ID/tags`. In the body of the request, there should be a `tags` attribute containing a list of tag names. No response body will be sent back, but the response code will indicate success. Specifically, the response code will be a 204, which means that the action was successful with no returned body data.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Add Tags to a Firewall
7
 * To assign a tag representing a group of Droplets to a firewall, send a POST
8
request to `/v2/firewalls/$FIREWALL_ID/tags`. In the body of the request,
9
there should be a `tags` attribute containing a list of tag names.
10

11
No response body will be sent back, but the response code will indicate
12
success. Specifically, the response code will be a 204, which means that the
13
action was successful with no returned body data.
14

15
 */
16
export async function main(
17
  auth: Digitalocean,
18
  firewall_id: string,
19
  body: { tags: string[] & {} },
20
) {
21
  const url = new URL(
22
    `https://api.digitalocean.com/v2/firewalls/${firewall_id}/tags`,
23
  );
24

25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39