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 {
|
|
|
|
db = fs.readFileSync(filename);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
Object.keys(resources).forEach((element) => {
|
|
|
|
if (db[user][element] < resources[element]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
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) {
|
|
|
|
Object.keys(resources).forEach((element) => {
|
|
|
|
db[user][element] -= resource[element];
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(filename, db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
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) {
|
|
|
|
Object.keys(resources).forEach((element) => {
|
|
|
|
db[user][element] += resource[element];
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(filename, db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 08:36:27 +00:00
|
|
|
|
|
|
|
module.exports = {init, requestResources, releaseResources};
|