Skip to content

Commit

Permalink
Add providerType property to provider specific Resource types
Browse files Browse the repository at this point in the history
OpenshiftResource is left unchanged because it's the basis for all
Providers.

Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko committed Sep 25, 2023
1 parent b98d36b commit a01c450
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 31 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];
};
14 changes: 14 additions & 0 deletions packages/mocks/src/definitions/basic/vms.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] } = {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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]: [],
Expand All @@ -188,6 +192,7 @@ export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = {
isTemplate: false,
host: '',
parent: null,
providerType: 'vsphere',
},
{
id: 'vm-431',
Expand Down Expand Up @@ -222,6 +227,7 @@ export const MOCK_VMWARE_VMS: { [uid in VmwareProviderIDs]: VSphereVM[] } = {
isTemplate: false,
host: '',
parent: null,
providerType: 'vsphere',
},
],
};
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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]: [
Expand All @@ -299,6 +309,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = {
concerns: [],
cluster: '',
host: '',
providerType: 'ovirt',
},
],
[OVIRT_03_UID]: [
Expand All @@ -315,6 +326,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = {
concerns: [],
cluster: '',
host: '',
providerType: 'ovirt',
},
{
id: 'be55c259-2415-448d-841e-f4b9d743242e',
Expand All @@ -329,6 +341,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = {
concerns: [],
cluster: '',
host: '',
providerType: 'ovirt',
},
],
[OVIRT_INSECURE_UID]: [
Expand All @@ -345,6 +358,7 @@ export const MOCK_RHV_VMS: { [uid in OvirtProviderIDs]: OVirtVM[] } = {
concerns: [],
cluster: '',
host: '',
providerType: 'ovirt',
},
],
};
2 changes: 2 additions & 0 deletions packages/types/src/types/provider/openshift/VM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
2 changes: 2 additions & 0 deletions packages/types/src/types/provider/openstack/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export interface OpenstackResource {
// Self link.
// SelfLink string `json:"selfLink"`
selfLink: string;
// prop added by the UI to implement narrowing (discriminated union)
providerType?: 'openstack';
}
2 changes: 2 additions & 0 deletions packages/types/src/types/provider/ova/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export interface OvaResource {
// Self link.
// SelfLink string `json:"selfLink"`
selfLink: string;
// prop added by the UI to implement narrowing (discriminated union)
providerType?: 'ova';
}
2 changes: 2 additions & 0 deletions packages/types/src/types/provider/ovirt/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export interface OVirtResource {
// Self link.
// SelfLink string `json:"selfLink"`
selfLink: string;
// prop added by the UI to implement narrowing (discriminated union)
providerType?: 'ovirt';
}
2 changes: 2 additions & 0 deletions packages/types/src/types/provider/vsphere/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ export interface VSphereResource {
// Self link.
// SelfLink string `json:"selfLink"`
selfLink: string;
// prop added by the UI to implement narrowing (discriminated union)
providerType?: 'vsphere';
}

0 comments on commit a01c450

Please sign in to comment.