Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid removing VMs from an archived/archiving plans. #1395

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"Delete Provider": "Delete Provider",
"Delete StorageMap": "Delete StorageMap",
"Delete virtual machines from migration plan": "Delete virtual machines from migration plan",
"Deleting virtual machines from an archived migration plan is not allowed.": "Deleting virtual machines from an archived migration plan is not allowed.",
"Description": "Description",
"Details": "Details",
"Determines the frequency with which the system checks the status of snapshot creation or removal during oVirt warm migration. The default value is 10 seconds.": "Determines the frequency with which the system checks the status of snapshot creation or removal during oVirt warm migration. The default value is 10 seconds.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,11 @@ export const isPlanEditable = (plan: V1beta1Plan) => {
);
};

export const isPlanArchived = (plan: V1beta1Plan) => {
const planStatus = getPlanPhase({ obj: plan });

return planStatus === 'Archiving' || planStatus === 'Archived';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Archiving and Archived will probably be widely used, therefore it better to add them to a const file and prevent future typos

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree but current code is using the PlanPhase type based on strings and this type is used all over by comparing to strings.
Not complicated to refactor, but better do it in a follow up PR as well since touching other files.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PlanPhase can be an enum and used in the compare planStatus === PlanPhase.Archiving etc... lets refactor it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@metalice
Sure!
But I really prefer not to do it as part of this PR, but rather solve it today in a follow up one, since:

  1. it is not related to this fix and touches other components/pages so it's better to separate.
  2. It will generate unnecessary code conflicts since there is a merged code from me which is related to the plan status, e.g.
    export const isPlanArchived = (plan: V1beta1Plan) => {
    const planStatus = getPlanPhase({ obj: plan });
    return planStatus === 'Archiving' || planStatus === 'Archived';
    };

Let's merge the opened PRs relevant for plan status and I'll handle this issue separately.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A follow up PR: #1408

};

const getConditions = (obj: V1beta1Plan) =>
obj?.status?.conditions?.filter((c) => c.status === 'True').map((c) => c.type);
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
StandardPageWithSelectionProps,
} from 'src/components/page/StandardPageWithSelection';
import { usePlanMigration } from 'src/modules/Plans/hooks/usePlanMigration';
import { isPlanExecuting } from 'src/modules/Plans/utils';
import { isPlanArchived, isPlanExecuting } from 'src/modules/Plans/utils';
import { useForkliftTranslation } from 'src/utils/i18n';

import { loadUserSettings, ResourceFieldFactory } from '@kubev2v/common';
Expand Down Expand Up @@ -244,10 +244,11 @@ export const MigrationVirtualMachinesList: FC<{ obj: PlanData }> = ({ obj }) =>
}));

const isExecuting = isPlanExecuting(plan);
const isArchived = isPlanArchived(plan);

// If plan executing allow to cancel vms, o/w remove from plan
// If plan executing and not archived (happens when archiving a running plan), allow to cancel vms, o/w remove from plan
let actions: PageGlobalActions;
if (isExecuting) {
if (isExecuting && !isArchived) {
actions = [
({ selectedIds }) => (
<MigrationVMsCancelButton selectedIds={selectedIds || []} migration={lastMigration} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { ReactNode, useCallback, useState } from 'react';
import { isPlanArchived } from 'src/modules/Plans/utils';
import { useToggle } from 'src/modules/Providers/hooks';
import { AlertMessageForModals, useModal } from 'src/modules/Providers/modals';
import { useForkliftTranslation } from 'src/utils/i18n';
Expand All @@ -23,17 +24,20 @@ export const PlanVMsDeleteModal: React.FC<PlanVMsDeleteModalProps> = ({ plan, se
const vms = (plan?.spec?.vms || []).filter((vm) => !selected.includes(vm.id)) || [];

React.useEffect(() => {
if (isPlanArchived(plan)) {
setAlertMessage(
Copy link
Collaborator

@metalice metalice Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why setting the whole comopnent as a state? make it much harder for React to compare, and not only the msg string? also, you will reduce the reputation in the code here of

<AlertMessageForModals title={t('Error')} message={something} />,

instead of declaring it 3 times, only once..

Copy link
Collaborator

@metalice metalice Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in current line 100 u can do after it
{msg && <AlertMessageForModals title={t('Error')} message={msg} />}

t('Deleting virtual machines from an archived migration plan is not allowed.'),
);
return;
}
if (vms.length < 1) {
setAlertMessage(
<AlertMessageForModals
title={t('Error')}
message={t(
'All virtual machines planned for migration are selected for deletion, deleting all virtual machines from a migration plan is not allowed.',
)}
/>,
t(
'All virtual machines planned for migration are selected for deletion, deleting all virtual machines from a migration plan is not allowed.',
),
);
}
}, [vms]);
}, [vms, plan]);

const handleSave = useCallback(async () => {
toggleIsLoading();
Expand All @@ -51,10 +55,7 @@ export const PlanVMsDeleteModal: React.FC<PlanVMsDeleteModalProps> = ({ plan, se
toggleModal();
} catch (err) {
toggleIsLoading();

setAlertMessage(
<AlertMessageForModals title={t('Error')} message={err.message || err.toString()} />,
);
setAlertMessage(err.message || err.toString());
}
}, [selected]);

Expand All @@ -63,7 +64,7 @@ export const PlanVMsDeleteModal: React.FC<PlanVMsDeleteModalProps> = ({ plan, se
key="confirm"
onClick={handleSave}
variant="danger"
isDisabled={vms.length < 1}
isDisabled={vms.length < 1 || isPlanArchived(plan)}
isLoading={isLoading}
>
{t('Delete')}
Expand All @@ -86,8 +87,7 @@ export const PlanVMsDeleteModal: React.FC<PlanVMsDeleteModalProps> = ({ plan, se
<div className="forklift-edit-modal-body">
{t('Are you sure you want to delete this virtual machines from the migration plan?')}
</div>

{alertMessage}
{alertMessage && <AlertMessageForModals title={t('Error')} message={alertMessage} />}
</Modal>
);
};
Loading