From 3d21e502aebaf8415c466a62c070d7880a00cf2e Mon Sep 17 00:00:00 2001 From: Marius Vollmer Date: Tue, 21 May 2024 09:35:26 +0300 Subject: [PATCH] vm: Support for VM titles and descriptions Based on work by @Britz, thanks a lot! --- src/components/vm/vmActions.jsx | 4 +- src/components/vm/vmDetailsPage.jsx | 6 ++- src/components/vm/vmRenameDialog.jsx | 64 ++++++++++++++++++--------- src/components/vms/hostvmslist.jsx | 14 +++--- src/libvirt-xml-parse.js | 6 +++ src/libvirtApi/domain.js | 27 ++++++++++++ test/check-machines-lifecycle | 65 +++++++++++++++++++++++++--- test/reference | 2 +- 8 files changed, 150 insertions(+), 38 deletions(-) diff --git a/src/components/vm/vmActions.jsx b/src/components/vm/vmActions.jsx index dd731df30..009022648 100644 --- a/src/components/vm/vmActions.jsx +++ b/src/components/vm/vmActions.jsx @@ -445,9 +445,7 @@ const VmActions = ({ vm, vms, onAddErrorNotification, isDetailsPage }) => { dropdownItems.push( Dialogs.show()}> + onClick={() => Dialogs.show()}> {_("Rename")} ); diff --git a/src/components/vm/vmDetailsPage.jsx b/src/components/vm/vmDetailsPage.jsx index 98fbfeb12..cdf907da7 100644 --- a/src/components/vm/vmDetailsPage.jsx +++ b/src/components/vm/vmDetailsPage.jsx @@ -67,7 +67,7 @@ export const VmDetailsPage = ({ const vmActionsPageSection = (
-

{vm.name}

+

{vm.title ? vm.title + " (" + vm.name + ")" : vm.name}

+ { + vm.description && +
{vm.description.split("\n").map((p, i) =>

{p}

)}
+ }
); diff --git a/src/components/vm/vmRenameDialog.jsx b/src/components/vm/vmRenameDialog.jsx index b9ab48619..0bff2231a 100644 --- a/src/components/vm/vmRenameDialog.jsx +++ b/src/components/vm/vmRenameDialog.jsx @@ -23,47 +23,57 @@ import { Button } from "@patternfly/react-core/dist/esm/components/Button"; import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form"; import { Modal } from "@patternfly/react-core/dist/esm/components/Modal"; import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput"; +import { TextArea } from "@patternfly/react-core/dist/esm/components/TextArea"; import { FormHelper } from 'cockpit-components-form-helper.jsx'; import { ModalError } from 'cockpit-components-inline-notification.jsx'; import { useDialogs } from 'dialogs.jsx'; import { isObjectEmpty } from '../../helpers.js'; -import { domainRename } from '../../libvirtApi/domain.js'; +import { domainRename, domainSetMetadata } from '../../libvirtApi/domain.js'; const _ = cockpit.gettext; -export const RenameDialog = ({ vmName, vmId, connectionName }) => { +export const RenameDialog = ({ vm }) => { const Dialogs = useDialogs(); - const [newName, setNewName] = useState(vmName); + const [name, setName] = useState(vm.name); + const [fullName, setFullName] = useState(vm.title || ""); + const [description, setDescription] = useState(vm.description || ""); const [error, dialogErrorSet] = useState({}); const [submitted, setSubmitted] = useState(false); - function onRename() { + async function onRename() { setSubmitted(true); - if (!newName) + if (!name) return; - return domainRename({ connectionName, id: vmId, newName }) - .then(() => { - Dialogs.close(); - // If we are on the VMs details page change the URL to reflect the new name after the rename operation succeeded - if (cockpit.location.path.length > 0) - cockpit.location.go(["vm"], { ...cockpit.location.options, name: newName, connection: connectionName }); - }, exc => { - dialogErrorSet({ dialogError: cockpit.format(_("Failed to rename VM $0"), vmName), dialogErrorDetail: exc.message }); - }); + try { + await domainSetMetadata(vm, { title: fullName, description }); + if (name != vm.name) + await domainRename({ connectionName: vm.connectionName, id: vm.id, newName: name }); + Dialogs.close(); + + // If we are on the VMs details page change the URL to + // reflect the new name after the rename operation + // succeeded + if (name != vm.name && cockpit.location.path.length > 0) + cockpit.location.go(["vm"], { ...cockpit.location.options, name, connection: vm.connectionName }); + } catch (exc) { + dialogErrorSet({ + dialogError: cockpit.format(_("Failed to rename VM $0"), vm.name), + dialogErrorDetail: exc.message + }); + } } return ( @@ -78,13 +88,25 @@ export const RenameDialog = ({ vmName, vmId, connectionName }) => { }} isHorizontal> {!isObjectEmpty(error) && } - setNewName(value)} /> - + validated={submitted && !name ? "error" : "default"} + value={name} + onChange={(_, value) => setName(value)} /> + + + + setFullName(value)} /> + + +