From ef5571b7404637bd003e116deaa8c37a34218ec3 Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Mon, 18 Sep 2023 22:25:19 +0200 Subject: [PATCH] Create a typed wrapper for specific Resource types Add providerType property to implement narrowing (discriminated union). Signed-off-by: Radoslaw Szwajkowski --- .../ProviderVirtualMachinesList.tsx | 34 ++---------- .../VirtualMachines/utils/useInventoryVms.tsx | 54 +++++++++++++++++++ .../mocks/src/definitions/basic/vms.mock.ts | 14 +++++ .../src/types/provider/openshift/Namespace.ts | 4 +- .../openshift/NetworkAttachmentDefinition.ts | 4 +- .../types/provider/openshift/StorageClass.ts | 4 +- .../types/provider/openshift/TypedResource.ts | 6 +++ .../types/src/types/provider/openshift/VM.ts | 4 +- .../src/types/provider/openstack/Network.ts | 4 +- .../types/provider/openstack/TypedResource.ts | 6 +++ .../types/src/types/provider/openstack/VM.ts | 4 +- .../types/provider/openstack/VolumeType.ts | 4 +- .../types/src/types/provider/ova/Network.ts | 4 +- .../types/src/types/provider/ova/Resource.ts | 2 + .../src/types/provider/ova/TypedResource.ts | 6 +++ packages/types/src/types/provider/ova/VM.ts | 4 +- .../types/src/types/provider/ovirt/Cluster.ts | 4 +- .../types/src/types/provider/ovirt/Disk.ts | 4 +- .../src/types/provider/ovirt/DiskProfile.ts | 4 +- .../types/src/types/provider/ovirt/Host.ts | 4 +- .../types/src/types/provider/ovirt/Network.ts | 4 +- .../src/types/provider/ovirt/NicProfile.ts | 4 +- .../src/types/provider/ovirt/StorageDomain.ts | 4 +- .../src/types/provider/ovirt/TypedResource.ts | 6 +++ packages/types/src/types/provider/ovirt/VM.ts | 4 +- .../src/types/provider/vsphere/DataStore.ts | 4 +- .../src/types/provider/vsphere/Network.ts | 4 +- .../types/provider/vsphere/TypedResource.ts | 6 +++ .../types/src/types/provider/vsphere/VM.ts | 4 +- 29 files changed, 143 insertions(+), 71 deletions(-) create mode 100644 packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/useInventoryVms.tsx create mode 100644 packages/types/src/types/provider/openshift/TypedResource.ts create mode 100644 packages/types/src/types/provider/openstack/TypedResource.ts create mode 100644 packages/types/src/types/provider/ova/TypedResource.ts create mode 100644 packages/types/src/types/provider/ovirt/TypedResource.ts create mode 100644 packages/types/src/types/provider/vsphere/TypedResource.ts 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/Namespace.ts b/packages/types/src/types/provider/openshift/Namespace.ts index 4b45e3666..797353d56 100644 --- a/packages/types/src/types/provider/openshift/Namespace.ts +++ b/packages/types/src/types/provider/openshift/Namespace.ts @@ -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; } diff --git a/packages/types/src/types/provider/openshift/NetworkAttachmentDefinition.ts b/packages/types/src/types/provider/openshift/NetworkAttachmentDefinition.ts index 75d2b0683..33a484141 100644 --- a/packages/types/src/types/provider/openshift/NetworkAttachmentDefinition.ts +++ b/packages/types/src/types/provider/openshift/NetworkAttachmentDefinition.ts @@ -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; } diff --git a/packages/types/src/types/provider/openshift/StorageClass.ts b/packages/types/src/types/provider/openshift/StorageClass.ts index 551b00740..73c01bf14 100644 --- a/packages/types/src/types/provider/openshift/StorageClass.ts +++ b/packages/types/src/types/provider/openshift/StorageClass.ts @@ -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; } diff --git a/packages/types/src/types/provider/openshift/TypedResource.ts b/packages/types/src/types/provider/openshift/TypedResource.ts new file mode 100644 index 000000000..5dcb00c8e --- /dev/null +++ b/packages/types/src/types/provider/openshift/TypedResource.ts @@ -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'; +} diff --git a/packages/types/src/types/provider/openshift/VM.ts b/packages/types/src/types/provider/openshift/VM.ts index c0fb0302f..d70b431c8 100644 --- a/packages/types/src/types/provider/openshift/VM.ts +++ b/packages/types/src/types/provider/openshift/VM.ts @@ -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; } diff --git a/packages/types/src/types/provider/openstack/Network.ts b/packages/types/src/types/provider/openstack/Network.ts index 058a91194..6a6e223d6 100644 --- a/packages/types/src/types/provider/openstack/Network.ts +++ b/packages/types/src/types/provider/openstack/Network.ts @@ -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"` diff --git a/packages/types/src/types/provider/openstack/TypedResource.ts b/packages/types/src/types/provider/openstack/TypedResource.ts new file mode 100644 index 000000000..bf4e120bf --- /dev/null +++ b/packages/types/src/types/provider/openstack/TypedResource.ts @@ -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'; +} diff --git a/packages/types/src/types/provider/openstack/VM.ts b/packages/types/src/types/provider/openstack/VM.ts index f7665813c..32f83996e 100644 --- a/packages/types/src/types/provider/openstack/VM.ts +++ b/packages/types/src/types/provider/openstack/VM.ts @@ -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' diff --git a/packages/types/src/types/provider/openstack/VolumeType.ts b/packages/types/src/types/provider/openstack/VolumeType.ts index 1b41987b7..3615f9e44 100644 --- a/packages/types/src/types/provider/openstack/VolumeType.ts +++ b/packages/types/src/types/provider/openstack/VolumeType.ts @@ -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"` diff --git a/packages/types/src/types/provider/ova/Network.ts b/packages/types/src/types/provider/ova/Network.ts index 29abbe631..b05b4d5d9 100644 --- a/packages/types/src/types/provider/ova/Network.ts +++ b/packages/types/src/types/provider/ova/Network.ts @@ -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; } diff --git a/packages/types/src/types/provider/ova/Resource.ts b/packages/types/src/types/provider/ova/Resource.ts index 76f99b1be..3dd908ee5 100644 --- a/packages/types/src/types/provider/ova/Resource.ts +++ b/packages/types/src/types/provider/ova/Resource.ts @@ -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'; } diff --git a/packages/types/src/types/provider/ova/TypedResource.ts b/packages/types/src/types/provider/ova/TypedResource.ts new file mode 100644 index 000000000..3bce1b662 --- /dev/null +++ b/packages/types/src/types/provider/ova/TypedResource.ts @@ -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'; +} diff --git a/packages/types/src/types/provider/ova/VM.ts b/packages/types/src/types/provider/ova/VM.ts index dcf979b19..5b0982661 100644 --- a/packages/types/src/types/provider/ova/VM.ts +++ b/packages/types/src/types/provider/ova/VM.ts @@ -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; diff --git a/packages/types/src/types/provider/ovirt/Cluster.ts b/packages/types/src/types/provider/ovirt/Cluster.ts index 68c6dd312..22faa6586 100644 --- a/packages/types/src/types/provider/ovirt/Cluster.ts +++ b/packages/types/src/types/provider/ovirt/Cluster.ts @@ -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"` diff --git a/packages/types/src/types/provider/ovirt/Disk.ts b/packages/types/src/types/provider/ovirt/Disk.ts index c51eb4a29..c9e1c77a5 100644 --- a/packages/types/src/types/provider/ovirt/Disk.ts +++ b/packages/types/src/types/provider/ovirt/Disk.ts @@ -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"` diff --git a/packages/types/src/types/provider/ovirt/DiskProfile.ts b/packages/types/src/types/provider/ovirt/DiskProfile.ts index 81ab7787c..37f8a5bbb 100644 --- a/packages/types/src/types/provider/ovirt/DiskProfile.ts +++ b/packages/types/src/types/provider/ovirt/DiskProfile.ts @@ -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"` diff --git a/packages/types/src/types/provider/ovirt/Host.ts b/packages/types/src/types/provider/ovirt/Host.ts index 286a0fa5f..13bc3b4be 100644 --- a/packages/types/src/types/provider/ovirt/Host.ts +++ b/packages/types/src/types/provider/ovirt/Host.ts @@ -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"` diff --git a/packages/types/src/types/provider/ovirt/Network.ts b/packages/types/src/types/provider/ovirt/Network.ts index f65c02767..5ebaf36f8 100644 --- a/packages/types/src/types/provider/ovirt/Network.ts +++ b/packages/types/src/types/provider/ovirt/Network.ts @@ -1,7 +1,7 @@ -import { OVirtResource } from './Resource'; +import { TypedOVirtResource } from './TypedResource'; // https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/network.go -export interface OVirtNetwork extends OVirtResource { +export interface OVirtNetwork extends TypedOVirtResource { // DataCenter string `json:"dataCenter"` dataCenter: string; // VLan string `json:"vlan"` diff --git a/packages/types/src/types/provider/ovirt/NicProfile.ts b/packages/types/src/types/provider/ovirt/NicProfile.ts index 70cf3a7a6..ff1d3f9d9 100644 --- a/packages/types/src/types/provider/ovirt/NicProfile.ts +++ b/packages/types/src/types/provider/ovirt/NicProfile.ts @@ -1,8 +1,8 @@ import { OVirtProperty } from './model'; -import { OVirtResource } from './Resource'; +import { TypedOVirtResource } from './TypedResource'; // https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/nicprofile.go -export interface OVirtNicProfile extends OVirtResource { +export interface OVirtNicProfile extends TypedOVirtResource { // Network string `json:"network"` network: string; // NetworkFilter string `json:"networkFilter"` diff --git a/packages/types/src/types/provider/ovirt/StorageDomain.ts b/packages/types/src/types/provider/ovirt/StorageDomain.ts index 5366ddcee..f33192cc9 100644 --- a/packages/types/src/types/provider/ovirt/StorageDomain.ts +++ b/packages/types/src/types/provider/ovirt/StorageDomain.ts @@ -1,7 +1,7 @@ -import { OVirtResource } from './Resource'; +import { TypedOVirtResource } from './TypedResource'; // https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/storage.go -export interface OVirtStorageDomain extends OVirtResource { +export interface OVirtStorageDomain extends TypedOVirtResource { // DataCenter string `json:"dataCenter"` dataCenter: string; // Type string `json:"type"` diff --git a/packages/types/src/types/provider/ovirt/TypedResource.ts b/packages/types/src/types/provider/ovirt/TypedResource.ts new file mode 100644 index 000000000..0b317db6f --- /dev/null +++ b/packages/types/src/types/provider/ovirt/TypedResource.ts @@ -0,0 +1,6 @@ +import { OVirtResource } from './Resource'; + +export interface TypedOVirtResource extends OVirtResource { + // prop added by the UI to implement narrowing (discriminated union) + providerType: 'ovirt'; +} diff --git a/packages/types/src/types/provider/ovirt/VM.ts b/packages/types/src/types/provider/ovirt/VM.ts index 5a594ae6a..3f1e0d884 100644 --- a/packages/types/src/types/provider/ovirt/VM.ts +++ b/packages/types/src/types/provider/ovirt/VM.ts @@ -1,10 +1,10 @@ import { Concern } from '../base/model'; import { OVirtDiskAttachment, OVirtNIC } from './model'; -import { OVirtResource } from './Resource'; +import { TypedOVirtResource } from './TypedResource'; // https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/ovirt/vm.go -export interface OVirtVM extends OVirtResource { +export interface OVirtVM extends TypedOVirtResource { // Cluster string `json:"cluster"` cluster: string; // 'up' | 'down' diff --git a/packages/types/src/types/provider/vsphere/DataStore.ts b/packages/types/src/types/provider/vsphere/DataStore.ts index 6c32b5ccc..14a40762a 100644 --- a/packages/types/src/types/provider/vsphere/DataStore.ts +++ b/packages/types/src/types/provider/vsphere/DataStore.ts @@ -1,7 +1,7 @@ -import { VSphereResource } from './Resource'; +import { TypedVSphereResource } from './TypedResource'; // https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/vsphere/datastore.go -export interface VSphereDataStore extends VSphereResource { +export interface VSphereDataStore extends TypedVSphereResource { // Type string `json:"type"` type: string; // Capacity int64 `json:"capacity"` diff --git a/packages/types/src/types/provider/vsphere/Network.ts b/packages/types/src/types/provider/vsphere/Network.ts index 0c4ba36b8..7f539291e 100644 --- a/packages/types/src/types/provider/vsphere/Network.ts +++ b/packages/types/src/types/provider/vsphere/Network.ts @@ -1,10 +1,10 @@ import { Ref } from '../base/model'; import { VSphereDVSHost } from './model'; -import { VSphereResource } from './Resource'; +import { TypedVSphereResource } from './TypedResource'; // https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/vsphere/network.go -export interface VSphereNetwork extends VSphereResource { +export interface VSphereNetwork extends TypedVSphereResource { // Variant string `json:"variant"` variant: string; // DVSwitch *model.Ref `json:"dvSwitch,omitempty"` diff --git a/packages/types/src/types/provider/vsphere/TypedResource.ts b/packages/types/src/types/provider/vsphere/TypedResource.ts new file mode 100644 index 000000000..252cd7572 --- /dev/null +++ b/packages/types/src/types/provider/vsphere/TypedResource.ts @@ -0,0 +1,6 @@ +import { VSphereResource } from './Resource'; + +export interface TypedVSphereResource extends VSphereResource { + // prop added by the UI to implement narrowing (discriminated union) + providerType: 'vsphere'; +} diff --git a/packages/types/src/types/provider/vsphere/VM.ts b/packages/types/src/types/provider/vsphere/VM.ts index a446579ff..f72bf076f 100644 --- a/packages/types/src/types/provider/vsphere/VM.ts +++ b/packages/types/src/types/provider/vsphere/VM.ts @@ -1,10 +1,10 @@ import { Concern, Ref } from '../base/model'; import { VSphereDisk } from './model'; -import { VSphereResource } from './Resource'; +import { TypedVSphereResource } from './TypedResource'; // https://github.com/kubev2v/forklift/tree/main/pkg/controller/provider/web/vsphere/vm.go -export interface VSphereVM extends VSphereResource { +export interface VSphereVM extends TypedVSphereResource { // RevisionValidated int64 `json:"revisionValidated"` revisionValidated: number; // IsTemplate bool `json:"isTemplate"`