mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-01 17:35:17 +00:00
util: qemu: Add wrapper for JSON -> commandline conversion
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.
This commit is contained in:
parent
bc4339719e
commit
f0276c3489
@ -2204,6 +2204,7 @@ virProcessWait;
|
|||||||
|
|
||||||
# util/virqemu.h
|
# util/virqemu.h
|
||||||
virQEMUBuildBufferEscapeComma;
|
virQEMUBuildBufferEscapeComma;
|
||||||
|
virQEMUBuildCommandLineJSON;
|
||||||
virQEMUBuildLuksOpts;
|
virQEMUBuildLuksOpts;
|
||||||
virQEMUBuildObjectCommandlineFromJSON;
|
virQEMUBuildObjectCommandlineFromJSON;
|
||||||
|
|
||||||
|
@ -33,10 +33,10 @@ VIR_LOG_INIT("util.qemu");
|
|||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
virQEMUBuildObjectCommandLinePropsInternal(const char *key,
|
virQEMUBuildCommandLineJSONRecurse(const char *key,
|
||||||
const virJSONValue *value,
|
const virJSONValue *value,
|
||||||
virBufferPtr buf,
|
virBufferPtr buf,
|
||||||
bool nested)
|
bool nested)
|
||||||
{
|
{
|
||||||
virJSONValuePtr elem;
|
virJSONValuePtr elem;
|
||||||
virBitmapPtr bitmap = NULL;
|
virBitmapPtr bitmap = NULL;
|
||||||
@ -64,7 +64,8 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key,
|
|||||||
case VIR_JSON_TYPE_ARRAY:
|
case VIR_JSON_TYPE_ARRAY:
|
||||||
if (nested) {
|
if (nested) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("nested -object property arrays are not supported"));
|
_("nested JSON array to commandline conversion is "
|
||||||
|
"not supported"));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,8 +88,7 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key,
|
|||||||
elem = virJSONValueArrayGet((virJSONValuePtr)value, i);
|
elem = virJSONValueArrayGet((virJSONValuePtr)value, i);
|
||||||
|
|
||||||
/* recurse to avoid duplicating code */
|
/* recurse to avoid duplicating code */
|
||||||
if (virQEMUBuildObjectCommandLinePropsInternal(key, elem, buf,
|
if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf, true) < 0)
|
||||||
true) < 0)
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,11 +108,34 @@ virQEMUBuildObjectCommandLinePropsInternal(const char *key,
|
|||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
virQEMUBuildObjectCommandLineProps(const char *key,
|
virQEMUBuildCommandLineJSONIterate(const char *key,
|
||||||
const virJSONValue *value,
|
const virJSONValue *value,
|
||||||
void *opaque)
|
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);
|
virBufferAsprintf(&buf, "%s,id=%s", type, alias);
|
||||||
|
|
||||||
if (virJSONValueObjectForeachKeyValue(props,
|
if (virQEMUBuildCommandLineJSON(props, &buf) < 0)
|
||||||
virQEMUBuildObjectCommandLineProps,
|
|
||||||
&buf) < 0)
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (virBufferCheckError(&buf) < 0)
|
if (virBufferCheckError(&buf) < 0)
|
||||||
|
@ -29,6 +29,9 @@
|
|||||||
# include "virjson.h"
|
# include "virjson.h"
|
||||||
# include "virstorageencryption.h"
|
# include "virstorageencryption.h"
|
||||||
|
|
||||||
|
int virQEMUBuildCommandLineJSON(const virJSONValue *value,
|
||||||
|
virBufferPtr buf);
|
||||||
|
|
||||||
char *virQEMUBuildObjectCommandlineFromJSON(const char *type,
|
char *virQEMUBuildObjectCommandlineFromJSON(const char *type,
|
||||||
const char *alias,
|
const char *alias,
|
||||||
virJSONValuePtr props);
|
virJSONValuePtr props);
|
||||||
|
@ -33,11 +33,12 @@ typedef struct
|
|||||||
} testQemuCommandBuildObjectFromJSONData;
|
} testQemuCommandBuildObjectFromJSONData;
|
||||||
|
|
||||||
static int
|
static int
|
||||||
testQemuCommandBuildObjectFromJSON(const void *opaque)
|
testQemuCommandBuildFromJSON(const void *opaque)
|
||||||
{
|
{
|
||||||
const testQemuCommandBuildObjectFromJSONData *data = opaque;
|
const testQemuCommandBuildObjectFromJSONData *data = opaque;
|
||||||
virJSONValuePtr val = NULL;
|
virJSONValuePtr val = NULL;
|
||||||
char *expect = NULL;
|
char *expect = NULL;
|
||||||
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
char *result = NULL;
|
char *result = NULL;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
@ -46,13 +47,18 @@ testQemuCommandBuildObjectFromJSON(const void *opaque)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virAsprintf(&expect, "testobject,id=testalias%s%s",
|
if (data->expectprops &&
|
||||||
data->expectprops ? "," : "",
|
virAsprintf(&expect, ",%s", data->expectprops) < 0)
|
||||||
data->expectprops ? data->expectprops : "") < 0)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
result = virQEMUBuildObjectCommandlineFromJSON("testobject",
|
if (virQEMUBuildCommandLineJSON(val, &buf) < 0) {
|
||||||
"testalias", val);
|
fprintf(stderr,
|
||||||
|
"\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n",
|
||||||
|
data->props);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = virBufferContentAndReset(&buf);
|
||||||
|
|
||||||
if (STRNEQ_NULLABLE(expect, result)) {
|
if (STRNEQ_NULLABLE(expect, result)) {
|
||||||
fprintf(stderr, "\nFailed to create object string. "
|
fprintf(stderr, "\nFailed to create object string. "
|
||||||
@ -80,14 +86,14 @@ mymain(void)
|
|||||||
return EXIT_AM_SKIP;
|
return EXIT_AM_SKIP;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
virTestCounterReset("testQemuCommandBuildObjectFromJSON");
|
virTestCounterReset("testQemuCommandBuildFromJSON");
|
||||||
|
|
||||||
#define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT) \
|
#define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT) \
|
||||||
do { \
|
do { \
|
||||||
data1.props = PROPS; \
|
data1.props = PROPS; \
|
||||||
data1.expectprops = EXPECT; \
|
data1.expectprops = EXPECT; \
|
||||||
if (virTestRun(virTestCounterNext(), \
|
if (virTestRun(virTestCounterNext(), \
|
||||||
testQemuCommandBuildObjectFromJSON, \
|
testQemuCommandBuildFromJSON, \
|
||||||
&data1) < 0) \
|
&data1) < 0) \
|
||||||
ret = -1; \
|
ret = -1; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user