From 9255bc3133450e53b465bcbae283879472c1b2e1 Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Mon, 25 Sep 2023 15:21:14 +0200 Subject: [PATCH] Refactor legacy method getVmPowerState Reference-Url: https://github.com/kubev2v/forklift-console-plugin/blob/b98d36babe11c14ca53cc0cc5437968fd2b9d4bd/packages/legacy/src/common/components/VMNameWithPowerState.tsx#L24 Signed-off-by: Radoslaw Szwajkowski --- .../OpenShiftVirtualMachinesList.tsx | 3 +- .../OpenShiftVirtualMachinesRow.tsx | 2 +- .../utils/helpers/getVmPowerState.ts | 96 +++++++++++-------- 3 files changed, 58 insertions(+), 43 deletions(-) diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx index 3680138f3..bf902801c 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx @@ -2,8 +2,7 @@ import React from 'react'; import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common'; -import { ProviderVirtualMachinesList, VmData } from './components/ProviderVirtualMachinesList'; -import { getVmPowerState } from './utils/helpers/getVmPowerState'; +import { ProviderVirtualMachinesList } from './components/ProviderVirtualMachinesList'; import { OpenShiftVirtualMachinesRow } from './OpenShiftVirtualMachinesRow'; import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines'; diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx index 6e5c304a1..71ae071ec 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx @@ -3,8 +3,8 @@ import React from 'react'; import { ResourceField, RowProps } from '@kubev2v/common'; import { Td, Tr } from '@patternfly/react-table'; -import { VMCellProps, VMConcernsCellRenderer, VmData, VMNameCellRenderer } from './components'; import { PowerStateCellRenderer } from './components/PowerStateCellRenderer'; +import { VMCellProps, VMConcernsCellRenderer, VmData, VMNameCellRenderer } from './components'; const cellRenderers: Record> = { name: VMNameCellRenderer, diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getVmPowerState.ts b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getVmPowerState.ts index 57cd6ece5..8afc276f6 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getVmPowerState.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getVmPowerState.ts @@ -8,47 +8,63 @@ import { export type PowerState = 'on' | 'off' | 'unknown'; -// moved from packages/legacy/src/common/components/VMNameWithPowerState.tsx export const getVmPowerState = (vm?: ProviderVirtualMachine): PowerState => { - let powerStatus: PowerState = 'unknown'; - if (!vm) return powerStatus; + if (!vm) return 'unknown'; + switch (vm?.providerType) { - case 'ovirt': { - if ((vm as OVirtVM).status === 'up') powerStatus = 'on'; - if ((vm as OVirtVM).status === 'down') powerStatus = 'off'; - break; - } - case 'vsphere': { - if ((vm as VSphereVM).powerState === 'poweredOn') powerStatus = 'on'; - if ((vm as VSphereVM).powerState === 'poweredOff') powerStatus = 'off'; - break; - } - case 'openstack': { - const status = (vm as OpenstackVM).status; - if (status === 'ACTIVE') { - powerStatus = 'on'; - } - if (status === 'SHUTOFF') { - powerStatus = 'off'; - } - break; - } - case 'openshift': { - const status = (vm as OpenshiftVM)?.object?.status?.printableStatus; - if (status === 'Running') { - powerStatus = 'on'; - } else { - powerStatus = 'off'; - } - break; - } - case 'ova': { - powerStatus = 'off'; - break; - } - default: { - powerStatus = 'unknown'; - } + case 'ovirt': + return getOVirtVmPowerState(vm as OVirtVM); + case 'vsphere': + return getVSphereVmPowerState(vm as VSphereVM); + case 'openstack': + return getOpenStackVmPowerState(vm as OpenstackVM); + case 'openshift': + return getOpenShiftVmPowerState(vm as OpenshiftVM); + case 'ova': + return 'off'; + default: + return 'unknown'; + } +}; + +const getOVirtVmPowerState = (vm: OVirtVM): PowerState => { + switch (vm?.status) { + case 'up': + return 'on'; + case 'down': + return 'off'; + default: + return 'unknown'; + } +}; + +const getVSphereVmPowerState = (vm: VSphereVM): PowerState => { + switch (vm?.powerState) { + case 'poweredOn': + return 'on'; + case 'poweredOff': + return 'off'; + default: + return 'unknown'; + } +}; + +const getOpenStackVmPowerState = (vm: OpenstackVM): PowerState => { + switch (vm?.status) { + case 'ACTIVE': + return 'on'; + case 'SHUTOFF': + return 'off'; + default: + return 'unknown'; + } +}; + +const getOpenShiftVmPowerState = (vm: OpenshiftVM): PowerState => { + switch (vm?.object?.status?.printableStatus) { + case 'Running': + return 'on'; + default: + return 'off'; } - return powerStatus; };