From 4bd4e136dd0664007cdfb0d91a65cb8e83244b9f Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 21 Feb 2025 21:54:02 +0000 Subject: [PATCH] change instance disk net and pci logic to fabric --- src/routes/cluster.js | 28 +++++-- src/routes/cluster/disk.js | 59 +++++++------- src/routes/cluster/net.js | 55 ++++++++----- src/routes/cluster/pci.js | 153 +++++++++++++++++-------------------- 4 files changed, 153 insertions(+), 142 deletions(-) diff --git a/src/routes/cluster.js b/src/routes/cluster.js index 976e8ca..b3c9db6 100644 --- a/src/routes/cluster.js +++ b/src/routes/cluster.js @@ -98,17 +98,26 @@ router.get(`/:node(${nodeRegexP})/pci`, async (req, res) => { } // get remaining user resources const userAvailPci = (await getUserResources(req, userObj)).pci.nodes[params.node]; - if (userAvailPci == undefined) { // user has no avaliable devices on this node, so send an empty list + if (userAvailPci === undefined) { // user has no avaliable devices on this node, so send an empty list res.status(200).send([]); res.end(); } else { // get node avail devices - let nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies); - nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => { + const node = await global.pve.getNode(params.node); + let availableDevices = []; + // get each device and filter out only thise which are not reserved + for (const device of Object.values(node.devices)) { + if (device.reserved === false) { + availableDevices.push(device); + } + } + // further filter out only devices which the user has access to + availableDevices = availableDevices.filter(nodeAvail => userAvailPci.some((userAvail) => { return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0; })); - res.status(200).send(nodeAvailPci); + + res.status(200).send(availableDevices); res.end(); } }); @@ -150,13 +159,13 @@ router.post(`${basePath}/resources`, async (req, res) => { return; } // get current config - const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true }); + const instance = await global.pve.getInstance(params.node, params.vmid); const request = { - cores: Number(params.cores) - Number(currentConfig.data.data.cores), - memory: Number(params.memory) - Number(currentConfig.data.data.memory) + cores: Number(params.cores) - Number(instance.cores), + memory: Number(params.memory) - Number(instance.memory) }; if (params.type === "lxc") { - request.swap = Number(params.swap) - Number(currentConfig.data.data.swap); + request.swap = Number(params.swap) - Number(instance.swap); } else if (params.type === "qemu") { request.cpu = params.proctype; @@ -180,6 +189,7 @@ router.post(`${basePath}/resources`, async (req, res) => { // commit action const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -301,6 +311,7 @@ router.post(`${basePath}/create`, async (req, res) => { // commit action const result = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}`, "POST", { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncNode(params.node); }); /** @@ -329,4 +340,5 @@ router.delete(`${basePath}/delete`, async (req, res) => { // commit action const result = await global.pve.requestPVE(vmpath, "DELETE", { token: true }); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncNode(params.node); }); diff --git a/src/routes/cluster/disk.js b/src/routes/cluster/disk.js index ec25ce6..5c5ba65 100644 --- a/src/routes/cluster/disk.js +++ b/src/routes/cluster/disk.js @@ -31,10 +31,9 @@ router.post("/:disk/detach", async (req, res) => { if (!auth) { return; } - // get current config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; // disk must exist - if (!config[params.disk]) { + const disk = await global.pve.getDisk(params.node, params.vmid, params.disk); + if (!disk) { res.status(500).send({ error: `Disk ${params.disk} does not exist.` }); res.end(); return; @@ -49,6 +48,7 @@ router.post("/:disk/detach", async (req, res) => { const method = params.type === "qemu" ? "POST" : "PUT"; const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -80,29 +80,30 @@ router.post("/:disk/attach", async (req, res) => { if (!auth) { return; } - // get current config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; + // disk must exist - if (!config[`unused${params.source}`]) { - res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` }); + const disk = await global.pve.getDisk(params.node, params.vmid, `unused${params.source}`); + if (!disk) { + res.status(500).send({ error: `Requested disk unused${params.source} does not exist.` }); res.end(); return; } // target disk must be allowed according to source disk's storage options - const diskConfig = await global.pve.getDiskInfo(params.node, config, `unused${params.source}`); // get target disk const resourceConfig = global.config.resources; - if (!resourceConfig[diskConfig.storage].disks.some(diskPrefix => params.disk.startsWith(diskPrefix))) { - res.status(500).send({ error: `Requested target ${params.disk} is not in allowed list [${resourceConfig[diskConfig.storage].disks}].` }); + if (!resourceConfig[disk.storage].disks.some(diskPrefix => params.disk.startsWith(diskPrefix))) { + res.status(500).send({ error: `Requested target ${params.disk} is not in allowed list [${resourceConfig[disk.storage].disks}].` }); res.end(); return; } // setup action using source disk info from vm config const action = {}; - action[params.disk] = config[`unused${params.source}`]; + action[params.disk] = disk.volid; const method = params.type === "qemu" ? "POST" : "PUT"; + // commit action const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -138,17 +139,15 @@ router.post("/:disk/resize", async (req, res) => { if (!auth) { return; } - // get current config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; // check disk existence - const diskConfig = await global.pve.getDiskInfo(params.node, config, params.disk); // get target disk - if (!diskConfig) { // exit if disk does not exist + const disk = await global.pve.getDisk(params.node, params.vmid, params.disk); // get target disk + if (!disk) { // exit if disk does not exist res.status(500).send({ error: `requested disk ${params.disk} does not exist.` }); res.end(); return; } // setup request - const storage = diskConfig.storage; // get the storage + const storage = disk.storage; // get the storage const request = {}; request[storage] = Number(params.size * 1024 ** 3); // setup request object // check request approval @@ -161,6 +160,7 @@ router.post("/:disk/resize", async (req, res) => { const action = { disk: params.disk, size: `+${params.size}G` }; const result = await global.pve.requestPVE(`${vmpath}/resize`, "PUT", { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -198,17 +198,15 @@ router.post("/:disk/move", async (req, res) => { if (!auth) { return; } - // get current config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; // check disk existence - const diskConfig = await global.pve.getDiskInfo(params.node, config, params.disk); // get target disk - if (!diskConfig) { // exit if disk does not exist + const disk = await global.pve.getDisk(params.node, params.vmid, params.disk); // get target disk + if (!disk) { // exit if disk does not exist res.status(500).send({ error: `requested disk ${params.disk} does not exist.` }); res.end(); return; } // setup request - const size = parseInt(diskConfig.size); // get source disk size + const size = parseInt(disk.size); // get source disk size const dstStorage = params.storage; // get destination storage const request = {}; if (!params.delete) { // if not delete, then request storage, otherwise it is net 0 @@ -232,6 +230,7 @@ router.post("/:disk/move", async (req, res) => { // commit action const result = await global.pve.requestPVE(`${vmpath}/${route}`, "POST", { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -261,11 +260,10 @@ router.delete("/:disk/delete", async (req, res) => { if (!auth) { return; } - // get current config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; // disk must exist - if (!config[params.disk]) { - res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` }); + const disk = await global.pve.getDisk(params.node, params.vmid, params.disk); + if (!disk) { + res.status(500).send({ error: `Disk ${params.disk} does not exist.` }); res.end(); return; } @@ -281,6 +279,7 @@ router.delete("/:disk/delete", async (req, res) => { // commit action const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -310,20 +309,17 @@ router.post("/:disk/create", async (req, res) => { size: req.body.size, iso: req.body.iso }; - const userObj = global.utils.getUserObjFromUsername(req.cookies.username); - // check auth for specific instance const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`; const auth = await checkAuth(req.cookies, res, vmpath); if (!auth) { return; } - // get current config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; // disk must not exist - if (config[params.disk]) { - res.status(403).send({ error: `Requested disk ${params.disk} already exists.` }); + const disk = await global.pve.getDisk(params.node, params.vmid, params.disk); + if (disk) { + res.status(500).send({ error: `Disk ${params.disk} does already exists.` }); res.end(); return; } @@ -361,4 +357,5 @@ router.post("/:disk/create", async (req, res) => { // commit action const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); diff --git a/src/routes/cluster/net.js b/src/routes/cluster/net.js index cbafab4..737d70e 100644 --- a/src/routes/cluster/net.js +++ b/src/routes/cluster/net.js @@ -26,12 +26,16 @@ router.post("/:netid/create", async (req, res) => { node: req.params.node, type: req.params.type, vmid: req.params.vmid, - netid: req.params.netid.replace("net", ""), + netid: Number(req.params.netid.replace("net", "")), rate: req.body.rate, name: req.body.name }; - - const userObj = global.utils.getUserObjFromUsername(req.cookies.username); + // check netid is a valid number + if (isNaN(params.netid)) { + res.status(500).send({ error: `Network interface id must be a number, got ${req.params.netid}.` }); + res.end(); + return; + } // check auth for specific instance const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`; @@ -39,10 +43,9 @@ router.post("/:netid/create", async (req, res) => { if (!auth) { return; } - // get current config - const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true }); // net interface must not exist - if (currentConfig.data.data[`net${params.netid}`]) { + const net = await global.pve.getNet(params.node, params.vmid, params.netid); + if (net) { res.status(500).send({ error: `Network interface net${params.netid} already exists.` }); res.end(); return; @@ -56,6 +59,7 @@ router.post("/:netid/create", async (req, res) => { network: Number(params.rate) }; // check resource approval + const userObj = global.utils.getUserObjFromUsername(req.cookies.username); if (!await approveResources(req, userObj, request, params.node)) { res.status(500).send({ request, error: `Could not fulfil network request of ${params.rate}MB/s.` }); res.end(); @@ -74,6 +78,7 @@ router.post("/:netid/create", async (req, res) => { // commit action const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -97,32 +102,33 @@ router.post("/:netid/modify", async (req, res) => { node: req.params.node, type: req.params.type, vmid: req.params.vmid, - netid: req.params.netid.replace("net", ""), + netid: Number(req.params.netid.replace("net", "")), rate: req.body.rate }; - - const userObj = global.utils.getUserObjFromUsername(req.cookies.username); - + // check netid is a valid number + if (isNaN(params.netid)) { + res.status(500).send({ error: `Network interface id must be a number, got ${req.params.netid}.` }); + res.end(); + return; + } // check auth for specific instance const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`; const auth = await checkAuth(req.cookies, res, vmpath); if (!auth) { return; } - // get current config - const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true }); // net interface must already exist - if (!currentConfig.data.data[`net${params.netid}`]) { + const net = await global.pve.getNet(params.node, params.vmid, params.netid); + if (!net) { res.status(500).send({ error: `Network interface net${params.netid} does not exist.` }); res.end(); return; } - const currentNetworkConfig = currentConfig.data.data[`net${params.netid}`]; - const currentNetworkRate = currentNetworkConfig.split("rate=")[1].split(",")[0]; const request = { - network: Number(params.rate) - Number(currentNetworkRate) + network: Number(params.rate) - Number(net.rate) }; // check resource approval + const userObj = global.utils.getUserObjFromUsername(req.cookies.username); if (!await approveResources(req, userObj, request, params.node)) { res.status(500).send({ request, error: `Could not fulfil network request of ${params.rate}MB/s.` }); res.end(); @@ -130,11 +136,12 @@ router.post("/:netid/modify", async (req, res) => { } // setup action const action = {}; - action[`net${params.netid}`] = currentNetworkConfig.replace(`rate=${currentNetworkRate}`, `rate=${params.rate}`); + action[`net${params.netid}`] = net.value.replace(`rate=${net.rate}`, `rate=${params.rate}`); const method = params.type === "qemu" ? "POST" : "PUT"; // commit action const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); /** @@ -156,18 +163,23 @@ router.delete("/:netid/delete", async (req, res) => { node: req.params.node, type: req.params.type, vmid: req.params.vmid, - netid: req.params.netid.replace("net", "") + netid: Number(req.params.netid.replace("net", "")) }; + // check netid is a valid number + if (isNaN(params.netid)) { + res.status(500).send({ error: `Network interface id must be a number, got ${req.params.netid}.` }); + res.end(); + return; + } // check auth for specific instance const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`; const auth = await checkAuth(req.cookies, res, vmpath); if (!auth) { return; } - // get current config - const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true }); // net interface must already exist - if (!currentConfig.data.data[`net${params.netid}`]) { + const net = await global.pve.getNet(params.node, params.vmid, params.netid); + if (!net) { res.status(500).send({ error: `Network interface net${params.netid} does not exist.` }); res.end(); return; @@ -177,4 +189,5 @@ router.delete("/:netid/delete", async (req, res) => { // commit action const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, { delete: `net${params.netid}` }); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncInstance(params.node, params.vmid); }); diff --git a/src/routes/cluster/pci.js b/src/routes/cluster/pci.js index 5e91c69..4ac971b 100644 --- a/src/routes/cluster/pci.js +++ b/src/routes/cluster/pci.js @@ -1,5 +1,4 @@ import { Router } from "express"; -import { token } from "morgan"; export const router = Router({ mergeParams: true }); ; const checkAuth = global.utils.checkAuth; @@ -23,30 +22,28 @@ router.get("/:hostpci", async (req, res) => { node: req.params.node, type: req.params.type, vmid: req.params.vmid, - hostpci: req.params.hostpci.replace("hostpci", "") + hostpci: Number(req.params.hostpci.replace("hostpci", "")) }; + // check hostpci is a valid number + if (isNaN(params.hostpci)) { + res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` }); + res.end(); + return; + } // check auth for specific instance const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`; const auth = await checkAuth(req.cookies, res, vmpath); if (!auth) { return; } - // check device is in instance config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; - if (!config[`hostpci${params.hostpci}`]) { - res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` }); - res.end(); - return; - } - const device = config[`hostpci${params.hostpci}`].split(",")[0]; - // get node's pci devices - const deviceData = await global.pve.getDeviceInfo(params.node, device); - if (!deviceData) { + // get device + const device = await global.pve.getDevice(params.node, params.vmid, params.hostpci); + if (!device) { res.status(500).send({ error: `Could not find hostpci${params.hostpci}=${device} in ${params.node}.` }); res.end(); return; } - res.status(200).send(deviceData); + res.status(200).send(device); res.end(); }); @@ -71,13 +68,16 @@ router.post("/:hostpci/modify", async (req, res) => { node: req.params.node, type: req.params.type, vmid: req.params.vmid, - hostpci: req.params.hostpci.replace("hostpci", ""), + hostpci: Number(req.params.hostpci.replace("hostpci", "")), device: req.body.device, pcie: req.body.pcie }; - - const userObj = global.utils.getUserObjFromUsername(req.cookies.username); - + // check hostpci is a valid number + if (isNaN(params.hostpci)) { + res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` }); + res.end(); + return; + } // check if type is qemu if (params.type !== "qemu") { res.status(500).send({ error: "Type must be qemu (vm)." }); @@ -92,28 +92,33 @@ router.post("/:hostpci/modify", async (req, res) => { } // force all functions params.device = params.device.split(".")[0]; - // get instance config to check if device has not changed - const config = (await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true })).data.data; - const currentDeviceData = await global.pve.getDeviceInfo(params.node, config[`hostpci${params.hostpci}`].split(",")[0]); - if (!currentDeviceData) { + // device must exist to be modified + const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci); + if (!existingDevice) { res.status(500).send({ error: `No device in hostpci${params.hostpci}.` }); res.end(); return; } - // only check user and node availability if base id is different - if (currentDeviceData.id.split(".")[0] !== params.device) { + // only check user and node availability if base id is different, we do the split in case of existing partial-function hostpci + const userObj = global.utils.getUserObjFromUsername(req.cookies.username); + if (existingDevice.device_id.split(".")[0] !== params.device) { // setup request - const deviceData = await global.pve.getDeviceInfo(params.node, params.device); - const request = { pci: deviceData.device_name }; + const node = await global.pve.getNode(params.node); + const requestedDevice = node.devices[`${params.device}`]; + const request = { pci: requestedDevice.device_name }; + if (!requestedDevice) { + res.status(500).send({ request, error: `Could not fulfil request for ${params.device}.` }); + res.end(); + return; + } // check resource approval if (!await approveResources(req, userObj, request, params.node)) { - res.status(500).send({ request, error: `Could not fulfil request for ${deviceData.device_name}.` }); + res.status(500).send({ request, error: `Could not fulfil request for ${requestedDevice.device_name}.` }); res.end(); return; } // check node availability - const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies); - if (!nodeAvailPci.some(element => element.id.split(".")[0] === params.device)) { + if (!Object.values(node.devices).some(element => element.device_id.split(".")[0] === params.device && element.reserved === false)) { res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` }); res.end(); return; @@ -123,18 +128,9 @@ router.post("/:hostpci/modify", async (req, res) => { const action = {}; action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`; // commit action - const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root); - if (!(rootauth.status === 200)) { - res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." }); - res.end(); - return; - } - const rootcookies = { - PVEAuthCookie: rootauth.data.data.ticket, - CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken - }; - const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action); + const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncNode(params.node); }); /** @@ -151,18 +147,22 @@ router.post("/:hostpci/modify", async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post("/create", async (req, res) => { +router.post("/:hostpci/create", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, type: req.params.type, vmid: req.params.vmid, + hostpci: Number(req.params.hostpci.replace("hostpci", "")), device: req.body.device, pcie: req.body.pcie }; - - const userObj = global.utils.getUserObjFromUsername(req.cookies.username); - + // check hostpci is a valid number + if (isNaN(params.hostpci)) { + res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` }); + res.end(); + return; + } // check if type is qemu if (params.type !== "qemu") { res.status(500).send({ error: "Type must be qemu (vm)." }); @@ -177,46 +177,38 @@ router.post("/create", async (req, res) => { } // force all functions params.device = params.device.split(".")[0]; - // get instance config to find next available hostpci slot - const config = (await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true })).data.data; - let hostpci = 0; - while (config[`hostpci${hostpci}`]) { - hostpci++; + // device must not exist to be added + const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci); + if (existingDevice) { + res.status(500).send({ error: `Existing device in hostpci${params.hostpci}.` }); + res.end(); + return; } // setup request - const deviceData = await global.pve.getDeviceInfo(params.node, params.device); - const request = { - pci: deviceData.device_name - }; + const node = await global.pve.getNode(params.node); + const requestedDevice = node.devices[`${params.device}`]; + const request = { pci: requestedDevice.device_name }; // check resource approval + const userObj = global.utils.getUserObjFromUsername(req.cookies.username); if (!await approveResources(req, userObj, request, params.node)) { - res.status(500).send({ request, error: `Could not fulfil request for ${deviceData.device_name}.` }); + res.status(500).send({ request, error: `Could not fulfil request for ${requestedDevice.device_name}.` }); res.end(); return; } // check node availability - const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies); - if (!nodeAvailPci.some(element => element.id.split(".")[0] === params.device)) { + // const node = await global.pve.getNode(params.node); + if (!Object.values(node.devices).some(element => element.device_id.split(".")[0] === params.device && element.reserved === false)) { res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` }); res.end(); return; } // setup action const action = {}; - action[`hostpci${hostpci}`] = `${params.device},pcie=${params.pcie}`; + action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`; // commit action - const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root); - if (!(rootauth.status === 200)) { - res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." }); - res.end(); - return; - } - const rootcookies = { - PVEAuthCookie: rootauth.data.data.ticket, - CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken - }; - const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action); + const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncNode(params.node); }); /** @@ -238,8 +230,14 @@ router.delete("/:hostpci/delete", async (req, res) => { node: req.params.node, type: req.params.type, vmid: req.params.vmid, - hostpci: req.params.hostpci.replace("hostpci", "") + hostpci: Number(req.params.hostpci.replace("hostpci", "")) }; + // check hostpci is a valid number + if (isNaN(params.hostpci)) { + res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` }); + res.end(); + return; + } // check if type is qemu if (params.type !== "qemu") { res.status(500).send({ error: "Type must be qemu (vm)." }); @@ -253,8 +251,8 @@ router.delete("/:hostpci/delete", async (req, res) => { return; } // check device is in instance config - const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data; - if (!config[`hostpci${params.hostpci}`]) { + const device = global.pve.getDevice(params.node, params.vmid, params.hostpci); + if (!device) { res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` }); res.end(); return; @@ -262,16 +260,7 @@ router.delete("/:hostpci/delete", async (req, res) => { // setup action const action = { delete: `hostpci${params.hostpci}` }; // commit action, need to use root user here because proxmox api only allows root to modify hostpci for whatever reason - const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root); - if (!(rootauth.status === 200)) { - res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." }); - res.end(); - return; - } - const rootcookies = { - PVEAuthCookie: rootauth.data.data.ticket, - CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken - }; - const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action); + const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action); await global.pve.handleResponse(params.node, result, res); + await global.pve.syncNode(params.node); });