|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * virt/managing_vms/virt-customize-web-console.adoc |
| 4 | + |
| 5 | +:_mod-docs-content-type: PROCEDURE |
| 6 | +[id="virt-enable-bulk-operations-web-console_{context}"] |
| 7 | += Enable bulk operations for virtual machines |
| 8 | + |
| 9 | +[role="_abstract"] |
| 10 | +You can enable virtual machine (VM) owners to perform large-scale management tasks, such as backups and storage migrations, across multiple virtual machines simultaneously, by creating a dynamic plugin that enables bulk actions in the web console. |
| 11 | + |
| 12 | +This integration reduces manual overhead for multi-VM environments and ensures that custom actions are available as native, selectable bulk actions from within the *Virtualization* page. |
| 13 | + |
| 14 | +.Prerequisites |
| 15 | + |
| 16 | +* You have created a dynamic plugin. |
| 17 | +* You have cluster administrator permissions. |
| 18 | +* You have access to an {product-title} cluster where {VirtProductName} is installed. |
| 19 | +
|
| 20 | +.Procedure |
| 21 | + |
| 22 | +. In the configuration file of your plugin, add a `console.action/provider` extension. |
| 23 | ++ |
| 24 | +To enable bulk actions, you must use a `contextId` field that targets an array of `VirtualMachine` resources. |
| 25 | ++ |
| 26 | +Example `console-extensions.json` file excerpt: |
| 27 | ++ |
| 28 | +[source,json] |
| 29 | +---- |
| 30 | +{ |
| 31 | + type: 'console.action/provider', |
| 32 | + properties: { |
| 33 | + contextId: 'kubevirt.io~v1~VirtualMachine[]', |
| 34 | + provider: { |
| 35 | + $codeRef: 'useSimpleBulkActions', |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | +---- |
| 40 | +** `properties.contextId` specifies a string for which the KubeVirt plugin declares support. |
| 41 | +** `properties.provider` specifies the React hook or function in your source code that generates the action items. |
| 42 | + |
| 43 | +. In the source file referenced by the extension, implement a hook that handles the array of selected resources. |
| 44 | ++ |
| 45 | +Example plugin: |
| 46 | ++ |
| 47 | +[source,ts] |
| 48 | +---- |
| 49 | +import { |
| 50 | + type ExtensionHook, |
| 51 | + AccessReviewResourceAttributes, |
| 52 | + Action, |
| 53 | +} from '@openshift-console/dynamic-plugin-sdk'; |
| 54 | +import { V1VirtualMachine } from '@kubevirt-ui-ext/kubevirt-api/kubevirt'; |
| 55 | +import { VirtualMachineModel } from '@kubevirt-ui-ext/kubevirt-api/console'; |
| 56 | +import { useMemo } from 'react'; |
| 57 | + |
| 58 | +const useSimpleBulkActions: ExtensionHook<Action[], (V1VirtualMachine & { cluster?: string })[]> = ( |
| 59 | + vms, |
| 60 | +) => { |
| 61 | + const areAllRunning = vms.every((vm) => vm.status?.printableStatus === 'Running'); |
| 62 | + const isCrossCluster = new Set(vms.map((vm) => vm.cluster)).size > 1; |
| 63 | + const firstVm = vms[0]; |
| 64 | + |
| 65 | + const accessReview: AccessReviewResourceAttributes = useMemo( |
| 66 | + () => ({ |
| 67 | + cluster: firstVm?.cluster, |
| 68 | + group: VirtualMachineModel.apiGroup, |
| 69 | + name: firstVm?.metadata?.name, |
| 70 | + namespace: firstVm?.metadata?.namespace, |
| 71 | + resource: VirtualMachineModel.plural, |
| 72 | + verb: 'delete', |
| 73 | + }), |
| 74 | + [firstVm?.cluster, firstVm?.metadata?.name, firstVm?.metadata?.namespace], |
| 75 | + ); |
| 76 | + |
| 77 | + const checkAllRunningAction: Action = useMemo( |
| 78 | + () => ({ |
| 79 | + id: 'check-all-running', |
| 80 | + cta: () => console.log('All selected VMs are running?', areAllRunning), |
| 81 | + label: 'Check VMs are running', |
| 82 | + disabled: isCrossCluster, |
| 83 | + disabledTooltip: isCrossCluster ? 'VMs from different clusters detected' : '', |
| 84 | + accessReview, |
| 85 | + }), |
| 86 | + [areAllRunning, isCrossCluster, accessReview], |
| 87 | + ); |
| 88 | + |
| 89 | + const actions = useMemo(() => [checkAllRunningAction], [checkAllRunningAction]); |
| 90 | + return [actions, true, null]; |
| 91 | +}; |
| 92 | + |
| 93 | +export default useSimpleBulkActions; |
| 94 | +---- |
| 95 | ++ |
| 96 | +The plugin shown in the previous example checks if all selected VMs are running and prints a log message to the console. |
| 97 | +
|
| 98 | +. Deploy the plugin to the cluster. |
| 99 | +
|
| 100 | +.Verification |
| 101 | +
|
| 102 | +. Log in to the {product-title} web console. |
| 103 | +
|
| 104 | +. Verify that you can apply bulk actions to VMs. |
| 105 | +.. Go to *Virtualization* -> *VirtualMachines*. |
| 106 | +.. Select the checkboxes for two or more existing VMs. |
| 107 | +.. Click the *Actions* drop-down menu. Confirm that you can run the custom action you created. |
0 commit comments