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

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