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