//native
function wapiUrl(auth: RT.InfobloxNios, path: string) {
const host = auth.host
.trim()
.replace(/^https?:\/\//, "")
.replace(/\/+$/, "")
const version = auth.wapi_version.trim().replace(/^v/, "")
return new URL(`https://${host}/wapi/v${version}/${path}`)
}
function refUrl(auth: RT.InfobloxNios, ref: string, type: string) {
// Encode each segment so ?, # or .. in a ref can't reach another path or query.
const segments = ref.split("/")
if (
segments[0] !== type ||
segments.length < 2 ||
segments.some((s) => s === "." || s === "..")
) {
throw new Error(`Expected a ${type} reference, got: ${ref}`)
}
return wapiUrl(
auth,
segments.map((s) => encodeURIComponent(s).replace(/%3A/gi, ":")).join("/")
)
}
function basicAuth(auth: RT.InfobloxNios) {
const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`)
return `Basic ${btoa(String.fromCharCode(...bytes))}`
}
/**
* Update A record
* Change the name, IPv4 address, TTL or comment of an A record, identified by its _ref (from Search records). Empty inputs are left unchanged.
*/
export async function main(
auth: RT.InfobloxNios,
ref: string,
name: string | undefined,
ipv4addr: string | undefined,
ttl: number | undefined,
comment: string | undefined
) {
const body: { [key: string]: any } = {}
if (name) body.name = name
if (ipv4addr) body.ipv4addr = ipv4addr
if (ttl != null) {
body.ttl = ttl
body.use_ttl = true
}
if (comment) body.comment = comment
if (Object.keys(body).length === 0) {
throw new Error("Nothing to update: set at least one field")
}
const url = refUrl(auth, ref, "record:a")
url.searchParams.append("_return_fields+", "name,ipv4addr,view,ttl,comment")
url.searchParams.append("_return_as_object", "1")
const response = await fetch(url, {
method: "PUT",
headers: {
Authorization: basicAuth(auth),
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 7 hours ago