various code cleanup and commenting
This commit is contained in:
1 parent
46295fabde
commit
af2194a8b3
14 files changed
+229
-123
No files matched your search
+34
-12
@@ -1,10 +1,6 @@
|
||||
import { Router } from "express";
|
||||
export const router = Router({ mergeParams: true }); ;
|
||||
|
||||
const checkAuth = global.utils.checkAuth;
|
||||
const getPoolResources = global.utils.getPoolResources;
|
||||
const approveResources = global.utils.approveResources;
|
||||
|
||||
/**
|
||||
* GET - get available pcie devices for the given node and user
|
||||
* request:
|
||||
@@ -21,8 +17,9 @@ router.get("/", async (req, res) => {
|
||||
type: req.params.type,
|
||||
vmid: req.params.vmid,
|
||||
};
|
||||
|
||||
// check auth
|
||||
const auth = await checkAuth(req.cookies, res);
|
||||
const auth = await global.utils.checkAuth(req.cookies, res);
|
||||
if (!auth) {
|
||||
return;
|
||||
}
|
||||
@@ -48,7 +45,7 @@ router.get("/", async (req, res) => {
|
||||
}
|
||||
|
||||
// get remaining user resources
|
||||
const poolAvailPci = (await getPoolResources(req, instance.pool)).pci.nodes[params.node]; // we assume that the node list is used. TODO support global lists
|
||||
const poolAvailPci = (await global.utils.getPoolResources(req, instance.pool)).pci.nodes[params.node]; // we assume that the node list is used. TODO support global lists
|
||||
if (poolAvailPci === undefined) { // user has no available devices on this node, so send an empty list
|
||||
res.status(200).send([]);
|
||||
res.end();
|
||||
@@ -93,12 +90,14 @@ router.get("/:hostpci", async (req, res) => {
|
||||
vmid: req.params.vmid,
|
||||
hostpci: req.params.hostpci
|
||||
};
|
||||
|
||||
// check auth for specific instance
|
||||
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
||||
const auth = await checkAuth(req.cookies, res, vmpath);
|
||||
const auth = await global.utils.checkAuth(req.cookies, res, vmpath);
|
||||
if (!auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get device
|
||||
const device = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||
if (!device) {
|
||||
@@ -135,22 +134,27 @@ router.post("/:hostpci/modify", async (req, res) => {
|
||||
device: req.body.device,
|
||||
pcie: req.body.pcie
|
||||
};
|
||||
|
||||
// check if type is qemu
|
||||
if (params.type !== "qemu") {
|
||||
res.status(500).send({ error: "Type must be qemu (vm)." });
|
||||
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);
|
||||
const auth = await global.utils.checkAuth(req.cookies, res, vmpath);
|
||||
if (!auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get instance config for pool membership
|
||||
const instance = await global.pve.getInstance(params.node, params.vmid);
|
||||
|
||||
// force all functions
|
||||
params.device = params.device.split(".")[0];
|
||||
|
||||
// device must exist to be modified
|
||||
const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||
if (!existingDevice) {
|
||||
@@ -158,6 +162,7 @@ router.post("/:hostpci/modify", async (req, res) => {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// 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_bus.split(".")[0] !== params.device) {
|
||||
@@ -171,7 +176,7 @@ router.post("/:hostpci/modify", async (req, res) => {
|
||||
return;
|
||||
}
|
||||
// check resource approval
|
||||
const { approved } = await approveResources(req, userObj, params.node, instance.pool, request);
|
||||
const { approved } = await global.utils.approveResources(req, userObj, params.node, instance.pool, request);
|
||||
if (!approved) {
|
||||
res.status(500).send({ request, error: `Could not fulfil request for ${requestedDevice.device_name}.` });
|
||||
res.end();
|
||||
@@ -184,9 +189,11 @@ router.post("/:hostpci/modify", async (req, res) => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// setup action
|
||||
const action = {};
|
||||
action[`${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
|
||||
|
||||
// commit action
|
||||
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
|
||||
await global.pve.handleResponse(params.node, result, res);
|
||||
@@ -217,22 +224,27 @@ router.post("/:hostpci/create", async (req, res) => {
|
||||
device: req.body.device,
|
||||
pcie: req.body.pcie
|
||||
};
|
||||
|
||||
// check if type is qemu
|
||||
if (params.type !== "qemu") {
|
||||
res.status(500).send({ error: "Type must be qemu (vm)." });
|
||||
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);
|
||||
const auth = await global.utils.checkAuth(req.cookies, res, vmpath);
|
||||
if (!auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get instance config for pool membership
|
||||
const instance = await global.pve.getInstance(params.node, params.vmid);
|
||||
|
||||
// force all functions
|
||||
params.device = params.device.split(".")[0];
|
||||
|
||||
// device must not exist to be added
|
||||
const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||
if (existingDevice) {
|
||||
@@ -240,27 +252,32 @@ router.post("/:hostpci/create", async (req, res) => {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// setup request
|
||||
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);
|
||||
const { approved } = await approveResources(req, userObj, params.node, instance.pool, request);
|
||||
const { approved } = await global.utils.approveResources(req, userObj, params.node, instance.pool, request);
|
||||
if (!approved) {
|
||||
res.status(500).send({ request, error: `Could not fulfil request for ${requestedDevice.device_name}.` });
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// check node availability
|
||||
if (!Object.values(node.devices).some(element => element.device_bus.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[`${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
|
||||
|
||||
// commit action
|
||||
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
|
||||
await global.pve.handleResponse(params.node, result, res);
|
||||
@@ -288,18 +305,21 @@ router.delete("/:hostpci/delete", async (req, res) => {
|
||||
vmid: req.params.vmid,
|
||||
hostpci: req.params.hostpci
|
||||
};
|
||||
|
||||
// check if type is qemu
|
||||
if (params.type !== "qemu") {
|
||||
res.status(500).send({ error: "Type must be qemu (vm)." });
|
||||
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);
|
||||
const auth = await global.utils.checkAuth(req.cookies, res, vmpath);
|
||||
if (!auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
// check device is in instance config
|
||||
const device = global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||
if (!device) {
|
||||
@@ -307,8 +327,10 @@ router.delete("/:hostpci/delete", async (req, res) => {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// setup action
|
||||
const action = { delete: `${params.hostpci}` };
|
||||
|
||||
// commit action, need to use root user here because proxmox api only allows root to modify hostpci for whatever reason
|
||||
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
|
||||
await global.pve.handleResponse(params.node, result, res);
|
||||
|
||||
Reference in new issue
Block a user