qemu: monitor: Make wrapping of 'props' of 'object-add' optional

Construct the JSON object which is used for object-add without the
'props' wrapper and add the wrapper only in the monitor code.

This simplifies the JSON->commandline generator in the first place and
also prepares for upcoming qemu where 'props' will be removed.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2020-11-30 16:03:57 +01:00
parent 3d50ecc9bb
commit e0eeb2cc67
2 changed files with 59 additions and 45 deletions

View File

@ -109,6 +109,9 @@ struct _qemuMonitor {
qemuMonitorReportDomainLogError logFunc; qemuMonitorReportDomainLogError logFunc;
void *logOpaque; void *logOpaque;
virFreeCallback logDestroy; virFreeCallback logDestroy;
/* true if qemu no longer wants 'props' sub-object of object-add */
bool objectAddNoWrap;
}; };
/** /**
@ -3017,14 +3020,12 @@ qemuMonitorCreateObjectPropsWrap(const char *type,
const char *alias, const char *alias,
virJSONValuePtr *props) virJSONValuePtr *props)
{ {
virJSONValuePtr ret;
ignore_value(virJSONValueObjectCreate(&ret, if (virJSONValueObjectPrependString(*props, "id", alias) < 0 ||
"s:qom-type", type, virJSONValueObjectPrependString(*props, "qom-type", type))
"s:id", alias, return NULL;
"A:props", props,
NULL)); return g_steal_pointer(props);
return ret;
} }
@ -3044,26 +3045,28 @@ qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
const char *alias, const char *alias,
...) ...)
{ {
virJSONValuePtr props = NULL; g_autoptr(virJSONValue) props = NULL;
int ret = -1; int rc;
va_list args; va_list args;
*propsret = NULL; if (virJSONValueObjectCreate(&props,
"s:qom-type", type,
"s:id", alias,
NULL) < 0)
return -1;
va_start(args, alias); va_start(args, alias);
if (virJSONValueObjectCreateVArgs(&props, args) < 0) rc = virJSONValueObjectAddVArgs(props, args);
goto cleanup;
if (!(*propsret = qemuMonitorCreateObjectPropsWrap(type, alias, &props)))
goto cleanup;
ret = 0;
cleanup:
virJSONValueFree(props);
va_end(args); va_end(args);
return ret;
if (rc < 0)
return -1;
*propsret = g_steal_pointer(&props);
return 0;
} }
@ -3083,6 +3086,7 @@ qemuMonitorAddObject(qemuMonitorPtr mon,
virJSONValuePtr *props, virJSONValuePtr *props,
char **alias) char **alias)
{ {
g_autoptr(virJSONValue) pr = NULL;
const char *type = NULL; const char *type = NULL;
const char *id = NULL; const char *id = NULL;
g_autofree char *aliasCopy = NULL; g_autofree char *aliasCopy = NULL;
@ -3110,7 +3114,31 @@ qemuMonitorAddObject(qemuMonitorPtr mon,
if (alias) if (alias)
aliasCopy = g_strdup(id); aliasCopy = g_strdup(id);
if (qemuMonitorJSONAddObject(mon, props) < 0) if (mon->objectAddNoWrap) {
pr = g_steal_pointer(props);
} else {
/* we need to create a wrapper which has the 'qom-type' and 'id' and
* store everything else under a 'props' sub-object */
g_autoptr(virJSONValue) typeobj = NULL;
g_autoptr(virJSONValue) idobj = NULL;
ignore_value(virJSONValueObjectRemoveKey(*props, "qom-type", &typeobj));
ignore_value(virJSONValueObjectRemoveKey(*props, "id", &idobj));
if (!virJSONValueObjectGetKey(*props, 0)) {
virJSONValueFree(*props);
*props = NULL;
}
if (virJSONValueObjectCreate(&pr,
"s:qom-type", type,
"s:id", id,
"A:props", props,
NULL) < 0)
return -1;
}
if (qemuMonitorJSONAddObject(mon, &pr) < 0)
return -1; return -1;
if (alias) if (alias)

View File

@ -303,12 +303,13 @@ virQEMUBuildNetdevCommandlineFromJSON(virJSONValuePtr props,
} }
static int int
virQEMUBuildObjectCommandlineFromJSONInternal(virBufferPtr buf, virQEMUBuildObjectCommandlineFromJSON(virBufferPtr buf,
const char *type, virJSONValuePtr objprops)
const char *alias,
virJSONValuePtr props)
{ {
const char *type = virJSONValueObjectGetString(objprops, "qom-type");
const char *alias = virJSONValueObjectGetString(objprops, "id");
if (!type || !alias) { if (!type || !alias) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("missing 'type'(%s) or 'alias'(%s) field of QOM 'object'"), _("missing 'type'(%s) or 'alias'(%s) field of QOM 'object'"),
@ -316,31 +317,16 @@ virQEMUBuildObjectCommandlineFromJSONInternal(virBufferPtr buf,
return -1; return -1;
} }
virBufferAsprintf(buf, "%s,id=%s", type, alias); virBufferAsprintf(buf, "%s,", type);
if (props) { if (virQEMUBuildCommandLineJSON(objprops, buf, "qom-type",
virBufferAddLit(buf, ","); virQEMUBuildCommandLineJSONArrayBitmap) < 0)
if (virQEMUBuildCommandLineJSON(props, buf, NULL, return -1;
virQEMUBuildCommandLineJSONArrayBitmap) < 0)
return -1;
}
return 0; return 0;
} }
int
virQEMUBuildObjectCommandlineFromJSON(virBufferPtr buf,
virJSONValuePtr objprops)
{
const char *type = virJSONValueObjectGetString(objprops, "qom-type");
const char *alias = virJSONValueObjectGetString(objprops, "id");
virJSONValuePtr props = virJSONValueObjectGetObject(objprops, "props");
return virQEMUBuildObjectCommandlineFromJSONInternal(buf, type, alias, props);
}
char * char *
virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef) virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef)
{ {