]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Add a qemuMonitorGetMachines() method for QMP query-machines command
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 15 Aug 2012 15:18:41 +0000 (16:18 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Thu, 27 Sep 2012 09:47:16 +0000 (10:47 +0100)
Add a new qemuMonitorGetMachines() method to support invocation
of the 'query-machines' JSON monitor command. No HMP equivalent
is required, since this will only be present for QEMU >= 1.2

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/qemu/qemu_monitor.c
src/qemu/qemu_monitor.h
src/qemu/qemu_monitor_json.c
src/qemu/qemu_monitor_json.h
tests/qemumonitorjsontest.c

index a6dd47a5b86e8f59fdbf82866d552dbafc4b3caa..a952ff915335619c681f78c3362001f69f899454 100644 (file)
@@ -3046,3 +3046,33 @@ int qemuMonitorGetVersion(qemuMonitorPtr mon,
 
     return qemuMonitorJSONGetVersion(mon, major, minor, micro, package);
 }
+
+int qemuMonitorGetMachines(qemuMonitorPtr mon,
+                           qemuMonitorMachineInfoPtr **machines)
+{
+    VIR_DEBUG("mon=%p machines=%p",
+              mon, machines);
+
+    if (!mon) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("monitor must not be NULL"));
+        return -1;
+    }
+
+    if (!mon->json) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("JSON monitor is required"));
+        return -1;
+    }
+
+    return qemuMonitorJSONGetMachines(mon, machines);
+}
+
+void qemuMonitorMachineInfoFree(qemuMonitorMachineInfoPtr machine)
+{
+    if (!machine)
+        return;
+    VIR_FREE(machine->name);
+    VIR_FREE(machine->alias);
+    VIR_FREE(machine);
+}
index 9d887475c4c1fde6b7b2a6748bc79434ce17ff4a..d26608bc015d14cfd40cd01e075f46cdfc38afb2 100644 (file)
@@ -578,6 +578,21 @@ int qemuMonitorGetVersion(qemuMonitorPtr mon,
                           char **package)
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
 
+
+typedef struct _qemuMonitorMachineInfo qemuMonitorMachineInfo;
+typedef qemuMonitorMachineInfo *qemuMonitorMachineInfoPtr;
+
+struct _qemuMonitorMachineInfo {
+    char *name;
+    bool isDefault;
+    char *alias;
+};
+
+int qemuMonitorGetMachines(qemuMonitorPtr mon,
+                           qemuMonitorMachineInfoPtr **machines);
+
+void qemuMonitorMachineInfoFree(qemuMonitorMachineInfoPtr machine);
+
 /**
  * When running two dd process and using <> redirection, we need a
  * shell that will not truncate files.  These two strings serve that
index f6a9e0b822839e8fbce6773278b531fd6e8860df..b8cd627fccc20923308335b450d34c0929119db9 100644 (file)
@@ -3941,3 +3941,104 @@ cleanup:
     virJSONValueFree(reply);
     return ret;
 }
+
+
+int qemuMonitorJSONGetMachines(qemuMonitorPtr mon,
+                               qemuMonitorMachineInfoPtr **machines)
+{
+    int ret;
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+    virJSONValuePtr data;
+    qemuMonitorMachineInfoPtr *infolist = NULL;
+    int n = 0;
+    size_t i;
+
+    *machines = NULL;
+
+    if (!(cmd = qemuMonitorJSONMakeCommand("query-machines", NULL)))
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
+
+    if (ret == 0)
+        ret = qemuMonitorJSONCheckError(cmd, reply);
+
+    if (ret < 0)
+        goto cleanup;
+
+    ret = -1;
+
+    if (!(data = virJSONValueObjectGet(reply, "return"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("query-machines reply was missing return data"));
+        goto cleanup;
+    }
+
+    if ((n = virJSONValueArraySize(data)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("query-machines reply data was not an array"));
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC_N(infolist, n) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    for (i = 0 ; i < n ; i++) {
+        virJSONValuePtr child = virJSONValueArrayGet(data, i);
+        const char *tmp;
+        qemuMonitorMachineInfoPtr info;
+
+        if (VIR_ALLOC(info) < 0) {
+            virReportOOMError();
+            goto cleanup;
+        }
+
+        infolist[i] = info;
+
+        if (!(tmp = virJSONValueObjectGetString(child, "name"))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("query-machines reply data was missing 'name'"));
+            goto cleanup;
+        }
+
+        if (!(info->name = strdup(tmp))) {
+            virReportOOMError();
+            goto cleanup;
+        }
+
+        if (virJSONValueObjectHasKey(child, "is-default") &&
+            virJSONValueObjectGetBoolean(child, "is-default", &info->isDefault) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("query-machines reply has malformed 'is-default' data"));
+            goto cleanup;
+        }
+
+        if (virJSONValueObjectHasKey(child, "alias")) {
+            if (!(tmp = virJSONValueObjectGetString(child, "alias"))) {
+                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                               _("query-machines reply has malformed 'alias' data"));
+                goto cleanup;
+            }
+            if (!(info->alias = strdup(tmp))) {
+                virReportOOMError();
+                goto cleanup;
+            }
+        }
+    }
+
+    ret = n;
+    *machines = infolist;
+
+cleanup:
+    if (ret < 0 && infolist) {
+        for (i = 0 ; i < n ; i++)
+            qemuMonitorMachineInfoFree(infolist[i]);
+        VIR_FREE(infolist);
+    }
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
index f37e4a00aea8fe246bc3c06dc7fd1c5e95fc741a..f384a97801ebc54c691ef81a5d73420b6d044a1a 100644 (file)
@@ -290,4 +290,8 @@ int qemuMonitorJSONGetVersion(qemuMonitorPtr mon,
                               char **package)
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
 
+int qemuMonitorJSONGetMachines(qemuMonitorPtr mon,
+                               qemuMonitorMachineInfoPtr **machines)
+    ATTRIBUTE_NONNULL(2);
+
 #endif /* QEMU_MONITOR_JSON_H */
index c3a0c45d17a9587a56b156c8d45bbc2e45291093..f1947c5ae84111ccf3e7ae3929eaea368e958c14 100644 (file)
@@ -223,6 +223,83 @@ cleanup:
     return ret;
 }
 
+static int
+testQemuMonitorJSONGetMachines(const void *data)
+{
+    virCapsPtr caps = (virCapsPtr)data;
+    qemuMonitorTestPtr test = qemuMonitorTestNew(true, caps);
+    int ret = -1;
+    qemuMonitorMachineInfoPtr *info;
+    int ninfo;
+    const char *null = NULL;
+
+    if (!test)
+        return -1;
+
+    if (qemuMonitorTestAddItem(test, "query-machines",
+                               "{ "
+                               "  \"return\": [ "
+                               "   { "
+                               "     \"name\": \"pc-1.0\" "
+                               "   }, "
+                               "   { "
+                               "     \"name\": \"pc-1.1\" "
+                               "   }, "
+                               "   { "
+                               "     \"name\": \"pc-1.2\", "
+                               "     \"is-default\": true, "
+                               "     \"alias\": \"pc\" "
+                               "   } "
+                               "  ]"
+                               "}") < 0)
+        goto cleanup;
+
+    if ((ninfo = qemuMonitorGetMachines(qemuMonitorTestGetMonitor(test),
+                                        &info)) < 0)
+        goto cleanup;
+
+    if (ninfo != 3) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "ninfo %d is not 3", ninfo);
+        goto cleanup;
+    }
+
+#define CHECK(i, wantname, wantisDefault, wantalias)                    \
+    do {                                                                \
+        if (STRNEQ(info[i]->name, (wantname))) {                        \
+            virReportError(VIR_ERR_INTERNAL_ERROR,                      \
+                           "name %s is not %s",                         \
+                           info[i]->name, (wantname));                  \
+            goto cleanup;                                               \
+        }                                                               \
+        if (info[i]->isDefault != (wantisDefault)) {                    \
+            virReportError(VIR_ERR_INTERNAL_ERROR,                      \
+                           "isDefault %d is not %d",                    \
+                           info[i]->isDefault, (wantisDefault));        \
+            goto cleanup;                                               \
+        }                                                               \
+        if (STRNEQ_NULLABLE(info[i]->alias, (wantalias))) {             \
+            virReportError(VIR_ERR_INTERNAL_ERROR,                      \
+                           "alias %s is not %s",                        \
+                           info[i]->alias, NULLSTR(wantalias));         \
+            goto cleanup;                                               \
+        }                                                               \
+    } while (0)
+
+    CHECK(0, "pc-1.0", false, null);
+    CHECK(1, "pc-1.1", false, null);
+    CHECK(2, "pc-1.2", true, "pc");
+
+#undef CHECK
+
+    ret = 0;
+
+cleanup:
+    qemuMonitorTestFree(test);
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -243,6 +320,7 @@ mymain(void)
 
     DO_TEST(GetStatus);
     DO_TEST(GetVersion);
+    DO_TEST(GetMachines);
 
     virCapabilitiesFree(caps);