Skip to content

Commit

Permalink
Render VMs power state as icon with tooltip
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko committed Sep 15, 2023
1 parent 85867ae commit 9eb8c88
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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",
Expand Down Expand Up @@ -227,6 +228,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.",
Expand Down Expand Up @@ -265,6 +268,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",
Expand Down Expand Up @@ -369,6 +375,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => [
{
Expand Down Expand Up @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,7 +20,12 @@ const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdPro
const CellRenderer = cellRenderers?.[fieldId] ?? (() => <></>);
return (
<Td key={fieldId} dataLabel={fieldId}>
<CellRenderer data={resourceData} fieldId={fieldId} fields={resourceFields} />
<CellRenderer
data={resourceData}
fieldId={fieldId}
fields={resourceFields}
providerType="ovirt"
/>
</Td>
);
};
Expand All @@ -36,7 +42,7 @@ const cellRenderers: Record<string, React.FC<VMCellProps>> = {
host: ({ data }) => <TableCell>{(data?.vm as OVirtVM)?.host}</TableCell>,
cluster: ({ data }) => <TableCell>{(data?.vm as OVirtVM)?.cluster}</TableCell>,
path: ({ data }) => <TableCell>{(data?.vm as OVirtVM)?.path}</TableCell>,
status: ({ data }) => <TableCell>{(data?.vm as OVirtVM)?.status}</TableCell>,
status: PowerStateCellRenderer,
description: ({ data }) => <TableCell>{(data?.vm as OVirtVM)?.description}</TableCell>,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';

import { ResourceFieldFactory } from '@kubev2v/common';
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';

Expand All @@ -19,6 +20,19 @@ const openShiftVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [
},
sortable: true,
},
{
resourceFieldId: 'status',
jsonPath: (data) => getVmPowerState('openshift', (data as VmData)?.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<ProviderVirtualMachinesProps> = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import React from 'react';
import { ResourceField, RowProps } from '@kubev2v/common';
import { Td, Tr } from '@patternfly/react-table';

import { PowerStateCellRenderer } from './components/PowerStateCellRenderer';
import { VMCellProps, VmData, VMNameCellRenderer } from './components';

const cellRenderers: Record<string, React.FC<VMCellProps>> = {
name: VMNameCellRenderer,
status: PowerStateCellRenderer,
};

const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdProps) => {
Expand All @@ -15,7 +17,12 @@ const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdPro
const CellRenderer = cellRenderers?.[fieldId] ?? (() => <></>);
return (
<Td key={fieldId} dataLabel={fieldId}>
<CellRenderer data={resourceData} fieldId={fieldId} fields={resourceFields} />
<CellRenderer
data={resourceData}
fieldId={fieldId}
fields={resourceFields}
providerType="openshift"
/>
</Td>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdPro
const CellRenderer = cellRenderers?.[fieldId] ?? (() => <></>);
return (
<Td key={fieldId} dataLabel={fieldId}>
<CellRenderer data={resourceData} fieldId={fieldId} fields={resourceFields} />
<CellRenderer
data={resourceData}
fieldId={fieldId}
fields={resourceFields}
providerType="openstack"
/>
</Td>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdPro
const CellRenderer = cellRenderers?.[fieldId] ?? (() => <></>);
return (
<Td key={fieldId} dataLabel={fieldId}>
<CellRenderer data={resourceData} fieldId={fieldId} fields={resourceFields} />
<CellRenderer
data={resourceData}
fieldId={fieldId}
fields={resourceFields}
providerType="ova"
/>
</Td>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => [
Expand Down Expand Up @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +24,12 @@ const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdPro
const CellRenderer = cellRenderers?.[fieldId] ?? (() => <></>);
return (
<Td key={fieldId} dataLabel={fieldId}>
<CellRenderer data={resourceData} fieldId={fieldId} fields={resourceFields} />
<CellRenderer
data={resourceData}
fieldId={fieldId}
fields={resourceFields}
providerType="vsphere"
/>
</Td>
);
};
Expand All @@ -38,7 +48,7 @@ const cellRenderers: Record<string, React.FC<VMCellProps>> = {
<TableCell>{Boolean((data?.vm as VSphereVM)?.isTemplate).toString()}</TableCell>
),
path: ({ data }) => <TableCell>{(data?.vm as VSphereVM)?.path}</TableCell>,
status: ({ data }) => <TableCell>{(data?.vm as VSphereVM)?.powerState}</TableCell>,
powerState: PowerStateCellRenderer,
};

export const VSphereVirtualMachinesRow: React.FC<RowProps<VmData>> = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -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<VMCellProps> = ({ data, providerType }) => {
const { t } = useForkliftTranslation();
const powerState = getVmPowerState(providerType, data?.vm);
const states: { [key in PowerState]: [JSX.Element, string, string] } = {
on: [<PowerOffIcon color="green" key="on" />, t('Powered on'), t('On')],
off: [<OffIcon color="red" key="off" />, t('Powered off'), t('Off')],
unknown: [<UnknownIcon key="unknown" />, t('Unknown power state'), t('Unknown')],
};
const [icon, tooltipText, shortText] = states[powerState] || states.unknown;

return (
<TableIconCell icon={<Tooltip content={tooltipText}>{icon}</Tooltip>}>
{shortText}
</TableIconCell>
);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ResourceField } from '@kubev2v/common';
import { ProviderVirtualMachine } from '@kubev2v/types';
import { ProviderType, ProviderVirtualMachine } from '@kubev2v/types';

export interface VmData {
vm: ProviderVirtualMachine;
Expand All @@ -8,6 +8,7 @@ export interface VmData {
}

export interface VMCellProps {
providerType: ProviderType;
data: VmData;
fieldId: string;
fields: ResourceField[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @index(['./*', /style/g], f => `export * from '${f.path}';`)
export * from './PowerStateCellRenderer';
export * from './VMCellProps';
export * from './VMConcernsCellRenderer';
export * from './VMNameCellRenderer';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
OpenshiftVM,
OpenstackVM,
OVirtVM,
ProviderType,
ProviderVirtualMachine,
VSphereVM,
} from '@kubev2v/types';

export type PowerState = 'on' | 'off' | 'unknown';

// moved from packages/legacy/src/common/components/VMNameWithPowerState.tsx
export const getVmPowerState = (
providerType: ProviderType,
vm?: ProviderVirtualMachine,
): PowerState => {
let powerStatus: PowerState = 'unknown';
if (!vm) return powerStatus;
switch (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;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @index(['./*', /style/g], f => `export * from '${f.path}';`)
export * from './getHighestPriorityConcern';
export * from './getVmPowerState';
// @endindex

0 comments on commit 9eb8c88

Please sign in to comment.