Skip to content

Commit

Permalink
Create a typed wrapper for specific Resource types
Browse files Browse the repository at this point in the history
Add providerType property to implement narrowing (discriminated
union).

Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko committed Sep 26, 2023
1 parent b98d36b commit ef5571b
Show file tree
Hide file tree
Showing 29 changed files with 143 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];
};
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',
},
],
};
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/openshift/Namespace.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { V1Namespace } from '../../k8s/V1Namespace';

import { OpenshiftResource } from './Resource';
import { TypedOpenshiftResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ocp/namespace.go
export interface OpenShiftNamespace extends OpenshiftResource {
export interface OpenShiftNamespace extends TypedOpenshiftResource {
// Object core.Namespace `json:"object"`
object: V1Namespace;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { V1NetworkAttachmentDefinition } from '../../k8s/V1NetworkAttachmentDefinition';

import { OpenshiftResource } from './Resource';
import { TypedOpenshiftResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ocp/netattachdefinition.go
export interface OpenShiftNetworkAttachmentDefinition extends OpenshiftResource {
export interface OpenShiftNetworkAttachmentDefinition extends TypedOpenshiftResource {
// Object net.NetworkAttachmentDefinition `json:"object"`
object: V1NetworkAttachmentDefinition;
}
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/openshift/StorageClass.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IoK8sApiStorageV1StorageClass } from '../../k8s/IoK8sApiStorageV1StorageClass';

import { OpenshiftResource } from './Resource';
import { TypedOpenshiftResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ocp/storageclass.go
export interface OpenShiftStorageClass extends OpenshiftResource {
export interface OpenShiftStorageClass extends TypedOpenshiftResource {
// Object storage.StorageClass `json:"object"`
object: IoK8sApiStorageV1StorageClass;
}
6 changes: 6 additions & 0 deletions packages/types/src/types/provider/openshift/TypedResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { OpenshiftResource } from './Resource';

export interface TypedOpenshiftResource extends OpenshiftResource {
// prop added by the UI to implement narrowing (discriminated union)
providerType: 'openshift';
}
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/openshift/VM.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { V1VirtualMachine } from '../../k8s/V1VirtualMachine';
import { Concern } from '../base';

import { OpenshiftResource } from './Resource';
import { TypedOpenshiftResource } from './TypedResource';

// https://github.com/kubev2v/forklift/blob/main/pkg/controller/provider/web/ocp/vm.go
export interface OpenshiftVM extends OpenshiftResource {
export interface OpenshiftVM extends TypedOpenshiftResource {
concerns: Concern[];
object: V1VirtualMachine;
}
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/openstack/Network.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenstackResource } from './Resource';
import { TypedOpenstackResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/openstack/network.go
export interface OpenstackNetwork extends OpenstackResource {
export interface OpenstackNetwork extends TypedOpenstackResource {
// Description string `json:"description"`
description: string;
// AdminStateUp bool `json:"adminStateUp"`
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/types/provider/openstack/TypedResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { OpenstackResource } from './Resource';

export interface TypedOpenstackResource extends OpenstackResource {
// prop added by the UI to implement narrowing (discriminated union)
providerType: 'openstack';
}
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/openstack/VM.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Concern } from '../base/model';

import { OpenstackResource } from './Resource';
import { TypedOpenstackResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/openstack/vm.go
export interface OpenstackVM extends OpenstackResource {
export interface OpenstackVM extends TypedOpenstackResource {
// TenantID string `json:"tenantID"`
tenantID: string;
// 'ACTIVE' | 'SHUTOFF' | 'PAUSED' | 'SHELVED_OFFLOADED' | 'SUSPENDED'
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/openstack/VolumeType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenstackResource } from './Resource';
import { TypedOpenstackResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/openstack/volumetype.go
export interface OpenstackVolumeType extends OpenstackResource {
export interface OpenstackVolumeType extends TypedOpenstackResource {
// Description string `json:"description"`
description: string;
// ExtraSpecs map[string]string `json:"extraSpecs,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/ova/Network.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OvaResource } from './Resource';
import { TypedOvaResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ova/network.go
export interface OvaNetwork extends OvaResource {
export interface OvaNetwork extends TypedOvaResource {
Description?: string;
}
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';
}
6 changes: 6 additions & 0 deletions packages/types/src/types/provider/ova/TypedResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { OvaResource } from './Resource';

export interface TypedOvaResource extends OvaResource {
// prop added by the UI to implement narrowing (discriminated union)
providerType: 'ova';
}
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/ova/VM.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Concern } from '../base/model';

import { OvaNetwork } from './Network';
import { OvaResource } from './Resource';
import { TypedOvaResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ova/vm.go
export interface OvaVM extends OvaResource {
export interface OvaVM extends TypedOvaResource {
OvaPath: string;
RevisionValidated: number;
PolicyVersion: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/ovirt/Cluster.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OVirtResource } from './Resource';
import { TypedOVirtResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/cluster.go
export interface OVirtCluster extends OVirtResource {
export interface OVirtCluster extends TypedOVirtResource {
// DataCenter string `json:"dataCenter"`
dataCenter: string;
// HaReservation bool `json:"haReservation"`
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/ovirt/Disk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OVirtResource } from './Resource';
import { TypedOVirtResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/disk.go
export interface OVirtDisk extends OVirtResource {
export interface OVirtDisk extends TypedOVirtResource {
// Shared bool `json:"shared"`
shared: boolean;
// StorageDomain string `json:"storageDomain"`
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/ovirt/DiskProfile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OVirtResource } from './Resource';
import { TypedOVirtResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/diskprofile.go
export interface OVirtDiskProfile extends OVirtResource {
export interface OVirtDiskProfile extends TypedOVirtResource {
// StorageDomain string `json:"storageDomain"`
storageDomain: string;
// QoS string `json:"qos"`
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/types/provider/ovirt/Host.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { OVirtHostNIC, OVirtNetworkAttachment } from './model';
import { OVirtResource } from './Resource';
import { TypedOVirtResource } from './TypedResource';

// https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/host.go
export interface OVirtHost extends OVirtResource {
export interface OVirtHost extends TypedOVirtResource {
// Cluster string `json:"cluster"`
cluster: string;
// Status string `json:"status"`
Expand Down
Loading

0 comments on commit ef5571b

Please sign in to comment.