Files
api/app/core/modules/info/get-vms-count.ts
T
Alexis 010efe75bd Revert "chore: lint all the files"
This reverts commit 4ef387b5bb.
2021-09-07 13:46:23 +09:30

64 lines
1.5 KiB
TypeScript

/*!
* 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
}
}
}
};