-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kubevirt: add basic actions to VM list
- Loading branch information
Showing
4 changed files
with
189 additions
and
21 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
frontend/packages/kubevirt-plugin/src/components/modals/start-stop-vm-modal.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,71 @@ | ||
import * as React from 'react'; | ||
|
||
import { withHandlePromise } from '@console/internal/components/utils'; | ||
|
||
import { | ||
createModalLauncher, | ||
ModalTitle, | ||
ModalBody, | ||
ModalSubmitFooter, | ||
} from '@console/internal/components/factory'; | ||
|
||
import { k8sPatch } from '@console/internal/module/k8s'; | ||
|
||
import { getPxeBootPatch } from 'kubevirt-web-ui-components'; | ||
import { getName, getNamespace } from '@console/shared/src'; | ||
import { VirtualMachineModel } from '../../models'; | ||
import { VMKind } from '../../types/vm'; | ||
|
||
const StartStopVmModal = withHandlePromise((props: StartStopVmModalProps) => { | ||
const { vm, start, inProgress, errorMessage, handlePromise, close, cancel } = props; | ||
|
||
const submit = (event) => { | ||
event.preventDefault(); | ||
|
||
const patches = []; | ||
|
||
// handle PXE boot | ||
if (start) { | ||
const pxePatch = getPxeBootPatch(vm); | ||
patches.push(...pxePatch); | ||
} | ||
|
||
patches.push({ | ||
op: 'replace', | ||
path: '/spec/running', | ||
value: start, | ||
}); | ||
|
||
const promise = k8sPatch(VirtualMachineModel, vm, patches); | ||
return handlePromise(promise).then(close); | ||
}; | ||
|
||
const action = start ? 'Start' : 'Stop'; | ||
return ( | ||
<form onSubmit={submit} name="form" className="modal-content"> | ||
<ModalTitle>{action} Virtual Machine</ModalTitle> | ||
<ModalBody> | ||
Are you sure you want to {action.toLowerCase()} <strong>{getName(vm)}</strong> in namespace{' '} | ||
<strong>{getNamespace(vm)}</strong>? | ||
</ModalBody> | ||
<ModalSubmitFooter | ||
errorMessage={errorMessage} | ||
inProgress={inProgress} | ||
submitText={action} | ||
cancel={cancel} | ||
/> | ||
</form> | ||
); | ||
}); | ||
|
||
export type StartStopVmModalProps = { | ||
vm: VMKind; | ||
start: boolean; | ||
handlePromise: <T>(promise: Promise<T>) => Promise<T>; | ||
inProgress: boolean; | ||
errorMessage: string; | ||
cancel: () => void; | ||
close: () => void; | ||
}; | ||
|
||
export const startStopVmModal = createModalLauncher(StartStopVmModal); |
57 changes: 57 additions & 0 deletions
57
frontend/packages/kubevirt-plugin/src/components/vms/menu-actions.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,57 @@ | ||
import { | ||
getVmStatus, | ||
VM_STATUS_IMPORTING, | ||
VM_STATUS_V2V_CONVERSION_IN_PROGRESS, | ||
} from 'kubevirt-web-ui-components'; | ||
|
||
import { asAccessReview, Kebab, KebabAction } from '@console/internal/components/utils'; | ||
import { K8sKind, PodKind } from '@console/internal/module/k8s'; | ||
import { isVMRunning } from '../../selectors/vm'; | ||
import { startStopVmModal } from '../modals/start-stop-vm-modal'; | ||
import { VMKind } from '../../types/vm'; | ||
|
||
type ActionArgs = { | ||
pods: PodKind[]; | ||
migrations: any[]; | ||
}; | ||
|
||
const isImporting = (vm: VMKind, { pods, migrations }: ActionArgs): boolean => { | ||
const status = getVmStatus(vm, pods, migrations); | ||
return ( | ||
status && [VM_STATUS_IMPORTING, VM_STATUS_V2V_CONVERSION_IN_PROGRESS].includes(status.status) | ||
); | ||
}; | ||
|
||
const menuActionStart = (kindObj: K8sKind, vm: VMKind, actionArgs: ActionArgs): KebabAction => { | ||
return { | ||
hidden: isImporting(vm, actionArgs) || isVMRunning(vm), | ||
label: 'Start Virtual Machine', | ||
callback: () => | ||
startStopVmModal({ | ||
vm, | ||
start: true, | ||
}), | ||
accessReview: asAccessReview(kindObj, vm, 'patch'), | ||
}; | ||
}; | ||
|
||
const menuActionStop = (kindObj: K8sKind, vm: VMKind): KebabAction => { | ||
return { | ||
hidden: !isVMRunning(vm), | ||
label: 'Stop Virtual Machine', | ||
callback: () => | ||
startStopVmModal({ | ||
vm, | ||
start: false, | ||
}), | ||
accessReview: asAccessReview(kindObj, vm, 'patch'), | ||
}; | ||
}; | ||
|
||
export const menuActions = [ | ||
menuActionStart, | ||
menuActionStop, | ||
Kebab.factory.ModifyLabels, | ||
Kebab.factory.ModifyAnnotations, | ||
Kebab.factory.Delete, | ||
]; |
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
11 changes: 11 additions & 0 deletions
11
frontend/packages/kubevirt-plugin/src/selectors/vm/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,11 @@ | ||
import * as _ from 'lodash'; | ||
import { VMKind } from '../../types/vm'; | ||
|
||
export const isVMRunning = (value: VMKind) => | ||
_.get(value, 'spec.running', false) as VMKind['spec']['running']; | ||
|
||
export const isVMReady = (value: VMKind) => | ||
_.get(value, 'status.ready', false) as VMKind['status']['ready']; | ||
|
||
export const isVMCreated = (value: VMKind) => | ||
_.get(value, 'status.created', false) as VMKind['status']['created']; |