add path to get remaining resources

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-04-03 21:45:06 +00:00
parent 6bda62ae15
commit 4b37ababda
2 changed files with 25 additions and 2 deletions

15
db.js
View File

@ -91,4 +91,17 @@ function releaseResources (user, resources) {
}
}
module.exports = {init, requestResources, allocateResources, releaseResources};
/**
* return a read only copy of the user resources
* @param {string} user user's proxmox username in the form username@authrealm
* @returns {Object} user's remaining resources as k-v pairs with resource name as keys and resource ammount as values
*/
function getResources (user) {
let returnVal = {};
if(user in db) {
Object.assign(returnVal, db[user]);
}
return returnVal;
}
module.exports = {init, requestResources, allocateResources, releaseResources, getResources};

12
main.js
View File

@ -8,7 +8,7 @@ var api = require("./package.json");
const {pveAPIToken, listenPort, domain} = require("./vars.js");
const {checkAuth, requestPVE, handleResponse, getUnusedDiskData, getDiskConfig} = require("./pveutils.js");
const {init, requestResources, allocateResources, releaseResources} = require("./db.js");
const {init, requestResources, allocateResources, releaseResources, getResources} = require("./db.js");
const app = express();
app.use(helmet());
@ -43,6 +43,16 @@ app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST prox
res.send(result.data, result.status);
});
app.get("/api/user/resources", async(req, res) => {
let auth = await checkAuth(req.cookies, vmpath);
if (!auth) {
res.status(401).send({auth: auth});
return;
}
return getResources(req.cookies.username);
});
app.post("/api/disk/detach", async (req, res) => {
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;