-
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.
Merge pull request #1046 from yaacov/refactor-plan-actions
🐞 Fix plan phase, successfull before warn
- Loading branch information
Showing
13 changed files
with
366 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,4 @@ storagedomains | |
esxi | ||
KJUR | ||
millicores | ||
inputgroup |
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
3 changes: 3 additions & 0 deletions
3
packages/forklift-console-plugin/src/modules/Plans/hooks/index.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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// @index(['./*', /style/g], f => `export * from '${f.path}';`) | ||
export * from './usePlanMigration'; | ||
// @endindex |
26 changes: 26 additions & 0 deletions
26
packages/forklift-console-plugin/src/modules/Plans/hooks/usePlanMigration.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { MigrationModelGroupVersionKind, V1beta1Migration, V1beta1Plan } from '@kubev2v/types'; | ||
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'; | ||
|
||
export const usePlanMigration = (plan: V1beta1Plan) => { | ||
const [migrations, migrationLoaded, migrationLoadError] = useK8sWatchResource<V1beta1Migration[]>( | ||
{ | ||
groupVersionKind: MigrationModelGroupVersionKind, | ||
namespaced: true, | ||
isList: true, | ||
namespace: plan?.metadata?.namespace, | ||
}, | ||
); | ||
|
||
const planMigrations = ( | ||
migrations && migrationLoaded && !migrationLoadError ? migrations : [] | ||
).filter((m) => m?.metadata?.ownerReferences?.[0]?.uid === plan.metadata.uid); | ||
|
||
planMigrations?.sort( | ||
(a, b) => | ||
new Date(b.metadata.creationTimestamp).getTime() - | ||
new Date(a.metadata.creationTimestamp).getTime(), | ||
); | ||
const lastMigration = planMigrations[0]; | ||
|
||
return [lastMigration, migrationLoaded, migrationLoadError]; | ||
}; |
5 changes: 5 additions & 0 deletions
5
...ages/forklift-console-plugin/src/modules/Plans/modals/PlanCutoverMigrationModal.style.css
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,5 @@ | ||
.forklift-plan-cutover-migration-inputgroup { | ||
padding-top: var(--pf-global--spacer--lg); | ||
padding-bottom: var(--pf-global--spacer--lg); | ||
} | ||
|
182 changes: 182 additions & 0 deletions
182
packages/forklift-console-plugin/src/modules/Plans/modals/PlanCutoverMigrationModal.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,182 @@ | ||
import React, { ReactNode, useCallback, useEffect, useState } from 'react'; | ||
import { useToggle } from 'src/modules/Providers/hooks'; | ||
import { AlertMessageForModals, useModal } from 'src/modules/Providers/modals'; | ||
import { ForkliftTrans, useForkliftTranslation } from 'src/utils/i18n'; | ||
|
||
import { MigrationModel, V1beta1Migration, V1beta1Plan } from '@kubev2v/types'; | ||
import { k8sPatch } from '@openshift-console/dynamic-plugin-sdk'; | ||
import { | ||
Button, | ||
DatePicker, | ||
InputGroup, | ||
Modal, | ||
ModalVariant, | ||
TimePicker, | ||
yyyyMMddFormat, | ||
} from '@patternfly/react-core'; | ||
|
||
import { usePlanMigration } from '../hooks/usePlanMigration'; | ||
|
||
import './PlanCutoverMigrationModal.style.css'; | ||
|
||
/** | ||
* Props for the DeleteModal component | ||
* @typedef PlanStartMigrationModalProps | ||
* @property {string} title - The title to display in the modal | ||
* @property {V1beta1Plan} resource - The resource object to delete | ||
* @property {K8sModel} model - The model used for deletion | ||
* @property {string} [redirectTo] - Optional redirect URL after deletion | ||
*/ | ||
interface PlanCutoverMigrationModalProps { | ||
resource: V1beta1Plan; | ||
title?: string; | ||
} | ||
|
||
/** | ||
* A generic delete modal component | ||
* @component | ||
* @param {DeleteModalProps} props - Props for DeleteModal | ||
* @returns {React.Element} The DeleteModal component | ||
*/ | ||
export const PlanCutoverMigrationModal: React.FC<PlanCutoverMigrationModalProps> = ({ | ||
title, | ||
resource, | ||
}) => { | ||
const { t } = useForkliftTranslation(); | ||
const { toggleModal } = useModal(); | ||
const [isLoading, toggleIsLoading] = useToggle(); | ||
const [cutoverDate, setCutoverDate] = useState<string>(); | ||
const [alertMessage, setAlertMessage] = useState<ReactNode>(null); | ||
|
||
const title_ = title || t('Cutover'); | ||
const { name } = resource?.metadata || {}; | ||
|
||
const [lastMigration] = usePlanMigration(resource); | ||
|
||
useEffect(() => { | ||
const migrationCutoverDate = lastMigration?.spec?.cutover || new Date().toISOString(); | ||
|
||
setCutoverDate(migrationCutoverDate); | ||
}, [lastMigration]); | ||
|
||
const onDateChange = (inputDate, newDate: string) => { | ||
const updatedFromDate = cutoverDate ? new Date(cutoverDate) : new Date(); | ||
|
||
const [year, month, day] = newDate.split('-').map((num: string) => parseInt(num, 10)); | ||
|
||
updatedFromDate.setFullYear(year); | ||
updatedFromDate.setMonth(month - 1); | ||
updatedFromDate.setDate(day); | ||
|
||
setCutoverDate(updatedFromDate.toISOString()); | ||
}; | ||
|
||
const onTimeChange = (_event, _time, hour: number, minute: number) => { | ||
const updatedFromDate = cutoverDate ? new Date(cutoverDate) : new Date(); | ||
|
||
updatedFromDate.setHours(hour); | ||
updatedFromDate.setMinutes(minute); | ||
|
||
setCutoverDate(updatedFromDate.toISOString()); | ||
}; | ||
|
||
const onCutover = useCallback(async () => { | ||
toggleIsLoading(); | ||
|
||
try { | ||
await patchMigrationCutover(lastMigration, cutoverDate); | ||
|
||
toggleModal(); | ||
} catch (err) { | ||
toggleIsLoading(); | ||
|
||
setAlertMessage(<AlertMessageForModals title={t('Error')} message={err.toString()} />); | ||
} | ||
}, [cutoverDate, lastMigration]); | ||
|
||
const onDeleteCutover = useCallback(async () => { | ||
toggleIsLoading(); | ||
|
||
try { | ||
await patchMigrationCutover(lastMigration, undefined); | ||
|
||
toggleModal(); | ||
} catch (err) { | ||
toggleIsLoading(); | ||
|
||
setAlertMessage(<AlertMessageForModals title={t('Error')} message={err.toString()} />); | ||
} | ||
}, [lastMigration]); | ||
|
||
const actions = [ | ||
<Button key="confirm" onClick={onCutover} isLoading={isLoading}> | ||
{t('Set cutover')} | ||
</Button>, | ||
<Button key="delete" variant="secondary" onClick={onDeleteCutover} isLoading={isLoading}> | ||
{t('Remove cutover')} | ||
</Button>, | ||
<Button key="cancel" variant="secondary" onClick={toggleModal}> | ||
{t('Cancel')} | ||
</Button>, | ||
]; | ||
|
||
return ( | ||
<Modal | ||
title={title_} | ||
position="top" | ||
showClose={false} | ||
variant={ModalVariant.small} | ||
isOpen={true} | ||
onClose={toggleModal} | ||
actions={actions} | ||
> | ||
<> | ||
<ForkliftTrans> | ||
<p> | ||
Schedule the cutover for migration{' '} | ||
<strong className="co-break-word">{{ resourceName: name }}</strong>? | ||
</p> | ||
<br /> | ||
<p> | ||
You can schedule cutover for now or a future date and time. VMs included in the | ||
migration plan will be shut down when cutover starts. | ||
</p> | ||
</ForkliftTrans> | ||
|
||
<InputGroup className="forklift-plan-cutover-migration-inputgroup"> | ||
<DatePicker | ||
onChange={onDateChange} | ||
aria-label="Cutover date" | ||
placeholder="YYYY-MM-DD" | ||
appendTo={document.body} | ||
value={yyyyMMddFormat(cutoverDate ? new Date(cutoverDate) : new Date())} | ||
/> | ||
<TimePicker | ||
aria-label="Cutover time" | ||
style={{ width: '150px' }} | ||
onChange={onTimeChange} | ||
menuAppendTo={document.body} | ||
time={cutoverDate ? new Date(cutoverDate) : new Date()} | ||
/> | ||
</InputGroup> | ||
</> | ||
{alertMessage} | ||
</Modal> | ||
); | ||
}; | ||
|
||
async function patchMigrationCutover(migration: V1beta1Migration, cutover: string) { | ||
const op = migration?.spec?.cutover ? 'replace' : 'add'; | ||
|
||
await k8sPatch({ | ||
model: MigrationModel, | ||
resource: migration, | ||
data: [ | ||
{ | ||
op, | ||
path: '/spec/cutover', | ||
value: cutover, | ||
}, | ||
], | ||
}); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/forklift-console-plugin/src/modules/Plans/modals/index.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,6 +1,7 @@ | ||
// @index(['./*', /style/g], f => `export * from '${f.path}';`) | ||
export * from './ArchiveModal'; | ||
export * from './DuplicateModal'; | ||
export * from './PlanCutoverMigrationModal'; | ||
export * from './PlanDeleteModal'; | ||
export * from './PlanStartMigrationModal'; | ||
// @endindex |
Oops, something went wrong.