Skip to content

Commit

Permalink
Refactor legacy method getVmPowerState
Browse files Browse the repository at this point in the history
  • Loading branch information
rszwajko committed Sep 27, 2023
1 parent 05c8f11 commit 7b497c2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, React.FC<VMCellProps>> = {
name: VMNameCellRenderer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 7b497c2

Please sign in to comment.