import {requestPVE, requestAPI, goToPage} from "./utils.js"; import {Dialog} from "./dialog.js"; window.addEventListener("DOMContentLoaded", init); async function init () { await populateInstances(); let addInstanceBtn = document.querySelector("#instance-add"); addInstanceBtn.addEventListener("click", handleInstanceAdd); } async function populateInstances () { let cookie = document.cookie; if (cookie === "") { goToPage("login.html"); } let resources = await requestPVE("/cluster/resources", "GET", null); let instanceContainer = document.getElementById("instance-container") let instances = []; resources.data.forEach((element) => { if (element.type === "lxc" || element.type === "qemu") { let nodeName = element.node; let nodeStatus = resources.data.find(item => item.node === nodeName && item.type === "node").status; element.node = {name: nodeName, status: nodeStatus}; instances.push(element); } }); instances.sort((a, b) => (a.vmid > b.vmid) ? 1 : -1); instanceContainer.innerHTML = `

VM ID

VM Name

VM Type

VM Status

Host Name

Host Status

Actions

`; for(let i = 0; i < instances.length; i++) { let newInstance = document.createElement("instance-obj"); newInstance.data = instances[i]; instanceContainer.append(newInstance); } } async function handleInstanceAdd () { let dialog = document.createElement("dialog-form"); document.body.append(dialog); dialog.header = "Create New Instance"; dialog.formBody = `

Container Options

`; let typeSelect = dialog.shadowRoot.querySelector("#type"); typeSelect.selectedIndex = -1; typeSelect.addEventListener("change", () => { if(typeSelect.value === "qemu") { dialog.shadowRoot.querySelectorAll(".container-specific").forEach((element) => { element.classList.add("none"); element.disabled = true; }); } else { dialog.shadowRoot.querySelectorAll(".container-specific").forEach((element) => { element.classList.remove("none"); element.disabled = false; }); } }); let templateContent = "iso"; let templateStorage = dialog.shadowRoot.querySelector("#template-storage"); templateStorage.selectedIndex = -1; let rootfsContent = "rootdir"; let rootfsStorage = dialog.shadowRoot.querySelector("#rootfs-storage"); rootfsStorage.selectedIndex = -1; let nodeSelect = dialog.shadowRoot.querySelector("#node"); let nodes = await requestPVE("/nodes", "GET"); nodes.data.forEach((element) => { if (element.status === "online") { nodeSelect.add(new Option(element.node)); } }); nodeSelect.selectedIndex = -1; nodeSelect.addEventListener("change", async () => { // change template and rootfs storage based on node let node = nodeSelect.value; let storage = await requestPVE(`/nodes/${node}/storage`, "GET"); storage.data.forEach((element) => { if (element.content.includes(templateContent)) { templateStorage.add(new Option(element.storage)); } if (element.content.includes(rootfsContent)) { rootfsStorage.add(new Option(element.storage)); } }); templateStorage.selectedIndex = -1; rootfsStorage.selectedIndex = -1; }); let templateImage = dialog.shadowRoot.querySelector("#template-image"); // populate templateImage by templateStorage.addEventListener("change", async () => { let content = "vztmpl"; let images = await requestPVE(`/nodes/${nodeSelect.value}/storage/${templateStorage.value}/content`, "GET"); images.data.forEach((element) => { if (element.content.includes(content)) { templateImage.append(new Option(element.volid.replace(`${templateStorage.value}:${content}/`, ""), element.volid)); } }); templateImage.selectedIndex = -1; }); dialog.callback = async (result, form) => { if (result === "confirm") { let body = { node: form.get("node"), type: form.get("type"), name: form.get("name"), vmid: form.get("vmid"), cores: form.get("cores"), memory: form.get("memory") }; if (form.get("type") === "lxc") { body.swap = form.get("swap"); body.password = form.get("password"); body.ostemplate = form.get("template-image"); body.rootfslocation = form.get("rootfs-storage"); body.rootfssize = form.get("rootfs-size"); } let result = await requestAPI("/instance", "POST", body); if (result.status === 200) { populateInstances(); } else { console.error(result); populateInstances(); } } } dialog.show(); }