0

Delete CNAME record

by
Published today

Delete a CNAME record identified by its _ref (from Search records). Returns the _ref of the deleted record.

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 refUrl(auth: RT.InfobloxNios, ref: string, type: string) {
13
  // Encode each segment so ?, # or .. in a ref can't reach another path or query.
14
  const segments = ref.split("/")
15
  if (
16
    segments[0] !== type ||
17
    segments.length < 2 ||
18
    segments.some((s) => s === "." || s === "..")
19
  ) {
20
    throw new Error(`Expected a ${type} reference, got: ${ref}`)
21
  }
22
  return wapiUrl(
23
    auth,
24
    segments.map((s) => encodeURIComponent(s).replace(/%3A/gi, ":")).join("/")
25
  )
26
}
27

28
function basicAuth(auth: RT.InfobloxNios) {
29
  const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`)
30
  return `Basic ${btoa(String.fromCharCode(...bytes))}`
31
}
32

33
/**
34
 * Delete CNAME record
35
 * Delete a CNAME record identified by its _ref (from Search records). Returns the _ref of the deleted record.
36
 */
37
export async function main(auth: RT.InfobloxNios, ref: string) {
38
  const url = refUrl(auth, ref, "record:cname")
39
  url.searchParams.append("_return_as_object", "1")
40

41
  const response = await fetch(url, {
42
    method: "DELETE",
43
    headers: {
44
      Authorization: basicAuth(auth),
45
      Accept: "application/json",
46
    },
47
  })
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  return await response.json()
54
}
55