Blame SOURCES/0014-Introduce-auxiliary-function-ovirt_sub_collection_ne.patch

546e1e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
546e1e
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
546e1e
Date: Thu, 18 May 2017 16:45:37 -0300
546e1e
Subject: [PATCH] Introduce auxiliary function
546e1e
 ovirt_sub_collection_new_from_resource()
546e1e
546e1e
This function eliminates duplication of code used to create a
546e1e
subcollection from a resource. Users are updated accordingly.
546e1e
546e1e
Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
546e1e
---
546e1e
 govirt/ovirt-api.c                | 50 +++++++++++--------------------
546e1e
 govirt/ovirt-collection-private.h |  5 ++++
546e1e
 govirt/ovirt-collection.c         | 15 ++++++++++
546e1e
 govirt/ovirt-storage-domain.c     | 18 ++++-------
546e1e
 govirt/ovirt-vm.c                 | 18 ++++-------
546e1e
 5 files changed, 50 insertions(+), 56 deletions(-)
546e1e
546e1e
diff --git a/govirt/ovirt-api.c b/govirt/ovirt-api.c
546e1e
index 001ee42..37c0935 100644
546e1e
--- a/govirt/ovirt-api.c
546e1e
+++ b/govirt/ovirt-api.c
546e1e
@@ -123,18 +123,14 @@ OvirtApi *ovirt_api_new(void)
546e1e
  */
546e1e
 OvirtCollection *ovirt_api_get_vms(OvirtApi *api)
546e1e
 {
546e1e
-    const char *href;
546e1e
-
546e1e
     g_return_val_if_fail(OVIRT_IS_API(api), NULL);
546e1e
 
546e1e
-    if (api->priv->vms != NULL)
546e1e
-        return api->priv->vms;
546e1e
-
546e1e
-    href = ovirt_resource_get_sub_collection(OVIRT_RESOURCE(api), "vms");
546e1e
-    if (href == NULL)
546e1e
-        return NULL;
546e1e
-
546e1e
-    api->priv->vms = ovirt_collection_new(href, "vms", OVIRT_TYPE_VM, "vm");
546e1e
+    if (api->priv->vms == NULL)
546e1e
+        api->priv->vms = ovirt_sub_collection_new_from_resource(OVIRT_RESOURCE(api),
546e1e
+                                                                "vms",
546e1e
+                                                                "vms",
546e1e
+                                                                OVIRT_TYPE_VM,
546e1e
+                                                                "vm");
546e1e
 
546e1e
     return api->priv->vms;
546e1e
 }
546e1e
@@ -151,18 +147,14 @@ OvirtCollection *ovirt_api_get_vms(OvirtApi *api)
546e1e
  */
546e1e
 OvirtCollection *ovirt_api_get_vm_pools(OvirtApi *api)
546e1e
 {
546e1e
-    const char *href;
546e1e
-
546e1e
     g_return_val_if_fail(OVIRT_IS_API(api), NULL);
546e1e
 
546e1e
-    if (api->priv->vm_pools != NULL)
546e1e
-        return api->priv->vm_pools;
546e1e
-
546e1e
-    href = ovirt_resource_get_sub_collection(OVIRT_RESOURCE(api), "vmpools");
546e1e
-    if (href == NULL)
546e1e
-        return NULL;
546e1e
-
546e1e
-    api->priv->vm_pools = ovirt_collection_new(href, "vmpools", OVIRT_TYPE_VM_POOL, "vmpool");
546e1e
+    if (api->priv->vm_pools == NULL)
546e1e
+        api->priv->vm_pools = ovirt_sub_collection_new_from_resource(OVIRT_RESOURCE(api),
546e1e
+                                                                     "vmpools",
546e1e
+                                                                     "vmpools",
546e1e
+                                                                     OVIRT_TYPE_VM_POOL,
546e1e
+                                                                     "vmpool");
546e1e
 
546e1e
     return api->priv->vm_pools;
546e1e
 }
546e1e
@@ -180,20 +172,14 @@ OvirtCollection *ovirt_api_get_vm_pools(OvirtApi *api)
546e1e
  */
546e1e
 OvirtCollection *ovirt_api_get_storage_domains(OvirtApi *api)
546e1e
 {
546e1e
-    const char *href;
546e1e
-
546e1e
     g_return_val_if_fail(OVIRT_IS_API(api), NULL);
546e1e
 
546e1e
-    if (api->priv->storage_domains != NULL)
546e1e
-        return api->priv->storage_domains;
546e1e
-
546e1e
-    href = ovirt_resource_get_sub_collection(OVIRT_RESOURCE(api), "storagedomains");
546e1e
-    if (href == NULL)
546e1e
-        return NULL;
546e1e
-
546e1e
-    api->priv->storage_domains = ovirt_collection_new(href, "storage_domains",
546e1e
-                                                      OVIRT_TYPE_STORAGE_DOMAIN,
546e1e
-                                                      "storage_domain");
546e1e
+    if (api->priv->storage_domains == NULL)
546e1e
+        api->priv->storage_domains = ovirt_sub_collection_new_from_resource(OVIRT_RESOURCE(api),
546e1e
+                                                                            "storagedomains",
546e1e
+                                                                            "storage_domains",
546e1e
+                                                                            OVIRT_TYPE_STORAGE_DOMAIN,
546e1e
+                                                                            "storage_domain");
546e1e
 
546e1e
     return api->priv->storage_domains;
546e1e
 }
546e1e
diff --git a/govirt/ovirt-collection-private.h b/govirt/ovirt-collection-private.h
546e1e
index 5bc294f..d955fc6 100644
546e1e
--- a/govirt/ovirt-collection-private.h
546e1e
+++ b/govirt/ovirt-collection-private.h
546e1e
@@ -41,6 +41,11 @@ OvirtCollection *ovirt_collection_new_from_xml(RestXmlNode *root_node,
546e1e
                                                GType resource_type,
546e1e
                                                const char *resource_name,
546e1e
                                                GError **error);
546e1e
+OvirtCollection *ovirt_sub_collection_new_from_resource(OvirtResource *resource,
546e1e
+                                                        const char *href,
546e1e
+                                                        const char *collection_name,
546e1e
+                                                        GType resource_type,
546e1e
+                                                        const char *resource_name);
546e1e
 
546e1e
 G_END_DECLS
546e1e
 
546e1e
diff --git a/govirt/ovirt-collection.c b/govirt/ovirt-collection.c
546e1e
index a3b0f3f..6ec1c6e 100644
546e1e
--- a/govirt/ovirt-collection.c
546e1e
+++ b/govirt/ovirt-collection.c
546e1e
@@ -344,6 +344,21 @@ OvirtCollection *ovirt_collection_new_from_xml(RestXmlNode *root_node,
546e1e
 }
546e1e
 
546e1e
 
546e1e
+OvirtCollection *ovirt_sub_collection_new_from_resource(OvirtResource *resource,
546e1e
+                                                        const char *href,
546e1e
+                                                        const char *collection_name,
546e1e
+                                                        GType resource_type,
546e1e
+                                                        const char *resource_name)
546e1e
+{
546e1e
+    const char *link = ovirt_resource_get_sub_collection(resource, href);
546e1e
+
546e1e
+    if (link == NULL)
546e1e
+        return NULL;
546e1e
+
546e1e
+    return ovirt_collection_new(link, collection_name, resource_type, resource_name);
546e1e
+}
546e1e
+
546e1e
+
546e1e
 /**
546e1e
  * ovirt_collection_fetch:
546e1e
  * @collection: a #OvirtCollection
546e1e
diff --git a/govirt/ovirt-storage-domain.c b/govirt/ovirt-storage-domain.c
546e1e
index 0582203..38c4a62 100644
546e1e
--- a/govirt/ovirt-storage-domain.c
546e1e
+++ b/govirt/ovirt-storage-domain.c
546e1e
@@ -317,20 +317,14 @@ OvirtStorageDomain *ovirt_storage_domain_new(void)
546e1e
  */
546e1e
 OvirtCollection *ovirt_storage_domain_get_files(OvirtStorageDomain *domain)
546e1e
 {
546e1e
-    const char *href;
546e1e
-
546e1e
     g_return_val_if_fail(OVIRT_IS_STORAGE_DOMAIN(domain), NULL);
546e1e
 
546e1e
-    if (domain->priv->files != NULL)
546e1e
-        return domain->priv->files;
546e1e
-
546e1e
-    href = ovirt_resource_get_sub_collection(OVIRT_RESOURCE(domain), "files");
546e1e
-    if (href == NULL)
546e1e
-        return NULL;
546e1e
-
546e1e
-    domain->priv->files = ovirt_collection_new(href, "files",
546e1e
-                                               OVIRT_TYPE_RESOURCE,
546e1e
-                                               "file");
546e1e
+    if (domain->priv->files == NULL)
546e1e
+        domain->priv->files = ovirt_sub_collection_new_from_resource(OVIRT_RESOURCE(domain),
546e1e
+                                                                     "files",
546e1e
+                                                                     "files",
546e1e
+                                                                     OVIRT_TYPE_RESOURCE,
546e1e
+                                                                     "file");
546e1e
 
546e1e
     return domain->priv->files;
546e1e
 }
546e1e
diff --git a/govirt/ovirt-vm.c b/govirt/ovirt-vm.c
546e1e
index 9a07c2f..3d64649 100644
546e1e
--- a/govirt/ovirt-vm.c
546e1e
+++ b/govirt/ovirt-vm.c
546e1e
@@ -329,20 +329,14 @@ gboolean ovirt_vm_refresh_finish(OvirtVm *vm,
546e1e
  */
546e1e
 OvirtCollection *ovirt_vm_get_cdroms(OvirtVm *vm)
546e1e
 {
546e1e
-    const char *href;
546e1e
-
546e1e
     g_return_val_if_fail(OVIRT_IS_VM(vm), NULL);
546e1e
 
546e1e
-    if (vm->priv->cdroms != NULL)
546e1e
-        return vm->priv->cdroms;
546e1e
-
546e1e
-    href = ovirt_resource_get_sub_collection(OVIRT_RESOURCE(vm), "cdroms");
546e1e
-    if (href == NULL)
546e1e
-        return NULL;
546e1e
-
546e1e
-    vm->priv->cdroms =  ovirt_collection_new(href, "cdroms",
546e1e
-                                             OVIRT_TYPE_CDROM,
546e1e
-                                             "cdrom");
546e1e
+    if (vm->priv->cdroms == NULL)
546e1e
+        vm->priv->cdroms = ovirt_sub_collection_new_from_resource(OVIRT_RESOURCE(vm),
546e1e
+                                                                  "cdroms",
546e1e
+                                                                  "cdroms",
546e1e
+                                                                  OVIRT_TYPE_CDROM,
546e1e
+                                                                  "cdrom");
546e1e
 
546e1e
     return vm->priv->cdroms;
546e1e
 }