diff --git a/src/routes/cluster.js b/src/routes/cluster.js index 4104e1e..976e8ca 100644 --- a/src/routes/cluster.js +++ b/src/routes/cluster.js @@ -98,13 +98,19 @@ router.get(`/:node(${nodeRegexP})/pci`, async (req, res) => { } // get remaining user resources const userAvailPci = (await getUserResources(req, userObj)).pci.nodes[params.node]; - // get node avail devices - let nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies); - nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => { - return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0; - })); - res.status(200).send(nodeAvailPci); - res.end(); + 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) => { + return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0; + })); + res.status(200).send(nodeAvailPci); + res.end(); + } }); /** diff --git a/src/routes/cluster/pci.js b/src/routes/cluster/pci.js index 8367d87..5e91c69 100644 --- a/src/routes/cluster/pci.js +++ b/src/routes/cluster/pci.js @@ -1,4 +1,5 @@ import { Router } from "express"; +import { token } from "morgan"; export const router = Router({ mergeParams: true }); ; const checkAuth = global.utils.checkAuth; @@ -177,7 +178,7 @@ 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 = global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { cookies: params.cookies }); + 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++; diff --git a/src/utils.js b/src/utils.js index e153473..3b0be8a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -258,11 +258,13 @@ export async function getUserResources (req, user) { userResources.pci.nodes[nodeName][index].used++; userResources.pci.nodes[nodeName][index].avail--; } - // otherwise add the resource to the global pool + // otherwise try to add the resource to the global pool else { const index = userResources.pci.global.findIndex((availEelement) => deviceName.includes(availEelement.match)); - userResources.pci.global[index].used++; - userResources.pci.global[index].avail--; + if (index >= 0) { // device resource is in the user's global list then increment it by 1 + userResources.pci.global[index].used++; + userResources.pci.global[index].avail--; + } } const index = userResources.pci.total.findIndex((availEelement) => deviceName.includes(availEelement.match)); userResources.pci.total[index].used++;