0

Update a Load Balancer

by
Published Dec 20, 2024

To update a load balancer's settings, send a PUT request to `/v2/load_balancers/$LOAD_BALANCER_ID`. The request should contain a full representation of the load balancer including existing attributes. It may contain _one of_ the `droplets_ids` or `tag` attributes as they are mutually exclusive. **Note that any attribute that is not provided will be reset to its default value.**

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 a Load Balancer
7
 * To update a load balancer's settings, send a PUT request to
8
`/v2/load_balancers/$LOAD_BALANCER_ID`. The request should contain a full
9
representation of the load balancer including existing attributes. It may
10
contain _one of_ the `droplets_ids` or `tag` attributes as they are mutually
11
exclusive. **Note that any attribute that is not provided will be reset to its
12
default value.**
13

14
 */
15
export async function main(
16
  auth: Digitalocean,
17
  lb_id: string,
18
  body:
19
    | ({ droplet_ids?: number[] } & {
20
        region?:
21
          | "ams1"
22
          | "ams2"
23
          | "ams3"
24
          | "blr1"
25
          | "fra1"
26
          | "lon1"
27
          | "nyc1"
28
          | "nyc2"
29
          | "nyc3"
30
          | "sfo1"
31
          | "sfo2"
32
          | "sfo3"
33
          | "sgp1"
34
          | "tor1"
35
          | "syd1";
36
      } & {
37
        id?: string;
38
        name?: string;
39
        project_id?: string;
40
        ip?: string;
41
        size_unit?: number;
42
        size?: "lb-small" | "lb-medium" | "lb-large";
43
        algorithm?: "round_robin" | "least_connections";
44
        status?: "new" | "active" | "errored";
45
        created_at?: string;
46
        forwarding_rules: {
47
          entry_protocol: "http" | "https" | "http2" | "http3" | "tcp" | "udp";
48
          entry_port: number;
49
          target_protocol: "http" | "https" | "http2" | "tcp" | "udp";
50
          target_port: number;
51
          certificate_id?: string;
52
          tls_passthrough?: false | true;
53
        }[];
54
        health_check?: {
55
          protocol?: "http" | "https" | "tcp";
56
          port?: number;
57
          path?: string;
58
          check_interval_seconds?: number;
59
          response_timeout_seconds?: number;
60
          unhealthy_threshold?: number;
61
          healthy_threshold?: number;
62
        };
63
        sticky_sessions?: {
64
          type?: "cookies" | "none";
65
          cookie_name?: string;
66
          cookie_ttl_seconds?: number;
67
        };
68
        redirect_http_to_https?: false | true;
69
        enable_proxy_protocol?: false | true;
70
        enable_backend_keepalive?: false | true;
71
        http_idle_timeout_seconds?: number;
72
        vpc_uuid?: string;
73
        disable_lets_encrypt_dns_records?: false | true;
74
        firewall?: { deny?: string[]; allow?: string[] };
75
        network?: "EXTERNAL" | "INTERNAL";
76
      })
77
    | ({ tag?: string } & {
78
        region?:
79
          | "ams1"
80
          | "ams2"
81
          | "ams3"
82
          | "blr1"
83
          | "fra1"
84
          | "lon1"
85
          | "nyc1"
86
          | "nyc2"
87
          | "nyc3"
88
          | "sfo1"
89
          | "sfo2"
90
          | "sfo3"
91
          | "sgp1"
92
          | "tor1"
93
          | "syd1";
94
      } & {
95
        id?: string;
96
        name?: string;
97
        project_id?: string;
98
        ip?: string;
99
        size_unit?: number;
100
        size?: "lb-small" | "lb-medium" | "lb-large";
101
        algorithm?: "round_robin" | "least_connections";
102
        status?: "new" | "active" | "errored";
103
        created_at?: string;
104
        forwarding_rules: {
105
          entry_protocol: "http" | "https" | "http2" | "http3" | "tcp" | "udp";
106
          entry_port: number;
107
          target_protocol: "http" | "https" | "http2" | "tcp" | "udp";
108
          target_port: number;
109
          certificate_id?: string;
110
          tls_passthrough?: false | true;
111
        }[];
112
        health_check?: {
113
          protocol?: "http" | "https" | "tcp";
114
          port?: number;
115
          path?: string;
116
          check_interval_seconds?: number;
117
          response_timeout_seconds?: number;
118
          unhealthy_threshold?: number;
119
          healthy_threshold?: number;
120
        };
121
        sticky_sessions?: {
122
          type?: "cookies" | "none";
123
          cookie_name?: string;
124
          cookie_ttl_seconds?: number;
125
        };
126
        redirect_http_to_https?: false | true;
127
        enable_proxy_protocol?: false | true;
128
        enable_backend_keepalive?: false | true;
129
        http_idle_timeout_seconds?: number;
130
        vpc_uuid?: string;
131
        disable_lets_encrypt_dns_records?: false | true;
132
        firewall?: { deny?: string[]; allow?: string[] };
133
        network?: "EXTERNAL" | "INTERNAL";
134
      }),
135
) {
136
  const url = new URL(
137
    `https://api.digitalocean.com/v2/load_balancers/${lb_id}`,
138
  );
139

140
  const response = await fetch(url, {
141
    method: "PUT",
142
    headers: {
143
      "Content-Type": "application/json",
144
      Authorization: "Bearer " + auth.token,
145
    },
146
    body: JSON.stringify(body),
147
  });
148
  if (!response.ok) {
149
    const text = await response.text();
150
    throw new Error(`${response.status} ${text}`);
151
  }
152
  return await response.json();
153
}
154