2023-02-27 01:09:49 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
|
|
|
|
filename = "localdb.json";
|
|
|
|
|
2023-02-27 01:28:01 +00:00
|
|
|
let db = {};
|
|
|
|
|
2023-02-26 08:36:27 +00:00
|
|
|
/**
|
|
|
|
* called at app startup, can be used to initialize any variables needed for database access
|
|
|
|
*/
|
2023-02-27 01:28:01 +00:00
|
|
|
function init () {
|
|
|
|
try {
|
2023-02-28 23:24:49 +00:00
|
|
|
db = JSON.parse(fs.readFileSync(filename));
|
2023-02-27 01:28:01 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
fs.writeFileSync(filename, JSON.stringify(db));
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 08:36:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* user requests additional resources specified in k-v pairs
|
|
|
|
* @param {string} user user's proxmox username in the form username@authrealm
|
|
|
|
* @param {Object} resources k-v pairs with resource name as keys and resource ammount as values
|
2023-02-27 01:09:49 +00:00
|
|
|
* @returns {boolean} whether the user is approved to allocate requested resources
|
2023-02-26 08:36:27 +00:00
|
|
|
*/
|
2023-02-27 01:28:01 +00:00
|
|
|
function requestResources (user, resources) {
|
2023-02-28 23:24:49 +00:00
|
|
|
let approved = true;
|
|
|
|
Object.keys(resources).forEach((element) => {
|
2023-04-11 18:00:04 +00:00
|
|
|
if(!(element in db[user].available)) { // if the resource does not exist in the user's entry, assume the user is not allowed to use it
|
2023-03-01 00:50:24 +00:00
|
|
|
approved = false;
|
|
|
|
}
|
2023-04-11 18:00:04 +00:00
|
|
|
else if (db[user].available[element] - resources[element] < 0) {
|
2023-02-28 23:24:49 +00:00
|
|
|
approved = false;
|
2023-02-27 01:28:01 +00:00
|
|
|
}
|
|
|
|
});
|
2023-02-28 23:24:49 +00:00
|
|
|
return approved;
|
2023-02-27 01:28:01 +00:00
|
|
|
}
|
2023-02-26 08:36:27 +00:00
|
|
|
|
2023-02-27 01:09:49 +00:00
|
|
|
/**
|
|
|
|
* user allocates additional resources specified in k-v pairs
|
|
|
|
* @param {string} user user's proxmox username in the form username@authrealm
|
|
|
|
* @param {Object} resources k-v pairs with resource name as keys and resource ammount as values
|
|
|
|
* @returns {boolean} true if resources were successfully allocated, false otherwise
|
|
|
|
*/
|
2023-02-27 01:28:01 +00:00
|
|
|
function allocateResources (user, resources) {
|
2023-02-27 23:11:51 +00:00
|
|
|
let newdb = {};
|
|
|
|
Object.assign(newdb, db);
|
2023-02-28 23:24:49 +00:00
|
|
|
Object.keys(resources).forEach((element) => {
|
2023-03-24 22:22:00 +00:00
|
|
|
if(typeof(resources[element]) === "number" && isFinite(resources[element])) {
|
2023-04-11 18:00:04 +00:00
|
|
|
newdb[user].available[element] -= resources[element];
|
2023-03-24 19:35:16 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-27 01:28:01 +00:00
|
|
|
});
|
|
|
|
try {
|
2023-02-28 23:24:49 +00:00
|
|
|
fs.writeFileSync(filename, JSON.stringify(newdb));
|
2023-02-27 23:11:51 +00:00
|
|
|
Object.assign(db, newdb);
|
2023-02-27 01:28:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch {
|
2023-02-28 23:24:49 +00:00
|
|
|
fs.writeFileSync(filename, JSON.stringify(db))
|
2023-02-27 01:28:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 01:09:49 +00:00
|
|
|
|
2023-02-26 08:36:27 +00:00
|
|
|
/**
|
|
|
|
* user releases allocated resources specified in k-v pairs
|
|
|
|
* @param {string} user user's proxmox username in the form username@authrealm
|
|
|
|
* @param {Object} resources k-v pairs with resource name as keys and resource ammount as values
|
2023-02-27 01:09:49 +00:00
|
|
|
* @returns {boolean} true if resources were successfully deallocated, false otherwise
|
2023-02-26 08:36:27 +00:00
|
|
|
*/
|
2023-02-27 01:28:01 +00:00
|
|
|
function releaseResources (user, resources) {
|
2023-02-27 23:11:51 +00:00
|
|
|
let newdb = {};
|
|
|
|
Object.assign(newdb, db);
|
2023-02-28 23:24:49 +00:00
|
|
|
Object.keys(resources).forEach((element) => {
|
2023-03-24 22:22:00 +00:00
|
|
|
if(typeof(resources[element]) === "number" && isFinite(resources[element]) && resources[element]) {
|
2023-04-11 18:00:04 +00:00
|
|
|
newdb[user].available[element] += resources[element];
|
2023-03-24 19:35:16 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-27 01:28:01 +00:00
|
|
|
});
|
|
|
|
try {
|
2023-02-28 23:24:49 +00:00
|
|
|
fs.writeFileSync(filename, JSON.stringify(newdb));
|
2023-02-27 23:11:51 +00:00
|
|
|
Object.assign(db, newdb);
|
2023-02-27 01:28:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch {
|
2023-02-28 23:24:49 +00:00
|
|
|
fs.writeFileSync(filename, JSON.stringify(db))
|
2023-02-27 01:28:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 08:36:27 +00:00
|
|
|
|
2023-04-03 21:45:06 +00:00
|
|
|
/**
|
|
|
|
* 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};
|