-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alert when stoping a vm with logged in users
- Loading branch information
Showing
8 changed files
with
348 additions
and
64 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
...packages/kubevirt-plugin/src/components/modals/menu-actions-modals/confirm-vmi-modal.scss
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 @@ | ||
.kubevirt-confirm-vmi-model__alert { | ||
padding: 0 var(--pf-global--spacer--xl) 0; | ||
} |
123 changes: 123 additions & 0 deletions
123
.../packages/kubevirt-plugin/src/components/modals/menu-actions-modals/confirm-vmi-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,123 @@ | ||
import * as React from 'react'; | ||
import { Alert, Button } from '@patternfly/react-core'; | ||
import { HandlePromiseProps, history, withHandlePromise } from '@console/internal/components/utils'; | ||
import { getName, getNamespace } from '@console/shared/src/selectors/common'; | ||
import { | ||
createModalLauncher, | ||
ModalTitle, | ||
ModalBody, | ||
ModalSubmitFooter, | ||
ModalComponentProps, | ||
} from '@console/internal/components/factory'; | ||
import { useGuestAgentInfo } from '../../../hooks/use-guest-agent-info'; | ||
import { GuestAgentInfoWrapper } from '../../../k8s/wrapper/vm/guest-agent-info/guest-agent-info-wrapper'; | ||
import { VMIKind } from '../../../types'; | ||
|
||
import './confirm-vmi-modal.scss'; | ||
|
||
const redirectFn = (vmi: VMIKind) => { | ||
// If we are currently on the deleted resource's page, redirect to the resource list page | ||
const re = new RegExp(`/${getName(vmi)}(/|$)`); | ||
if (re.test(window.location.pathname)) { | ||
history.push(`/k8s/ns/${getNamespace(vmi)}/virtualization`); | ||
} | ||
}; | ||
|
||
export const ConfirmVMIModal = withHandlePromise((props: ConfirmVMIModalProps) => { | ||
const { | ||
inProgress, | ||
errorMessage, | ||
handlePromise, | ||
close, | ||
cancel, | ||
vmi, | ||
title, | ||
message, | ||
executeFn, | ||
btnText, | ||
cancelText, | ||
alertTitle, | ||
submitDanger, | ||
} = props; | ||
const [guestAgentInfoRaw] = useGuestAgentInfo({ vmi }); | ||
const guestAgentInfo = new GuestAgentInfoWrapper(guestAgentInfoRaw); | ||
const userListLength = guestAgentInfo.getUserList()?.length || 0; | ||
|
||
const name = getName(vmi); | ||
const namespace = getNamespace(vmi); | ||
const alertHref = `/k8s/ns/${namespace}/virtualmachines/${name}/details#logged-in-users`; | ||
const onLinkClick = () => { | ||
cancel(); | ||
history.push(alertHref); | ||
}; | ||
|
||
let alertBody = null; | ||
if (userListLength === 1) { | ||
alertBody = ( | ||
<> | ||
<Button variant="link" isInline onClick={onLinkClick}> | ||
One user | ||
</Button>{' '} | ||
is currently logged in to this VM. Proceeding with this operation may cause logged in users | ||
data loss. | ||
</> | ||
); | ||
} else if (userListLength > 1) { | ||
alertBody = ( | ||
<> | ||
<Button variant="link" isInline onClick={onLinkClick}> | ||
{userListLength} Users | ||
</Button>{' '} | ||
are currently logged in to this VM. Proceeding with this operation may cause logged in users | ||
data loss. | ||
</> | ||
); | ||
} | ||
|
||
const alert = userListLength > 0 && ( | ||
<div className="kubevirt-confirm-vmi-model__alert"> | ||
<Alert isInline variant="warning" title={alertTitle || 'Alert'}> | ||
{alertBody} | ||
</Alert> | ||
</div> | ||
); | ||
|
||
const submit = (e) => { | ||
e.preventDefault(); | ||
|
||
return handlePromise(executeFn()) | ||
.then(close) | ||
.then(() => redirectFn(vmi)); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={submit} className="modal-content"> | ||
<ModalTitle>{title}</ModalTitle> | ||
<ModalBody>{message}</ModalBody> | ||
{alert} | ||
<ModalSubmitFooter | ||
errorMessage={errorMessage} | ||
submitDisabled={inProgress} | ||
inProgress={inProgress} | ||
submitText={btnText || 'Confirm'} | ||
submitDanger={submitDanger} | ||
cancel={cancel} | ||
cancelText={cancelText || 'Cancel'} | ||
/> | ||
</form> | ||
); | ||
}); | ||
|
||
export type ConfirmVMIModalProps = { | ||
vmi: VMIKind; | ||
message: React.ReactNode | string; | ||
title: React.ReactNode | string; | ||
executeFn: () => Promise<any>; | ||
btnText?: React.ReactNode | string; | ||
cancelText?: React.ReactNode | string; | ||
alertTitle?: string; | ||
submitDanger?: boolean; | ||
} & ModalComponentProps & | ||
HandlePromiseProps; | ||
|
||
export const confirmVMIModal = createModalLauncher(ConfirmVMIModal); |
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
107 changes: 107 additions & 0 deletions
107
...es/kubevirt-plugin/src/components/modals/menu-actions-modals/delete-vm-template-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,107 @@ | ||
import * as React from 'react'; | ||
import { HandlePromiseProps, history, withHandlePromise } from '@console/internal/components/utils'; | ||
import { YellowExclamationTriangleIcon } from '@console/shared/src/components/status/icons'; | ||
import { getName, getNamespace } from '@console/shared/src/selectors/common'; | ||
import { | ||
createModalLauncher, | ||
ModalTitle, | ||
ModalBody, | ||
ModalSubmitFooter, | ||
ModalComponentProps, | ||
} from '@console/internal/components/factory'; | ||
import { TemplateModel } from '@console/internal/models'; | ||
import { apiVersionForModel, TemplateKind } from '@console/internal/module/k8s'; | ||
import { asVM, getVolumes } from '../../../selectors/vm'; | ||
import { useOwnedVolumeReferencedResources } from '../../../hooks/use-owned-volume-referenced-resources'; | ||
import { useUpToDateVMLikeEntity } from '../../../hooks/use-vm-like-entity'; | ||
import { deleteVMTemplate } from '../../../k8s/requests/vmtemplate/actions'; | ||
|
||
const redirectFn = (vmLikeEntity: TemplateKind) => { | ||
// If we are currently on the deleted resource's page, redirect to the resource list page | ||
const re = new RegExp(`/${getName(vmLikeEntity)}(/|$)`); | ||
if (re.test(window.location.pathname)) { | ||
history.push(`/k8s/ns/${getNamespace(vmLikeEntity)}/virtualization/templates`); | ||
} | ||
}; | ||
|
||
export const DeleteVMTemplateModal = withHandlePromise((props: DeleteVMTemplateModalProps) => { | ||
const { inProgress, errorMessage, handlePromise, close, cancel, vmTemplate } = props; | ||
const vmLikeEntity = useUpToDateVMLikeEntity<TemplateKind>(vmTemplate); | ||
const [deleteDisks, setDeleteDisks] = React.useState<boolean>(true); | ||
|
||
const namespace = getNamespace(vmLikeEntity); | ||
const name = getName(vmLikeEntity); | ||
|
||
const vmLikeReference = { | ||
name, | ||
kind: TemplateModel.kind, | ||
apiVersion: apiVersionForModel(TemplateModel), | ||
} as any; | ||
|
||
const [ownedVolumeResources, isOwnedVolumeResourcesLoaded] = useOwnedVolumeReferencedResources( | ||
vmLikeReference, | ||
namespace, | ||
getVolumes(asVM(vmLikeEntity), null), | ||
); | ||
const isInProgress = inProgress || !isOwnedVolumeResourcesLoaded; | ||
const numOfAllResources = ownedVolumeResources.length; | ||
|
||
const submit = (e) => { | ||
e.preventDefault(); | ||
|
||
const promise = deleteVMTemplate(vmLikeEntity, { | ||
ownedVolumeResources, | ||
deleteOwnedVolumeResources: deleteDisks, | ||
}); | ||
|
||
return handlePromise(promise) | ||
.then(close) | ||
.then(() => redirectFn(vmLikeEntity)); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={submit} className="modal-content"> | ||
<ModalTitle> | ||
<YellowExclamationTriangleIcon className="co-icon-space-r" /> Delete Virtual Machine | ||
Template ? | ||
</ModalTitle> | ||
<ModalBody> | ||
Are you sure you want to delete <strong className="co-break-word">{name}</strong> in | ||
namespace <strong>{namespace}</strong> ? | ||
{numOfAllResources > 0 && ( | ||
<p> | ||
The following resources will be deleted along with this virtual machine. Unchecked items | ||
will not be deleted. | ||
</p> | ||
)} | ||
{ownedVolumeResources.length > 0 && ( | ||
<div className="checkbox"> | ||
<label className="control-label"> | ||
<input | ||
type="checkbox" | ||
onChange={() => setDeleteDisks(!deleteDisks)} | ||
checked={deleteDisks} | ||
/> | ||
Delete Disks ({ownedVolumeResources.length}x) | ||
</label> | ||
</div> | ||
)} | ||
</ModalBody> | ||
<ModalSubmitFooter | ||
errorMessage={errorMessage} | ||
submitDisabled={isInProgress} | ||
inProgress={isInProgress} | ||
submitText="Delete" | ||
submitDanger | ||
cancel={cancel} | ||
/> | ||
</form> | ||
); | ||
}); | ||
|
||
export type DeleteVMTemplateModalProps = { | ||
vmTemplate: TemplateKind; | ||
} & ModalComponentProps & | ||
HandlePromiseProps; | ||
|
||
export const deleteVMTemplateModal = createModalLauncher(DeleteVMTemplateModal); |
Oops, something went wrong.