0

Create a New Firewall

by
Published Dec 20, 2024

To create a new firewall, send a POST request to `/v2/firewalls`. The request must contain at least one inbound or outbound access rule.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a New Firewall
7
 * To create a new firewall, send a POST request to `/v2/firewalls`. The request
8
must contain at least one inbound or outbound access rule.
9

10
 */
11
export async function main(
12
  auth: Digitalocean,
13
  body:
14
    | ({
15
        id?: string;
16
        status?: "waiting" | "succeeded" | "failed";
17
        created_at?: string;
18
        pending_changes?: {
19
          droplet_id?: number;
20
          removing?: false | true;
21
          status?: string;
22
        }[];
23
        name?: string;
24
        droplet_ids?: number[];
25
        tags?: string[] & {};
26
      } & {
27
        inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
28
          sources: {
29
            addresses?: string[];
30
            droplet_ids?: number[];
31
            load_balancer_uids?: string[];
32
            kubernetes_ids?: string[];
33
            tags?: string[] & {};
34
          } & {};
35
        }[];
36
        outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
37
          destinations: {
38
            addresses?: string[];
39
            droplet_ids?: number[];
40
            load_balancer_uids?: string[];
41
            kubernetes_ids?: string[];
42
            tags?: string[] & {};
43
          } & {};
44
        }[];
45
      } & {} & {})
46
    | ({
47
        id?: string;
48
        status?: "waiting" | "succeeded" | "failed";
49
        created_at?: string;
50
        pending_changes?: {
51
          droplet_id?: number;
52
          removing?: false | true;
53
          status?: string;
54
        }[];
55
        name?: string;
56
        droplet_ids?: number[];
57
        tags?: string[] & {};
58
      } & {
59
        inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
60
          sources: {
61
            addresses?: string[];
62
            droplet_ids?: number[];
63
            load_balancer_uids?: string[];
64
            kubernetes_ids?: string[];
65
            tags?: string[] & {};
66
          } & {};
67
        }[];
68
        outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
69
          destinations: {
70
            addresses?: string[];
71
            droplet_ids?: number[];
72
            load_balancer_uids?: string[];
73
            kubernetes_ids?: string[];
74
            tags?: string[] & {};
75
          } & {};
76
        }[];
77
      } & {} & {}),
78
) {
79
  const url = new URL(`https://api.digitalocean.com/v2/firewalls`);
80

81
  const response = await fetch(url, {
82
    method: "POST",
83
    headers: {
84
      "Content-Type": "application/json",
85
      Authorization: "Bearer " + auth.token,
86
    },
87
    body: JSON.stringify(body),
88
  });
89
  if (!response.ok) {
90
    const text = await response.text();
91
    throw new Error(`${response.status} ${text}`);
92
  }
93
  return await response.json();
94
}
95