improve api responses
Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
parent
1c27b3c2a8
commit
8118a31ef6
30
main.js
30
main.js
@ -57,7 +57,7 @@ app.post("/api/disk/detach", async (req, res) => {
|
|||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, status: result.status, data: result.data});
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/attach", async (req, res) => {
|
app.post("/api/disk/attach", async (req, res) => {
|
||||||
@ -76,7 +76,7 @@ app.post("/api/disk/attach", async (req, res) => {
|
|||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, status: result.status, data: result.data});
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/resize", async (req, res) => {
|
app.post("/api/disk/resize", async (req, res) => {
|
||||||
@ -116,7 +116,7 @@ app.post("/api/disk/move", async (req, res) => {
|
|||||||
|
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
let auth = await checkAuth(req.cookies, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
res.send({auth: auth});
|
res.status(401).send({auth: auth});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ app.post("/api/disk/move", async (req, res) => {
|
|||||||
|
|
||||||
let result = await requestPVE(`${vmpath}/${route}`, "POST", req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`${vmpath}/${route}`, "POST", req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, data: result.data}, result.status);
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/delete", async (req, res) => {
|
app.post("/api/disk/delete", async (req, res) => {
|
||||||
@ -139,7 +139,7 @@ app.post("/api/disk/delete", async (req, res) => {
|
|||||||
|
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
let auth = await checkAuth(req.cookies, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
res.send({auth: auth});
|
res.status(401).send({auth: auth});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ app.post("/api/disk/delete", async (req, res) => {
|
|||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, data: result.data}, result.status);
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/create", async (req, res) => {
|
app.post("/api/disk/create", async (req, res) => {
|
||||||
@ -156,7 +156,7 @@ app.post("/api/disk/create", async (req, res) => {
|
|||||||
|
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
let auth = await checkAuth(req.cookies, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
res.send({auth: auth});
|
res.status(401).send({auth: auth});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,7 +175,7 @@ app.post("/api/disk/create", async (req, res) => {
|
|||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, data: result.data}, result.status);
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/resources", async (req, res) => {
|
app.post("/api/resources", async (req, res) => {
|
||||||
@ -183,7 +183,7 @@ app.post("/api/resources", async (req, res) => {
|
|||||||
|
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
let auth = await checkAuth(req.cookies, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
res.send({auth: auth});
|
res.status(401).send({auth: auth});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,20 +191,20 @@ app.post("/api/resources", async (req, res) => {
|
|||||||
action = JSON.stringify({cores: req.body.cores, memory: req.body.memory});
|
action = JSON.stringify({cores: req.body.cores, memory: req.body.memory});
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, data: result.data}, result.status);
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/instance", async (req, res) => {
|
app.post("/api/instance", async (req, res) => {
|
||||||
let auth = await checkAuth(req.cookies);
|
let auth = await checkAuth(req.cookies);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
res.send({auth: auth});
|
res.status(401).send({auth: auth});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
let group = user.data.data.groups[0];
|
let group = user.data.data.groups[0];
|
||||||
if (!group) {
|
if (!group) {
|
||||||
res.send({auth: auth, data: {error: `user ${req.cookies.username} has no group membership`}});
|
res.status(500).send({auth: auth, data: {error: `user ${req.cookies.username} has no group membership`}});
|
||||||
}
|
}
|
||||||
|
|
||||||
let action = {
|
let action = {
|
||||||
@ -229,7 +229,7 @@ app.post("/api/instance", async (req, res) => {
|
|||||||
|
|
||||||
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}`, "POST", req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}`, "POST", req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, data: result.data}, result.status);
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete("/api/instance", async (req, res) => {
|
app.delete("/api/instance", async (req, res) => {
|
||||||
@ -237,13 +237,13 @@ app.delete("/api/instance", async (req, res) => {
|
|||||||
|
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
let auth = await checkAuth(req.cookies, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
res.send({auth: auth});
|
res.status(401).send({auth: auth});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken);
|
let result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
result = await handleResponse(req.body.node, result);
|
||||||
res.send({auth: auth, data: result.data}, result.status);
|
res.status(result.status).send({auth: auth, data: result.data});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(listenPort, () => {
|
app.listen(listenPort, () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user