0

Create CNAME record

by
Published today

Create a DNS CNAME record (record:cname) pointing the alias name to a canonical name. extattrs uses the WAPI shape {"Site": {"value": "East"}}. Returns the created record with its _ref.

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
 * Create CNAME record
19
 * Create a DNS CNAME record (record:cname) pointing the alias name to a canonical name. extattrs uses the WAPI shape {"Site": {"value": "East"}}. Returns the created record with its _ref.
20
 */
21
export async function main(
22
  auth: RT.InfobloxNios,
23
  name: string,
24
  canonical: string,
25
  view: string | undefined,
26
  ttl: number | undefined,
27
  comment: string | undefined,
28
  extattrs: { [key: string]: any } | undefined
29
) {
30
  const url = wapiUrl(auth, "record:cname")
31
  url.searchParams.append("_return_fields+", "name,canonical,view,ttl,comment")
32
  url.searchParams.append("_return_as_object", "1")
33

34
  const body: { [key: string]: any } = { name, canonical }
35
  if (view) body.view = view
36
  if (ttl != null) {
37
    body.ttl = ttl
38
    body.use_ttl = true
39
  }
40
  if (comment) body.comment = comment
41
  if (extattrs && Object.keys(extattrs).length > 0) body.extattrs = extattrs
42

43
  const response = await fetch(url, {
44
    method: "POST",
45
    headers: {
46
      Authorization: basicAuth(auth),
47
      "Content-Type": "application/json",
48
      Accept: "application/json",
49
    },
50
    body: JSON.stringify(body),
51
  })
52

53
  if (!response.ok) {
54
    throw new Error(`${response.status} ${await response.text()}`)
55
  }
56

57
  return await response.json()
58
}
59