0

Add Forwarding Rules to a Load Balancer

by
Published Dec 20, 2024

To add an additional forwarding rule to a load balancer instance, send a POST request to `/v2/load_balancers/$LOAD_BALANCER_ID/forwarding_rules`. In the body of the request, there should be a `forwarding_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 Forwarding Rules to a Load Balancer
7
 * To add an additional forwarding rule to a load balancer instance, send a POST
8
request to `/v2/load_balancers/$LOAD_BALANCER_ID/forwarding_rules`. In the body
9
of the request, there should be a `forwarding_rules` attribute containing an
10
array of rules to 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
  lb_id: string,
20
  body: {
21
    forwarding_rules: {
22
      entry_protocol: "http" | "https" | "http2" | "http3" | "tcp" | "udp";
23
      entry_port: number;
24
      target_protocol: "http" | "https" | "http2" | "tcp" | "udp";
25
      target_port: number;
26
      certificate_id?: string;
27
      tls_passthrough?: false | true;
28
    }[];
29
  },
30
) {
31
  const url = new URL(
32
    `https://api.digitalocean.com/v2/load_balancers/${lb_id}/forwarding_rules`,
33
  );
34

35
  const response = await fetch(url, {
36
    method: "POST",
37
    headers: {
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49