implement quick input checks

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-03-24 19:35:16 +00:00
parent b7018aae33
commit 7272bbe683
2 changed files with 25 additions and 10 deletions

15
db.js
View File

@ -45,8 +45,12 @@ function allocateResources (user, resources) {
let newdb = {}; let newdb = {};
Object.assign(newdb, db); Object.assign(newdb, db);
Object.keys(resources).forEach((element) => { Object.keys(resources).forEach((element) => {
console.log(newdb[user][element] + "-" + resources[element]) if(resources[element] instanceof Number && isFinite(resources[element])) {
newdb[user][element] -= resources[element]; newdb[user][element] -= resources[element];
}
else {
return false;
}
}); });
try { try {
fs.writeFileSync(filename, JSON.stringify(newdb)); fs.writeFileSync(filename, JSON.stringify(newdb));
@ -69,7 +73,12 @@ function releaseResources (user, resources) {
let newdb = {}; let newdb = {};
Object.assign(newdb, db); Object.assign(newdb, db);
Object.keys(resources).forEach((element) => { Object.keys(resources).forEach((element) => {
newdb[user][element] += resources[element]; if(resources[element] instanceof Number && isFinite(resources[element]) && resources[element]) {
newdb[user][element] += resources[element];
}
else {
return false;
}
}); });
try { try {
fs.writeFileSync(filename, JSON.stringify(newdb)); fs.writeFileSync(filename, JSON.stringify(newdb));

20
main.js
View File

@ -102,7 +102,7 @@ app.post("/api/disk/resize", async (req, res) => {
} }
let storage = diskConfig.split(":")[0]; // get the storage let storage = diskConfig.split(":")[0]; // get the storage
let request = {}; let request = {};
request[storage] = req.body.size; // setup request object request[storage] = Number(req.body.size); // setup request object
if (!requestResources(req.cookies.username, request)) { // check request approval if (!requestResources(req.cookies.username, request)) { // check request approval
res.status(500).send({auth: auth, data:{request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`}}); res.status(500).send({auth: auth, data:{request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`}});
return; return;
@ -142,9 +142,9 @@ app.post("/api/disk/move", async (req, res) => {
let request = {}; let request = {};
let release = {}; let release = {};
if (req.body.delete) { // if delete is true, increase resource used by the source storage if (req.body.delete) { // if delete is true, increase resource used by the source storage
release[srcStorage] = size; release[srcStorage] = Number(size);
} }
request[dstStorage] = size; // always decrease destination storage by size request[dstStorage] = Number(size); // always decrease destination storage by size
if (!requestResources(req.cookies.username, request)) { // check resource approval if (!requestResources(req.cookies.username, request)) { // check resource approval
res.status(500).send({auth: auth, data:{request: request, release: release, error: `Storage ${dstStorage} could not fulfill request of size ${size}G.`}}); res.status(500).send({auth: auth, data:{request: request, release: release, error: `Storage ${dstStorage} could not fulfill request of size ${size}G.`}});
return; return;
@ -191,7 +191,7 @@ app.post("/api/disk/delete", async (req, res) => {
let diskConfig = await getUnusedDiskData(req.body.node, req.body.type, req.body.vmid, req.body.disk); // get disk config of unused disk let diskConfig = await getUnusedDiskData(req.body.node, req.body.type, req.body.vmid, req.body.disk); // get disk config of unused disk
let storage = diskConfig.storage; // get disk storage let storage = diskConfig.storage; // get disk storage
let size = diskConfig.size / (1024**3); // get disk size let size = diskConfig.size / (1024**3); // get disk size
release[storage] = size; release[storage] = Number(size);
} }
let action = JSON.stringify({delete: req.body.disk}); let action = JSON.stringify({delete: req.body.disk});
@ -220,7 +220,7 @@ app.post("/api/disk/create", async (req, res) => {
// check resource allocation // check resource allocation
let request = {}; let request = {};
if (!req.body.disk.includes("ide")) { if (!req.body.disk.includes("ide")) {
request[req.body.storage] = req.body.size; // setup request object request[req.body.storage] = Number(req.body.siz); // setup request object
if (!requestResources(req.cookies.username, request)) { // check request approval if (!requestResources(req.cookies.username, request)) { // check request approval
res.status(500).send({auth: auth, data:{request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`}}); res.status(500).send({auth: auth, data:{request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`}});
return; return;
@ -261,7 +261,10 @@ app.post("/api/resources", async (req, res) => {
} }
let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken); let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken);
let request = {cores: req.body.cores - currentConfig.data.data.cores, memory: req.body.memory - currentConfig.data.data.memory}; let request = {
cores: Number(req.body.cores) - currentConfig.data.data.cores,
memory: Number(req.body.memory) - currentConfig.data.data.memory
};
if (!requestResources(req.cookies.username, request)) { // check resource approval if (!requestResources(req.cookies.username, request)) { // check resource approval
res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}}); res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}});
return; return;
@ -289,7 +292,10 @@ app.post("/api/instance", async (req, res) => {
} }
// setup request // setup request
let request = {cores: req.body.cores, memory: req.body.memory}; let request = {
cores: Number(req.body.cores),
memory: Number(req.body.memory)
};
// setup action // setup action
let user = await requestPVE(`/access/users/${req.cookies.username}`, "GET", null, null, pveAPIToken); let user = await requestPVE(`/access/users/${req.cookies.username}`, "GET", null, null, pveAPIToken);