From 26dc801fe47528022cd79ddf892c07e824f48ae0 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 21 Feb 2023 22:50:01 +0000 Subject: [PATCH] add delete instance logic Signed-off-by: Arthur Lu --- scripts/instance.js | 45 +++++++++++++++++++++++++++++++++++++++++---- scripts/utils.js | 4 ++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/scripts/instance.js b/scripts/instance.js index 4ca2396..97fedc5 100644 --- a/scripts/instance.js +++ b/scripts/instance.js @@ -1,4 +1,4 @@ -import {requestPVE, goToPage, goToURL, instances} from "./utils.js"; +import {requestPVE, requestAPI, goToPage, goToURL, instances} from "./utils.js"; import { Dialog } from "./dialog.js"; export class Instance extends HTMLElement { @@ -104,7 +104,7 @@ export class Instance extends HTMLElement { document.body.append(dialog); dialog.header = `${this.status === "running" ? "Stop" : "Start"} VM ${this.vmid}`; - dialog.formBody = `

Are you sure you want to ${this.status === "running" ? "stop" : "start"}

VM ${this.vmid}

` + dialog.formBody = `

Are you sure you want to ${this.status === "running" ? "stop" : "start"} VM

${this.vmid}

` dialog.callback = async (result, form) => { if (result === "confirm") { @@ -153,14 +153,51 @@ export class Instance extends HTMLElement { } handleConsoleButton () { - if (this.status === "running") { + if (!this.actionLock && this.status === "running") { let data = {console: `${this.type === "qemu" ? "kvm" : "lxc"}`, vmid: this.vmid, vmname: this.name, node: this.node.name, resize: "off", cmd: ""}; data[`${this.type === "qemu" ? "novnc" : "xtermjs"}`] = 1; goToURL("https://pve.tronnet.net", data, true); } } - handleDeleteButton () {} + handleDeleteButton () { + if (!this.actionLock && this.status === "stopped") { + let dialog = document.createElement("dialog-form"); + document.body.append(dialog); + + dialog.header = `Delete VM ${this.vmid}`; + dialog.formBody = `

Are you sure you want to delete VM

${this.vmid}

` + + dialog.callback = async (result, form) => { + if (result === "confirm") { + this.actionLock = true; + this.status = "loading"; + this.update(); + + let action = {}; + action.purge = 1; + action["destroy-unreferenced-disks"] = 1; + + let body = { + node: this.node.name, + type: this.type, + vmid: this.vmid, + action: JSON.stringify(action) + }; + + let result = await requestAPI("/instance", "DELETE", body); + if (result.status === 200) { + this.parentNode.removeChild(this); + } + else { + console.error(result); + } + } + } + + dialog.show(); + } + } } customElements.define("instance-article", Instance); \ No newline at end of file diff --git a/scripts/utils.js b/scripts/utils.js index 27d7df5..be988dc 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -54,7 +54,7 @@ export const instances = { deleteButtonAlt: "Delete Instance" }, loading: { - powerButtonSrc: "images/actions/instance/loading.svg", + powerButtonSrc: "images/actions/loading.svg", powerButtonAlt: "Loading Instance", configButtonSrc: "images/actions/instance/config-inactive.svg", configButtonAlt: "Change Configuration (Inactive)", @@ -124,7 +124,7 @@ export async function requestAPI (path, method, body = null) { "Content-Type": "application/x-www-form-urlencoded" } } - if (method === "POST") { + if (method === "POST" || method === "DELETE") { content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken"); } if (body) {