1 | |
2 |
|
3 | function wapiUrl(auth: RT.InfobloxNios, path: string) { |
4 | const host = auth.host |
5 | .trim() |
6 | .replace(/^https?:\/\//, "") |
7 | .replace(/\/+$/, "") |
8 | const version = auth.wapi_version.trim().replace(/^v/, "") |
9 | return new URL(`https://${host}/wapi/v${version}/${path}`) |
10 | } |
11 |
|
12 | function basicAuth(auth: RT.InfobloxNios) { |
13 | const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`) |
14 | return `Basic ${btoa(String.fromCharCode(...bytes))}` |
15 | } |
16 |
|
17 | async function wapiError(response: Response) { |
18 | const text = await response.text() |
19 | if (response.status === 401) { |
20 | return new Error( |
21 | "401 Unauthorized: wrong username or password, or the user's admin group has no API access" |
22 | ) |
23 | } |
24 | try { |
25 | |
26 | const { Error: error, code, text: detail } = JSON.parse(text) |
27 | if (error) { |
28 | const message = |
29 | detail && !error.includes(detail) ? `${error}: ${detail}` : error |
30 | return new Error( |
31 | `${response.status} ${message}${code ? ` (${code})` : ""}` |
32 | ) |
33 | } |
34 | } catch {} |
35 | return new Error(`${response.status} ${text}`) |
36 | } |
37 |
|
38 | |
39 | * Get next available IP |
40 | * Return the next free IPv4 address(es) of a network, given its CIDR (e.g. 10.20.0.0/24), or of one of its DHCP ranges (range_start_addr), skipping the addresses in exclude. Nothing is reserved: to allocate atomically, create the host record or fixed address with next_available_in_network instead. |
41 | */ |
42 | export async function main( |
43 | auth: RT.InfobloxNios, |
44 | network: string, |
45 | network_view: string | undefined, |
46 | num: number | undefined, |
47 | exclude: string[] | undefined, |
48 | range_start_addr: string | undefined |
49 | ) { |
50 | const objectType = range_start_addr ? "range" : "network" |
51 | const label = range_start_addr |
52 | ? `DHCP range starting at ${range_start_addr} in ${network}` |
53 | : `Network ${network}` |
54 |
|
55 | const lookup = wapiUrl(auth, objectType) |
56 | lookup.searchParams.append("network", network) |
57 | if (range_start_addr) |
58 | lookup.searchParams.append("start_addr", range_start_addr) |
59 | if (network_view) lookup.searchParams.append("network_view", network_view) |
60 | lookup.searchParams.append("_return_as_object", "1") |
61 |
|
62 | const lookupResponse = await fetch(lookup, { |
63 | headers: { |
64 | Authorization: basicAuth(auth), |
65 | Accept: "application/json", |
66 | }, |
67 | }) |
68 | if (!lookupResponse.ok) { |
69 | throw await wapiError(lookupResponse) |
70 | } |
71 | const { result: matches } = (await lookupResponse.json()) as { |
72 | result: { _ref: string; network_view: string }[] |
73 | } |
74 | if (matches.length === 0) { |
75 | throw new Error(`${label} not found`) |
76 | } |
77 | if (matches.length > 1) { |
78 | throw new Error( |
79 | `${label} exists in several network views (${matches.map((n) => n.network_view).join(", ")}); set network_view` |
80 | ) |
81 | } |
82 | const ref = matches[0]._ref |
83 |
|
84 | const url = wapiUrl(auth, ref) |
85 | url.searchParams.append("_function", "next_available_ip") |
86 | url.searchParams.append("_return_as_object", "1") |
87 |
|
88 | const body: { [key: string]: any } = { num: num || 1 } |
89 | if (exclude?.length) body.exclude = exclude |
90 |
|
91 | const response = await fetch(url, { |
92 | method: "POST", |
93 | headers: { |
94 | Authorization: basicAuth(auth), |
95 | "Content-Type": "application/json", |
96 | Accept: "application/json", |
97 | }, |
98 | body: JSON.stringify(body), |
99 | }) |
100 |
|
101 | if (!response.ok) { |
102 | throw await wapiError(response) |
103 | } |
104 |
|
105 | const data = await response.json() |
106 | return { |
107 | ref, |
108 | [range_start_addr ? "range_ref" : "network_ref"]: ref, |
109 | ips: data.result.ips, |
110 | } |
111 | } |
112 |
|