0

Search records

Published 20 days ago

Search host, A, CNAME or PTR records by name (exact or regular expression), zone, IPv4 address or, for PTR records, the target ptrdname, 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 15 days 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
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
    // WAPI errors are JSON: {"Error": "AdmConDataNotFoundError: ...", "code": "Client.Ibap.Data.NotFound", "text": "..."}
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
 * Search records
40
 * Search host, A, CNAME or PTR records by name (exact or regular expression), zone, IPv4 address or, for PTR records, the target ptrdname, 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.
41
 */
42
export async function main(
43
  auth: RT.InfobloxNios,
44
  record_type: "record:host" | "record:a" | "record:cname" | "record:ptr",
45
  name: string | undefined,
46
  name_match: "exact" | "regex" | undefined,
47
  zone: string | undefined,
48
  ipv4addr: string | undefined,
49
  view: string | undefined,
50
  max_results: number | undefined,
51
  page_id: string | undefined,
52
  return_fields: string[] | undefined,
53
  ptrdname: string | undefined
54
) {
55
  if (ipv4addr && record_type === "record:cname") {
56
    throw new Error(
57
      "CNAME records have no IPv4 address; search them by name or zone"
58
    )
59
  }
60

61
  if (ptrdname && record_type !== "record:ptr") {
62
    throw new Error("ptrdname only applies to PTR records")
63
  }
64

65
  const url = wapiUrl(auth, record_type)
66
  if (name)
67
    url.searchParams.append(name_match === "regex" ? "name~" : "name", name)
68
  if (zone) url.searchParams.append("zone", zone)
69
  if (ipv4addr) url.searchParams.append("ipv4addr", ipv4addr)
70
  if (ptrdname) url.searchParams.append("ptrdname", ptrdname)
71
  if (view) url.searchParams.append("view", view)
72
  const fields = [
73
    ...(record_type === "record:ptr" ? ["name", "ipv4addr"] : []),
74
    "comment",
75
    "zone",
76
    "ttl",
77
    "extattrs",
78
    ...(return_fields ?? []),
79
  ]
80
  url.searchParams.append("_return_fields+", fields.join(","))
81
  url.searchParams.append("_paging", "1")
82
  url.searchParams.append("_return_as_object", "1")
83
  url.searchParams.append("_max_results", String(max_results || 100))
84
  if (page_id) url.searchParams.append("_page_id", page_id)
85

86
  const response = await fetch(url, {
87
    headers: {
88
      Authorization: basicAuth(auth),
89
      Accept: "application/json",
90
    },
91
  })
92

93
  if (!response.ok) {
94
    throw await wapiError(response)
95
  }
96

97
  const data = await response.json()
98
  return { result: data.result, next_page_id: data.next_page_id ?? null }
99
}
100