[PATCH v2 05/11] virsh: Add new command `nwfilter-metadata`

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



With the new command `nwfilter-metadata`, users will be able to view and
modify the `<metadata>` field of the Network Filter XML.

Signed-off-by: K Shiva Kiran <shiva_kr@xxxxxxxxxx>
---
 docs/manpages/virsh.rst |  48 ++++++++++++++
 tools/virsh-nwfilter.c  | 142 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)

diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
index 3c7cbf1e11..e15f1832e7 100644
--- a/docs/manpages/virsh.rst
+++ b/docs/manpages/virsh.rst
@@ -8174,6 +8174,54 @@ If neither of *--edit* or *--new-desc* are specified, the title or description
 is displayed instead of being modified.
 
 
+nwfilter-metadata
+-----------------
+
+   nwfilter-metadata [--nwfilter] nwfilter-name [--uri] uri
+      [--edit] [[--key] nskey] [--set new-metadata-xml]
+      [--remove]
+
+Show or modify custom XML metadata of a network filter.
+
+The metadata is a user defined XML that allows storing arbitrary XML data
+in the network filter definition.
+Multiple separate custom metadata pieces can be stored in the
+network filter XML. The pieces are identified by a private XML namespace
+provided via the *uri* argument.
+(See also ``nwfilter-desc`` that works with textual metadata of
+a network filter, such as title and description.)
+
+- *--uri*
+
+   Specifies the URI for the private namespace.
+
+- *--edit*
+
+  Opens an editor with the metadata identified by the *uri* argument.
+  Modifications to the contents will be saved back.
+  Alternatively, the new contents can be provided via the *--set* argument.
+
+- *--key*
+
+   Specifies the namespace key to be used.
+
+- *--set*
+
+  Validates and stores the provided metadata string.
+
+  **Note:**  When setting metadata via *--edit* or *--set* the *--key* argument
+  must be specified and is used to prefix the custom elements to bind them
+  to the private namespace.
+
+If neither of *--edit* and *--set* are specified the XML metadata corresponding
+to the *uri* namespace is displayed instead of being modified.
+
+- *--remove*
+
+  Specifies that the metadata element specified by the *--uri* argument should
+  be removed rather than updated.
+
+
 NWFILTER BINDING COMMANDS
 =========================
 
diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c
index 615d126def..801a52746e 100644
--- a/tools/virsh-nwfilter.c
+++ b/tools/virsh-nwfilter.c
@@ -971,6 +971,142 @@ cmdNWFilterDesc(vshControl *ctl, const vshCmd *cmd)
 }
 
 
+/*
+ * "nwfilter-metadata" command
+ */
+static const vshCmdInfo info_nwfilter_metadata[] = {
+    {.name = "help",
+     .data = N_("show or set network filter's custom XML metadata")
+    },
+    {.name = "desc",
+     .data = N_("Shows or modifies the XML metadata of a network filter.")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_nwfilter_metadata[] = {
+    {.name = "nwfilter",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("network filter name or uuid"),
+     .completer = virshNWFilterNameCompleter,
+    },
+    {.name = "uri",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("URI of the namespace")
+    },
+    {.name = "edit",
+     .type = VSH_OT_BOOL,
+     .help = N_("use an editor to change the metadata")
+    },
+    {.name = "key",
+     .type = VSH_OT_STRING,
+     .help = N_("key to be used as a namespace identifier"),
+    },
+    {.name = "set",
+     .type = VSH_OT_STRING,
+     .completer = virshCompleteEmpty,
+     .help = N_("new metadata to set"),
+    },
+    {.name = "remove",
+     .type = VSH_OT_BOOL,
+     .help = N_("remove the metadata corresponding to an uri")
+    },
+    {.name = NULL}
+};
+
+/* helper to add new metadata using the --edit option */
+static char *
+virshNWFilterGetEditMetadata(vshControl *ctl G_GNUC_UNUSED,
+                             virNWFilterPtr nwfilter,
+                             const char *uri,
+                             unsigned int flags)
+{
+    char *ret;
+
+    if (!(ret = virNWFilterGetMetadata(nwfilter, VIR_NWFILTER_METADATA_ELEMENT,
+                                       uri, flags))) {
+        vshResetLibvirtError();
+        ret = g_strdup("\n");
+    }
+
+    return ret;
+}
+
+static bool
+cmdNWFilterMetadata(vshControl *ctl, const vshCmd *cmd)
+{
+    g_autoptr(virshNWFilter) nwfilter = NULL;
+    bool edit = vshCommandOptBool(cmd, "edit");
+    bool rem = vshCommandOptBool(cmd, "remove");
+    const char *set = NULL;
+    const char *uri = NULL;
+    const char *key = NULL;
+    unsigned int flags = 0;
+    bool ret = false;
+
+    VSH_EXCLUSIVE_OPTIONS("edit", "set");
+    VSH_EXCLUSIVE_OPTIONS("remove", "set");
+    VSH_EXCLUSIVE_OPTIONS("remove", "edit");
+
+    if (!(nwfilter = virshCommandOptNWFilter(ctl, cmd, NULL)))
+        return false;
+
+    if (vshCommandOptStringReq(ctl, cmd, "uri", &uri) < 0 ||
+        vshCommandOptStringReq(ctl, cmd, "key", &key) < 0 ||
+        vshCommandOptStringReq(ctl, cmd, "set", &set) < 0)
+        return false;
+
+    if ((set || edit) && !key) {
+        vshError(ctl, "%s",
+                 _("namespace key is required when modifying metadata"));
+        return false;
+    }
+
+    if (set || rem) {
+        if (virNWFilterSetMetadata(nwfilter, VIR_NWFILTER_METADATA_ELEMENT,
+                                   set, key, uri, flags))
+            return false;
+
+        if (rem)
+            vshPrintExtra(ctl, "%s\n", _("Metadata removed"));
+        else
+            vshPrintExtra(ctl, "%s\n", _("Metadata modified"));
+    } else if (edit) {
+#define EDIT_GET_XML \
+        virshNWFilterGetEditMetadata(ctl, nwfilter, uri, flags)
+#define EDIT_NOT_CHANGED \
+        do { \
+            vshPrintExtra(ctl, "%s", _("Metadata not changed")); \
+            ret = true; \
+            goto edit_cleanup; \
+        } while (0)
+
+#define EDIT_DEFINE \
+        (virNWFilterSetMetadata(nwfilter, VIR_NWFILTER_METADATA_ELEMENT, doc_edited, \
+                                key, uri, flags) == 0)
+#include "virsh-edit.c"
+
+        vshPrintExtra(ctl, "%s\n", _("Metadata modified"));
+    } else {
+        g_autofree char *data = NULL;
+
+        /* get */
+        if (!(data = virNWFilterGetMetadata(nwfilter, VIR_NWFILTER_METADATA_ELEMENT,
+                                            uri, flags)))
+            return false;
+
+        vshPrint(ctl, "%s\n", data);
+    }
+
+    ret = true;
+
+ cleanup:
+    return ret;
+}
+
+
 const vshCmdDef nwfilterCmds[] = {
     {.name = "nwfilter-define",
      .handler = cmdNWFilterDefine,
@@ -1032,5 +1168,11 @@ const vshCmdDef nwfilterCmds[] = {
      .info = info_nwfilter_desc,
      .flags = 0
     },
+    {.name = "nwfilter-metadata",
+     .handler = cmdNWFilterMetadata,
+     .opts = opts_nwfilter_metadata,
+     .info = info_nwfilter_metadata,
+     .flags = 0
+    },
     {.name = NULL}
 };
-- 
2.42.0





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux