From 05c8f11cc86b0f0f3eff8c0a4dab27f2ca0e9fab Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Fri, 8 Sep 2023 19:07:16 +0200 Subject: [PATCH 1/3] Render VMs power state as icon with tooltip Signed-off-by: Radoslaw Szwajkowski --- .../en/plugin__forklift-console-plugin.json | 8 +++ .../OVirtVirtualMachinesList.tsx | 8 +-- .../OVirtVirtualMachinesRow.tsx | 3 +- .../OpenShiftVirtualMachinesList.tsx | 3 +- .../OpenShiftVirtualMachinesRow.tsx | 2 + .../OpenStackVirtualMachinesList.tsx | 8 +-- .../VSphereVirtualMachinesList.tsx | 14 ++--- .../VSphereVirtualMachinesRow.tsx | 9 +++- .../components/PowerStateCellRenderer.tsx | 28 ++++++++++ .../tabs/VirtualMachines/components/index.ts | 1 + .../utils/helpers/getVmPowerState.ts | 54 +++++++++++++++++++ .../VirtualMachines/utils/helpers/index.ts | 1 + 12 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/PowerStateCellRenderer.tsx create mode 100644 packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getVmPowerState.ts diff --git a/packages/forklift-console-plugin/locales/en/plugin__forklift-console-plugin.json b/packages/forklift-console-plugin/locales/en/plugin__forklift-console-plugin.json index 9497848fb..3c94c8e42 100644 --- a/packages/forklift-console-plugin/locales/en/plugin__forklift-console-plugin.json +++ b/packages/forklift-console-plugin/locales/en/plugin__forklift-console-plugin.json @@ -128,6 +128,7 @@ "Filter by namespace": "Filter by namespace", "Filter by network": "Filter by network", "Filter by path": "Filter by path", + "Filter by power state": "Filter by power state", "Filter by status": "Filter by status", "Filter by tenant": "Filter by tenant", "Flavor": "Flavor", @@ -233,6 +234,8 @@ "Number of storage volumes in cluster": "Number of storage volumes in cluster", "Number of virtual machines in cluster": "Number of virtual machines in cluster", "Number of virtual machines in OVA files": "Number of virtual machines in OVA files", + "Off": "Off", + "On": "On", "OpenShift cluster API endpoint. Empty may be used for the host provider.": "OpenShift cluster API endpoint. Empty may be used for the host provider.", "OpenStack domain for application credential credentials.": "OpenStack domain for application credential credentials.", "OpenStack domain for password credentials.": "OpenStack domain for password credentials.", @@ -271,6 +274,9 @@ "Please enter URL for the kubernetes API server, if empty URL default to this cluster.": "Please enter URL for the kubernetes API server, if empty URL default to this cluster.", "Please enter URL for vSphere REST APIs server.": "Please enter URL for vSphere REST APIs server.", "Pod network": "Pod network", + "Power state": "Power state", + "Powered off": "Powered off", + "Powered on": "Powered on", "Precopy interval (minutes)": "Precopy interval (minutes)", "Product": "Product", "Project": "Project", @@ -376,6 +382,8 @@ "Unable to retrieve data": "Unable to retrieve data", "Undefined": "Undefined", "Unique Kubernetes resource name identifier": "Unique Kubernetes resource name identifier", + "Unknown": "Unknown", + "Unknown power state": "Unknown power state", "Unsupported provider type": "Unsupported provider type", "Update credentials": "Update credentials", "Updated": "Updated", diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx index 13c579521..788f25166 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx @@ -2,9 +2,10 @@ import React from 'react'; import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common'; -import { ProviderVirtualMachinesList } from './components/ProviderVirtualMachinesList'; +import { ProviderVirtualMachinesList, VmData } from './components/ProviderVirtualMachinesList'; import { OVirtVirtualMachinesRow } from './OVirtVirtualMachinesRow'; import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines'; +import { getVmPowerState } from './utils'; export const oVirtVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ { @@ -70,13 +71,14 @@ export const oVirtVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'status', - jsonPath: '$.vm.status', + jsonPath: (data) => getVmPowerState('ovirt', (data as VmData)?.vm), label: t('Status'), isVisible: true, isIdentity: false, filter: { - type: 'freetext', + type: 'enum', placeholderLabel: t('Filter by status'), + values: EnumToTuple({ off: 'Off', on: 'On', unknown: 'Unknown' }), }, sortable: true, }, diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesRow.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesRow.tsx index 3a3099754..084862cde 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesRow.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesRow.tsx @@ -5,6 +5,7 @@ import { ResourceField, RowProps } from '@kubev2v/common'; import { OVirtVM } from '@kubev2v/types'; import { Td, Tr } from '@patternfly/react-table'; +import { PowerStateCellRenderer } from './components/PowerStateCellRenderer'; import { VMCellProps, VMConcernsCellRenderer, VMNameCellRenderer } from './components'; export interface VmData { @@ -36,7 +37,7 @@ const cellRenderers: Record> = { host: ({ data }) => {(data?.vm as OVirtVM)?.host}, cluster: ({ data }) => {(data?.vm as OVirtVM)?.cluster}, path: ({ data }) => {(data?.vm as OVirtVM)?.path}, - status: ({ data }) => {(data?.vm as OVirtVM)?.status}, + status: PowerStateCellRenderer, description: ({ data }) => {(data?.vm as OVirtVM)?.description}, }; 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 bf902801c..3680138f3 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,7 +2,8 @@ import React from 'react'; import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common'; -import { ProviderVirtualMachinesList } from './components/ProviderVirtualMachinesList'; +import { ProviderVirtualMachinesList, VmData } from './components/ProviderVirtualMachinesList'; +import { getVmPowerState } from './utils/helpers/getVmPowerState'; 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 8b11970fb..6e5c304a1 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 @@ -4,10 +4,12 @@ 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'; const cellRenderers: Record> = { name: VMNameCellRenderer, concerns: VMConcernsCellRenderer, + status: PowerStateCellRenderer, }; const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdProps) => { diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx index 6fe4271a6..2991401bb 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx @@ -2,7 +2,8 @@ import React from 'react'; import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common'; -import { ProviderVirtualMachinesList } from './components/ProviderVirtualMachinesList'; +import { ProviderVirtualMachinesList, VmData } from './components/ProviderVirtualMachinesList'; +import { getVmPowerState } from './utils/helpers/getVmPowerState'; import { OpenStackVirtualMachinesRow } from './OpenStackVirtualMachinesRow'; import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines'; @@ -58,13 +59,14 @@ export const openStackVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'status', - jsonPath: '$.vm.status', + jsonPath: (data) => getVmPowerState('openstack', (data as VmData)?.vm), label: t('Status'), isVisible: true, isIdentity: false, filter: { - type: 'freetext', + type: 'enum', placeholderLabel: t('Filter by status'), + values: EnumToTuple({ off: 'Off', on: 'On', unknown: 'Unknown' }), }, sortable: true, }, diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx index 2413a605c..d5fb42788 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx @@ -2,8 +2,9 @@ import React from 'react'; import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common'; -import { ProviderVirtualMachinesList } from './components/ProviderVirtualMachinesList'; +import { ProviderVirtualMachinesList, VmData } from './components/ProviderVirtualMachinesList'; import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines'; +import { getVmPowerState } from './utils'; import { VSphereVirtualMachinesRow } from './VSphereVirtualMachinesRow'; export const vSphereVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ @@ -65,14 +66,15 @@ export const vSphereVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ sortable: true, }, { - resourceFieldId: 'status', - jsonPath: '$.vm.powerState', - label: t('Status'), + resourceFieldId: 'powerState', + jsonPath: (data) => getVmPowerState('vsphere', (data as VmData)?.vm), + label: t('Power state'), isVisible: true, isIdentity: false, filter: { - type: 'freetext', - placeholderLabel: t('Filter by status'), + type: 'enum', + placeholderLabel: t('Filter by power state'), + values: EnumToTuple({ off: 'Off', on: 'On', unknown: 'Unknown' }), }, sortable: true, }, diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesRow.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesRow.tsx index 3f1c571a4..936dcbd40 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesRow.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesRow.tsx @@ -5,7 +5,12 @@ import { ResourceField, RowProps } from '@kubev2v/common'; import { VSphereVM } from '@kubev2v/types'; import { Td, Tr } from '@patternfly/react-table'; -import { VMCellProps, VMConcernsCellRenderer, VMNameCellRenderer } from './components'; +import { + PowerStateCellRenderer, + VMCellProps, + VMConcernsCellRenderer, + VMNameCellRenderer, +} from './components'; export interface VmData { vm: VSphereVM; @@ -38,7 +43,7 @@ const cellRenderers: Record> = { {Boolean((data?.vm as VSphereVM)?.isTemplate).toString()} ), path: ({ data }) => {(data?.vm as VSphereVM)?.path}, - status: ({ data }) => {(data?.vm as VSphereVM)?.powerState}, + powerState: PowerStateCellRenderer, }; export const VSphereVirtualMachinesRow: React.FC> = ({ diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/PowerStateCellRenderer.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/PowerStateCellRenderer.tsx new file mode 100644 index 000000000..6bff7bcf8 --- /dev/null +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/PowerStateCellRenderer.tsx @@ -0,0 +1,28 @@ +import React from 'react'; +import { TableIconCell } from 'src/modules/Providers/utils'; +import { useForkliftTranslation } from 'src/utils/i18n'; + +import { Tooltip } from '@patternfly/react-core'; +import { OffIcon, PowerOffIcon, UnknownIcon } from '@patternfly/react-icons'; + +import { getVmPowerState } from '../utils'; +import { PowerState } from '../utils/helpers/getVmPowerState'; + +import { VMCellProps } from './VMCellProps'; + +export const PowerStateCellRenderer: React.FC = ({ data }) => { + const { t } = useForkliftTranslation(); + const powerState = getVmPowerState(data?.vm); + const states: { [key in PowerState]: [JSX.Element, string, string] } = { + on: [, t('Powered on'), t('On')], + off: [, t('Powered off'), t('Off')], + unknown: [, t('Unknown power state'), t('Unknown')], + }; + const [icon, tooltipText, shortText] = states[powerState] || states.unknown; + + return ( + {icon}}> + {shortText} + + ); +}; diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/index.ts b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/index.ts index bfa04fb19..f374d52b9 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/index.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/index.ts @@ -1,4 +1,5 @@ // @index(['./*', /style/g], f => `export * from '${f.path}';`) +export * from './PowerStateCellRenderer'; export * from './VMCellProps'; export * from './VMConcernsCellRenderer'; export * from './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 new file mode 100644 index 000000000..57cd6ece5 --- /dev/null +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getVmPowerState.ts @@ -0,0 +1,54 @@ +import { + OpenshiftVM, + OpenstackVM, + OVirtVM, + ProviderVirtualMachine, + VSphereVM, +} from '@kubev2v/types'; + +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; + 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'; + } + } + return powerStatus; +}; diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/index.ts b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/index.ts index 07e829de1..266cbc91d 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/index.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/index.ts @@ -1,3 +1,4 @@ // @index(['./*', /style/g], f => `export * from '${f.path}';`) export * from './getHighestPriorityConcern'; +export * from './getVmPowerState'; // @endindex From 7b497c288ff5afcf2bcba1435a2d1e66285fdcc0 Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Mon, 25 Sep 2023 15:21:14 +0200 Subject: [PATCH 2/3] 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 --- .../OVirtVirtualMachinesList.tsx | 2 +- .../OpenShiftVirtualMachinesList.tsx | 3 +- .../OpenShiftVirtualMachinesRow.tsx | 2 +- .../OpenStackVirtualMachinesList.tsx | 2 +- .../VSphereVirtualMachinesList.tsx | 2 +- .../utils/helpers/getVmPowerState.ts | 90 ++++++++++--------- 6 files changed, 55 insertions(+), 46 deletions(-) diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx index 788f25166..7974b5431 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx @@ -71,7 +71,7 @@ export const oVirtVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'status', - jsonPath: (data) => getVmPowerState('ovirt', (data as VmData)?.vm), + jsonPath: (data) => getVmPowerState((data as VmData)?.vm), label: t('Status'), isVisible: true, isIdentity: false, 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/OpenStackVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx index 2991401bb..3072f31a4 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx @@ -59,7 +59,7 @@ export const openStackVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'status', - jsonPath: (data) => getVmPowerState('openstack', (data as VmData)?.vm), + jsonPath: (data) => getVmPowerState((data as VmData)?.vm), label: t('Status'), isVisible: true, isIdentity: false, diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx index d5fb42788..6797458ef 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx @@ -67,7 +67,7 @@ export const vSphereVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'powerState', - jsonPath: (data) => getVmPowerState('vsphere', (data as VmData)?.vm), + jsonPath: (data) => getVmPowerState((data as VmData)?.vm), label: t('Power state'), isVisible: true, isIdentity: false, 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..4682eab7b 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,57 @@ 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'; } - return powerStatus; }; + +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 => + vm?.object?.status?.printableStatus === 'Running' ? 'on' : 'off'; From 14d0f641950877d8bd64f135779c35722200f64f Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Wed, 27 Sep 2023 14:37:36 +0200 Subject: [PATCH 3/3] Add status column to OpenShift VM list and remove unnecessary casts Signed-off-by: Radoslaw Szwajkowski --- .../VirtualMachines/OVirtVirtualMachinesList.tsx | 2 +- .../OpenShiftVirtualMachinesList.tsx | 15 +++++++++++++++ .../OpenStackVirtualMachinesList.tsx | 2 +- .../VSphereVirtualMachinesList.tsx | 2 +- .../utils/helpers/getVmPowerState.ts | 8 ++++---- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx index 7974b5431..a3c48759c 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OVirtVirtualMachinesList.tsx @@ -71,7 +71,7 @@ export const oVirtVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'status', - jsonPath: (data) => getVmPowerState((data as VmData)?.vm), + jsonPath: (data: VmData) => getVmPowerState(data?.vm), label: t('Status'), isVisible: true, isIdentity: false, 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 bf902801c..09acba996 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 @@ -3,8 +3,10 @@ import React from 'react'; import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common'; import { ProviderVirtualMachinesList } from './components/ProviderVirtualMachinesList'; +import { VmData } from './components'; import { OpenShiftVirtualMachinesRow } from './OpenShiftVirtualMachinesRow'; import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines'; +import { getVmPowerState } from './utils'; const openShiftVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ { @@ -32,6 +34,19 @@ const openShiftVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ values: EnumToTuple({ Critical: 'Critical', Warning: 'Warning', Information: 'Information' }), }, }, + { + resourceFieldId: 'status', + jsonPath: (data: VmData) => getVmPowerState(data?.vm), + label: t('Status'), + isVisible: true, + isIdentity: false, + filter: { + type: 'enum', + placeholderLabel: t('Filter by status'), + values: EnumToTuple({ off: 'Off', on: 'On', unknown: 'Unknown' }), + }, + sortable: true, + }, ]; export const OpenShiftVirtualMachinesList: React.FC = ({ diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx index 3072f31a4..28d5eeb9b 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenStackVirtualMachinesList.tsx @@ -59,7 +59,7 @@ export const openStackVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'status', - jsonPath: (data) => getVmPowerState((data as VmData)?.vm), + jsonPath: (data: VmData) => getVmPowerState(data?.vm), label: t('Status'), isVisible: true, isIdentity: false, diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx index 6797458ef..433944b2d 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesList.tsx @@ -67,7 +67,7 @@ export const vSphereVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, { resourceFieldId: 'powerState', - jsonPath: (data) => getVmPowerState((data as VmData)?.vm), + jsonPath: (data: VmData) => getVmPowerState(data?.vm), label: t('Power state'), isVisible: true, isIdentity: false, 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 4682eab7b..e86fdcd3d 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 @@ -13,13 +13,13 @@ export const getVmPowerState = (vm?: ProviderVirtualMachine): PowerState => { switch (vm?.providerType) { case 'ovirt': - return getOVirtVmPowerState(vm as OVirtVM); + return getOVirtVmPowerState(vm); case 'vsphere': - return getVSphereVmPowerState(vm as VSphereVM); + return getVSphereVmPowerState(vm); case 'openstack': - return getOpenStackVmPowerState(vm as OpenstackVM); + return getOpenStackVmPowerState(vm); case 'openshift': - return getOpenShiftVmPowerState(vm as OpenshiftVM); + return getOpenShiftVmPowerState(vm); case 'ova': return 'off'; default: