Blob Blame History Raw
From 0618c3c5baf0ef14ef62877727b797ef6bea4085 Mon Sep 17 00:00:00 2001
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
Date: Thu, 13 Jul 2017 18:03:11 -0300
Subject: [PATCH] vm: Introduce ovirt_vm_get_cluster()

Similar to previous commit, this new function can be used to retrieve
the cluster the virtual machine belongs to.

Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
---
 govirt/govirt.sym |  1 +
 govirt/ovirt-vm.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 govirt/ovirt-vm.h |  1 +
 3 files changed, 76 insertions(+)

diff --git a/govirt/govirt.sym b/govirt/govirt.sym
index 4bbd1d4..a7f7516 100644
--- a/govirt/govirt.sym
+++ b/govirt/govirt.sym
@@ -135,6 +135,7 @@ GOVIRT_0.3.2 {
         ovirt_host_new;
 
         ovirt_vm_get_host;
+        ovirt_vm_get_cluster;
 } GOVIRT_0.3.1;
 
 # .... define new API here using predicted next version number ....
diff --git a/govirt/ovirt-vm.c b/govirt/ovirt-vm.c
index 5f7fed9..e7e9ae3 100644
--- a/govirt/ovirt-vm.c
+++ b/govirt/ovirt-vm.c
@@ -44,6 +44,8 @@ struct _OvirtVmPrivate {
     OvirtVmDisplay *display;
     gchar *host_href;
     gchar *host_id;
+    gchar *cluster_href;
+    gchar *cluster_id;
 } ;
 G_DEFINE_TYPE(OvirtVm, ovirt_vm, OVIRT_TYPE_RESOURCE);
 
@@ -61,6 +63,8 @@ enum {
     PROP_DISPLAY,
     PROP_HOST_HREF,
     PROP_HOST_ID,
+    PROP_CLUSTER_HREF,
+    PROP_CLUSTER_ID,
 };
 
 static char *ensure_href_from_id(const char *id,
@@ -80,6 +84,14 @@ static const char *get_host_href(OvirtVm *vm)
     return vm->priv->host_href;
 }
 
+static const char *get_cluster_href(OvirtVm *vm)
+{
+    if (vm->priv->cluster_href == NULL)
+        vm->priv->cluster_href = ensure_href_from_id(vm->priv->cluster_id, "/ovirt-engine/api/clusters");
+
+    return vm->priv->cluster_href;
+}
+
 static void ovirt_vm_get_property(GObject *object,
                                   guint prop_id,
                                   GValue *value,
@@ -100,6 +112,12 @@ static void ovirt_vm_get_property(GObject *object,
     case PROP_HOST_ID:
         g_value_set_string(value, vm->priv->host_id);
         break;
+    case PROP_CLUSTER_HREF:
+        g_value_set_string(value, get_cluster_href(vm));
+        break;
+    case PROP_CLUSTER_ID:
+        g_value_set_string(value, vm->priv->cluster_id);
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
     }
@@ -129,6 +147,14 @@ static void ovirt_vm_set_property(GObject *object,
         g_free(vm->priv->host_id);
         vm->priv->host_id = g_value_dup_string(value);
         break;
+    case PROP_CLUSTER_HREF:
+        g_free(vm->priv->cluster_href);
+        vm->priv->cluster_href = g_value_dup_string(value);
+        break;
+    case PROP_CLUSTER_ID:
+        g_free(vm->priv->cluster_id);
+        vm->priv->cluster_id = g_value_dup_string(value);
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
     }
@@ -143,6 +169,8 @@ static void ovirt_vm_dispose(GObject *object)
     g_clear_object(&vm->priv->display);
     g_clear_pointer(&vm->priv->host_href, g_free);
     g_clear_pointer(&vm->priv->host_id, g_free);
+    g_clear_pointer(&vm->priv->cluster_href, g_free);
+    g_clear_pointer(&vm->priv->cluster_id, g_free);
 
     G_OBJECT_CLASS(ovirt_vm_parent_class)->dispose(object);
 }
@@ -165,6 +193,16 @@ static gboolean ovirt_vm_init_from_xml(OvirtResource *resource,
           .xml_path = "host",
           .xml_attr = "id",
         },
+        { .prop_name = "cluster-href",
+          .type = G_TYPE_STRING,
+          .xml_path = "cluster",
+          .xml_attr = "href",
+        },
+        { .prop_name = "cluster-id",
+          .type = G_TYPE_STRING,
+          .xml_path = "cluster",
+          .xml_attr = "id",
+        },
         { NULL, },
     };
 
@@ -226,6 +264,22 @@ static void ovirt_vm_class_init(OvirtVmClass *klass)
                                                         NULL,
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_STATIC_STRINGS));
+    g_object_class_install_property(object_class,
+                                    PROP_CLUSTER_HREF,
+                                    g_param_spec_string("cluster-href",
+                                                        "Cluster href",
+                                                        "Cluster href for the Virtual Machine",
+                                                        NULL,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
+    g_object_class_install_property(object_class,
+                                    PROP_CLUSTER_ID,
+                                    g_param_spec_string("cluster-id",
+                                                        "Cluster Id",
+                                                        "Cluster Id for the Virtual Machine",
+                                                        NULL,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
 }
 
 static void ovirt_vm_init(G_GNUC_UNUSED OvirtVm *vm)
@@ -434,3 +488,23 @@ OvirtHost *ovirt_vm_get_host(OvirtVm *vm)
     g_return_val_if_fail(vm->priv->host_id != NULL, NULL);
     return ovirt_host_new_from_id(vm->priv->host_id, get_host_href(vm));
 }
+
+
+/**
+ * ovirt_vm_get_cluster:
+ * @vm: a #OvirtVm
+ *
+ * Gets a #OvirtCluster representing the cluster the virtual machine belongs
+ * to. This method does not initiate any network activity, the remote host must
+ * be then be fetched using ovirt_resource_refresh() or
+ * ovirt_resource_refresh_async().
+ *
+ * Return value: (transfer full): a #OvirtCluster representing cluster the @vm
+ * belongs to.
+ */
+OvirtCluster *ovirt_vm_get_cluster(OvirtVm *vm)
+{
+    g_return_val_if_fail(OVIRT_IS_VM(vm), NULL);
+    g_return_val_if_fail(vm->priv->cluster_id != NULL, NULL);
+    return ovirt_cluster_new_from_id(vm->priv->cluster_id, get_cluster_href(vm));
+}
diff --git a/govirt/ovirt-vm.h b/govirt/ovirt-vm.h
index e230ebb..bef4289 100644
--- a/govirt/ovirt-vm.h
+++ b/govirt/ovirt-vm.h
@@ -121,6 +121,7 @@ gboolean ovirt_vm_refresh_finish(OvirtVm *vm,
 
 OvirtCollection *ovirt_vm_get_cdroms(OvirtVm *vm);
 OvirtHost *ovirt_vm_get_host(OvirtVm *vm);
+OvirtCluster *ovirt_vm_get_cluster(OvirtVm *vm);
 
 G_END_DECLS
 
-- 
2.13.6