]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: Refactor qemuBlockStorageSourceBuildHostsJSONSocketAddress
authorAshish Mittal <Ashish.Mittal@veritas.com>
Tue, 12 Sep 2017 11:43:31 +0000 (07:43 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Wed, 20 Sep 2017 01:10:21 +0000 (21:10 -0400)
Extract out the "guts" of building a server entry into it's own
separately callable/usable function in order to allow building
a server entry for a consumer with src->nhosts == 1.

src/qemu/qemu_block.c

index d07269f4ede3342dd0e7d96016da0eb2c10a48c9..c97b787c56cde73366af1f59f305d5c5f02af8cf 100644 (file)
@@ -379,6 +379,74 @@ qemuBlockGetNodeData(virJSONValuePtr data)
 }
 
 
+/**
+ * qemuBlockStorageSourceBuildJSONSocketAddress
+ * @host: the virStorageNetHostDefPtr definition to build
+ * @legacy: use 'tcp' instead of 'inet' for compatibility reasons
+ *
+ * Formats @hosts into a json object conforming to the 'SocketAddress' type
+ * in qemu.
+ *
+ * This function can be used when only 1 src->nhosts is expected in order
+ * to build a command without the array indices after "server.". That is
+ * to see "server.type", "server.host", and "server.port" instead of
+ * "server.#.type", "server.#.host", and "server.#.port".
+ *
+ * Returns a virJSONValuePtr for a single server.
+ */
+static virJSONValuePtr
+qemuBlockStorageSourceBuildJSONSocketAddress(virStorageNetHostDefPtr host,
+                                             bool legacy)
+{
+    virJSONValuePtr server = NULL;
+    virJSONValuePtr ret = NULL;
+    const char *transport;
+    char *port = NULL;
+
+    switch ((virStorageNetHostTransport) host->transport) {
+    case VIR_STORAGE_NET_HOST_TRANS_TCP:
+        if (legacy)
+            transport = "tcp";
+        else
+            transport = "inet";
+
+        if (virAsprintf(&port, "%u", host->port) < 0)
+            goto cleanup;
+
+        if (virJSONValueObjectCreate(&server,
+                                     "s:type", transport,
+                                     "s:host", host->name,
+                                     "s:port", port,
+                                     NULL) < 0)
+            goto cleanup;
+        break;
+
+    case VIR_STORAGE_NET_HOST_TRANS_UNIX:
+        if (virJSONValueObjectCreate(&server,
+                                     "s:type", "unix",
+                                     "s:socket", host->socket,
+                                     NULL) < 0)
+            goto cleanup;
+        break;
+
+    case VIR_STORAGE_NET_HOST_TRANS_RDMA:
+    case VIR_STORAGE_NET_HOST_TRANS_LAST:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("transport protocol '%s' is not yet supported"),
+                       virStorageNetHostTransportTypeToString(host->transport));
+        goto cleanup;
+    }
+
+    VIR_STEAL_PTR(ret, server);
+
+ cleanup:
+    VIR_FREE(port);
+    virJSONValueFree(server);
+
+    return ret;
+}
+
+
 /**
  * qemuBlockStorageSourceBuildHostsJSONSocketAddress:
  * @src: disk storage source
@@ -395,8 +463,6 @@ qemuBlockStorageSourceBuildHostsJSONSocketAddress(virStorageSourcePtr src,
     virJSONValuePtr server = NULL;
     virJSONValuePtr ret = NULL;
     virStorageNetHostDefPtr host;
-    const char *transport;
-    char *port = NULL;
     size_t i;
 
     if (!(servers = virJSONValueNewArray()))
@@ -405,39 +471,8 @@ qemuBlockStorageSourceBuildHostsJSONSocketAddress(virStorageSourcePtr src,
     for (i = 0; i < src->nhosts; i++) {
         host = src->hosts + i;
 
-        switch ((virStorageNetHostTransport) host->transport) {
-        case VIR_STORAGE_NET_HOST_TRANS_TCP:
-            if (legacy)
-                transport = "tcp";
-            else
-                transport = "inet";
-
-            if (virAsprintf(&port, "%u", host->port) < 0)
-                goto cleanup;
-
-            if (virJSONValueObjectCreate(&server,
-                                         "s:type", transport,
-                                         "s:host", host->name,
-                                         "s:port", port,
-                                         NULL) < 0)
-                goto cleanup;
-            break;
-
-        case VIR_STORAGE_NET_HOST_TRANS_UNIX:
-            if (virJSONValueObjectCreate(&server,
-                                         "s:type", "unix",
-                                         "s:socket", host->socket,
-                                         NULL) < 0)
-                goto cleanup;
-            break;
-
-        case VIR_STORAGE_NET_HOST_TRANS_RDMA:
-        case VIR_STORAGE_NET_HOST_TRANS_LAST:
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("transport protocol '%s' is not yet supported"),
-                           virStorageNetHostTransportTypeToString(host->transport));
-            goto cleanup;
-        }
+        if (!(server = qemuBlockStorageSourceBuildJSONSocketAddress(host, legacy)))
+              goto cleanup;
 
         if (virJSONValueArrayAppend(servers, server) < 0)
             goto cleanup;
@@ -450,7 +485,6 @@ qemuBlockStorageSourceBuildHostsJSONSocketAddress(virStorageSourcePtr src,
  cleanup:
     virJSONValueFree(servers);
     virJSONValueFree(server);
-    VIR_FREE(port);
 
     return ret;
 }