add delete instance logic

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-02-21 22:50:01 +00:00
parent 01074e87f8
commit 26dc801fe4
2 changed files with 43 additions and 6 deletions

View File

@ -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"; import { Dialog } from "./dialog.js";
export class Instance extends HTMLElement { export class Instance extends HTMLElement {
@ -104,7 +104,7 @@ export class Instance extends HTMLElement {
document.body.append(dialog); document.body.append(dialog);
dialog.header = `${this.status === "running" ? "Stop" : "Start"} VM ${this.vmid}`; dialog.header = `${this.status === "running" ? "Stop" : "Start"} VM ${this.vmid}`;
dialog.formBody = `<p>Are you sure you want to ${this.status === "running" ? "stop" : "start"}</p><p>VM ${this.vmid}</p>` dialog.formBody = `<p>Are you sure you want to ${this.status === "running" ? "stop" : "start"} VM</p><p>${this.vmid}</p>`
dialog.callback = async (result, form) => { dialog.callback = async (result, form) => {
if (result === "confirm") { if (result === "confirm") {
@ -153,14 +153,51 @@ export class Instance extends HTMLElement {
} }
handleConsoleButton () { 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: ""}; 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; data[`${this.type === "qemu" ? "novnc" : "xtermjs"}`] = 1;
goToURL("https://pve.tronnet.net", data, true); 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 = `<p>Are you sure you want to <strong>delete</strong> VM </p><p>${this.vmid}</p>`
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); customElements.define("instance-article", Instance);

View File

@ -54,7 +54,7 @@ export const instances = {
deleteButtonAlt: "Delete Instance" deleteButtonAlt: "Delete Instance"
}, },
loading: { loading: {
powerButtonSrc: "images/actions/instance/loading.svg", powerButtonSrc: "images/actions/loading.svg",
powerButtonAlt: "Loading Instance", powerButtonAlt: "Loading Instance",
configButtonSrc: "images/actions/instance/config-inactive.svg", configButtonSrc: "images/actions/instance/config-inactive.svg",
configButtonAlt: "Change Configuration (Inactive)", configButtonAlt: "Change Configuration (Inactive)",
@ -124,7 +124,7 @@ export async function requestAPI (path, method, body = null) {
"Content-Type": "application/x-www-form-urlencoded" "Content-Type": "application/x-www-form-urlencoded"
} }
} }
if (method === "POST") { if (method === "POST" || method === "DELETE") {
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken"); content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
} }
if (body) { if (body) {