Skip to content

Commit

Permalink
Merge pull request #1188 from yaacov/show-host-and-folder-in-vsphere-…
Browse files Browse the repository at this point in the history
…vms-list

🐾 Add hosts and folders to vSphere vms list
  • Loading branch information
yaacov authored Jun 3, 2024
2 parents 35a0d8c + cb1e9c3 commit 6850be7
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@
"Filter by endpoint": "Filter by endpoint",
"Filter by features": "Filter by features",
"Filter by flavor": "Filter by flavor",
"Filter by folder": "Filter by folder",
"Filter by host": "Filter by host",
"Filter by host ID": "Filter by host ID",
"Filter by image": "Filter by image",
"Filter by name": "Filter by name",
"Filter by namespace": "Filter by namespace",
Expand All @@ -175,14 +175,14 @@
"Filter by tenant": "Filter by tenant",
"Filter provider": "Filter provider",
"Flavor": "Flavor",
"Folder": "Folder",
"GPUs/Host Devices": "GPUs/Host Devices",
"Hide from view": "Hide from view",
"Hide values": "Hide values",
"Hooks": "Hooks",
"Hooks for virtualization": "Hooks for virtualization",
"Host": "Host",
"Host cluster": "Host cluster",
"Host ID": "Host ID",
"Hosts": "Hosts",
"How to create a migration plan": "How to create a migration plan",
"I trust the authenticity of this certificate": "I trust the authenticity of this certificate",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react';
import { ProviderVirtualMachinesListWrapper, VmData } from 'src/modules/Providers/views';
import { useInventoryVms } from 'src/modules/Providers/views/details/tabs/VirtualMachines/utils/useInventoryVms';
import {
ProviderVirtualMachinesListWrapper,
useInventoryVms,
VmData,
} from 'src/modules/Providers/views';

import { ProviderModelGroupVersionKind, V1beta1Provider } from '@kubev2v/types';
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ export const hostsFieldsMetadataFactory: ResourceFieldFactory = (t) => [
},
sortable: true,
},
{
resourceFieldId: 'id',
jsonPath: '$.inventory.id',
label: t('Host ID'),
isVisible: true,
filter: {
type: 'freetext',
placeholderLabel: t('Filter by host ID'),
},
sortable: true,
},
{
resourceFieldId: 'network',
jsonPath: '$.networkAdapter.name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common';

import { concernFilter } from './utils/concernFilter';
import { concernFilter } from './utils/filters/concernFilter';
import { ProviderVirtualMachinesList, VmData } from './components';
import { OVirtVirtualMachinesCells } from './OVirtVirtualMachinesRow';
import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common';

import { concernFilter } from './utils/concernFilter';
import { concernFilter } from './utils/filters/concernFilter';
import { getVmPowerState } from './utils/helpers/getVmPowerState';
import { ProviderVirtualMachinesList, VmData } from './components';
import { OpenStackVirtualMachinesCells } from './OpenStackVirtualMachinesRow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import { ResourceFieldFactory } from '@kubev2v/common';

import { concernFilter } from './utils/concernFilter';
import { concernFilter } from './utils/filters/concernFilter';
import { ProviderVirtualMachinesList } from './components';
import { OvaVirtualMachinesCells } from './OvaVirtualMachinesRow';
import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';
import { Alert, PageSection } from '@patternfly/react-core';
import BellIcon from '@patternfly/react-icons/dist/esm/icons/bell-icon';

import { useInventoryVms } from './utils/useInventoryVms';
import { useInventoryVms } from './utils/hooks/useInventoryVms';
import { VmData } from './components';
import { OpenShiftVirtualMachinesList } from './OpenShiftVirtualMachinesList';
import { OpenStackVirtualMachinesList } from './OpenStackVirtualMachinesList';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';

import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common';
import { VSphereVM } from '@kubev2v/types';

import { concernFilter } from './utils/concernFilter';
import { concernFilter } from './utils/filters/concernFilter';
import { ProviderVirtualMachinesList, VmData } from './components';
import { ProviderVirtualMachinesProps } from './ProviderVirtualMachines';
import { getVmPowerState } from './utils';
import { getVmPowerState, useVSphereInventoryVms } from './utils';
import { VSphereVirtualMachinesCells } from './VSphereVirtualMachinesRow';

export const vSphereVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [
Expand Down Expand Up @@ -39,21 +40,33 @@ export const vSphereVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [
},
{
resourceFieldId: 'host',
jsonPath: '$.vm.host',
label: t('Host ID'),
jsonPath: '$.hostName',
label: t('Host'),
isVisible: true,
isIdentity: false,
filter: {
type: 'freetext',
placeholderLabel: t('Filter by host ID'),
placeholderLabel: t('Filter by host'),
},
sortable: true,
},
{
resourceFieldId: 'folder',
jsonPath: '$.folderName',
label: t('Folder'),
isVisible: true,
isIdentity: false,
filter: {
type: 'freetext',
placeholderLabel: t('Filter by folder'),
},
sortable: true,
},
{
resourceFieldId: 'path',
jsonPath: '$.vm.path',
label: t('Path'),
isVisible: true,
isVisible: false,
isIdentity: false,
filter: {
type: 'freetext',
Expand Down Expand Up @@ -85,18 +98,41 @@ export const VSphereVirtualMachinesList: React.FC<ProviderVirtualMachinesProps>
initialSelectedIds,
showActions,
className,
}) => (
<ProviderVirtualMachinesList
title={title}
obj={obj}
loaded={loaded}
loadError={loadError}
cellMapper={VSphereVirtualMachinesCells}
fieldsMetadataFactory={vSphereVmFieldsMetadataFactory}
pageId="VSphereVirtualMachinesList"
onSelect={onSelect}
initialSelectedIds={initialSelectedIds}
showActions={showActions}
className={className}
/>
);
}) => {
const [hostsDict, foldersDict] = useVSphereInventoryVms({ provider: obj.provider }, true, null);
const { vmData } = obj;

/**
* Processes the vmData to include folderName and hostName for each VM.
*
* @param {Array} vmData - The array of VM data objects.
* @returns {Array} An array with updated VM data objects.
*/
const newVMData = vmData.map((data) => {
const vm = data.vm as VSphereVM;
const folder = foldersDict?.[vm.parent.id];
const host = hostsDict?.[vm.host];

return {
...data,
folderName: folder?.name,
hostName: host?.name,
};
});

return (
<ProviderVirtualMachinesList
title={title}
obj={{ ...obj, vmData: newVMData }}
loaded={loaded}
loadError={loadError}
cellMapper={VSphereVirtualMachinesCells}
fieldsMetadataFactory={vSphereVmFieldsMetadataFactory}
pageId="VSphereVirtualMachinesList"
onSelect={onSelect}
initialSelectedIds={initialSelectedIds}
showActions={showActions}
className={className}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ interface RenderTdProps {
const cellRenderers: Record<string, React.FC<VMCellProps>> = {
name: VMNameCellRenderer,
concerns: VMConcernsCellRenderer,
host: ({ data }) => <TableCell>{(data?.vm as VSphereVM)?.host}</TableCell>,
host: ({ data }) => <TableCell>{data?.hostName}</TableCell>,
folder: ({ data }) => <TableCell>{data?.folderName}</TableCell>,
isTemplate: ({ data }) => (
<TableCell>{Boolean((data?.vm as VSphereVM)?.isTemplate).toString()}</TableCell>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface VmData {
name: string;
namespace: string;
isProviderLocalOpenshift?: boolean;
folderName?: string;
hostName?: string;
}

export interface VMCellProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @index(['./*', /style/g], f => `export * from '${f.path}';`)
export * from './concernFilter';
// @endindex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @index(['./*', /style/g], f => `export * from '${f.path}';`)
export * from './useInventoryVms';
export * from './useVSphereInventoryVms';
// @endindex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isProviderLocalOpenshift, isProviderOpenshift } from 'src/utils/resourc

import { OpenshiftVM, ProviderVirtualMachine } from '@kubev2v/types';

import { VmData } from '../components';
import { VmData } from '../../components';

/**
* A hook for retrieving VMs from the inventory.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useProviderInventory, UseProviderInventoryParams } from 'src/modules/Providers/hooks';
import { ProviderData } from 'src/modules/Providers/utils';

import { ProviderHost, VSphereResource } from '@kubev2v/types';

/**
* A hook for retrieving hosts and folders from the inventory.
*
* @param providerData provider that is the source of the data
* @param providerLoaded loading status of the parent provider
* @param providerLoadError load error of the parent provider (if any)
* @returns {Array} tuple containing: the hosts and folders data
*/
export const useVSphereInventoryVms = (
{ provider }: ProviderData,
providerLoaded: boolean,
providerLoadError: unknown,
): [{ [key: string]: ProviderHost }, { [key: string]: VSphereResource }] => {
const validProvider = providerLoaded && !providerLoadError && provider;

const hostsInventoryOptions: UseProviderInventoryParams = {
provider: validProvider,
subPath: 'hosts?detail=4',
interval: 180000,
};

const { inventory: hosts } = useProviderInventory<ProviderHost[]>(hostsInventoryOptions);

const foldersInventoryOptions: UseProviderInventoryParams = {
provider: validProvider,
subPath: 'folders?detail=4',
interval: 180000,
};

const { inventory: folders } = useProviderInventory<VSphereResource[]>(foldersInventoryOptions);

const foldersDict = convertArrayToDictionary(folders);
const hostsDict = convertArrayToDictionary(hosts);

return [hostsDict, foldersDict];
};

/**
* Converts an array of Resource objects into a dictionary where the keys are the resource IDs.
*
* @param {T[]} resources - The array of Resource objects to convert.
* @returns {{ [key: string]: T }} - A dictionary with resource IDs as keys and Resource objects as values.
*/
function convertArrayToDictionary<T>(resources: T[]): { [key: string]: T } {
if (!resources) {
return undefined;
}

return resources.reduce((dict, resource) => {
dict[resource['id']] = resource;
return dict;
}, {} as { [key: string]: T });
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @index(['./*', /style/g], f => `export * from '${f.path}';`)
export * from './filters';
export * from './helpers';
export * from './hooks';
// @endindex

0 comments on commit 6850be7

Please sign in to comment.