//native
/**
* List job templates
* List one page of job templates visible to the credential. Returns the API's page envelope: count, next and previous page URLs, and results. Pass page + 1 to fetch the next page while next is not null.
*/
export async function main(
auth: RT.Awx,
page: number | undefined,
page_size: number | undefined,
search: string | undefined
) {
const url = new URL(`${apiRoot(auth)}/job_templates/`)
if (page) {
url.searchParams.append("page", String(page))
}
if (page_size != null) {
url.searchParams.append("page_size", String(page_size))
}
if (search != null && search !== "") {
url.searchParams.append("search", search)
}
const response = await fetch(url, {
headers: {
Authorization: authorization(auth),
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
function apiRoot(auth: RT.Awx) {
const path = (auth.api_path || "/api/v2").replace(/^\/+|\/+$/g, "")
// Tolerate a base_url pasted with the API path already on it.
const base = auth.base_url
.replace(/\/+$/, "")
.replace(new RegExp(`/${path}$`), "")
return `${base}/${path}`
}
function authorization(auth: RT.Awx) {
if (auth.token) {
return `Bearer ${auth.token}`
}
if (auth.username) {
const bytes = new TextEncoder().encode(
`${auth.username}:${auth.password ?? ""}`
)
return `Basic ${btoa(String.fromCharCode(...bytes))}`
}
throw new Error(
"The AWX resource needs either a token or a username and password"
)
}
Submitted by hugo989 3 hours ago