ProxmoxAAS-Dashboard/scripts/config.js

81 lines
2.9 KiB
JavaScript
Raw Normal View History

import {request, goToPage, getURIData} from "./utils.js";
window.addEventListener("DOMContentLoaded", init);
async function init () {
2022-12-20 23:15:08 +00:00
let cookie = document.cookie;
if (cookie === "") {
goToPage("login.html");
}
let uriData = getURIData();
let node = uriData.node;
let type = uriData.type;
let vmid = uriData.vmid;
await populateForm(node, type, vmid);
2022-12-20 23:42:13 +00:00
let cancelButton = document.querySelector("#cancel");
cancelButton.addEventListener("click", () => {
goToPage("index.html");
});
}
async function populateForm (node, type, vmid) {
2022-12-20 00:21:24 +00:00
let config = await request(`/nodes/${node}/${type}/${vmid}/config`);
console.log(config);
2022-12-20 01:59:34 +00:00
2022-12-21 23:10:13 +00:00
let name = type === "qemu" ? "name" : "hostname";
2022-12-21 23:59:54 +00:00
addFormLine("name", null, "Name", {type: "text", value: config.data[name]});
2022-12-21 23:56:52 +00:00
addFormLine("resources", "images/resources/cpu.svg", "Cores", {type: "number", value: config.data.cores, min: 1, max: 8192}, "Threads");
addFormLine("resources", "images/resources/ram.svg", "Memory", {type: "number", value: config.data.memory / 1024, min: 0, step: 0.001}, "GiB");
2022-12-21 23:39:09 +00:00
if (type === "lxc") {
2022-12-21 23:56:52 +00:00
addFormLine("resources", "images/resources/swap.svg", "Swap", {type: "number", value: config.data.swap / 1024, min: 0, step: 0.001}, "GiB");
2022-12-21 23:39:09 +00:00
}
2022-12-20 23:37:04 +00:00
2022-12-21 23:06:26 +00:00
if (type === "qemu") {
let i = 0;
while(Object.hasOwn(config.data, `sata${i}`)){
let sata = config.data[`sata${i}`];
sata = `{"${sata.replaceAll(":", '":"').replaceAll("=", '":"').replaceAll(",", '","')}"}`;
sata = JSON.parse(sata);
let sizeNum = +(sata.size.replaceAll("G", "").replaceAll("M", ""));
let sizeUnit = sata.size.includes("G") ? "GiB" : "MiB";
2022-12-21 23:56:52 +00:00
addFormLine("resources", "images/resources/disk.svg", `SATA ${i}`, {type: "number", value: sizeUnit === "GiB" ? sizeNum.toFixed(3) : (sizeNum / 1024).toFixed(3), min: 0, step: 0.001}, "GiB");
2022-12-21 23:06:26 +00:00
i++;
}
}
else {
let rootfs = config.data.rootfs;
rootfs = `{"${rootfs.replaceAll(":", '":"').replaceAll("=", '":"').replaceAll(",", '","')}"}`;
rootfs = JSON.parse(rootfs);
let sizeNum = +(rootfs.size.replaceAll("G", "").replaceAll("M", ""));
let sizeUnit = rootfs.size.includes("G") ? "GiB" : "MiB";
2022-12-21 23:56:52 +00:00
addFormLine("resources", "images/resources/disk.svg", "Root FS", {type: "number", value: sizeUnit === "GiB" ? sizeNum.toFixed(3) : (sizeNum / 1024).toFixed(3), min: 0, step: 0.001}, "GiB");
2022-12-20 23:37:04 +00:00
}
2022-12-20 01:59:34 +00:00
}
2022-12-21 23:56:52 +00:00
function addFormLine (fieldset, iconHref, labelText, inputAttr, unitText=null) {
2022-12-21 08:49:57 +00:00
let field = document.querySelector(`#${fieldset}`);
2022-12-20 02:46:28 +00:00
2022-12-22 00:00:28 +00:00
if (iconHref) {
2022-12-21 23:59:54 +00:00
let icon = document.createElement("img");
icon.src = iconHref;
field.append(icon);
}
2022-12-21 23:56:52 +00:00
2022-12-20 01:59:34 +00:00
let label = document.createElement("label");
2022-12-21 09:31:58 +00:00
label.innerHTML = labelText;
2022-12-21 08:49:57 +00:00
field.append(label);
2022-12-20 01:59:34 +00:00
let input = document.createElement("input");
2022-12-20 02:08:16 +00:00
for (let k in inputAttr) {
2022-12-20 02:12:39 +00:00
input.setAttribute(k, inputAttr[k])
2022-12-20 01:59:34 +00:00
}
2022-12-21 08:49:57 +00:00
field.append(input);
2022-12-21 09:31:58 +00:00
if (unitText) {
let unit = document.createElement("p");
unit.innerText = unitText;
field.append(unit);
}
}