Blame SOURCES/0008-storage-domain-Move-out-ovirt_resource_parse_xml-to-.patch

7cfb7a
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
7cfb7a
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
7cfb7a
Date: Thu, 22 Jun 2017 13:06:53 -0300
7cfb7a
Subject: [PATCH] storage-domain: Move out ovirt_resource_parse_xml() to
7cfb7a
 ovirt-utils
7cfb7a
7cfb7a
There were a couple of tweaks to the function:
7cfb7a
7cfb7a
1) Renamed to ovirt_rest_xml_node_parse()
7cfb7a
    More suited to the task performed.
7cfb7a
7cfb7a
2) Validates GObject instead of OvirtResource
7cfb7a
    This removes the restriction of usage by a OvirtResource, and in the
7cfb7a
    future it can also be used by OvirtVmDisplay to parse the elements.
7cfb7a
    It also makes it more coherent to its purpose, as the function only
7cfb7a
    sets properties of a GObject and does not really require a
7cfb7a
    OvirtResource.
7cfb7a
7cfb7a
Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
7cfb7a
---
7cfb7a
 govirt/ovirt-storage-domain.c | 152 +++++++---------------------------
7cfb7a
 govirt/ovirt-utils.c          |  71 +++++++++++++++-
7cfb7a
 govirt/ovirt-utils.h          |  13 ++-
7cfb7a
 3 files changed, 113 insertions(+), 123 deletions(-)
7cfb7a
7cfb7a
diff --git a/govirt/ovirt-storage-domain.c b/govirt/ovirt-storage-domain.c
7cfb7a
index 07c0ef0..0582203 100644
7cfb7a
--- a/govirt/ovirt-storage-domain.c
7cfb7a
+++ b/govirt/ovirt-storage-domain.c
7cfb7a
@@ -53,10 +53,6 @@ enum {
7cfb7a
     PROP_STATE
7cfb7a
 };
7cfb7a
 
7cfb7a
-static gboolean
7cfb7a
-ovirt_storage_domain_refresh_from_xml(OvirtStorageDomain *domain,
7cfb7a
-                                      RestXmlNode *node);
7cfb7a
-
7cfb7a
 static void ovirt_storage_domain_get_property(GObject *object,
7cfb7a
                                               guint prop_id,
7cfb7a
                                               GValue *value,
7cfb7a
@@ -145,10 +141,39 @@ static gboolean ovirt_storage_domain_init_from_xml(OvirtResource *resource,
7cfb7a
 {
7cfb7a
     gboolean parsed_ok;
7cfb7a
     OvirtResourceClass *parent_class;
7cfb7a
-    OvirtStorageDomain *domain;
7cfb7a
+    OvirtXmlElement storage_domain_elements[] = {
7cfb7a
+        { .prop_name = "type",
7cfb7a
+          .type = OVIRT_TYPE_STORAGE_DOMAIN_TYPE,
7cfb7a
+          .xml_path = "type",
7cfb7a
+        },
7cfb7a
+        { .prop_name = "master",
7cfb7a
+          .type = G_TYPE_BOOLEAN,
7cfb7a
+          .xml_path = "master",
7cfb7a
+        },
7cfb7a
+        { .prop_name = "available",
7cfb7a
+          .type = G_TYPE_UINT64,
7cfb7a
+          .xml_path = "available",
7cfb7a
+        },
7cfb7a
+        { .prop_name = "used",
7cfb7a
+          .type = G_TYPE_UINT64,
7cfb7a
+          .xml_path = "used",
7cfb7a
+        },
7cfb7a
+        { .prop_name = "committed",
7cfb7a
+          .type = G_TYPE_UINT64,
7cfb7a
+          .xml_path = "committed",
7cfb7a
+        },
7cfb7a
+        { .prop_name = "version",
7cfb7a
+          .type = OVIRT_TYPE_STORAGE_DOMAIN_FORMAT_VERSION,
7cfb7a
+          .xml_path = "storage_format",
7cfb7a
+        },
7cfb7a
+        { .prop_name = "state",
7cfb7a
+          .type = OVIRT_TYPE_STORAGE_DOMAIN_STATE,
7cfb7a
+          .xml_path = "status/state",
7cfb7a
+        },
7cfb7a
+        { NULL , }
7cfb7a
+    };
7cfb7a
 
7cfb7a
-    domain = OVIRT_STORAGE_DOMAIN(resource);
7cfb7a
-    parsed_ok = ovirt_storage_domain_refresh_from_xml(domain, node);
7cfb7a
+    parsed_ok = ovirt_rest_xml_node_parse(node, G_OBJECT(resource), storage_domain_elements);
7cfb7a
     if (!parsed_ok) {
7cfb7a
         return FALSE;
7cfb7a
     }
7cfb7a
@@ -277,119 +302,6 @@ OvirtStorageDomain *ovirt_storage_domain_new(void)
7cfb7a
     return OVIRT_STORAGE_DOMAIN(domain);
7cfb7a
 }
7cfb7a
 
7cfb7a
-static gboolean
7cfb7a
-_set_property_value_from_type(GValue *value,
7cfb7a
-                              GType type,
7cfb7a
-                              const char *value_str,
7cfb7a
-                              RestXmlNode *node)
7cfb7a
-{
7cfb7a
-    gboolean ret = TRUE;
7cfb7a
-
7cfb7a
-    if (g_type_is_a(type, OVIRT_TYPE_RESOURCE)) {
7cfb7a
-        GObject *resource_value = g_initable_new(type, NULL, NULL, "xml-node", node, NULL);
7cfb7a
-        g_value_set_object(value, resource_value);
7cfb7a
-        goto end;
7cfb7a
-    }
7cfb7a
-
7cfb7a
-    /* All other types require valid value_str */
7cfb7a
-    if (value_str == NULL)
7cfb7a
-        return FALSE;
7cfb7a
-
7cfb7a
-    if (G_TYPE_IS_ENUM(type)) {
7cfb7a
-        int enum_value = ovirt_utils_genum_get_value(type, value_str, 0);
7cfb7a
-        g_value_set_enum(value, enum_value);
7cfb7a
-        goto end;
7cfb7a
-    }
7cfb7a
-
7cfb7a
-    switch(type) {
7cfb7a
-    case G_TYPE_BOOLEAN: {
7cfb7a
-        gboolean bool_value = ovirt_utils_boolean_from_string(value_str);
7cfb7a
-        g_value_set_boolean(value, bool_value);
7cfb7a
-        break;
7cfb7a
-    }
7cfb7a
-    case G_TYPE_UINT64: {
7cfb7a
-        guint64 int64_value = g_ascii_strtoull(value_str, NULL, 0);
7cfb7a
-        g_value_set_uint64(value, int64_value);
7cfb7a
-        break;
7cfb7a
-    }
7cfb7a
-    default: {
7cfb7a
-        g_warning("Unexpected type '%s' with value '%s'", g_type_name(type), value_str);
7cfb7a
-        ret = FALSE;
7cfb7a
-    }
7cfb7a
-    }
7cfb7a
-
7cfb7a
-end:
7cfb7a
-    return ret;
7cfb7a
-}
7cfb7a
-
7cfb7a
-typedef struct {
7cfb7a
-    const char *prop_name;
7cfb7a
-    GType type;
7cfb7a
-    const char *xml_path;
7cfb7a
-} OvirtXmlElement;
7cfb7a
-
7cfb7a
-static gboolean
7cfb7a
-ovirt_resource_parse_xml(OvirtResource *resource,
7cfb7a
-                         RestXmlNode *node,
7cfb7a
-                         OvirtXmlElement *elements)
7cfb7a
-{
7cfb7a
-    g_return_val_if_fail(OVIRT_IS_RESOURCE(resource), FALSE);
7cfb7a
-    g_return_val_if_fail(elements != NULL, FALSE);
7cfb7a
-
7cfb7a
-    for (;elements->xml_path != NULL; elements++) {
7cfb7a
-        const char *value_str;
7cfb7a
-        GValue value = { 0, };
7cfb7a
-
7cfb7a
-        value_str = ovirt_rest_xml_node_get_content_from_path(node, elements->xml_path);
7cfb7a
-
7cfb7a
-        g_value_init(&value, elements->type);
7cfb7a
-        if (_set_property_value_from_type(&value, elements->type, value_str, node))
7cfb7a
-            g_object_set_property(G_OBJECT(resource), elements->prop_name, &value);
7cfb7a
-        g_value_unset(&value);
7cfb7a
-    }
7cfb7a
-
7cfb7a
-    return TRUE;
7cfb7a
-}
7cfb7a
-
7cfb7a
-static gboolean
7cfb7a
-ovirt_storage_domain_refresh_from_xml(OvirtStorageDomain *domain,
7cfb7a
-                                      RestXmlNode *node)
7cfb7a
-{
7cfb7a
-    OvirtXmlElement storage_domain_elements[] = {
7cfb7a
-        { .prop_name = "type",
7cfb7a
-          .type = OVIRT_TYPE_STORAGE_DOMAIN_TYPE,
7cfb7a
-          .xml_path = "type",
7cfb7a
-        },
7cfb7a
-        { .prop_name = "master",
7cfb7a
-          .type = G_TYPE_BOOLEAN,
7cfb7a
-          .xml_path = "master",
7cfb7a
-        },
7cfb7a
-        { .prop_name = "available",
7cfb7a
-          .type = G_TYPE_UINT64,
7cfb7a
-          .xml_path = "available",
7cfb7a
-        },
7cfb7a
-        { .prop_name = "used",
7cfb7a
-          .type = G_TYPE_UINT64,
7cfb7a
-          .xml_path = "used",
7cfb7a
-        },
7cfb7a
-        { .prop_name = "committed",
7cfb7a
-          .type = G_TYPE_UINT64,
7cfb7a
-          .xml_path = "committed",
7cfb7a
-        },
7cfb7a
-        { .prop_name = "version",
7cfb7a
-          .type = OVIRT_TYPE_STORAGE_DOMAIN_FORMAT_VERSION,
7cfb7a
-          .xml_path = "storage_format",
7cfb7a
-        },
7cfb7a
-        { .prop_name = "state",
7cfb7a
-          .type = OVIRT_TYPE_STORAGE_DOMAIN_STATE,
7cfb7a
-          .xml_path = "status/state",
7cfb7a
-        },
7cfb7a
-        { NULL , }
7cfb7a
-    };
7cfb7a
-
7cfb7a
-    return ovirt_resource_parse_xml(OVIRT_RESOURCE(domain), node, storage_domain_elements);
7cfb7a
-}
7cfb7a
-
7cfb7a
 
7cfb7a
 /**
7cfb7a
  * ovirt_storage_domain_get_files:
7cfb7a
diff --git a/govirt/ovirt-utils.c b/govirt/ovirt-utils.c
7cfb7a
index 0e0134c..44ea7da 100644
7cfb7a
--- a/govirt/ovirt-utils.c
7cfb7a
+++ b/govirt/ovirt-utils.c
7cfb7a
@@ -31,6 +31,7 @@
7cfb7a
 #include "ovirt-utils.h"
7cfb7a
 
7cfb7a
 #include "ovirt-error.h"
7cfb7a
+#include "ovirt-resource.h"
7cfb7a
 
7cfb7a
 RestXmlNode *
7cfb7a
 ovirt_rest_xml_node_from_call(RestProxyCall *call)
7cfb7a
@@ -77,7 +78,7 @@ ovirt_rest_xml_node_get_content_va(RestXmlNode *node,
7cfb7a
     return node->content;
7cfb7a
 }
7cfb7a
 
7cfb7a
-G_GNUC_INTERNAL const char *
7cfb7a
+static const char *
7cfb7a
 ovirt_rest_xml_node_get_content_from_path(RestXmlNode *node, const char *path)
7cfb7a
 {
7cfb7a
     GStrv pathv;
7cfb7a
@@ -109,6 +110,74 @@ ovirt_rest_xml_node_get_content(RestXmlNode *node, ...)
7cfb7a
     return content;
7cfb7a
 }
7cfb7a
 
7cfb7a
+static gboolean
7cfb7a
+_set_property_value_from_type(GValue *value,
7cfb7a
+                              GType type,
7cfb7a
+                              const char *value_str,
7cfb7a
+                              RestXmlNode *node)
7cfb7a
+{
7cfb7a
+    gboolean ret = TRUE;
7cfb7a
+
7cfb7a
+    if (g_type_is_a(type, OVIRT_TYPE_RESOURCE)) {
7cfb7a
+        GObject *resource_value = g_initable_new(type, NULL, NULL, "xml-node", node, NULL);
7cfb7a
+        g_value_set_object(value, resource_value);
7cfb7a
+        goto end;
7cfb7a
+    }
7cfb7a
+
7cfb7a
+    /* All other types require valid value_str */
7cfb7a
+    if (value_str == NULL)
7cfb7a
+        return FALSE;
7cfb7a
+
7cfb7a
+    if (G_TYPE_IS_ENUM(type)) {
7cfb7a
+        int enum_value = ovirt_utils_genum_get_value(type, value_str, 0);
7cfb7a
+        g_value_set_enum(value, enum_value);
7cfb7a
+        goto end;
7cfb7a
+    }
7cfb7a
+
7cfb7a
+    switch(type) {
7cfb7a
+    case G_TYPE_BOOLEAN: {
7cfb7a
+        gboolean bool_value = ovirt_utils_boolean_from_string(value_str);
7cfb7a
+        g_value_set_boolean(value, bool_value);
7cfb7a
+        break;
7cfb7a
+    }
7cfb7a
+    case G_TYPE_UINT64: {
7cfb7a
+        guint64 int64_value = g_ascii_strtoull(value_str, NULL, 0);
7cfb7a
+        g_value_set_uint64(value, int64_value);
7cfb7a
+        break;
7cfb7a
+    }
7cfb7a
+    default: {
7cfb7a
+        g_warning("Unexpected type '%s' with value '%s'", g_type_name(type), value_str);
7cfb7a
+        ret = FALSE;
7cfb7a
+    }
7cfb7a
+    }
7cfb7a
+
7cfb7a
+end:
7cfb7a
+    return ret;
7cfb7a
+}
7cfb7a
+
7cfb7a
+gboolean
7cfb7a
+ovirt_rest_xml_node_parse(RestXmlNode *node,
7cfb7a
+                          GObject *object,
7cfb7a
+                          OvirtXmlElement *elements)
7cfb7a
+{
7cfb7a
+    g_return_val_if_fail(G_IS_OBJECT(object), FALSE);
7cfb7a
+    g_return_val_if_fail(elements != NULL, FALSE);
7cfb7a
+
7cfb7a
+    for (;elements->xml_path != NULL; elements++) {
7cfb7a
+        const char *value_str;
7cfb7a
+        GValue value = { 0, };
7cfb7a
+
7cfb7a
+        value_str = ovirt_rest_xml_node_get_content_from_path(node, elements->xml_path);
7cfb7a
+
7cfb7a
+        g_value_init(&value, elements->type);
7cfb7a
+        if (_set_property_value_from_type(&value, elements->type, value_str, node))
7cfb7a
+            g_object_set_property(object, elements->prop_name, &value);
7cfb7a
+        g_value_unset(&value);
7cfb7a
+    }
7cfb7a
+
7cfb7a
+    return TRUE;
7cfb7a
+}
7cfb7a
+
7cfb7a
 
7cfb7a
 /* These 2 functions come from
7cfb7a
  * libvirt-glib/libvirt-gconfig/libvirt-gconfig-helpers.c
7cfb7a
diff --git a/govirt/ovirt-utils.h b/govirt/ovirt-utils.h
7cfb7a
index 3f43fc9..4fd4164 100644
7cfb7a
--- a/govirt/ovirt-utils.h
7cfb7a
+++ b/govirt/ovirt-utils.h
7cfb7a
@@ -27,10 +27,19 @@
7cfb7a
 
7cfb7a
 G_BEGIN_DECLS
7cfb7a
 
7cfb7a
+typedef struct _OvirtXmlElement OvirtXmlElement;
7cfb7a
+struct _OvirtXmlElement
7cfb7a
+{
7cfb7a
+    const char *prop_name;
7cfb7a
+    GType type;
7cfb7a
+    const char *xml_path;
7cfb7a
+};
7cfb7a
+
7cfb7a
 RestXmlNode *ovirt_rest_xml_node_from_call(RestProxyCall *call);
7cfb7a
 const char *ovirt_rest_xml_node_get_content(RestXmlNode *node, ...);
7cfb7a
-const char *ovirt_rest_xml_node_get_content_from_path(RestXmlNode *node,
7cfb7a
-                                                      const char *path);
7cfb7a
+gboolean ovirt_rest_xml_node_parse(RestXmlNode *node,
7cfb7a
+                                   GObject *object,
7cfb7a
+                                   OvirtXmlElement *elements);
7cfb7a
 gboolean ovirt_utils_gerror_from_xml_fault(RestXmlNode *root, GError **error);
7cfb7a
 gboolean g_object_set_guint_property_from_xml(GObject *g_object,
7cfb7a
                                                    RestXmlNode *node,