Skip to content

Commit

Permalink
Create a wrapper with providerType prop for all provider specific Res…
Browse files Browse the repository at this point in the history
…ource types

Add an optional providerType property to implement narrowing
(discriminated union).
The property is populated only for VMs with a dedicated hook.

Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko committed Sep 26, 2023
1 parent b98d36b commit fd20678
Show file tree
Hide file tree
Showing 33 changed files with 193 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,42 +38,15 @@ export const ProviderVirtualMachinesList: React.FC<ProviderVirtualMachinesListPr

const [userSettings] = useState(() => 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<ProviderVirtualMachine[]>(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 (
<StandardPage<VmData>
data-testid="vm-list"
dataSource={[vmData || [], !loading, null]}
RowMapper={rowMapper}
fieldsMetadata={fieldsMetadataFactory(t)}
namespace={namespace}
namespace={obj?.provider?.metadata?.namespace}
title={t('Virtual Machines')}
userSettings={userSettings}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ProviderVirtualMachine[]>(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];
};
5 changes: 5 additions & 0 deletions packages/mocks/src/definitions/basic/disks.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const MOCK_DISKS: { [uid in OvirtProviderIDs]: OVirtDisk[] } = {
actualSize: 134217728,
storageType: 'image',
status: 'ok',
providerType: 'ovirt',
},
],
[OVIRT_02_UID]: [
Expand All @@ -85,6 +86,7 @@ export const MOCK_DISKS: { [uid in OvirtProviderIDs]: OVirtDisk[] } = {
actualSize: 0,
storageType: 'image',
status: 'ok',
providerType: 'ovirt',
},
],
[OVIRT_INSECURE_UID]: [
Expand All @@ -101,6 +103,7 @@ export const MOCK_DISKS: { [uid in OvirtProviderIDs]: OVirtDisk[] } = {
actualSize: 2501120,
storageType: 'image',
status: 'ok',
providerType: 'ovirt',
},
],
[OVIRT_03_UID]: [
Expand All @@ -117,6 +120,7 @@ export const MOCK_DISKS: { [uid in OvirtProviderIDs]: OVirtDisk[] } = {
actualSize: 2501120,
storageType: 'image',
status: 'locked',
providerType: 'ovirt',
},
{
id: disk5DaId,
Expand All @@ -131,6 +135,7 @@ export const MOCK_DISKS: { [uid in OvirtProviderIDs]: OVirtDisk[] } = {
actualSize: 0,
storageType: 'image',
status: 'ok',
providerType: 'ovirt',
},
],
};
5 changes: 5 additions & 0 deletions packages/mocks/src/definitions/basic/namespaces.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const MOCK_OPENSHIFT_NAMESPACES: { [uid in OpenshiftProviderIDs]: OpenShi
name: NAMESPACE_MIGRATION,
selfLink: `providers/openshift/${OPENSHIFT_HOST_UID}/namespaces/11a0375f-f33e-489b-812a-d9a0929388ae`,
object: { kind: 'Namespace', apiVersion: 'v1' },
providerType: 'openshift',
},
{
uid: '68d88b58-22f4-49ba-8665-74f4256aa617',
Expand All @@ -27,6 +28,7 @@ export const MOCK_OPENSHIFT_NAMESPACES: { [uid in OpenshiftProviderIDs]: OpenShi
name: NAMESPACE_FORKLIFT,
selfLink: `providers/openshift/${OPENSHIFT_HOST_UID}/namespaces/68d88b58-22f4-49ba-8665-74f4256aa617`,
object: { kind: 'Namespace', apiVersion: 'v1' },
providerType: 'openshift',
},
],
[OPENSHIFT_01_UID]: [
Expand All @@ -37,6 +39,7 @@ export const MOCK_OPENSHIFT_NAMESPACES: { [uid in OpenshiftProviderIDs]: OpenShi
name: 'example-namespace-1',
selfLink: `providers/openshift/${OPENSHIFT_01_UID}/namespaces/338c69a1-bee1-40a9-af33-e83b0ec3cb99`,
object: { kind: 'Namespace', apiVersion: 'v1' },
providerType: 'openshift',
},
],
[OPENSHIFT_02_UID]: [
Expand All @@ -47,6 +50,7 @@ export const MOCK_OPENSHIFT_NAMESPACES: { [uid in OpenshiftProviderIDs]: OpenShi
name: 'example-namespace-2',
selfLink: `providers/openshift/${OPENSHIFT_02_UID}/namespaces/287d34b7-763f-4065-ba3a-2b7d6440baf6`,
object: { kind: 'Namespace', apiVersion: 'v1' },
providerType: 'openshift',
},
],
[OPENSHIFT_03_UID]: [
Expand All @@ -57,6 +61,7 @@ export const MOCK_OPENSHIFT_NAMESPACES: { [uid in OpenshiftProviderIDs]: OpenShi
name: 'example-namespace-3',
selfLink: `providers/openshift/${OPENSHIFT_03_UID}/namespaces/c4f375bb-39dc-4276-913e-0d4c43691d2f`,
object: { kind: 'Namespace', apiVersion: 'v1' },
providerType: 'openshift',
},
],
};
16 changes: 16 additions & 0 deletions packages/mocks/src/definitions/basic/networks.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const MOCK_VMWARE_NETWORKS: { [uid in VmwareProviderIDs]: VSphereNetwork[
variant: '',
host: [],
parent: null,
providerType: 'vsphere',
},
{
id: '2',
Expand All @@ -41,6 +42,7 @@ export const MOCK_VMWARE_NETWORKS: { [uid in VmwareProviderIDs]: VSphereNetwork[
variant: '',
host: [],
parent: null,
providerType: 'vsphere',
},
],
[VMWARE_02_UID]: [
Expand All @@ -53,6 +55,7 @@ export const MOCK_VMWARE_NETWORKS: { [uid in VmwareProviderIDs]: VSphereNetwork[
variant: '',
host: [],
parent: null,
providerType: 'vsphere',
},
],
[VMWARE_03_UID]: [
Expand All @@ -65,6 +68,7 @@ export const MOCK_VMWARE_NETWORKS: { [uid in VmwareProviderIDs]: VSphereNetwork[
variant: '',
host: [],
parent: null,
providerType: 'vsphere',
},
{
id: '5',
Expand All @@ -75,6 +79,7 @@ export const MOCK_VMWARE_NETWORKS: { [uid in VmwareProviderIDs]: VSphereNetwork[
variant: '',
host: [],
parent: null,
providerType: 'vsphere',
},
],
};
Expand All @@ -92,6 +97,7 @@ export const MOCK_RHV_NETWORKS: { [uid in OvirtProviderIDs]: OVirtNetwork[] } =
vlan: '',
usages: [],
nicProfiles: [],
providerType: 'ovirt',
},
{
id: '8b6f4200-cba6-4579-8edd-ea08b7ddd97b',
Expand All @@ -103,6 +109,7 @@ export const MOCK_RHV_NETWORKS: { [uid in OvirtProviderIDs]: OVirtNetwork[] } =
vlan: '',
usages: [],
nicProfiles: [],
providerType: 'ovirt',
},
{
id: '0189c310-19e6-41e3-97b1-33894562b5fb',
Expand All @@ -114,6 +121,7 @@ export const MOCK_RHV_NETWORKS: { [uid in OvirtProviderIDs]: OVirtNetwork[] } =
vlan: '',
usages: [],
nicProfiles: [],
providerType: 'ovirt',
},
],
[OVIRT_02_UID]: [
Expand All @@ -128,6 +136,7 @@ export const MOCK_RHV_NETWORKS: { [uid in OvirtProviderIDs]: OVirtNetwork[] } =
vlan: '',
usages: [],
nicProfiles: [],
providerType: 'ovirt',
},
],
[OVIRT_03_UID]: [
Expand All @@ -142,6 +151,7 @@ export const MOCK_RHV_NETWORKS: { [uid in OvirtProviderIDs]: OVirtNetwork[] } =
vlan: '',
usages: [],
nicProfiles: [],
providerType: 'ovirt',
},
],
[OVIRT_INSECURE_UID]: [
Expand All @@ -156,6 +166,7 @@ export const MOCK_RHV_NETWORKS: { [uid in OvirtProviderIDs]: OVirtNetwork[] } =
vlan: '',
usages: [],
nicProfiles: [],
providerType: 'ovirt',
},
],
};
Expand All @@ -180,6 +191,7 @@ export const MOCK_OPENSHIFT_NETWORKS: {
name: 'example-network-1',
},
},
providerType: 'openshift',
},
{
uid: '8b6f4200-cba6-4579-8edd-ea08b7ddd97b',
Expand All @@ -197,6 +209,7 @@ export const MOCK_OPENSHIFT_NETWORKS: {
name: 'example-network-2',
},
},
providerType: 'openshift',
},
],
[OPENSHIFT_01_UID]: [
Expand All @@ -215,6 +228,7 @@ export const MOCK_OPENSHIFT_NETWORKS: {
name: 'ocp-network-1',
},
},
providerType: 'openshift',
},
],
[OPENSHIFT_02_UID]: [
Expand All @@ -234,6 +248,7 @@ export const MOCK_OPENSHIFT_NETWORKS: {
resourceVersion: '1',
},
},
providerType: 'openshift',
},
],
[OPENSHIFT_03_UID]: [
Expand All @@ -253,6 +268,7 @@ export const MOCK_OPENSHIFT_NETWORKS: {
name: 'ocp-network-3',
},
},
providerType: 'openshift',
},
],
};
5 changes: 5 additions & 0 deletions packages/mocks/src/definitions/basic/nicProfiles.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const MOCK_NIC_PROFILES: { [uid in OvirtProviderIDs]: OVirtNicProfile[] }
qos: '',
properties: [],
passThrough: false,
providerType: 'ovirt',
},
{
id: np2Id,
Expand All @@ -97,6 +98,7 @@ export const MOCK_NIC_PROFILES: { [uid in OvirtProviderIDs]: OVirtNicProfile[] }
qos: 'e00fb4fb-e88e-4245-9b68-077db776d74c',
properties: [],
passThrough: false,
providerType: 'ovirt',
},
],
[OVIRT_02_UID]: [
Expand All @@ -111,6 +113,7 @@ export const MOCK_NIC_PROFILES: { [uid in OvirtProviderIDs]: OVirtNicProfile[] }
qos: '',
properties: [],
passThrough: false,
providerType: 'ovirt',
},
],
[OVIRT_03_UID]: [
Expand All @@ -125,6 +128,7 @@ export const MOCK_NIC_PROFILES: { [uid in OvirtProviderIDs]: OVirtNicProfile[] }
qos: '',
properties: [],
passThrough: false,
providerType: 'ovirt',
},
],
[OVIRT_INSECURE_UID]: [
Expand All @@ -139,6 +143,7 @@ export const MOCK_NIC_PROFILES: { [uid in OvirtProviderIDs]: OVirtNicProfile[] }
qos: '',
properties: [],
passThrough: false,
providerType: 'ovirt',
},
],
};
Loading

0 comments on commit fd20678

Please sign in to comment.