From f236ae544bc72262711eecc5b8980a4cd618aa15 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 16 May 2023 16:34:39 +0000 Subject: [PATCH] implement network interface endpoint Signed-off-by: Arthur Lu --- localdb.json.template | 40 ++++++++++++++++++++++++++++++++-------- main.js | 26 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/localdb.json.template b/localdb.json.template index 2021e31..a488fb4 100644 --- a/localdb.json.template +++ b/localdb.json.template @@ -12,19 +12,41 @@ "compact": true, "unit": "B" }, + "swap": { + "type": "numeric", + "multiplier": 1048576, + "compact": true, + "unit": "B" + }, "local": { "type": "storage", "multiplier": 1, "compact": true, "unit": "B", - "disks": ["rootfs", "mp", "sata", "unused"] + "disks": [ + "rootfs", + "mp", + "sata", + "unused" + ] }, "cephpl": { "type": "storage", "multiplier": 1, "compact": true, "unit": "B", - "disks": ["rootfs", "mp", "sata", "unused"] + "disks": [ + "rootfs", + "mp", + "sata", + "unused" + ] + }, + "network": { + "type": "network", + "multiplier": 1, + "compact": true, + "unit": "MB/s" } }, "users": { @@ -33,26 +55,28 @@ "max": { "cores": 128, "memory": 131072, + "swap": 131072, "local": 1099511627776, - "cephpl": 1099511627776 + "cephpl": 1099511627776, + "network": 131072 } }, "nodes": [ - "nodeid-1", - "nodeid-2" + "node1", + "node2" ], "instances": { "vmid": { "min": 100, "max": 199 }, - "pool": "examplegroup", + "pool": "examplepool", "templates": { "lxc": { - "net0": "name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,tag=10,type=veth" + "net0": "name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,tag=10,type=veth,rate=1000" }, "qemu": { - "net0": "virtio,bridge=vmbr0,tag=10" + "net0": "virtio,bridge=vmbr0,tag=10,rate=1000" } } } diff --git a/main.js b/main.js index d4ee831..d81289a 100644 --- a/main.js +++ b/main.js @@ -238,6 +238,32 @@ app.post("/api/instance/disk/create", async (req, res) => { await handleResponse(req.body.node, result, res); }); +app.post("/api/instance/network", async (req, res) => { + // check auth + let auth = await checkAuth(req.cookies, res); + if (!auth) { return; } + // get current config + let currentConfig = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, "GET", null, null, pveAPIToken); + let currentNetworkConfig = currentConfig.data.data[`net${req.body.netid}`]; + let currentNetworkRate = currentNetworkConfig.split("rate=")[1].split(",")[0]; + let request = { + network: Number(req.body.rate) - Number(currentNetworkRate) + }; + // check resource approval + if (!await approveResources(req, req.cookies.username, request)) { + res.status(500).send({ request: request, error: `Could not fulfil request` }); + res.end(); + return; + } + // commit action + let action = {}; + action[`net${req.body.netid}`] = currentNetworkConfig.replace(`rate=${currentNetworkRate}`, `rate=${req.body.rate}`); + action = JSON.stringify(action); + let method = req.body.type === "qemu" ? "POST" : "PUT"; + let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, method, req.cookies, action, pveAPIToken); + await handleResponse(req.body.node, result, res); +}); + app.post("/api/instance/resources", async (req, res) => { // check auth let auth = await checkAuth(req.cookies, res);