Blob Blame History Raw
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
Date: Thu, 13 Jul 2017 16:56:34 -0300
Subject: [PATCH] Introduce ovirt_resource_new*() functions

These functions should be used to replace usage of g_initable_new()
around the codebase, as it avoids code duplication by combining all
different ways of creating a resource in a single function.

Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
---
 govirt/ovirt-resource-private.h |  4 +++
 govirt/ovirt-resource.c         | 47 +++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/govirt/ovirt-resource-private.h b/govirt/ovirt-resource-private.h
index ff4e705..ef47557 100644
--- a/govirt/ovirt-resource-private.h
+++ b/govirt/ovirt-resource-private.h
@@ -27,6 +27,10 @@
 
 G_BEGIN_DECLS
 
+OvirtResource *ovirt_resource_new(GType type);
+OvirtResource *ovirt_resource_new_from_id(GType type, const char *id, const char *href);
+OvirtResource *ovirt_resource_new_from_xml(GType type, RestXmlNode *node, GError **error);
+
 const char *ovirt_resource_get_action(OvirtResource *resource,
                                       const char *action);
 char *ovirt_resource_to_xml(OvirtResource *resource);
diff --git a/govirt/ovirt-resource.c b/govirt/ovirt-resource.c
index 7f79ab7..1413a77 100644
--- a/govirt/ovirt-resource.c
+++ b/govirt/ovirt-resource.c
@@ -1065,3 +1065,50 @@ gboolean ovirt_resource_delete_finish(OvirtResource *resource,
 
     return ovirt_rest_call_finish(result, err);
 }
+
+
+static OvirtResource *ovirt_resource_new_valist(GType type, GError **error, const char *prop_name, ...)
+{
+    gpointer resource;
+    va_list var_args;
+    GError *local_error = NULL;
+
+    g_return_val_if_fail(g_type_is_a(type, OVIRT_TYPE_RESOURCE), NULL);
+
+    va_start(var_args, prop_name);
+    resource = g_initable_new_valist(type, prop_name, var_args, NULL, &local_error);
+    va_end(var_args);
+
+    if (local_error != NULL) {
+        g_warning("Failed to create resource of type %s: %s", g_type_name(type), local_error->message);
+        g_propagate_error(error, local_error);
+    }
+
+    return OVIRT_RESOURCE(resource);
+}
+
+
+G_GNUC_INTERNAL
+OvirtResource *ovirt_resource_new(GType type)
+{
+    return ovirt_resource_new_valist(type, NULL, NULL);
+}
+
+
+G_GNUC_INTERNAL
+OvirtResource *ovirt_resource_new_from_id(GType type, const char *id, const char *href)
+{
+    g_return_val_if_fail(id != NULL, NULL);
+    g_return_val_if_fail(href != NULL, NULL);
+
+    return ovirt_resource_new_valist(type, NULL, "guid", id, "href", href, NULL);
+}
+
+
+G_GNUC_INTERNAL
+OvirtResource *ovirt_resource_new_from_xml(GType type, RestXmlNode *node, GError **error)
+{
+    g_return_val_if_fail(node != NULL, NULL);
+
+    return ovirt_resource_new_valist(type, error, "xml-node", node, NULL);
+}