From 8862144524fac8401707f78b393d77ebfd5a1660 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 3 Apr 2023 21:45:06 +0000 Subject: [PATCH] add path to get remaining resources Signed-off-by: Arthur Lu --- db.js | 15 ++++++++++++++- main.js | 12 +++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/db.js b/db.js index 6db346b..6ef82e9 100644 --- a/db.js +++ b/db.js @@ -91,4 +91,17 @@ function releaseResources (user, resources) { } } -module.exports = {init, requestResources, allocateResources, releaseResources}; \ No newline at end of file +/** + * 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}; \ No newline at end of file diff --git a/main.js b/main.js index 687ddbd..5e75467 100644 --- a/main.js +++ b/main.js @@ -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}`;