0

Update Firewall Rules (Trusted Sources) for a Database

by
Published Dec 20, 2024

To update a database cluster's firewall rules (known as "trusted sources" in the control panel), send a PUT request to `/v2/databases/$DATABASE_ID/firewall` specifying which resources should be able to open connections to the database.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Update Firewall Rules (Trusted Sources) for a Database
7
 * To update a database cluster's firewall rules (known as "trusted sources" in the control panel), send a PUT request to `/v2/databases/$DATABASE_ID/firewall` specifying which resources should be able to open connections to the database.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  database_cluster_uuid: string,
12
  body: {
13
    rules?: {
14
      uuid?: string;
15
      cluster_uuid?: string;
16
      type: "droplet" | "k8s" | "ip_addr" | "tag" | "app";
17
      value: string;
18
      created_at?: string;
19
    }[];
20
  },
21
) {
22
  const url = new URL(
23
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/firewall`,
24
  );
25

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