0

Add Rules to a Firewall

by
Published Dec 20, 2024

To add additional access rules to a firewall, send a POST request to `/v2/firewalls/$FIREWALL_ID/rules`. The body of the request may include an inbound_rules and/or outbound_rules attribute containing an array of rules to be added. 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 Rules to a Firewall
7
 * To add additional access rules to a firewall, send a POST request to
8
`/v2/firewalls/$FIREWALL_ID/rules`. The body of the request may include an
9
inbound_rules and/or outbound_rules attribute containing an array of rules to
10
be added.
11

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

16
 */
17
export async function main(
18
  auth: Digitalocean,
19
  firewall_id: string,
20
  body:
21
    | ({
22
        inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
23
          sources: {
24
            addresses?: string[];
25
            droplet_ids?: number[];
26
            load_balancer_uids?: string[];
27
            kubernetes_ids?: string[];
28
            tags?: string[] & {};
29
          } & {};
30
        }[];
31
        outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
32
          destinations: {
33
            addresses?: string[];
34
            droplet_ids?: number[];
35
            load_balancer_uids?: string[];
36
            kubernetes_ids?: string[];
37
            tags?: string[] & {};
38
          } & {};
39
        }[];
40
      } & {})
41
    | ({
42
        inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
43
          sources: {
44
            addresses?: string[];
45
            droplet_ids?: number[];
46
            load_balancer_uids?: string[];
47
            kubernetes_ids?: string[];
48
            tags?: string[] & {};
49
          } & {};
50
        }[];
51
        outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
52
          destinations: {
53
            addresses?: string[];
54
            droplet_ids?: number[];
55
            load_balancer_uids?: string[];
56
            kubernetes_ids?: string[];
57
            tags?: string[] & {};
58
          } & {};
59
        }[];
60
      } & {}),
61
) {
62
  const url = new URL(
63
    `https://api.digitalocean.com/v2/firewalls/${firewall_id}/rules`,
64
  );
65

66
  const response = await fetch(url, {
67
    method: "POST",
68
    headers: {
69
      "Content-Type": "application/json",
70
      Authorization: "Bearer " + auth.token,
71
    },
72
    body: JSON.stringify(body),
73
  });
74
  if (!response.ok) {
75
    const text = await response.text();
76
    throw new Error(`${response.status} ${text}`);
77
  }
78
  return await response.json();
79
}
80