Blame SOURCES/libvirt-virsh-domain-Add-command-to-allow-modifications-of-XML-metadata.patch

898951
From 7aa95972f21eb0b39c06909360bcf8b51171b263 Mon Sep 17 00:00:00 2001
898951
Message-Id: <7aa95972f21eb0b39c06909360bcf8b51171b263@dist-git>
898951
From: Peter Krempa <pkrempa@redhat.com>
898951
Date: Thu, 22 Jan 2015 15:53:44 +0100
898951
Subject: [PATCH] virsh-domain: Add command to allow modifications of XML
898951
 metadata
898951
898951
https://bugzilla.redhat.com/show_bug.cgi?id=1184929
898951
898951
The metadata modification functions will support modification of the XML
898951
metadata. Add a virsh command to allow using this approach.
898951
898951
(cherry picked from commit 01b03f59e76865feed3097cbe6dc0a809caccc14)
898951
898951
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
898951
---
898951
 tools/virsh-domain.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++
898951
 1 file changed, 161 insertions(+)
898951
898951
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
898951
index 6076e8d..a2796e5 100644
898951
--- a/tools/virsh-domain.c
898951
+++ b/tools/virsh-domain.c
898951
@@ -6813,6 +6813,161 @@ cleanup:
898951
     return ret;
898951
 }
898951
 
898951
+
898951
+static const vshCmdInfo info_metadata[] = {
898951
+    {.name = "help",
898951
+     .data = N_("show or set domain's custom XML metadata")
898951
+    },
898951
+    {.name = "desc",
898951
+     .data = N_("Shows or modifies the XML metadata of a domain.")
898951
+    },
898951
+    {.name = NULL}
898951
+};
898951
+
898951
+static const vshCmdOptDef opts_metadata[] = {
898951
+    {.name = "domain",
898951
+     .type = VSH_OT_DATA,
898951
+     .flags = VSH_OFLAG_REQ,
898951
+     .help = N_("domain name, id or uuid")
898951
+    },
898951
+    {.name = "live",
898951
+     .type = VSH_OT_BOOL,
898951
+     .help = N_("modify/get running state")
898951
+    },
898951
+    {.name = "config",
898951
+     .type = VSH_OT_BOOL,
898951
+     .help = N_("modify/get persistent configuration")
898951
+    },
898951
+    {.name = "current",
898951
+     .type = VSH_OT_BOOL,
898951
+     .help = N_("modify/get current state configuration")
898951
+    },
898951
+    {.name = "edit",
898951
+     .type = VSH_OT_BOOL,
898951
+     .help = N_("use an editor to change the metadata")
898951
+    },
898951
+    {.name = "uri",
898951
+     .type = VSH_OT_DATA,
898951
+     .flags = VSH_OFLAG_REQ,
898951
+     .help = N_("URI of the namespace")
898951
+    },
898951
+    {.name = "key",
898951
+     .type = VSH_OT_DATA,
898951
+     .help = N_("key to be used as a namespace identifier"),
898951
+    },
898951
+    {.name = "set",
898951
+     .type = VSH_OT_DATA,
898951
+     .help = N_("new metadata to set"),
898951
+    },
898951
+    {.name = "remove",
898951
+     .type = VSH_OT_BOOL,
898951
+     .help = N_("remove the metadata corresponding to an uri")
898951
+    },
898951
+    {.name = NULL}
898951
+};
898951
+
898951
+
898951
+/* helper to add new metadata using the --edit option */
898951
+static char *
898951
+vshDomainGetEditMetadata(vshControl *ctl,
898951
+                         virDomainPtr dom,
898951
+                         const char *uri,
898951
+                         unsigned int flags)
898951
+{
898951
+    char *ret;
898951
+
898951
+    if (!(ret = virDomainGetMetadata(dom, VIR_DOMAIN_METADATA_ELEMENT,
898951
+                                     uri, flags))) {
898951
+        vshResetLibvirtError();
898951
+        ret = vshStrdup(ctl, "\n");
898951
+    }
898951
+
898951
+    return ret;
898951
+}
898951
+
898951
+
898951
+static bool
898951
+cmdMetadata(vshControl *ctl, const vshCmd *cmd)
898951
+{
898951
+    virDomainPtr dom;
898951
+    bool config = vshCommandOptBool(cmd, "config");
898951
+    bool live = vshCommandOptBool(cmd, "live");
898951
+    bool current = vshCommandOptBool(cmd, "current");
898951
+    bool edit = vshCommandOptBool(cmd, "edit");
898951
+    bool remove = vshCommandOptBool(cmd, "remove");
898951
+    const char *set = NULL;
898951
+    const char *uri = NULL;
898951
+    const char *key = NULL;
898951
+    unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
898951
+    bool ret = false;
898951
+
898951
+    VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
898951
+    VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
898951
+    VSH_EXCLUSIVE_OPTIONS("edit", "set");
898951
+    VSH_EXCLUSIVE_OPTIONS("remove", "set");
898951
+    VSH_EXCLUSIVE_OPTIONS("remove", "edit");
898951
+
898951
+    if (config)
898951
+        flags |= VIR_DOMAIN_AFFECT_CONFIG;
898951
+    if (live)
898951
+        flags |= VIR_DOMAIN_AFFECT_LIVE;
898951
+
898951
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
898951
+        return false;
898951
+
898951
+    if (vshCommandOptStringReq(ctl, cmd, "uri", &uri) < 0 ||
898951
+        vshCommandOptStringReq(ctl, cmd, "key", &key) < 0 ||
898951
+        vshCommandOptStringReq(ctl, cmd, "set", &set) < 0)
898951
+        goto cleanup;
898951
+
898951
+    if ((set || edit) && !key) {
898951
+        vshError(ctl, "%s",
898951
+                 _("namespace key is required when modifying metadata"));
898951
+        goto cleanup;
898951
+    }
898951
+
898951
+    if (set || remove) {
898951
+        if (virDomainSetMetadata(dom, VIR_DOMAIN_METADATA_ELEMENT,
898951
+                                 set, key, uri, flags))
898951
+            goto cleanup;
898951
+
898951
+        if (remove)
898951
+            vshPrint("%s\n", _("Metadata removed"));
898951
+        else
898951
+            vshPrint("%s\n", _("Metadata modified"));
898951
+    } else if (edit) {
898951
+#define EDIT_GET_XML \
898951
+        vshDomainGetEditMetadata(ctl, dom, uri, flags)
898951
+#define EDIT_NOT_CHANGED                                    \
898951
+        vshPrint(ctl, "%s", _("Metadata not changed"));     \
898951
+        ret = true;                                         \
898951
+        goto edit_cleanup;
898951
+#define EDIT_DEFINE                                                         \
898951
+        (virDomainSetMetadata(dom, VIR_DOMAIN_METADATA_ELEMENT, doc_edited, \
898951
+                              key, uri, flags) == 0)
898951
+#define EDIT_FREE /* nothing */
898951
+#include "virsh-edit.c"
898951
+
898951
+        vshPrint("%s\n", _("Metadata modified"));
898951
+    } else {
898951
+        char *data;
898951
+        /* get */
898951
+        if (!(data = virDomainGetMetadata(dom, VIR_DOMAIN_METADATA_ELEMENT,
898951
+                                          uri, flags)))
898951
+            goto cleanup;
898951
+
898951
+        vshPrint(ctl, "%s\n", data);
898951
+        VIR_FREE(data);
898951
+    }
898951
+
898951
+    ret = true;
898951
+
898951
+cleanup:
898951
+    virDomainFree(dom);
898951
+    return ret;
898951
+}
898951
+
898951
+
898951
 /*
898951
  * "inject-nmi" command
898951
  */
898951
@@ -10603,6 +10758,12 @@ const vshCmdDef domManagementCmds[] = {
898951
      .info = info_memtune,
898951
      .flags = 0
898951
     },
898951
+    {.name = "metadata",
898951
+     .handler = cmdMetadata,
898951
+     .opts = opts_metadata,
898951
+     .info = info_metadata,
898951
+     .flags = 0
898951
+    },
898951
     {.name = "migrate",
898951
      .handler = cmdMigrate,
898951
      .opts = opts_migrate,
898951
-- 
898951
2.2.1
898951