diff --git a/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Hooks/PlanHooks.tsx b/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Hooks/PlanHooks.tsx index 09b2daa4f..4c9907a2d 100644 --- a/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Hooks/PlanHooks.tsx +++ b/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Hooks/PlanHooks.tsx @@ -1,6 +1,7 @@ import React, { useEffect, useReducer } from 'react'; import { Base64 } from 'js-base64'; import SectionHeading from 'src/components/headers/SectionHeading'; +import { AlertMessageForModals } from 'src/modules/Providers/modals'; import { useForkliftTranslation } from 'src/utils/i18n'; import { FormGroupWithHelpText } from '@kubev2v/common'; @@ -27,7 +28,7 @@ import { onUpdatePlanHooks } from './utils'; export const PlanHooks: React.FC<{ name: string; namespace: string }> = ({ name, namespace }) => { const { t } = useForkliftTranslation(); - const [plan, preHookResource, postHookResource, loaded, loadError] = usePlanHooks( + const [plan, preHookResource, postHookResource, loaded, loadError, warning] = usePlanHooks( name, namespace, ); @@ -96,6 +97,24 @@ export const PlanHooks: React.FC<{ name: string; namespace: string }> = ({ name, {state.alertMessage && {state.alertMessage}} + {warning && ( + + +

Warning: {warning},

+

updating the hooks will override the current configuration.

+ + } + /> +
+ )} + { const postHookResource = hookRecourses.find((h) => h.metadata.name === postHook?.hook?.name); const preHookResource = hookRecourses.find((h) => h.metadata.name === preHook?.hook?.name); - return [plan, preHookResource, postHookResource, loaded, loadError]; + // Check for unsupported hooks + const warning = validateHooks(plan); + + return [plan, preHookResource, postHookResource, loaded, loadError, warning]; }; + +/** + * Validates that there is only one 'PostHook' and one 'PreHook' defined + * and that the exact same hooks are defined on all VMs in the plan. + * + * @param {V1beta1Plan} plan - The plan object containing VM specifications. + * @returns {string} - Returns a warning string. + */ +function validateHooks(plan: V1beta1Plan): string { + if (!plan?.spec?.vms) { + return; + } + + const hooksOnFirstVM = plan.spec.vms[0]?.hooks || []; + + const hasMultiplePostHook = hooksOnFirstVM.filter((h) => h.step === 'PostHook').length > 1; + const hasMultiplePreHook = hooksOnFirstVM.filter((h) => h.step === 'PreHook').length > 1; + + if (hasMultiplePostHook || hasMultiplePreHook) { + return 'the plan is configured with more then one hook per step'; + } + + const sortedFirstVMHooks = hooksOnFirstVM.sort((a, b) => a.step.localeCompare(b.step)); + + const sameHooks = plan.spec.vms.every((vm) => { + const sortedVMHooks = (vm.hooks || []).sort((a, b) => a.step.localeCompare(b.step)); + return JSON.stringify(sortedFirstVMHooks) === JSON.stringify(sortedVMHooks); + }); + + if (!sameHooks) { + return 'the plan is configured with different hooks for different virtual machines'; + } + + return; +} diff --git a/packages/forklift-console-plugin/src/modules/Providers/modals/components/AlertMessageForModals.tsx b/packages/forklift-console-plugin/src/modules/Providers/modals/components/AlertMessageForModals.tsx index b921b3771..76429972b 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/modals/components/AlertMessageForModals.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/modals/components/AlertMessageForModals.tsx @@ -4,11 +4,12 @@ import { Alert } from '@patternfly/react-core'; import './alerts.style.css'; -export const AlertMessageForModals: React.FC<{ title: string; message: string }> = ({ - title, - message, -}) => ( - +export const AlertMessageForModals: React.FC<{ + title: string; + message: React.ReactNode | string; + variant?: 'default' | 'danger' | 'success' | 'warning' | 'info'; +}> = ({ title, message, variant = 'danger' }) => ( + {message} );