Update GRE Tunnel

Updates a specific GRE tunnel. 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 GRE Tunnel
8
 * Updates a specific GRE tunnel. 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_gre_endpoint: string;
16
    customer_gre_endpoint: string;
17
    description?: string;
18
    health_check?: {
19
      direction?: "unidirectional" | "bidirectional";
20
      enabled?: boolean;
21
      rate?: "low" | "mid" | "high";
22
      target?: string;
23
      type?: "reply" | "request";
24
      [k: string]: unknown;
25
    };
26
    interface_address: string;
27
    mtu?: number;
28
    name: string;
29
    ttl?: number;
30
    [k: string]: unknown;
31
  }
32
) {
33
  const url = new URL(
34
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/magic/gre_tunnels/${tunnel_identifier}`
35
  );
36

37
  const response = await fetch(url, {
38
    method: "PUT",
39
    headers: {
40
      "X-AUTH-EMAIL": auth.email,
41
      "X-AUTH-KEY": auth.key,
42
      "Content-Type": "application/json",
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53