mirror of
https://github.com/unraid/api.git
synced 2026-01-12 03:30:08 -06:00
feat: add vms count
This commit is contained in:
63
app/core/modules/info/get-vms-count.ts
Normal file
63
app/core/modules/info/get-vms-count.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
/*!
|
||||
* Copyright 2019-2020 Lime Technology Inc. All rights reserved.
|
||||
* Written by: Alexis Tyler
|
||||
*/
|
||||
|
||||
import { ConnectListAllDomainsFlags } from '@vmngr/libvirt';
|
||||
import { CoreResult, CoreContext } from '../../types';
|
||||
import { ensurePermission, getHypervisor } from '../../utils';
|
||||
|
||||
/**
|
||||
* Two arrays containing the installed and started VMs.
|
||||
*
|
||||
* @param installed The amount of installed VMs.
|
||||
* @param started The amount of running VMs.
|
||||
* @interface Result
|
||||
* @extends {CoreResult}
|
||||
*/
|
||||
interface Result extends CoreResult {
|
||||
json: {
|
||||
installed: number;
|
||||
started: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count of VMs.
|
||||
*/
|
||||
export const getVmsCount = async function (context: CoreContext): Promise<Result> {
|
||||
const { user } = context;
|
||||
|
||||
// Check permissions
|
||||
ensurePermission(user, {
|
||||
resource: 'vms',
|
||||
action: 'read',
|
||||
possession: 'any'
|
||||
});
|
||||
|
||||
try {
|
||||
const hypervisor = await getHypervisor();
|
||||
const activeDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.ACTIVE) as [];
|
||||
const inactiveDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.INACTIVE) as [];
|
||||
const installed = activeDomains.length + inactiveDomains.length;
|
||||
const started = activeDomains.length;
|
||||
|
||||
return {
|
||||
text: `Installed: ${installed} \nStarted: ${started}`,
|
||||
json: {
|
||||
installed,
|
||||
started
|
||||
}
|
||||
};
|
||||
} catch {
|
||||
const installed = 0;
|
||||
const started = 0;
|
||||
return {
|
||||
text: `Installed: ${installed} \nStarted: ${started}`,
|
||||
json: {
|
||||
installed,
|
||||
started
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,5 +1,3 @@
|
||||
// Created from 'create-ts-index'
|
||||
|
||||
export * from './get-all-devices';
|
||||
export * from './get-app-count';
|
||||
export * from './get-baseboard';
|
||||
@@ -11,3 +9,4 @@ export * from './get-os';
|
||||
export * from './get-software-versions';
|
||||
export * from './get-unraid-version';
|
||||
export * from './get-versions';
|
||||
export * from './get-vms-count';
|
||||
|
||||
@@ -26,7 +26,7 @@ export interface CoreContext {
|
||||
* Result object
|
||||
*/
|
||||
export interface CoreResult {
|
||||
json?: Record<string, unknown> | Array<Record<string, unknown>>;
|
||||
json?: Record<string, unknown> | Array<Record<string, unknown>> | null;
|
||||
text?: string;
|
||||
html?: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user