simplify requestPVE args

This commit is contained in:
2023-09-12 19:16:19 +00:00
parent 8912ae5f3c
commit 40f36270aa
10 changed files with 66 additions and 60 deletions
+10 -10
View File
@@ -37,7 +37,7 @@ router.post("/:disk/detach", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data;
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist
if (!config[params.disk]) {
res.status(500).send({ error: `Disk ${params.disk} does not exist.` });
@@ -52,7 +52,7 @@ router.post("/:disk/detach", async (req, res) => {
}
const action = JSON.stringify({ delete: params.disk });
const method = params.type === "qemu" ? "POST" : "PUT";
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
@@ -86,7 +86,7 @@ router.post("/:disk/attach", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data;
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist
if (!config[`unused${params.source}`]) {
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
@@ -107,7 +107,7 @@ router.post("/:disk/attach", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
@@ -160,7 +160,7 @@ router.post("/:disk/resize", async (req, res) => {
}
// action approved, commit to action
const action = JSON.stringify({ disk: params.disk, size: `+${params.size}G` });
const result = await requestPVE(`${vmpath}/resize`, "PUT", req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/resize`, "PUT", { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
@@ -227,7 +227,7 @@ router.post("/:disk/move", async (req, res) => {
action = JSON.stringify(action);
const route = params.type === "qemu" ? "move_disk" : "move_volume";
// commit action
const result = await requestPVE(`${vmpath}/${route}`, "POST", req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/${route}`, "POST", { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
@@ -259,7 +259,7 @@ router.delete("/:disk/delete", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data;
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist
if (!config[params.disk]) {
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
@@ -276,7 +276,7 @@ router.delete("/:disk/delete", async (req, res) => {
const action = JSON.stringify({ delete: params.disk });
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
@@ -314,7 +314,7 @@ router.post("/:disk/create", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data;
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must not exist
if (config[params.disk]) {
res.status(403).send({ error: `Requested disk ${params.disk} already exists.` });
@@ -354,6 +354,6 @@ router.post("/:disk/create", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
+6 -6
View File
@@ -41,7 +41,7 @@ router.post("/:netid/create", async (req, res) => {
return;
}
// get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", null, null, pveAPIToken);
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
// net interface must not exist
if (currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} already exists.` });
@@ -74,7 +74,7 @@ router.post("/:netid/create", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
@@ -109,7 +109,7 @@ router.post("/:netid/modify", async (req, res) => {
return;
}
// get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", null, null, pveAPIToken);
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
// net interface must already exist
if (!currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
@@ -133,7 +133,7 @@ router.post("/:netid/modify", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
@@ -165,7 +165,7 @@ router.delete("/:netid/delete", async (req, res) => {
return;
}
// get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", null, null, pveAPIToken);
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
// net interface must already exist
if (!currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
@@ -176,6 +176,6 @@ router.delete("/:netid/delete", async (req, res) => {
const action = JSON.stringify({ delete: `net${params.netid}` });
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
});
+10 -10
View File
@@ -37,7 +37,7 @@ router.get("/:hostpci", async (req, res) => {
return;
}
// check device is in instance config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies)).data.data;
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end();
@@ -95,7 +95,7 @@ router.post("/:hostpci/modify", async (req, res) => {
// force all functions
params.device = params.device.split(".")[0];
// get instance config to check if device has not changed
const config = (await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", params.cookies, null, pveAPIToken)).data.data;
const config = (await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken })).data.data;
const currentDeviceData = await getDeviceInfo(params.node, params.type, params.vmid, config[`hostpci${params.hostpci}`].split(",")[0]);
if (!currentDeviceData) {
res.status(500).send({ error: `No device in hostpci${params.hostpci}.` });
@@ -126,7 +126,7 @@ router.post("/:hostpci/modify", async (req, res) => {
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
action = JSON.stringify(action);
// commit action
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot), null);
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot));
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
@@ -136,7 +136,7 @@ router.post("/:hostpci/modify", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await requestPVE(`${vmpath}/config`, "POST", rootcookies, action, null);
const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res);
});
@@ -178,7 +178,7 @@ router.post("/create", async (req, res) => {
// force all functions
params.device = params.device.split(".")[0];
// get instance config to find next available hostpci slot
const config = requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", params.cookies, null, null);
const config = requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { cookies: params.cookies });
let hostpci = 0;
while (config[`hostpci${hostpci}`]) {
hostpci++;
@@ -206,7 +206,7 @@ router.post("/create", async (req, res) => {
action[`hostpci${hostpci}`] = `${params.device},pcie=${params.pcie}`;
action = JSON.stringify(action);
// commit action
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot), null);
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot));
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
@@ -216,7 +216,7 @@ router.post("/create", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await requestPVE(`${vmpath}/config`, "POST", rootcookies, action, null);
const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res);
});
@@ -254,7 +254,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
return;
}
// check device is in instance config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies)).data.data;
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end();
@@ -263,7 +263,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
// setup action
const action = JSON.stringify({ delete: `hostpci${params.hostpci}` });
// commit action, need to use root user here because proxmox api only allows root to modify hostpci for whatever reason
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot), null);
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot));
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
@@ -273,6 +273,6 @@ router.delete("/:hostpci/delete", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await requestPVE(`${vmpath}/config`, "POST", rootcookies, action, null);
const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res);
});