storage: util: Split out the gluster volume extraction code into new function

To allow testing of the algorithm, split out the extractor into a
separate helper.
This commit is contained in:
Peter Krempa 2017-04-04 13:39:37 +02:00
parent a92160dbd5
commit e238bfa6d4
2 changed files with 70 additions and 51 deletions

View File

@ -2836,68 +2836,21 @@ virStorageBackendDeleteLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
}
/**
* virStorageBackendFindGlusterPoolSources:
* @host: host to detect volumes on
* @pooltype: type of the pool
* @list: list of storage pool sources to be filled
* @report: report error if the 'gluster' cli tool is missing
*
* Looks up gluster volumes on @host and fills them to @list.
*
* @pooltype allows to influence the specific differences between netfs and
* native gluster pools. Users should pass only VIR_STORAGE_POOL_NETFS or
* VIR_STORAGE_POOL_GLUSTER.
*
* Returns number of volumes on the host on success, or -1 on error.
*/
int
virStorageBackendFindGlusterPoolSources(const char *host,
virStoragePoolType pooltype,
virStorageUtilGlusterExtractPoolSources(const char *host,
const char *xml,
virStoragePoolSourceListPtr list,
bool report)
virStoragePoolType pooltype)
{
char *glusterpath = NULL;
char *outbuf = NULL;
virCommandPtr cmd = NULL;
xmlDocPtr doc = NULL;
xmlXPathContextPtr ctxt = NULL;
xmlNodePtr *nodes = NULL;
virStoragePoolSource *src = NULL;
size_t i;
int nnodes;
int rc;
int ret = -1;
if (!(glusterpath = virFindFileInPath("gluster"))) {
if (report) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'gluster' command line tool not found"));
return -1;
} else {
return 0;
}
}
cmd = virCommandNewArgList(glusterpath,
"--xml",
"--log-file=/dev/null",
"volume", "info", "all", NULL);
virCommandAddArgFormat(cmd, "--remote-host=%s", host);
virCommandSetOutputBuffer(cmd, &outbuf);
if (virCommandRun(cmd, &rc) < 0)
goto cleanup;
if (rc != 0) {
ret = 0;
goto cleanup;
}
if (!(doc = virXMLParseStringCtxt(outbuf, _("(gluster_cli_output)"),
&ctxt)))
if (!(doc = virXMLParseStringCtxt(xml, _("(gluster_cli_output)"), &ctxt)))
goto cleanup;
if ((nnodes = virXPathNodeSet("//volumes/volume", ctxt, &nodes)) < 0)
@ -2932,6 +2885,68 @@ virStorageBackendFindGlusterPoolSources(const char *host,
VIR_FREE(nodes);
xmlXPathFreeContext(ctxt);
xmlFreeDoc(doc);
return ret;
}
/**
* virStorageBackendFindGlusterPoolSources:
* @host: host to detect volumes on
* @pooltype: type of the pool
* @list: list of storage pool sources to be filled
* @report: report error if the 'gluster' cli tool is missing
*
* Looks up gluster volumes on @host and fills them to @list.
*
* @pooltype allows to influence the specific differences between netfs and
* native gluster pools. Users should pass only VIR_STORAGE_POOL_NETFS or
* VIR_STORAGE_POOL_GLUSTER.
*
* Returns number of volumes on the host on success, or -1 on error.
*/
int
virStorageBackendFindGlusterPoolSources(const char *host,
virStoragePoolType pooltype,
virStoragePoolSourceListPtr list,
bool report)
{
char *glusterpath = NULL;
char *outbuf = NULL;
virCommandPtr cmd = NULL;
int rc;
int ret = -1;
if (!(glusterpath = virFindFileInPath("gluster"))) {
if (report) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'gluster' command line tool not found"));
return -1;
} else {
return 0;
}
}
cmd = virCommandNewArgList(glusterpath,
"--xml",
"--log-file=/dev/null",
"volume", "info", "all", NULL);
virCommandAddArgFormat(cmd, "--remote-host=%s", host);
virCommandSetOutputBuffer(cmd, &outbuf);
if (virCommandRun(cmd, &rc) < 0)
goto cleanup;
if (rc != 0) {
ret = 0;
goto cleanup;
}
ret = virStorageUtilGlusterExtractPoolSources(host, outbuf, list, pooltype);
cleanup:
VIR_FREE(outbuf);
virCommandFree(cmd);
VIR_FREE(glusterpath);

View File

@ -93,6 +93,10 @@ int virStorageBackendDeleteLocal(virConnectPtr conn,
int virStorageBackendRefreshLocal(virConnectPtr conn,
virStoragePoolObjPtr pool);
int virStorageUtilGlusterExtractPoolSources(const char *host,
const char *xml,
virStoragePoolSourceListPtr list,
virStoragePoolType pooltype);
int virStorageBackendFindGlusterPoolSources(const char *host,
virStoragePoolType pooltype,
virStoragePoolSourceListPtr list,