From f9691f08576394c86d5af8fb5b1a455cefc80fd0 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 25 Jan 2023 02:06:31 +0000 Subject: [PATCH] add detach disk handler --- scripts/config.js | 21 +++++++++++++++++++-- scripts/utils.js | 31 +++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/scripts/config.js b/scripts/config.js index 1cc8e84..2d4c160 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -1,4 +1,4 @@ -import {requestPVE, goToPage, getURIData, reload, resources} from "./utils.js"; +import {requestPVE, requestAPI, goToPage, getURIData, reload, resources} from "./utils.js"; window.addEventListener("DOMContentLoaded", init); @@ -29,7 +29,7 @@ async function init () { } async function populateResources () { - let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`); + let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET"); console.log(config); let name = type === "qemu" ? "name" : "hostname"; @@ -116,6 +116,7 @@ async function addDiskLine (fieldset, busPrefix, busName, device, disk) { else if (element === "delete_detach_attach" && diskMetaData[type][busPrefix].actions.includes("detach")){ action.src = "images/actions/detach.svg"; action.title = "Detach Disk"; + action.addEventListener("click", handleDiskDetach); } else if (element === "delete_detach_attach"){ let active = diskMetaData[type][busPrefix].actions.includes("delete") ? "active" : "inactive"; @@ -133,6 +134,22 @@ async function addDiskLine (fieldset, busPrefix, busName, device, disk) { field.append(actionDiv); } +async function handleDiskDetach () { + let body = { + node: node, + type: type, + vmid: vmid, + action: `delete=${this.id}` + }; + let result = await requestAPI("/disk/detach", "POST", body); + if (result.status === 200) { + reload(); + } + else{ + console.error(result); + } +} + function getOrderedUsed(disks){ let ordered_keys = Object.keys(disks).sort((a,b) => {parseInt(a) - parseInt(b)}); // ordered integer list return ordered_keys; diff --git a/scripts/utils.js b/scripts/utils.js index bbd1655..b0123f8 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,4 +1,4 @@ -import {pveAPI} from "/vars.js"; +import {pveAPI, paasAPI} from "/vars.js"; export class ResponseError extends Error { constructor(message) { @@ -62,9 +62,8 @@ export function setTicket (ticket, csrf) { document.cookie = `CSRFPreventionToken=${csrf}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net;` } -export async function requestPVE (path, method, body = null, auth = true) { +export async function requestPVE (path, method, body = null) { let prms = new URLSearchParams(body); - let content = { method: method, mode: "cors", @@ -78,7 +77,31 @@ export async function requestPVE (path, method, body = null, auth = true) { content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken"); } - let response = await fetch(`${pveAPI}${path}`, content) + let response = await request(`${pveAPI}${path}`, content); + return response; +} + +export async function requestAPI (path, method, body = null) { + let prms = new URLSearchParams(body); + let content = { + method: method, + mode: "cors", + credentials: "include", + headers: { + "Content-Type": "application/x-www-form-urlencoded" + } + } + if(method === "POST") { + content.body = prms.toString(); + content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken"); + } + + let response = await request(`${paasAPI}${path}`, content); + return response; +} + +async function request (url, content) { + let response = await fetch(url, content) .then((response) => { if (!response.ok) { throw new ResponseError(`recieved response status code ${response.status}`);