0

Search records

by
Published today

Search host, A or CNAME records by name (exact or regular expression), zone or IPv4 address, one page at a time. Pass the returned next_page_id as page_id to get the next page; it is null on the last page.

Script infoblox_nios Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 7 hours ago
1
//native
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
/**
18
 * Search records
19
 * Search host, A or CNAME records by name (exact or regular expression), zone or IPv4 address, one page at a time. Pass the returned next_page_id as page_id to get the next page; it is null on the last page.
20
 */
21
export async function main(
22
  auth: RT.InfobloxNios,
23
  record_type: "record:host" | "record:a" | "record:cname",
24
  name: string | undefined,
25
  name_match: "exact" | "regex" | undefined,
26
  zone: string | undefined,
27
  ipv4addr: string | undefined,
28
  view: string | undefined,
29
  max_results: number | undefined,
30
  page_id: string | undefined,
31
  return_fields: string[] | undefined
32
) {
33
  if (ipv4addr && record_type === "record:cname") {
34
    throw new Error(
35
      "CNAME records have no IPv4 address; search them by name or zone"
36
    )
37
  }
38

39
  const url = wapiUrl(auth, record_type)
40
  if (name)
41
    url.searchParams.append(name_match === "regex" ? "name~" : "name", name)
42
  if (zone) url.searchParams.append("zone", zone)
43
  if (ipv4addr) url.searchParams.append("ipv4addr", ipv4addr)
44
  if (view) url.searchParams.append("view", view)
45
  const fields = [
46
    "comment",
47
    "zone",
48
    "ttl",
49
    "extattrs",
50
    ...(return_fields ?? []),
51
  ]
52
  url.searchParams.append("_return_fields+", fields.join(","))
53
  url.searchParams.append("_paging", "1")
54
  url.searchParams.append("_return_as_object", "1")
55
  url.searchParams.append("_max_results", String(max_results || 100))
56
  if (page_id) url.searchParams.append("_page_id", page_id)
57

58
  const response = await fetch(url, {
59
    headers: {
60
      Authorization: basicAuth(auth),
61
      Accept: "application/json",
62
    },
63
  })
64

65
  if (!response.ok) {
66
    throw new Error(`${response.status} ${await response.text()}`)
67
  }
68

69
  const data = await response.json()
70
  return { result: data.result, next_page_id: data.next_page_id ?? null }
71
}
72