]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: qemu: Add wrapper for JSON -> commandline conversion
authorPeter Krempa <pkrempa@redhat.com>
Fri, 22 Jul 2016 13:54:57 +0000 (15:54 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 27 Jul 2016 07:39:46 +0000 (09:39 +0200)
Refactor the command line generator by adding a wrapper (with
documentation) that will handle the outermost object iteration.

This patch also renames the functions and tweaks the error message for
nested arrays to be more universal.

The new function is then reused to simplify qemucommandutiltest.

src/libvirt_private.syms
src/util/virqemu.c
src/util/virqemu.h
tests/qemucommandutiltest.c

index 1c170b6ded797e4b12c33efb0edba80effba0805..948c7b58aa3d1f4ccfc8214cccab31ece19a4f3c 100644 (file)
@@ -2204,6 +2204,7 @@ virProcessWait;
 
 # util/virqemu.h
 virQEMUBuildBufferEscapeComma;
+virQEMUBuildCommandLineJSON;
 virQEMUBuildLuksOpts;
 virQEMUBuildObjectCommandlineFromJSON;
 
index 7d181e19d3fdffc343cddf19abbbb5eae9d14156..a5d53852bd48ff73d201f37e7bf85202e8e846da 100644 (file)
@@ -33,10 +33,10 @@ VIR_LOG_INIT("util.qemu");
 
 
 static int
-virQEMUBuildObjectCommandLinePropsInternal(const char *key,
-                                           const virJSONValue *value,
-                                           virBufferPtr buf,
-                                           bool nested)
+virQEMUBuildCommandLineJSONRecurse(const char *key,
+                                   const virJSONValue *value,
+                                   virBufferPtr buf,
+                                   bool nested)
 {
     virJSONValuePtr elem;
     virBitmapPtr bitmap = NULL;
@@ -64,7 +64,8 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key,
     case VIR_JSON_TYPE_ARRAY:
         if (nested) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("nested -object property arrays are not supported"));
+                           _("nested JSON array to commandline conversion is "
+                             "not supported"));
             return -1;
         }
 
@@ -87,8 +88,7 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key,
                 elem = virJSONValueArrayGet((virJSONValuePtr)value, i);
 
                 /* recurse to avoid duplicating code */
-                if (virQEMUBuildObjectCommandLinePropsInternal(key, elem, buf,
-                                                               true) < 0)
+                if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf, true) < 0)
                     return -1;
             }
         }
@@ -108,11 +108,34 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key,
 
 
 static int
-virQEMUBuildObjectCommandLineProps(const char *key,
+virQEMUBuildCommandLineJSONIterate(const char *key,
                                    const virJSONValue *value,
                                    void *opaque)
 {
-    return virQEMUBuildObjectCommandLinePropsInternal(key, value, opaque, false);
+    return virQEMUBuildCommandLineJSONRecurse(key, value, opaque, false);
+}
+
+
+/**
+ * virQEMUBuildCommandLineJSON:
+ * @value: json object containing the value
+ * @buf: otuput buffer
+ *
+ * Formats JSON value object into command line parameters suitable for use with
+ * qemu.
+ *
+ * Returns 0 on success -1 on error.
+ */
+int
+virQEMUBuildCommandLineJSON(const virJSONValue *value,
+                            virBufferPtr buf)
+{
+    if (virJSONValueObjectForeachKeyValue(value,
+                                          virQEMUBuildCommandLineJSONIterate,
+                                          buf) < 0)
+        return -1;
+
+    return 0;
 }
 
 
@@ -126,9 +149,7 @@ virQEMUBuildObjectCommandlineFromJSON(const char *type,
 
     virBufferAsprintf(&buf, "%s,id=%s", type, alias);
 
-    if (virJSONValueObjectForeachKeyValue(props,
-                                          virQEMUBuildObjectCommandLineProps,
-                                          &buf) < 0)
+    if (virQEMUBuildCommandLineJSON(props, &buf) < 0)
         goto cleanup;
 
     if (virBufferCheckError(&buf) < 0)
index eb75d1b287a0abbe543e4b17e019cc6990506cc1..efc6c42c4d7ffe477715c4812a6a5518c13724e5 100644 (file)
@@ -29,6 +29,9 @@
 # include "virjson.h"
 # include "virstorageencryption.h"
 
+int virQEMUBuildCommandLineJSON(const virJSONValue *value,
+                                virBufferPtr buf);
+
 char *virQEMUBuildObjectCommandlineFromJSON(const char *type,
                                             const char *alias,
                                             virJSONValuePtr props);
index c02d1db1b4958a682d6f45ae65dac8d38be3ba84..21fef1c458af2db543931c1ec830fcd5ec66351c 100644 (file)
@@ -33,11 +33,12 @@ typedef struct
 } testQemuCommandBuildObjectFromJSONData;
 
 static int
-testQemuCommandBuildObjectFromJSON(const void *opaque)
+testQemuCommandBuildFromJSON(const void *opaque)
 {
     const testQemuCommandBuildObjectFromJSONData *data = opaque;
     virJSONValuePtr val = NULL;
     char *expect = NULL;
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *result = NULL;
     int ret = -1;
 
@@ -46,13 +47,18 @@ testQemuCommandBuildObjectFromJSON(const void *opaque)
         return -1;
     }
 
-    if (virAsprintf(&expect, "testobject,id=testalias%s%s",
-                    data->expectprops ? "," : "",
-                    data->expectprops ? data->expectprops : "") < 0)
+    if (data->expectprops &&
+        virAsprintf(&expect, ",%s", data->expectprops) < 0)
         return -1;
 
-    result = virQEMUBuildObjectCommandlineFromJSON("testobject",
-                                                   "testalias", val);
+    if (virQEMUBuildCommandLineJSON(val, &buf) < 0) {
+        fprintf(stderr,
+                "\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n",
+                data->props);
+        goto cleanup;
+    }
+
+    result = virBufferContentAndReset(&buf);
 
     if (STRNEQ_NULLABLE(expect, result)) {
         fprintf(stderr, "\nFailed to create object string. "
@@ -80,14 +86,14 @@ mymain(void)
     return EXIT_AM_SKIP;
 #endif
 
-    virTestCounterReset("testQemuCommandBuildObjectFromJSON");
+    virTestCounterReset("testQemuCommandBuildFromJSON");
 
 #define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT)             \
     do {                                                            \
         data1.props = PROPS;                                        \
         data1.expectprops = EXPECT;                                 \
         if (virTestRun(virTestCounterNext(),                        \
-                       testQemuCommandBuildObjectFromJSON,          \
+                       testQemuCommandBuildFromJSON,                \
                        &data1) < 0)                                 \
             ret = -1;                                               \
      } while (0)