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}`;