Along with the legacy URI based syntax add support for the brand-new
fully object based syntax.
---
src/util/virstoragefile.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++
tests/virstoragetest.c | 25 +++++++++++
2 files changed, 133 insertions(+)
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 852d7f6..5fc1fe7 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -2575,6 +2575,113 @@ virStorageSourceParseBackingJSONUri(virStorageSourcePtr src,
}
+static int
+virStorageSourceParseBackingJSONGlusterHost(virStorageNetHostDefPtr host,
+ virJSONValuePtr json)
+{
+ const char *type = virJSONValueObjectGetString(json, "type");
+ const char *hostname = virJSONValueObjectGetString(json, "host");
+ const char *port = virJSONValueObjectGetString(json, "port");
+ const char *socket = virJSONValueObjectGetString(json, "socket");
+ int transport;
+
+ if ((transport = virStorageNetHostTransportTypeFromString(type)) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("unknown backing store transport protocol
'%s'"), type);
+ return -1;
+ }
+
+ host->transport = transport;
+
+ switch ((virStorageNetHostTransport) transport) {
+ case VIR_STORAGE_NET_HOST_TRANS_TCP:
+ if (!hostname) {
+ virReportError(VIR_ERR_INVALID_ARG, "%s",
+ _("missing hostname for tcp backing server in "
+ "JSON backing definition for gluster volume"));
+ return -1;
+ }
+
+ if (VIR_STRDUP(host->name, hostname) < 0 ||
+ VIR_STRDUP(host->port, port) < 0)
+ return -1;
+ break;
+
+ case VIR_STORAGE_NET_HOST_TRANS_UNIX:
+ if (!socket) {
+ virReportError(VIR_ERR_INVALID_ARG, "%s",
+ _("missing socket path for udp backing server in "
+ "JSON backing definition for gluster volume"));
+ return -1;
+ }
+
+
+ if (VIR_STRDUP(host->socket, socket) < 0)
+ return -1;
+ break;
+
+ case VIR_STORAGE_NET_HOST_TRANS_RDMA:
+ case VIR_STORAGE_NET_HOST_TRANS_LAST:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("backing store protocol '%s' is not yet
supported"),
+ type);
+ return -1;
+ }
+
+ return 0;
+}
+
+
+static int
+virStorageSourceParseBackingJSONGluster(virStorageSourcePtr src,
+ virJSONValuePtr json,
+ int opaque ATTRIBUTE_UNUSED)
+{
+ const char *uri = virJSONValueObjectGetString(json, "file.filename");
+ const char *volume = virJSONValueObjectGetString(json, "file.volume");
+ const char *path = virJSONValueObjectGetString(json, "file.path");
+ virJSONValuePtr server = virJSONValueObjectGetArray(json, "file.server");
+ size_t nservers;
+ size_t i;
+
+ /* legacy URI based syntax passed via 'filename' option */
+ if (uri)
+ return virStorageSourceParseBackingJSONUriStr(src, uri,
+ VIR_STORAGE_NET_PROTOCOL_GLUSTER);
+
+ if (!volume || !path || !server) {
+ virReportError(VIR_ERR_INVALID_ARG, "%s",
+ _("missing 'volume', 'path' or
'server' attribute in "
+ "JSON backing definition for gluster volume"));
+ return -1;
+ }
+
+ if (VIR_STRDUP(src->volume, volume) < 0 ||
+ virAsprintf(&src->path, "/%s", path) < 0)
+ return -1;
+
+ nservers = virJSONValueArraySize(server);
+
+ if (nservers < 1) {
+ virReportError(VIR_ERR_INVALID_ARG, "%s",
+ _("at least 1 server is necessary in "
+ "JSON backing definition for gluster volume"));
+ }
+
+ if (VIR_ALLOC_N(src->hosts, nservers) < 0)
+ return -1;
+ src->nhosts = nservers;
+
+ for (i = 0; i < nservers; i++) {
+ if (virStorageSourceParseBackingJSONGlusterHost(src->hosts + i,
+ virJSONValueArrayGet(server, i))
< 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+
struct virStorageSourceJSONDriverParser {
const char *drvname;
int (*func)(virStorageSourcePtr src, virJSONValuePtr json, int opaque);
@@ -2590,6 +2697,7 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] =
{
{"ftp", virStorageSourceParseBackingJSONUri,
VIR_STORAGE_NET_PROTOCOL_FTP},
{"ftps", virStorageSourceParseBackingJSONUri,
VIR_STORAGE_NET_PROTOCOL_FTPS},
{"tftp", virStorageSourceParseBackingJSONUri,
VIR_STORAGE_NET_PROTOCOL_TFTP},
+ {"gluster", virStorageSourceParseBackingJSONGluster, 0},
};
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
index 364e359..4ebc1c5 100644
--- a/tests/virstoragetest.c
+++ b/tests/virstoragetest.c
@@ -1377,6 +1377,31 @@ mymain(void)
TEST_BACKING_PARSE("json:{\"file.driver\":\"ftp\", "
"\"file.uri\":\"http://example.com/file\"}",
NULL);
+ TEST_BACKING_PARSE("json:{\"file.driver\":\"gluster\",
"
+
"\"file.filename\":\"gluster://example.com/vol/file\"}",
+ "<source protocol='gluster'
name='vol/file'>\n"
+ " <host name='example.com'/>\n"
+ "</source>\n");
+
TEST_BACKING_PARSE("json:{\"file.driver\":\"gluster\","
+
"\"file.volume\":\"testvol\","
+
"\"file.path\":\"img.qcow2\","
+ "\"file.server\":[ {
\"type\":\"tcp\","
+
"\"host\":\"example.com\","
+
"\"port\":\"1234\""
+ "},"
+ "{
\"type\":\"unix\","
+
"\"socket\":\"/path/socket\""
+ "},"
+ "{
\"type\":\"tcp\","
+
"\"host\":\"example.com\""
+ "}"
+ "]"
+ "}",
+ "<source protocol='none'
name='testvol/img.qcow2'>\n"
+ " <host name='example.com'
port='1234'/>\n"
+ " <host transport='unix'
socket='/path/socket'/>\n"
+ " <host name='example.com'/>\n"
+ "</source>\n");
cleanup:
/* Final cleanup */
--
2.9.0