add /cluster/statushash endpoint

This commit is contained in:
Arthur Lu 2023-07-11 18:48:45 +00:00
parent faa0efa75c
commit 096be3d032
2 changed files with 30 additions and 4 deletions

View File

@ -6,7 +6,7 @@ import morgan from "morgan";
import { api } from "./package.js";
import { requestPVE, handleResponse, getDiskInfo, getDeviceInfo, getNodeAvailDevices } from "./pve.js";
import { checkAuth, approveResources, getUserResources } from "./utils.js";
import { checkAuth, approveResources, getUserResources, getObjectHash } from "./utils.js";
import { db, pveAPIToken, listenPort, hostname, domain } from "./db.js";
const app = express();
@ -1198,11 +1198,28 @@ app.delete(`/api/:node(${nodeRegexP})/:type(${typeRegexP})/:vmid(${vmidRegexP})/
/**
* GET - get hash of current cluster resources states
* Client can use this endpoint to check for cluster state changes to avoid costly data transfers to the client.
* responses:
* - string
* - 401: {auth: false}
* - 200: string
*/
app.get(`/cluster/statushash`, async (req, res) => {
app.get(`/api/cluster/statushash`, async (req, res) => {
// check auth
const auth = await checkAuth(req.cookies, res);
if (!auth) {
return;
}
// get current cluster resources
let status = (await requestPVE("/cluster/resources", "GET", req.cookies)).data.data;
// filter out just state information of resources that are needed
let resources = ["lxc", "qemu", "node"];
let state = {};
status.forEach((element) => {
if (resources.includes(element.type)) {
state[element.id] = element.status;
}
});
res.status(200).send(getObjectHash(state));
});
app.listen(listenPort, () => {

View File

@ -1,3 +1,5 @@
import {createHash} from "crypto";
import { getUsedResources, requestPVE } from "./pve.js";
import { db } from "./db.js";
@ -91,3 +93,10 @@ export async function approveResources (req, username, request) {
});
return approved; // if all requested resources pass, allow
}
export function getObjectHash (object, alg = "sha256", format = "hex") {
const hash = createHash(alg);
hash.update(JSON.stringify(object, Object.keys(object).sort()));
return hash.digest(format);
}