Skip to content

Commit

Permalink
plan name validation
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko committed Jan 18, 2024
1 parent a8ced9a commit db73a94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import SectionHeading from 'src/components/headers/SectionHeading';
import { useForkliftTranslation } from 'src/utils/i18n';
import { useImmerReducer } from 'use-immer';

import { ProviderModelGroupVersionKind, ProviderModelRef, V1beta1Provider } from '@kubev2v/types';
import {
PlanModelGroupVersionKind,
ProviderModelGroupVersionKind,
ProviderModelRef,
V1beta1Plan,
V1beta1Provider,
} from '@kubev2v/types';
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';
import { Button, Flex, FlexItem, PageSection } from '@patternfly/react-core';

import { useToggle } from '../../hooks';
import { getResourceUrl } from '../../utils';

import { setAvailableProviders } from './actions';
import { setAvailableProviders, setExistingPlans } from './actions';
import { PlansCreateForm } from './PlansCreateForm';
import { useCreateVmMigrationData } from './ProvidersCreateVmMigrationContext';
import { createInitialState, reducer } from './reducer';
Expand Down Expand Up @@ -52,6 +58,14 @@ const ProvidersCreateVmMigrationPage: FC<{
});
useEffect(() => dispatch(setAvailableProviders(providers ?? [])), [providers]);

const [plans] = useK8sWatchResource<V1beta1Plan[]>({
groupVersionKind: PlanModelGroupVersionKind,
namespaced: true,
isList: true,
namespace,
});
useEffect(() => dispatch(setExistingPlans(plans ?? [])), [plans]);

const [isLoading, toggleIsLoading] = useToggle();
const onUpdate = toggleIsLoading;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { V1beta1Provider } from '@kubev2v/types';
import { V1beta1Plan, V1beta1Provider } from '@kubev2v/types';

// action type names
export const SET_NAME = 'SET_NAME';
export const SET_DESCRIPTION = 'SET_DESCRIPTION';
export const SET_TARGET_PROVIDER = 'SET_TARGET_PROVIDER';
export const SET_TARGET_NAMESPACE = 'SET_TARGET_NAMESPACE';
export const SET_AVAILABLE_PROVIDERS = 'SET_AVAILABLE_PROVIDERS';
export const SET_EXISTING_PLANS = 'SET_EXISTING_PLANS';

export type CreateVmMigration =
| typeof SET_NAME
| typeof SET_DESCRIPTION
| typeof SET_TARGET_PROVIDER
| typeof SET_TARGET_NAMESPACE
| typeof SET_AVAILABLE_PROVIDERS;
| typeof SET_AVAILABLE_PROVIDERS
| typeof SET_EXISTING_PLANS;

export interface PageAction<S, T> {
type: S;
Expand Down Expand Up @@ -42,6 +44,10 @@ export interface PlanAvailableProviders {
availableProviders: V1beta1Provider[];
}

export interface PlanExistingPlans {
existingPlans: V1beta1Plan[];
}

// action creators

export const setPlanTargetProvider = (
Expand Down Expand Up @@ -84,3 +90,12 @@ export const setAvailableProviders = (
availableProviders,
},
});

export const setExistingPlans = (
existingPlans: V1beta1Plan[],
): PageAction<CreateVmMigration, PlanExistingPlans> => ({
type: 'SET_EXISTING_PLANS',
payload: {
existingPlans,
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
SET_NAME,
SET_TARGET_NAMESPACE,
SET_TARGET_PROVIDER,
SET_EXISTING_PLANS,
PlanExistingPlans,
} from './actions';

export interface CreateVmMigrationPageState {
Expand All @@ -33,6 +35,7 @@ export interface CreateVmMigrationPageState {
};
availableProviders: V1beta1Provider[];
selectedVms: VmData[];
existingPlans: V1beta1Plan[];
}

const validateUniqueName = (name: string, existingPlanNames: string[]) =>
Expand All @@ -46,11 +49,11 @@ const actions: {
} = {
[SET_NAME](
draft,
{ payload: { name, existingPlanNames } }: PageAction<CreateVmMigration, PlanName>,
{ payload: { name } }: PageAction<CreateVmMigration, PlanName>,
) {
draft.newPlan.metadata.name = name;
draft.validation.name =
validateK8sName(name) && validateUniqueName(name, existingPlanNames) ? 'success' : 'error';
validateK8sName(name) && validateUniqueName(name, draft.existingPlans.map(plan => plan?.metadata?.name ?? '')) ? 'success' : 'error';
return draft;
},
[SET_DESCRIPTION](
Expand Down Expand Up @@ -97,6 +100,13 @@ const actions: {
draft.availableProviders = availableProviders;
return draft;
},
[SET_EXISTING_PLANS](
draft,
{ payload: { existingPlans } }: PageAction<CreateVmMigration, PlanExistingPlans>,
) {
draft.existingPlans = existingPlans;
return draft;
},
};

export const reducer = (
Expand Down Expand Up @@ -153,6 +163,7 @@ export const createInitialState = ({
apiError: null,
availableProviders: [],
selectedVms,
existingPlans: [],
validation: {
name: 'default',
targetNamespace: 'default',
Expand Down

0 comments on commit db73a94

Please sign in to comment.