//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 basicAuth(auth: RT.InfobloxNios) {
const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`)
return `Basic ${btoa(String.fromCharCode(...bytes))}`
}
async function wapiError(response: Response) {
const text = await response.text()
if (response.status === 401) {
return new Error(
"401 Unauthorized: wrong username or password, or the user's admin group has no API access"
)
}
try {
// WAPI errors are JSON: {"Error": "AdmConDataNotFoundError: ...", "code": "Client.Ibap.Data.NotFound", "text": "..."}
const { Error: error, code, text: detail } = JSON.parse(text)
if (error) {
const message =
detail && !error.includes(detail) ? `${error}: ${detail}` : error
return new Error(
`${response.status} ${message}${code ? ` (${code})` : ""}`
)
}
} catch {}
return new Error(`${response.status} ${text}`)
}
// Accept {"Site": "East"} as well as the WAPI shape {"Site": {"value": "East"}}.
function toExtattrs(extattrs: { [key: string]: any }) {
return Object.fromEntries(
Object.entries(extattrs).map(([name, v]) => [
name,
v !== null && typeof v === "object" && !Array.isArray(v)
? v
: { value: v },
])
)
}
/**
* Create fixed address
* Create a static DHCP reservation (fixedaddress) binding a MAC address to an IPv4 address: a fixed ipv4addr, or the next free address of next_available_in_network (CIDR), allocated atomically. extattrs takes {"Site": "East"} or the WAPI shape {"Site": {"value": "East"}}.
*/
export async function main(
auth: RT.InfobloxNios,
mac: string,
ipv4addr: string | undefined,
next_available_in_network: string | undefined,
network_view: string | undefined,
name: string | undefined,
comment: string | undefined,
extattrs: { [key: string]: any } | undefined
) {
if (!ipv4addr === !next_available_in_network) {
throw new Error("Set exactly one of ipv4addr or next_available_in_network")
}
const body: { [key: string]: any } = {
ipv4addr:
ipv4addr ||
`func:nextavailableip:${next_available_in_network}${network_view ? `,${network_view}` : ""}`,
mac,
}
if (network_view) body.network_view = network_view
if (name) body.name = name
if (comment) body.comment = comment
if (extattrs && Object.keys(extattrs).length > 0)
body.extattrs = toExtattrs(extattrs)
const url = wapiUrl(auth, "fixedaddress")
url.searchParams.append(
"_return_fields+",
"ipv4addr,mac,network,network_view,name,comment"
)
url.searchParams.append("_return_as_object", "1")
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: basicAuth(auth),
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
})
if (!response.ok) {
throw await wapiError(response)
}
return await response.json()
}
Submitted by hugo989 15 days ago
//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 basicAuth(auth: RT.InfobloxNios) {
const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`)
return `Basic ${btoa(String.fromCharCode(...bytes))}`
}
/**
* Create fixed address
* Create a static DHCP reservation (fixedaddress) binding a MAC address to an IPv4 address: a fixed ipv4addr, or the next free address of next_available_in_network (CIDR), allocated atomically. extattrs uses the WAPI shape {"Site": {"value": "East"}}.
*/
export async function main(
auth: RT.InfobloxNios,
mac: string,
ipv4addr: string | undefined,
next_available_in_network: string | undefined,
network_view: string | undefined,
name: string | undefined,
comment: string | undefined,
extattrs: { [key: string]: any } | undefined
) {
if (!ipv4addr === !next_available_in_network) {
throw new Error("Set exactly one of ipv4addr or next_available_in_network")
}
const body: { [key: string]: any } = {
ipv4addr:
ipv4addr ||
`func:nextavailableip:${next_available_in_network}${network_view ? `,${network_view}` : ""}`,
mac,
}
if (network_view) body.network_view = network_view
if (name) body.name = name
if (comment) body.comment = comment
if (extattrs && Object.keys(extattrs).length > 0) body.extattrs = extattrs
const url = wapiUrl(auth, "fixedaddress")
url.searchParams.append(
"_return_fields+",
"ipv4addr,mac,network,network_view,name,comment"
)
url.searchParams.append("_return_as_object", "1")
const response = await fetch(url, {
method: "POST",
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 21 days ago