1 | |
2 |
|
3 | async function connect(auth: RT.F5Bigip): Promise<{ |
4 | base: string |
5 | headers: { [key: string]: string } |
6 | release: () => Promise<void> |
7 | }> { |
8 | const base = auth.base_url.replace(/\/+$/, "") |
9 | if (auth.auth_method === "basic") { |
10 | return { |
11 | base, |
12 | headers: { |
13 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
14 | }, |
15 | release: async () => {}, |
16 | } |
17 | } |
18 | const login = await fetch(`${base}/mgmt/shared/authn/login`, { |
19 | method: "POST", |
20 | headers: { "Content-Type": "application/json", Accept: "application/json" }, |
21 | body: JSON.stringify({ |
22 | username: auth.username, |
23 | password: auth.password, |
24 | loginProviderName: auth.login_provider_name || "tmos", |
25 | }), |
26 | }) |
27 | if (!login.ok) { |
28 | throw new Error(`${login.status} ${await login.text()}`) |
29 | } |
30 | const token: string = (await login.json()).token.token |
31 | return { |
32 | base, |
33 | headers: { "X-F5-Auth-Token": token }, |
34 | release: async () => { |
35 | await fetch( |
36 | `${base}/mgmt/shared/authz/tokens/${encodeURIComponent(token)}`, |
37 | { |
38 | method: "DELETE", |
39 | headers: { "X-F5-Auth-Token": token }, |
40 | } |
41 | ) |
42 | }, |
43 | } |
44 | } |
45 |
|
46 | |
47 | * Save configuration |
48 | * Save the running configuration to the stored configuration (tmsh save sys config), so changes made through the API survive a reboot. |
49 | */ |
50 | export async function main(auth: RT.F5Bigip) { |
51 | const f5 = await connect(auth) |
52 | try { |
53 | const response = await fetch(`${f5.base}/mgmt/tm/sys/config`, { |
54 | method: "POST", |
55 | headers: { |
56 | ...f5.headers, |
57 | "Content-Type": "application/json", |
58 | Accept: "application/json", |
59 | }, |
60 | body: JSON.stringify({ command: "save" }), |
61 | }) |
62 |
|
63 | if (!response.ok) { |
64 | throw new Error(`${response.status} ${await response.text()}`) |
65 | } |
66 |
|
67 | return await response.json() |
68 | } finally { |
69 | await f5.release() |
70 | } |
71 | } |
72 |
|