implement network interface endpoint

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-05-16 16:34:39 +00:00
parent e12e245abd
commit f236ae544b
2 changed files with 58 additions and 8 deletions

View File

@ -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"
}
}
}

26
main.js
View File

@ -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);