diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/ProviderVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/ProviderVirtualMachinesList.tsx index 89873b365..d43c047bc 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/ProviderVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/ProviderVirtualMachinesList.tsx @@ -1,14 +1,13 @@ import React, { useState } from 'react'; import { RouteComponentProps } from 'react-router-dom'; import StandardPage from 'src/components/page/StandardPage'; -import { useProviderInventory, UseProviderInventoryParams } from 'src/modules/Providers/hooks'; import { ProviderData } from 'src/modules/Providers/utils'; import { useForkliftTranslation } from 'src/utils/i18n'; import { loadUserSettings, ResourceFieldFactory, RowProps } from '@kubev2v/common'; import { ProviderVirtualMachine } from '@kubev2v/types'; -import { getHighestPriorityConcern } from '../utils'; +import { useInventoryVms } from '../utils/useInventoryVms'; export interface VmData { vm: ProviderVirtualMachine; @@ -39,34 +38,7 @@ export const ProviderVirtualMachinesList: React.FC loadUserSettings({ pageId })); - const { provider, inventory } = obj; - const { namespace } = provider.metadata; - - const largeInventory = inventory?.vmCount > 1000; - const customTimeoutAndInterval = largeInventory ? 250000 : undefined; - const validProvider = loaded && !loadError && provider; - - const inventoryOptions: UseProviderInventoryParams = { - provider: validProvider, - subPath: 'vms?detail=4', - fetchTimeout: customTimeoutAndInterval, - interval: customTimeoutAndInterval, - }; - - const { - inventory: vms, - loading, - error, - } = useProviderInventory(inventoryOptions); - - const vmData: VmData[] = - !loading && !error && Array.isArray(vms) - ? vms.map((vm) => ({ - vm, - name: vm.name, - concerns: getHighestPriorityConcern(vm), - })) - : []; + const [vmData, loading] = useInventoryVms(obj, loaded, loadError); return ( @@ -74,7 +46,7 @@ export const ProviderVirtualMachinesList: React.FC diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/useInventoryVms.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/useInventoryVms.tsx new file mode 100644 index 000000000..75bd72197 --- /dev/null +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/useInventoryVms.tsx @@ -0,0 +1,54 @@ +import { useProviderInventory, UseProviderInventoryParams } from 'src/modules/Providers/hooks'; +import { ProviderData } from 'src/modules/Providers/utils'; + +import { ProviderVirtualMachine } from '@kubev2v/types'; + +import { VmData } from '../components'; + +import { getHighestPriorityConcern } from './helpers'; + +/** + * A hook for retrieving VMs from the inventory. + * Adds providerType property to each VM. + * + * @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 data, loading status and load error (if any) + */ +export const useInventoryVms = ( + { provider, inventory }: ProviderData, + providerLoaded: boolean, + providerLoadError: unknown, +): [VmData[], boolean, Error] => { + const largeInventory = inventory?.vmCount > 1000; + const customTimeoutAndInterval = largeInventory ? 250000 : undefined; + const validProvider = providerLoaded && !providerLoadError && provider; + + const inventoryOptions: UseProviderInventoryParams = { + provider: validProvider, + subPath: 'vms?detail=4', + fetchTimeout: customTimeoutAndInterval, + interval: customTimeoutAndInterval, + }; + + const { + inventory: vms, + loading, + error, + } = useProviderInventory(inventoryOptions); + + const vmData: VmData[] = + !loading && !error && Array.isArray(vms) + ? vms.map((vm) => ({ + vm: { + ...vm, + providerType: provider?.spec?.type, + } as ProviderVirtualMachine, + name: vm.name, + concerns: getHighestPriorityConcern(vm), + })) + : []; + + return [vmData, loading, error]; +}; diff --git a/packages/mocks/src/definitions/basic/vms.mock.ts b/packages/mocks/src/definitions/basic/vms.mock.ts index 666e33ad6..81c91e856 100644 --- a/packages/mocks/src/definitions/basic/vms.mock.ts +++ b/packages/mocks/src/definitions/basic/vms.mock.ts @@ -69,6 +69,7 @@ const vm1: VSphereVM = { isTemplate: false, host: 'esx12.v2v.example.com', parent: null, + providerType: 'vsphere', }; export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = { @@ -105,6 +106,7 @@ export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = { isTemplate: false, host: 'esx12.v2v.example.com', parent: null, + providerType: 'vsphere', }, { id: 'vm-1008', @@ -139,6 +141,7 @@ export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = { isTemplate: false, host: 'esx13.v2v.example.com', parent: null, + providerType: 'vsphere', }, { id: 'vm-2686', @@ -162,6 +165,7 @@ export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = { isTemplate: false, host: 'esx13.v2v.example.com', parent: null, + providerType: 'vsphere', }, ], [VMWARE_02_UID]: [], @@ -188,6 +192,7 @@ export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = { isTemplate: false, host: '', parent: null, + providerType: 'vsphere', }, { id: 'vm-431', @@ -222,6 +227,7 @@ export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = { isTemplate: false, host: '', parent: null, + providerType: 'vsphere', }, ], }; @@ -241,6 +247,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: 'main', host: 'host.example.com', + providerType: 'ovirt', }, { id: '2a66a719-440c-4544-9da0-692d14338b12', @@ -255,6 +262,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: 'main', host: 'host.example.com', + providerType: 'ovirt', }, { id: '64333a40-ffbb-4c28-add7-5560bdf082fb', @@ -269,6 +277,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: 'main', host: 'host-2.example.com', + providerType: 'ovirt', }, { id: '6f9de857-ef39-43b7-8853-af982286dc59', @@ -283,6 +292,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: 'main', host: 'host-2.example.com', + providerType: 'ovirt', }, ], [OVIRT_02_UID]: [ @@ -299,6 +309,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: '', host: '', + providerType: 'ovirt', }, ], [OVIRT_03_UID]: [ @@ -315,6 +326,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: '', host: '', + providerType: 'ovirt', }, { id: 'be55c259-2415-448d-841e-f4b9d743242e', @@ -329,6 +341,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: '', host: '', + providerType: 'ovirt', }, ], [OVIRT_INSECURE_UID]: [ @@ -345,6 +358,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = { concerns: [], cluster: '', host: '', + providerType: 'ovirt', }, ], }; diff --git a/packages/types/src/types/provider/openshift/VM.ts b/packages/types/src/types/provider/openshift/VM.ts index c0fb0302f..34ebb0b02 100644 --- a/packages/types/src/types/provider/openshift/VM.ts +++ b/packages/types/src/types/provider/openshift/VM.ts @@ -7,4 +7,6 @@ import { OpenshiftResource } from './Resource'; export interface OpenshiftVM extends OpenshiftResource { concerns: Concern[]; object: V1VirtualMachine; + // prop added by the UI to implement narrowing (discriminated union) + providerType: 'openshift'; } diff --git a/packages/types/src/types/provider/openstack/VM.ts b/packages/types/src/types/provider/openstack/VM.ts index f7665813c..a6511bf62 100644 --- a/packages/types/src/types/provider/openstack/VM.ts +++ b/packages/types/src/types/provider/openstack/VM.ts @@ -23,6 +23,8 @@ export interface OpenstackVM extends OpenstackResource { attachedVolumes?: OpenstackAttachedVolume[]; // Concerns []Concern `json:"concerns"` concerns: Concern[]; + // prop added by the UI to implement narrowing (discriminated union) + providerType: 'openstack'; } export interface OpenstackAttachedVolume { diff --git a/packages/types/src/types/provider/ova/VM.ts b/packages/types/src/types/provider/ova/VM.ts index dcf979b19..7d4555eef 100644 --- a/packages/types/src/types/provider/ova/VM.ts +++ b/packages/types/src/types/provider/ova/VM.ts @@ -28,6 +28,8 @@ export interface OvaVM extends OvaResource { Disks: OvaDisk[]; Networks: OvaNetwork[]; concerns: Concern[]; + // prop added by the UI to implement narrowing (discriminated union) + providerType: 'ova'; } export interface OvaDevice { diff --git a/packages/types/src/types/provider/ovirt/VM.ts b/packages/types/src/types/provider/ovirt/VM.ts index 5a594ae6a..f740a884e 100644 --- a/packages/types/src/types/provider/ovirt/VM.ts +++ b/packages/types/src/types/provider/ovirt/VM.ts @@ -20,4 +20,6 @@ export interface OVirtVM extends OVirtResource { diskAttachments: OVirtDiskAttachment[]; // Concerns []Concern `json:"concerns"` concerns: Concern[]; + // prop added by the UI to implement narrowing (discriminated union) + providerType: 'ovirt'; } diff --git a/packages/types/src/types/provider/vsphere/VM.ts b/packages/types/src/types/provider/vsphere/VM.ts index a446579ff..d83ed326f 100644 --- a/packages/types/src/types/provider/vsphere/VM.ts +++ b/packages/types/src/types/provider/vsphere/VM.ts @@ -20,4 +20,6 @@ export interface VSphereVM extends VSphereResource { disks: VSphereDisk[]; // Concerns []model.Concern `json:"concerns"` concerns: Concern[]; + // prop added by the UI to implement narrowing (discriminated union) + providerType: 'vsphere'; }