ProxmoxAAS-API/scripts/utils.js

202 lines
5.8 KiB
JavaScript
Raw Normal View History

2023-05-17 21:40:37 +00:00
import { API, organization } from "/vars.js";
export const resources_config = {
disk: {
actionBarOrder: ["move", "resize", "detach_attach", "delete"],
lxc: {
prefixOrder: ["rootfs", "mp", "unused"],
2023-05-17 21:40:37 +00:00
rootfs: { name: "ROOTFS", icon: "images/resources/drive.svg", actions: ["move", "resize"] },
mp: { name: "MP", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] },
unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] }
},
qemu: {
prefixOrder: ["ide", "sata", "unused"],
2023-05-17 21:40:37 +00:00
ide: { name: "IDE", icon: "images/resources/disk.svg", actions: ["delete"] },
sata: { name: "SATA", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] },
unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] }
}
},
network: {
prefix: "net"
},
pcie: {
prefix: "hostpci"
}
}
export const instances_config = {
running: {
statusSrc: "images/status/active.svg",
statusAlt: "Instance is running",
powerButtonSrc: "images/actions/instance/stop.svg",
powerButtonAlt: "Shutdown Instance",
configButtonSrc: "images/actions/instance/config-inactive.svg",
configButtonAlt: "Change Configuration (Inactive)",
consoleButtonSrc: "images/actions/instance/console-active.svg",
consoleButtonAlt: "Open Console",
deleteButtonSrc: "images/actions/delete-inactive.svg",
deleteButtonAlt: "Delete Instance (Inactive)"
},
stopped: {
statusSrc: "images/status/inactive.svg",
statusAlt: "Instance is stopped",
powerButtonSrc: "images/actions/instance/start.svg",
powerButtonAlt: "Start Instance",
configButtonSrc: "images/actions/instance/config-active.svg",
configButtonAlt: "Change Configuration",
consoleButtonSrc: "images/actions/instance/console-inactive.svg",
consoleButtonAlt: "Open Console (Inactive)",
deleteButtonSrc: "images/actions/delete-active.svg",
deleteButtonAlt: "Delete Instance"
},
loading: {
statusSrc: "images/status/loading.svg",
statusAlt: "Instance is loading",
powerButtonSrc: "images/status/loading.svg",
powerButtonAlt: "Loading Instance",
configButtonSrc: "images/actions/instance/config-inactive.svg",
configButtonAlt: "Change Configuration (Inactive)",
consoleButtonSrc: "images/actions/instance/console-inactive.svg",
consoleButtonAlt: "Open Console (Inactive)",
deleteButtonSrc: "images/actions/delete-inactive.svg",
deleteButtonAlt: "Delete Instance (Inactive)"
}
}
export const nodes_config = {
online: {
statusSrc: "images/status/active.svg",
statusAlt: "Node is online",
},
offline: {
statusSrc: "images/status/inactive.svg",
statusAlt: "Node is offline",
},
uknown: {
statusSrc: "images/status/inactive.svg",
statusAlt: "Node status is unknown",
}
}
export function getCookie(cname) {
2022-12-16 07:40:04 +00:00
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
2022-12-18 00:07:18 +00:00
let ca = decodedCookie.split(";");
2023-05-17 21:40:37 +00:00
for (let i = 0; i < ca.length; i++) {
2022-12-16 07:40:04 +00:00
let c = ca[i];
2022-12-18 00:07:18 +00:00
while (c.charAt(0) === " ") {
2022-12-16 07:40:04 +00:00
c = c.substring(1);
}
2022-12-18 00:07:18 +00:00
if (c.indexOf(name) === 0) {
2022-12-16 07:40:04 +00:00
return c.substring(name.length, c.length);
}
}
return "";
}
2023-05-17 21:40:37 +00:00
export async function requestTicket(username, password, realm) {
let response = await requestAPI("/ticket", "POST", { username: `${username}@${realm}`, password: password }, false);
2022-12-13 00:21:33 +00:00
return response;
}
2023-05-17 21:40:37 +00:00
export async function requestPVE(path, method, body = null) {
let prms = new URLSearchParams(body);
2023-01-25 02:06:31 +00:00
let content = {
method: method,
mode: "cors",
credentials: "include",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}
2023-05-17 21:40:37 +00:00
if (method === "POST") {
2023-01-25 02:06:31 +00:00
content.body = prms.toString();
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
}
let response = await request(`${API}/proxmox${path}`, content);
2023-01-25 02:06:31 +00:00
return response;
}
2023-05-17 21:40:37 +00:00
export async function requestAPI(path, method, body = null) {
2023-01-25 02:06:31 +00:00
let prms = new URLSearchParams(body);
let content = {
method: method,
mode: "cors",
credentials: "include",
headers: {
2022-12-13 00:18:40 +00:00
"Content-Type": "application/x-www-form-urlencoded"
}
}
if (method === "POST" || method === "DELETE") {
2022-12-16 07:44:48 +00:00
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
}
if (body) {
content.body = prms.toString();
}
let response = await request(`${API}${path}`, content);
2023-01-25 02:06:31 +00:00
return response;
}
2023-05-17 21:40:37 +00:00
async function request(url, content) {
let response = await fetch(url, content);
let data = null;
try {
data = await response.json();
data.status = response.status;
}
catch {
data = null;
}
2023-05-17 21:40:37 +00:00
if (!response.ok) {
return { status: response.status, error: data ? data.error : response.status };
}
else {
data.status = response.status;
return data ? data : response;
}
2022-12-19 05:52:39 +00:00
}
2023-05-17 21:40:37 +00:00
export function goToPage(page, data = {}, newwindow = false) {
2023-02-17 05:12:28 +00:00
let url = new URL(`https://${window.location.host}/${page}`);
2023-05-17 21:40:37 +00:00
for (let k in data) {
2023-02-17 05:12:28 +00:00
url.searchParams.append(k, data[k]);
}
if (newwindow) {
window.open(url, `${organization} - client`, "height=480,width=848");
2023-02-17 05:12:28 +00:00
}
else {
window.location.assign(url.toString());
}
}
2023-05-17 21:40:37 +00:00
export function goToURL(href, data = {}, newwindow = false) {
2023-02-17 05:12:28 +00:00
let url = new URL(href);
2023-05-17 21:40:37 +00:00
for (let k in data) {
2022-12-19 05:52:39 +00:00
url.searchParams.append(k, data[k]);
}
2023-02-14 05:36:21 +00:00
if (newwindow) {
window.open(url, `${organization} - client`, "height=480,width=848");
2023-02-14 05:36:21 +00:00
}
else {
window.location.assign(url.toString());
}
}
2023-05-17 21:40:37 +00:00
export function getURIData() {
let url = new URL(window.location.href);
return Object.fromEntries(url.searchParams);
}
2023-05-17 21:40:37 +00:00
export async function deleteAllCookies() {
2023-05-13 07:28:09 +00:00
await requestAPI("/ticket", "DELETE");
}
2023-05-17 21:40:37 +00:00
export function setTitleAndHeader() {
document.title = `${organization} - client`;
document.querySelector("h1").innerText = organization;
}