Update IPsec Tunnel

Updates a specific IPsec tunnel associated with an account. Use `?validate_only=true` as an optional query parameter to only run validation without persisting changes.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Update IPsec Tunnel
8
 * Updates a specific IPsec tunnel associated with an account. Use `?validate_only=true` as an optional query parameter to only run validation without persisting changes.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  tunnel_identifier: string,
13
  account_identifier: string,
14
  body: {
15
    cloudflare_endpoint: string;
16
    customer_endpoint?: string;
17
    description?: string;
18
    interface_address: string;
19
    name: string;
20
    psk?: string;
21
    replay_protection?: boolean;
22
    [k: string]: unknown;
23
  }
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/magic/ipsec_tunnels/${tunnel_identifier}`
27
  );
28

29
  const response = await fetch(url, {
30
    method: "PUT",
31
    headers: {
32
      "X-AUTH-EMAIL": auth.email,
33
      "X-AUTH-KEY": auth.key,
34
      "Content-Type": "application/json",
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: JSON.stringify(body),
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45