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

Refactoring: replace plan phases strings with const enums #1408

Merged
merged 1 commit into from
Dec 18, 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
@@ -1,15 +1,19 @@
{
"{{Canceled}} canceled": "{{Canceled}} canceled",
"{{canceled}} VMs canceled": "{{canceled}} VMs canceled",
"{{completed}} / {{total}}": "{{completed}} / {{total}}",
"{{dateLabel}} Failed: {{value}}": "{{dateLabel}} Failed: {{value}}",
"{{dateLabel}} Running: {{value}}": "{{dateLabel}} Running: {{value}}",
"{{dateLabel}} Succeeded: {{value}}": "{{dateLabel}} Succeeded: {{value}}",
"{{error}} VMs failed": "{{error}} VMs failed",
"{{label}} field is missing from the secret data.": "{{label}} field is missing from the secret data.",
"{{name}} Details": "{{name}} Details",
"{{selectedLength}} hosts selected.": "{{selectedLength}} hosts selected.",
"{{success}} of {{total}} VMs migrated": "{{success}} of {{total}} VMs migrated",
"{{success}} VMs succeeded": "{{success}} VMs succeeded",
"{{total}} VM": "{{total}} VM",
"{{total}} VM_plural": "{{total}} VMs",
"{{total}} VMs": "{{total}} VMs",
"{{vmCount}} VMs selected ": "{{vmCount}} VMs selected ",
"{children}": "{children}",
"24 hours": "24 hours",
Expand Down Expand Up @@ -454,6 +458,7 @@
"Start migration": "Start migration",
"Started at": "Started at",
"Status": "Status",
"Status details": "Status details",
"Storage": "Storage",
"Storage classes": "Storage classes",
"Storage domains": "Storage domains",
Expand Down Expand Up @@ -508,6 +513,7 @@
"Token": "Token",
"Total CPU count:": "Total CPU count:",
"Total memory:": "Total memory:",
"Total of {{total}} VMs are planned for migration:": "Total of {{total}} VMs are planned for migration:",
"Total virtual machines": "Total virtual machines",
"Total: {{length}}": "Total: {{length}}",
"Transfer Network": "Transfer Network",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isPlanArchived,
isPlanExecuting,
PlanData,
PlanPhase,
} from '../utils';

export const PlanActionsDropdownItems = ({ data }: PlanActionsDropdownItemsProps) => {
Expand Down Expand Up @@ -93,7 +94,9 @@ export const PlanActionsDropdownItems = ({ data }: PlanActionsDropdownItemsProps

<DropdownItem
key="archive"
isDisabled={!data?.permissions?.canDelete || ['Archived', 'Archiving'].includes(phase)}
isDisabled={
!data?.permissions?.canDelete || [PlanPhase.Archived, PlanPhase.Archiving].includes(phase)
}
onClick={onClickArchive}
>
{t('Archive Plan')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PlanModel, V1beta1Plan } from '@kubev2v/types';
import { K8sModel, k8sPatch } from '@openshift-console/dynamic-plugin-sdk';
import { Alert, Button, Modal, ModalVariant } from '@patternfly/react-core';

import { getPlanPhase } from '../utils';
import { getPlanPhase, PlanPhase } from '../utils';

/**
* Props for the DeleteModal component
Expand Down Expand Up @@ -70,7 +70,7 @@ export const ArchiveModal: React.FC<ArchiveModalProps> = ({ title, resource, red
const actions = [
<Button
key="confirm"
variant={phase === 'Running' ? 'danger' : 'primary'}
variant={phase === PlanPhase.Running ? 'danger' : 'primary'}
onClick={onArchive}
isLoading={isLoading}
>
Expand Down Expand Up @@ -112,7 +112,7 @@ export const ArchiveModal: React.FC<ArchiveModalProps> = ({ title, resource, red
</p>
</ForkliftTrans>
}
{phase === 'Running' && <IsExecutingAlert />}
{phase === PlanPhase.Running && <IsExecutingAlert />}
{alertMessage}
</Modal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { V1beta1Plan } from '@kubev2v/types';
import { k8sDelete, K8sGroupVersionKind, K8sModel } from '@openshift-console/dynamic-plugin-sdk';
import { Alert, Button, Modal, ModalVariant } from '@patternfly/react-core';

import { getPlanPhase } from '../utils';
import { getPlanPhase, PlanPhase } from '../utils';

/**
* Props for the DeleteModal component
Expand Down Expand Up @@ -131,8 +131,8 @@ export const PlanDeleteModal: React.FC<PlanDeleteModalProps> = ({
Are you sure you want to delete <strong className="co-break-word">{name}</strong>?
</ForkliftTrans>
)}
{phase === 'Running' && <IsExecutingAlert />}
{phase !== 'Archived' && <IsNotArchivedAlert />}
{phase === PlanPhase.Running && <IsExecutingAlert />}
{phase !== PlanPhase.Archived && <IsNotArchivedAlert />}
{typeof owner === 'object' && <ItemIsOwnedAlert owner={owner} namespace={namespace} />}
{alertMessage}
</Modal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import { PlanPhase } from '../types';
* This array is intended to be used for creating filter dropdowns, where users can select a plan phase to filter the results shown.
*/
export const planPhases: { id: PlanPhase; label: string }[] = [
{ id: 'Error', label: 'Error' },
{ id: 'vmError', label: 'VM Error' },
{ id: 'Unknown', label: 'Unknown' },
{ id: 'Archiving', label: 'Archiving' },
{ id: 'Archived', label: 'Archived' },
{ id: 'Failed', label: 'Failed' },
{ id: 'Canceled', label: 'Canceled' },
{ id: 'Succeeded', label: 'Succeeded' },
{ id: 'Running', label: 'Running' },
{ id: 'Ready', label: 'Ready' },
{ id: 'Warning', label: 'Warning' },
{ id: 'NotReady', label: 'Not Ready' },
{ id: PlanPhase.Error, label: PlanPhase.Error },
{ id: PlanPhase.vmError, label: PlanPhase.vmError },
{ id: PlanPhase.Unknown, label: PlanPhase.Unknown },
{ id: PlanPhase.Archiving, label: PlanPhase.Archiving },
{ id: PlanPhase.Archived, label: PlanPhase.Archived },
{ id: PlanPhase.Failed, label: PlanPhase.Failed },
{ id: PlanPhase.Canceled, label: PlanPhase.Canceled },
{ id: PlanPhase.Succeeded, label: PlanPhase.Succeeded },
{ id: PlanPhase.Running, label: PlanPhase.Running },
{ id: PlanPhase.Ready, label: PlanPhase.Ready },
{ id: PlanPhase.Warning, label: PlanPhase.Warning },
{ id: PlanPhase.NotReady, label: PlanPhase.NotReady },
];

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import { PlanData, PlanPhase } from '../types';
export const getPlanPhase = (data: PlanData): PlanPhase => {
const plan = data?.obj;

if (!plan) return 'Unknown';
if (!plan) return PlanPhase.Unknown;

// Check condition type
const conditions = getConditions(plan);

if (!conditions || conditions?.length < 1) {
return 'Unknown';
return PlanPhase.Unknown;
}

// Check for Archived
if (plan?.spec?.archived && !conditions.includes('Archived')) {
return 'Archiving';
return PlanPhase.Archiving;
}

if (conditions.includes('Archived')) {
return 'Archived';
return PlanPhase.Archived;
}

// Check for Succeeded
if (conditions.includes('Succeeded')) {
return 'Succeeded';
return PlanPhase.Succeeded;
}

// Check for Canceled
if (conditions.includes('Canceled')) {
return 'Canceled';
return PlanPhase.Canceled;
}

// CHeck for Running
if (conditions.includes('Executing')) {
return 'Running';
return PlanPhase.Running;
}

// Check condition category
Expand All @@ -44,18 +44,18 @@ export const getPlanPhase = (data: PlanData): PlanPhase => {
);

if (isCritical) {
return 'Error';
return PlanPhase.Error;
}

// Check for vm errors
const vmError = plan?.status?.migration?.vms?.find((vm) => vm?.error);

if (conditions.includes('Failed')) {
return 'Failed';
return PlanPhase.Failed;
}

if (vmError) {
return 'vmError';
return PlanPhase.vmError;
}

// Check condition category
Expand All @@ -64,14 +64,14 @@ export const getPlanPhase = (data: PlanData): PlanPhase => {
);

if (isWarn) {
return 'Warning';
return PlanPhase.Warning;
}

if (conditions.includes('Ready')) {
return 'Ready';
return PlanPhase.Ready;
}

return 'NotReady';
return PlanPhase.NotReady;
};

export const canPlanStart = (plan: V1beta1Plan) => {
Expand Down Expand Up @@ -107,20 +107,20 @@ export const isPlanEditable = (plan: V1beta1Plan) => {
const planStatus = getPlanPhase({ obj: plan });

return (
planStatus === 'Unknown' ||
planStatus === 'Canceled' ||
planStatus === 'Error' ||
planStatus === 'Failed' ||
planStatus === 'vmError' ||
planStatus === 'Warning' ||
planStatus === 'Ready'
planStatus === PlanPhase.Unknown ||
planStatus === PlanPhase.Canceled ||
planStatus === PlanPhase.Error ||
planStatus === PlanPhase.Failed ||
planStatus === PlanPhase.vmError ||
planStatus === PlanPhase.Warning ||
planStatus === PlanPhase.Ready
);
};

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

return planStatus === 'Archiving' || planStatus === 'Archived';
return planStatus === PlanPhase.Archiving || planStatus === PlanPhase.Archived;
};

const getConditions = (obj: V1beta1Plan) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export const getPlanProgressVariant = (phase: PlanPhase): ProgressVariant => {
let progressVariant;

switch (phase) {
case 'Error':
case 'Failed':
case PlanPhase.Error:
case PlanPhase.Failed:
progressVariant = ProgressVariant.danger;
break;
case 'Unknown':
case 'Archived':
case 'Canceled':
case 'vmError':
case PlanPhase.Unknown:
case PlanPhase.Archived:
case PlanPhase.Canceled:
case PlanPhase.vmError:
progressVariant = ProgressVariant.warning;
break;
case 'Succeeded':
case PlanPhase.Succeeded:
progressVariant = ProgressVariant.success;
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @index(['./*', /style/g], f => `export * from '${f.path}';`)
export * from './getMigrationPhase';
export * from './getMigrationVmsCounts';
export * from './getPhaseLabel';
export * from './getPlanPhase';
export * from './getPlanProgressVariant';
// @endindex
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
export type PlanPhase =
| 'Error'
| 'vmError'
| 'Unknown'
| 'Archiving'
| 'Archived'
| 'Failed'
| 'Canceled'
| 'Succeeded'
| 'Running'
| 'Ready'
| 'Warning'
| 'NotReady';
export enum PlanPhase {
// t('Error')
Error = 'Error',
// t('Some VMs Failed')
vmError = 'Some VMs Failed',
// t('Unknown')
Unknown = 'Unknown',
// t('Archiving')
Archiving = 'Archiving',
// t('Archived')
Archived = 'Archived',
// t('Failed')
Failed = 'Failed',
// t('Canceled')
Canceled = 'Canceled',
// t('Succeeded')
Succeeded = 'Succeeded',
// t('Running')
Running = 'Running',
// t('Ready')
Ready = 'Ready',
// t('Warning')
Warning = 'Warning',
// t('Not Ready')
NotReady = 'Not Ready',
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import React from 'react';
import { ConsoleTimestamp } from 'src/components/ConsoleTimestamp';
import {
getMigrationVmsCounts,
getPhaseLabel,
getPlanProgressVariant,
} from 'src/modules/Plans/utils';
import { getMigrationVmsCounts, getPlanProgressVariant, PlanPhase } from 'src/modules/Plans/utils';
import { getMigrationPhase } from 'src/modules/Plans/utils/helpers/getMigrationPhase';
import { useForkliftTranslation } from 'src/utils/i18n';

Expand Down Expand Up @@ -90,8 +86,9 @@ const VMsLabel: React.FC<{ migration: V1beta1Migration }> = ({ migration }) => {
const { t } = useForkliftTranslation();

const phase = getMigrationPhase(migration);
const phaseLabel = t(getPhaseLabel(phase));
const progressVariant = getPlanProgressVariant(phase);
const phaseLabel = PlanPhase[phase] ? t(PlanPhase[phase]) : PlanPhase.Unknown;

const progressVariant = getPlanProgressVariant(PlanPhase[phase]);
const counters = getMigrationVmsCounts(migration?.status?.vms);

if (!counters?.total || counters.total === 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { PlanActionsDropdown } from 'src/modules/Plans/actions';
import { PlanStartMigrationModal } from 'src/modules/Plans/modals';
import { canPlanReStart, canPlanStart, getPlanPhase } from 'src/modules/Plans/utils';
import { canPlanReStart, canPlanStart, getPlanPhase, PlanPhase } from 'src/modules/Plans/utils';
import { useGetDeleteAndEditAccessReview } from 'src/modules/Providers/hooks';
import { useModal } from 'src/modules/Providers/modals';
import { PageHeadings } from 'src/modules/Providers/utils';
Expand Down Expand Up @@ -81,7 +81,7 @@ export const PlanPageHeadings: React.FC<{ name: string; namespace: string }> = (

const handleAlerts = () => {
// alerts are not relevant to display if plan was completed successfully
if (planStatus === 'Succeeded') return;
if (planStatus === PlanPhase.Succeeded) return;

if (criticalCondition) {
alerts.push(
Expand Down
Loading
Loading