feat: add vms count

This commit is contained in:
Alexis
2021-08-30 13:55:13 +09:30
parent 90f04eb5a0
commit a0a2dae2a0
3 changed files with 65 additions and 3 deletions

View 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
}
}
}
};

View File

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

View File

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