Signed-off-by: K Shiva Kiran <shiva_kr@xxxxxxxxxx> --- src/conf/virnwfilterobj.c | 102 ++++++++++++++++++++++++++++++++++++++ src/conf/virnwfilterobj.h | 8 +++ src/libvirt_private.syms | 1 + 3 files changed, 111 insertions(+) diff --git a/src/conf/virnwfilterobj.c b/src/conf/virnwfilterobj.c index eab864fe2e..437df9631b 100644 --- a/src/conf/virnwfilterobj.c +++ b/src/conf/virnwfilterobj.c @@ -681,3 +681,105 @@ virNWFilterObjGetMetadata(virNWFilterObj *obj, return ret; } + + +static int +virNWFilterDefSetMetadata(virNWFilterDef *def, + int type, + const char *metadata, + const char *key, + const char *uri) +{ + g_autoptr(xmlDoc) doc = NULL; + xmlNodePtr old; + g_autoptr(xmlNode) new = NULL; + + if (type >= VIR_NWFILTER_METADATA_LAST) { + virReportError(VIR_ERR_INVALID_ARG, + _("unknown metadata type '%1$d'"), type); + return -1; + } + + switch ((virNWFilterMetadataType) type) { + case VIR_NWFILTER_METADATA_DESCRIPTION: + g_clear_pointer(&def->description, g_free); + + if (STRNEQ_NULLABLE(metadata, "")) + def->description = g_strdup(metadata); + break; + + case VIR_NWFILTER_METADATA_TITLE: + g_clear_pointer(&def->title, g_free); + + if (STRNEQ_NULLABLE(metadata, "")) + def->title = g_strdup(metadata); + break; + + case VIR_NWFILTER_METADATA_ELEMENT: + if (metadata) { + + /* parse and modify the xml from the user */ + if (!(doc = virXMLParseStringCtxt(metadata, _("(metadata_xml)"), NULL))) + return -1; + + if (virXMLInjectNamespace(doc->children, uri, key) < 0) + return -1; + + /* create the root node if needed */ + if (!def->metadata) + def->metadata = virXMLNewNode(NULL, "metadata"); + + if (!(new = xmlCopyNode(doc->children, 1))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to copy XML node")); + return -1; + } + } + + /* remove possible other nodes sharing the namespace */ + while ((old = virXMLFindChildNodeByNs(def->metadata, uri))) { + xmlUnlinkNode(old); + xmlFreeNode(old); + } + + if (new) { + if (!(xmlAddChild(def->metadata, new))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("failed to add metadata to XML document")); + return -1; + } + new = NULL; + } + break; + + case VIR_NWFILTER_METADATA_LAST: + break; + } + + return 0; +} + + +int +virNWFilterObjSetMetadata(virNWFilterObj *obj, + int type, + const char *metadata, + const char *key, + const char *uri, + const char *configDir) +{ + virNWFilterDef *def; + + if (!(def = virNWFilterObjGetDef(obj))) + return -1; + + if (def) { + if (virNWFilterDefSetMetadata(def, type, metadata, key, uri) < 0) + return -1; + + if (virNWFilterSaveConfig(configDir, def) < 0) + return -1; + } + + return 0; +} diff --git a/src/conf/virnwfilterobj.h b/src/conf/virnwfilterobj.h index dd4dd63cee..4079c0ab3d 100644 --- a/src/conf/virnwfilterobj.h +++ b/src/conf/virnwfilterobj.h @@ -122,3 +122,11 @@ char * virNWFilterObjGetMetadata(virNWFilterObj *obj, int type, const char *uri); + +int +virNWFilterObjSetMetadata(virNWFilterObj *obj, + int type, + const char *metadata, + const char *key, + const char *uri, + const char *configDir); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 8c4983ae17..628723f06d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1422,6 +1422,7 @@ virNWFilterObjListNew; virNWFilterObjListNumOfNWFilters; virNWFilterObjListRemove; virNWFilterObjLock; +virNWFilterObjSetMetadata; virNWFilterObjTestUnassignDef; virNWFilterObjUnlock; virNWFilterObjWantRemoved; -- 2.42.0