add detach disk handler

This commit is contained in:
Arthur Lu 2023-01-25 02:06:31 +00:00
parent 10c197f3df
commit 6c3905c847
2 changed files with 46 additions and 6 deletions

View File

@ -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); window.addEventListener("DOMContentLoaded", init);
@ -29,7 +29,7 @@ async function init () {
} }
async function populateResources () { 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); console.log(config);
let name = type === "qemu" ? "name" : "hostname"; 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")){ else if (element === "delete_detach_attach" && diskMetaData[type][busPrefix].actions.includes("detach")){
action.src = "images/actions/detach.svg"; action.src = "images/actions/detach.svg";
action.title = "Detach Disk"; action.title = "Detach Disk";
action.addEventListener("click", handleDiskDetach);
} }
else if (element === "delete_detach_attach"){ else if (element === "delete_detach_attach"){
let active = diskMetaData[type][busPrefix].actions.includes("delete") ? "active" : "inactive"; 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); 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){ function getOrderedUsed(disks){
let ordered_keys = Object.keys(disks).sort((a,b) => {parseInt(a) - parseInt(b)}); // ordered integer list let ordered_keys = Object.keys(disks).sort((a,b) => {parseInt(a) - parseInt(b)}); // ordered integer list
return ordered_keys; return ordered_keys;

View File

@ -1,4 +1,4 @@
import {pveAPI} from "/vars.js"; import {pveAPI, paasAPI} from "/vars.js";
export class ResponseError extends Error { export class ResponseError extends Error {
constructor(message) { constructor(message) {
@ -62,9 +62,8 @@ export function setTicket (ticket, csrf) {
document.cookie = `CSRFPreventionToken=${csrf}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net;` 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 prms = new URLSearchParams(body);
let content = { let content = {
method: method, method: method,
mode: "cors", mode: "cors",
@ -78,7 +77,31 @@ export async function requestPVE (path, method, body = null, auth = true) {
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken"); 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) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw new ResponseError(`recieved response status code ${response.status}`); throw new ResponseError(`recieved response status code ${response.status}`);