Adds two new private methods for nwfilter metadata:
- virNWFilterObjGetMetadata()
- virNWFilterObjSetMetadata()
Signed-off-by: K Shiva Kiran <shiva_kr(a)riseup.net>
---
src/conf/virnwfilterobj.c | 148 ++++++++++++++++++++++++++++++++++++++
src/conf/virnwfilterobj.h | 13 ++++
src/libvirt_private.syms | 2 +
3 files changed, 163 insertions(+)
diff --git a/src/conf/virnwfilterobj.c b/src/conf/virnwfilterobj.c
index 6456add593..437df9631b 100644
--- a/src/conf/virnwfilterobj.c
+++ b/src/conf/virnwfilterobj.c
@@ -635,3 +635,151 @@ virNWFilterObjUnlock(virNWFilterObj *obj)
{
virMutexUnlock(&obj->lock);
}
+
+
+char *
+virNWFilterObjGetMetadata(virNWFilterObj *obj,
+ int type,
+ const char *uri)
+{
+ virNWFilterDef *def;
+ char *ret = NULL;
+
+ if (type >= VIR_NWFILTER_METADATA_LAST) {
+ virReportError(VIR_ERR_INVALID_ARG,
+ _("unknown metadata type '%1$d'"), type);
+ return NULL;
+ }
+
+ if (!(def = virNWFilterObjGetDef(obj)))
+ return NULL;
+
+ switch ((virNWFilterMetadataType) type) {
+ case VIR_NWFILTER_METADATA_DESCRIPTION:
+ ret = g_strdup(def->description);
+ break;
+
+ case VIR_NWFILTER_METADATA_TITLE:
+ ret = g_strdup(def->title);
+ break;
+
+ case VIR_NWFILTER_METADATA_ELEMENT:
+ if (!def->metadata)
+ break;
+
+ if (virXMLExtractNamespaceXML(def->metadata, uri, &ret) < 0)
+ return NULL;
+ break;
+
+ case VIR_NWFILTER_METADATA_LAST:
+ break;
+ }
+
+ if (!ret)
+ virReportError(VIR_ERR_NO_NWFILTER_METADATA, "%s",
+ _("Requested metadata element is not present"));
+
+ 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 b67dc017c5..4079c0ab3d 100644
--- a/src/conf/virnwfilterobj.h
+++ b/src/conf/virnwfilterobj.h
@@ -117,3 +117,16 @@ virNWFilterObjLock(virNWFilterObj *obj);
void
virNWFilterObjUnlock(virNWFilterObj *obj);
+
+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 4e475d5b1a..7774ee03c4 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1410,6 +1410,7 @@ virNWFilterBindingObjListRemove;
# conf/virnwfilterobj.h
virNWFilterObjGetDef;
+virNWFilterObjGetMetadata;
virNWFilterObjGetNewDef;
virNWFilterObjListAssignDef;
virNWFilterObjListExport;
@@ -1423,6 +1424,7 @@ virNWFilterObjListNew;
virNWFilterObjListNumOfNWFilters;
virNWFilterObjListRemove;
virNWFilterObjLock;
+virNWFilterObjSetMetadata;
virNWFilterObjTestUnassignDef;
virNWFilterObjUnlock;
virNWFilterObjWantRemoved;
--
2.42.0