-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a typed wrapper for specific Resource types
Add providerType property to implement narrowing (discriminated union). Signed-off-by: Radoslaw Szwajkowski <[email protected]>
- Loading branch information
Showing
29 changed files
with
143 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/useInventoryVms.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/types/src/types/provider/openshift/NetworkAttachmentDefinition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.