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 25, 2023
1 parent 5a067c1 commit 9255bc3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
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 @@ -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;
};

0 comments on commit 9255bc3

Please sign in to comment.