From a0a2dae2a0170201cace18d35cc96efedbabcf46 Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 30 Aug 2021 13:55:13 +0930 Subject: [PATCH] feat: add vms count --- app/core/modules/info/get-vms-count.ts | 63 ++++++++++++++++++++++++++ app/core/modules/info/index.ts | 3 +- app/core/types/global.ts | 2 +- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 app/core/modules/info/get-vms-count.ts diff --git a/app/core/modules/info/get-vms-count.ts b/app/core/modules/info/get-vms-count.ts new file mode 100644 index 000000000..cc2d1868d --- /dev/null +++ b/app/core/modules/info/get-vms-count.ts @@ -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 { + 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 + } + } + } +}; diff --git a/app/core/modules/info/index.ts b/app/core/modules/info/index.ts index 587258eef..f607089d9 100644 --- a/app/core/modules/info/index.ts +++ b/app/core/modules/info/index.ts @@ -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'; diff --git a/app/core/types/global.ts b/app/core/types/global.ts index a02a57980..540c6dffd 100644 --- a/app/core/types/global.ts +++ b/app/core/types/global.ts @@ -26,7 +26,7 @@ export interface CoreContext { * Result object */ export interface CoreResult { - json?: Record | Array>; + json?: Record | Array> | null; text?: string; html?: string; }