comment a bunch of code

This commit is contained in:
2023-07-10 06:50:29 +00:00
parent cd611e2590
commit 445e702d8d
4 changed files with 88 additions and 8 deletions

View File

@@ -1,6 +1,15 @@
import axios from "axios";
import { pveAPI, pveAPIToken } from "./db.js";
/**
* Send HTTP request to proxmox API. Allows requests to be made with user cookie credentials or an API token for controlled priviledge elevation.
* @param {string} path HTTP path, prepended with the proxmox API base path.
* @param {string} method HTTP method.
* @param {Object} cookies user cookies for authorization if an API token is not used. Safest option for authentication.
* @param {string} body body parameters and data to be sent. Optional.
* @param {string} token proxmox API token to be used for controled priviledge elevation, allows user requests to perform admin actions safely. Optional
* @returns {Obejct} HTTP response object or HTTP error object
*/
export async function requestPVE (path, method, cookies, body = null, token = null) {
const url = `${pveAPI}${path}`;
const content = {
@@ -33,6 +42,14 @@ export async function requestPVE (path, method, cookies, body = null, token = nu
}
}
/**
* Handle various proxmox API responses. Handles sync and async responses.
* In sync responses, responses are completed when the response arrives. Method returns the response directly.
* In async responses, proxmox sends responses with a UPID to track process completion. Method returns the status of the proxmox process once it completes.
* @param {string} node response originates from.
* @param {Object} result response from proxmox.
* @param {Object} res response object of ProxmoxAAS API call.
*/
export async function handleResponse (node, result, res) {
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
if (result.data.data && typeof (result.data.data) === "string" && result.data.data.startsWith("UPID:")) {
@@ -63,6 +80,12 @@ export async function handleResponse (node, result, res) {
}
}
/**
* Get the amount of resources used by specified user.
* @param {Object} req ProxmoxAAS API request object.
* @param {Object} resourceMeta data about application resources, to indicate which resources are tracked.
* @returns {Object} k-v pairs of resource name and used amounts
*/
export async function getUsedResources (req, resourceMeta) {
const response = await requestPVE("/cluster/resources", "GET", req.cookies);
const used = {};
@@ -110,6 +133,14 @@ export async function getUsedResources (req, resourceMeta) {
return used;
}
/**
* Get meta data for a specific disk. Adds info that is not normally available in a instance's config.
* @param {string} node containing the query disk.
* @param {string} type of instance with query disk.
* @param {string} vmid of instance with query disk
* @param {string} disk name of the query disk, ie. sata0.
* @returns {Objetc} k-v pairs of specific disk data, including storage and size of unused disks.
*/
export async function getDiskInfo (node, type, vmid, disk) {
try {
const config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
@@ -124,6 +155,14 @@ export async function getDiskInfo (node, type, vmid, disk) {
}
}
/**
* Get meta data for a specific pci device. Adds info that is not normally available in a instance's config.
* @param {string} node containing the query device.
* @param {string} type of instance with query device.
* @param {string} vmid of instance with query device.
* @param {string} qid pci bus id number of the query device, ie. 89ab:cd:ef.0.
* @returns {Object} k-v pairs of specific device data, including device name and manufacturer.
*/
export async function getDeviceInfo (node, type, vmid, qid) {
try {
const result = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", null, null, pveAPIToken)).data.data;
@@ -145,6 +184,12 @@ export async function getDeviceInfo (node, type, vmid, qid) {
}
}
/**
* Get available devices on specific node.
* @param {string} node to get devices from.
* @param {Object} cookies user authentication, unused since the API token is required.
* @returns {Array.<Object>} array of k-v pairs of specific device data, including device name and manufacturer, which are available on the specified node.
*/
export async function getNodeAvailDevices (node, cookies) {
// get node pci devices
let nodeAvailPci = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", cookies, null, pveAPIToken)).data.data;