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 | function toExtattrs(extattrs: { [key: string]: any }) { |
40 | return Object.fromEntries( |
41 | Object.entries(extattrs).map(([name, v]) => [ |
42 | name, |
43 | v !== null && typeof v === "object" && !Array.isArray(v) |
44 | ? v |
45 | : { value: v }, |
46 | ]) |
47 | ) |
48 | } |
49 |
|
50 | |
51 | * Create host record |
52 | * Create a host record (record:host) with one IPv4 address: a fixed ipv4addr, or the next free address of next_available_in_network (CIDR), allocated atomically. Setting mac also creates the DHCP reservation. extattrs takes {"Site": "East"} or the WAPI shape {"Site": {"value": "East"}}. |
53 | */ |
54 | export async function main( |
55 | auth: RT.InfobloxNios, |
56 | name: string, |
57 | ipv4addr: string | undefined, |
58 | next_available_in_network: string | undefined, |
59 | network_view: string | undefined, |
60 | mac: string | undefined, |
61 | view: string | undefined, |
62 | aliases: string[] | undefined, |
63 | ttl: number | undefined, |
64 | comment: string | undefined, |
65 | extattrs: { [key: string]: any } | undefined |
66 | ) { |
67 | if (!ipv4addr === !next_available_in_network) { |
68 | throw new Error("Set exactly one of ipv4addr or next_available_in_network") |
69 | } |
70 |
|
71 | const address: { [key: string]: any } = { |
72 | ipv4addr: |
73 | ipv4addr || |
74 | `func:nextavailableip:${next_available_in_network}${network_view ? `,${network_view}` : ""}`, |
75 | } |
76 | if (mac) address.mac = mac |
77 |
|
78 | const body: { [key: string]: any } = { name, ipv4addrs: [address] } |
79 | if (view) body.view = view |
80 | if (aliases?.length) body.aliases = aliases |
81 | if (ttl != null) { |
82 | body.ttl = ttl |
83 | body.use_ttl = true |
84 | } |
85 | if (comment) body.comment = comment |
86 | if (extattrs && Object.keys(extattrs).length > 0) |
87 | body.extattrs = toExtattrs(extattrs) |
88 |
|
89 | const url = wapiUrl(auth, "record:host") |
90 | url.searchParams.append( |
91 | "_return_fields+", |
92 | "name,ipv4addrs,view,aliases,ttl,comment" |
93 | ) |
94 | url.searchParams.append("_return_as_object", "1") |
95 |
|
96 | const response = await fetch(url, { |
97 | method: "POST", |
98 | headers: { |
99 | Authorization: basicAuth(auth), |
100 | "Content-Type": "application/json", |
101 | Accept: "application/json", |
102 | }, |
103 | body: JSON.stringify(body), |
104 | }) |
105 |
|
106 | if (!response.ok) { |
107 | throw await wapiError(response) |
108 | } |
109 |
|
110 | return await response.json() |
111 | } |
112 |
|