Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render VMs power state as icon with tooltip #723

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
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: VmData) => getVmPowerState(data?.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 Down Expand Up @@ -36,7 +37,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
Expand Up @@ -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) => [
{
Expand Down Expand Up @@ -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<ProviderVirtualMachinesProps> = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import React from 'react';
import { ResourceField, RowProps } from '@kubev2v/common';
import { Td, Tr } from '@patternfly/react-table';

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

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

const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdProps) => {
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: VmData) => getVmPowerState(data?.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 @@ -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: VmData) => getVmPowerState(data?.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 Down Expand Up @@ -38,7 +43,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 }) => {
rszwajko marked this conversation as resolved.
Show resolved Hide resolved
const { t } = useForkliftTranslation();
const powerState = getVmPowerState(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,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,64 @@
import {
OpenshiftVM,
OpenstackVM,
OVirtVM,
ProviderVirtualMachine,
VSphereVM,
} from '@kubev2v/types';

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

export const getVmPowerState = (vm?: ProviderVirtualMachine): PowerState => {
if (!vm) return 'unknown';

switch (vm?.providerType) {
case 'ovirt':
return getOVirtVmPowerState(vm);
case 'vsphere':
return getVSphereVmPowerState(vm);
case 'openstack':
return getOpenStackVmPowerState(vm);
case 'openshift':
return getOpenShiftVmPowerState(vm);
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 =>
vm?.object?.status?.printableStatus === 'Running' ? 'on' : 'off';
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';
yaacov marked this conversation as resolved.
Show resolved Hide resolved
// @endindex