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, "compact": true,
"unit": "B" "unit": "B"
}, },
"swap": {
"type": "numeric",
"multiplier": 1048576,
"compact": true,
"unit": "B"
},
"local": { "local": {
"type": "storage", "type": "storage",
"multiplier": 1, "multiplier": 1,
"compact": true, "compact": true,
"unit": "B", "unit": "B",
"disks": ["rootfs", "mp", "sata", "unused"] "disks": [
"rootfs",
"mp",
"sata",
"unused"
]
}, },
"cephpl": { "cephpl": {
"type": "storage", "type": "storage",
"multiplier": 1, "multiplier": 1,
"compact": true, "compact": true,
"unit": "B", "unit": "B",
"disks": ["rootfs", "mp", "sata", "unused"] "disks": [
"rootfs",
"mp",
"sata",
"unused"
]
},
"network": {
"type": "network",
"multiplier": 1,
"compact": true,
"unit": "MB/s"
} }
}, },
"users": { "users": {
@ -33,26 +55,28 @@
"max": { "max": {
"cores": 128, "cores": 128,
"memory": 131072, "memory": 131072,
"swap": 131072,
"local": 1099511627776, "local": 1099511627776,
"cephpl": 1099511627776 "cephpl": 1099511627776,
"network": 131072
} }
}, },
"nodes": [ "nodes": [
"nodeid-1", "node1",
"nodeid-2" "node2"
], ],
"instances": { "instances": {
"vmid": { "vmid": {
"min": 100, "min": 100,
"max": 199 "max": 199
}, },
"pool": "examplegroup", "pool": "examplepool",
"templates": { "templates": {
"lxc": { "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": { "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); 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) => { app.post("/api/instance/resources", async (req, res) => {
// check auth // check auth
let auth = await checkAuth(req.cookies, res); let auth = await checkAuth(req.cookies, res);