1bd27f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
3e596a
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
3e596a
Date: Thu, 13 Jul 2017 18:03:11 -0300
3e596a
Subject: [PATCH] vm: Introduce ovirt_vm_get_cluster()
3e596a
3e596a
Similar to previous commit, this new function can be used to retrieve
3e596a
the cluster the virtual machine belongs to.
3e596a
3e596a
Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
3e596a
---
3e596a
 govirt/govirt.sym |  1 +
1bd27f
 govirt/ovirt-vm.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++
3e596a
 govirt/ovirt-vm.h |  1 +
3e596a
 3 files changed, 76 insertions(+)
3e596a
3e596a
diff --git a/govirt/govirt.sym b/govirt/govirt.sym
1bd27f
index 039c88b..bbfbd79 100644
3e596a
--- a/govirt/govirt.sym
3e596a
+++ b/govirt/govirt.sym
3e596a
@@ -135,6 +135,7 @@ GOVIRT_0.3.2 {
3e596a
         ovirt_host_new;
3e596a
 
3e596a
         ovirt_vm_get_host;
3e596a
+        ovirt_vm_get_cluster;
3e596a
 } GOVIRT_0.3.1;
3e596a
 
1bd27f
 GOVIRT_0.3.4 {
3e596a
diff --git a/govirt/ovirt-vm.c b/govirt/ovirt-vm.c
1bd27f
index 6016c77..70b6f3a 100644
3e596a
--- a/govirt/ovirt-vm.c
3e596a
+++ b/govirt/ovirt-vm.c
3e596a
@@ -44,6 +44,8 @@ struct _OvirtVmPrivate {
3e596a
     OvirtVmDisplay *display;
3e596a
     gchar *host_href;
3e596a
     gchar *host_id;
3e596a
+    gchar *cluster_href;
3e596a
+    gchar *cluster_id;
3e596a
 } ;
3e596a
 G_DEFINE_TYPE(OvirtVm, ovirt_vm, OVIRT_TYPE_RESOURCE);
3e596a
 
3e596a
@@ -61,6 +63,8 @@ enum {
3e596a
     PROP_DISPLAY,
3e596a
     PROP_HOST_HREF,
3e596a
     PROP_HOST_ID,
3e596a
+    PROP_CLUSTER_HREF,
3e596a
+    PROP_CLUSTER_ID,
3e596a
 };
3e596a
 
3e596a
 static char *ensure_href_from_id(const char *id,
3e596a
@@ -80,6 +84,14 @@ static const char *get_host_href(OvirtVm *vm)
3e596a
     return vm->priv->host_href;
3e596a
 }
3e596a
 
3e596a
+static const char *get_cluster_href(OvirtVm *vm)
3e596a
+{
3e596a
+    if (vm->priv->cluster_href == NULL)
3e596a
+        vm->priv->cluster_href = ensure_href_from_id(vm->priv->cluster_id, "/ovirt-engine/api/clusters");
3e596a
+
3e596a
+    return vm->priv->cluster_href;
3e596a
+}
3e596a
+
3e596a
 static void ovirt_vm_get_property(GObject *object,
3e596a
                                   guint prop_id,
3e596a
                                   GValue *value,
3e596a
@@ -100,6 +112,12 @@ static void ovirt_vm_get_property(GObject *object,
3e596a
     case PROP_HOST_ID:
3e596a
         g_value_set_string(value, vm->priv->host_id);
3e596a
         break;
3e596a
+    case PROP_CLUSTER_HREF:
3e596a
+        g_value_set_string(value, get_cluster_href(vm));
3e596a
+        break;
3e596a
+    case PROP_CLUSTER_ID:
3e596a
+        g_value_set_string(value, vm->priv->cluster_id);
3e596a
+        break;
3e596a
     default:
3e596a
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
3e596a
     }
3e596a
@@ -129,6 +147,14 @@ static void ovirt_vm_set_property(GObject *object,
3e596a
         g_free(vm->priv->host_id);
3e596a
         vm->priv->host_id = g_value_dup_string(value);
3e596a
         break;
3e596a
+    case PROP_CLUSTER_HREF:
3e596a
+        g_free(vm->priv->cluster_href);
3e596a
+        vm->priv->cluster_href = g_value_dup_string(value);
3e596a
+        break;
3e596a
+    case PROP_CLUSTER_ID:
3e596a
+        g_free(vm->priv->cluster_id);
3e596a
+        vm->priv->cluster_id = g_value_dup_string(value);
3e596a
+        break;
3e596a
     default:
3e596a
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
3e596a
     }
3e596a
@@ -143,6 +169,8 @@ static void ovirt_vm_dispose(GObject *object)
3e596a
     g_clear_object(&vm->priv->display);
3e596a
     g_clear_pointer(&vm->priv->host_href, g_free);
3e596a
     g_clear_pointer(&vm->priv->host_id, g_free);
3e596a
+    g_clear_pointer(&vm->priv->cluster_href, g_free);
3e596a
+    g_clear_pointer(&vm->priv->cluster_id, g_free);
3e596a
 
3e596a
     G_OBJECT_CLASS(ovirt_vm_parent_class)->dispose(object);
3e596a
 }
3e596a
@@ -165,6 +193,16 @@ static gboolean ovirt_vm_init_from_xml(OvirtResource *resource,
3e596a
           .xml_path = "host",
3e596a
           .xml_attr = "id",
3e596a
         },
3e596a
+        { .prop_name = "cluster-href",
3e596a
+          .type = G_TYPE_STRING,
3e596a
+          .xml_path = "cluster",
3e596a
+          .xml_attr = "href",
3e596a
+        },
3e596a
+        { .prop_name = "cluster-id",
3e596a
+          .type = G_TYPE_STRING,
3e596a
+          .xml_path = "cluster",
3e596a
+          .xml_attr = "id",
3e596a
+        },
3e596a
         { NULL, },
3e596a
     };
3e596a
 
3e596a
@@ -226,6 +264,22 @@ static void ovirt_vm_class_init(OvirtVmClass *klass)
3e596a
                                                         NULL,
3e596a
                                                         G_PARAM_READWRITE |
3e596a
                                                         G_PARAM_STATIC_STRINGS));
3e596a
+    g_object_class_install_property(object_class,
3e596a
+                                    PROP_CLUSTER_HREF,
3e596a
+                                    g_param_spec_string("cluster-href",
3e596a
+                                                        "Cluster href",
3e596a
+                                                        "Cluster href for the Virtual Machine",
3e596a
+                                                        NULL,
3e596a
+                                                        G_PARAM_READWRITE |
3e596a
+                                                        G_PARAM_STATIC_STRINGS));
3e596a
+    g_object_class_install_property(object_class,
3e596a
+                                    PROP_CLUSTER_ID,
3e596a
+                                    g_param_spec_string("cluster-id",
3e596a
+                                                        "Cluster Id",
3e596a
+                                                        "Cluster Id for the Virtual Machine",
3e596a
+                                                        NULL,
3e596a
+                                                        G_PARAM_READWRITE |
3e596a
+                                                        G_PARAM_STATIC_STRINGS));
3e596a
 }
3e596a
 
3e596a
 static void ovirt_vm_init(G_GNUC_UNUSED OvirtVm *vm)
1bd27f
@@ -432,3 +486,23 @@ OvirtHost *ovirt_vm_get_host(OvirtVm *vm)
3e596a
     g_return_val_if_fail(vm->priv->host_id != NULL, NULL);
3e596a
     return ovirt_host_new_from_id(vm->priv->host_id, get_host_href(vm));
3e596a
 }
3e596a
+
3e596a
+
3e596a
+/**
3e596a
+ * ovirt_vm_get_cluster:
3e596a
+ * @vm: a #OvirtVm
3e596a
+ *
3e596a
+ * Gets a #OvirtCluster representing the cluster the virtual machine belongs
3e596a
+ * to. This method does not initiate any network activity, the remote host must
3e596a
+ * be then be fetched using ovirt_resource_refresh() or
3e596a
+ * ovirt_resource_refresh_async().
3e596a
+ *
3e596a
+ * Return value: (transfer full): a #OvirtCluster representing cluster the @vm
3e596a
+ * belongs to.
3e596a
+ */
3e596a
+OvirtCluster *ovirt_vm_get_cluster(OvirtVm *vm)
3e596a
+{
3e596a
+    g_return_val_if_fail(OVIRT_IS_VM(vm), NULL);
3e596a
+    g_return_val_if_fail(vm->priv->cluster_id != NULL, NULL);
3e596a
+    return ovirt_cluster_new_from_id(vm->priv->cluster_id, get_cluster_href(vm));
3e596a
+}
3e596a
diff --git a/govirt/ovirt-vm.h b/govirt/ovirt-vm.h
3e596a
index e230ebb..bef4289 100644
3e596a
--- a/govirt/ovirt-vm.h
3e596a
+++ b/govirt/ovirt-vm.h
3e596a
@@ -121,6 +121,7 @@ gboolean ovirt_vm_refresh_finish(OvirtVm *vm,
3e596a
 
3e596a
 OvirtCollection *ovirt_vm_get_cdroms(OvirtVm *vm);
3e596a
 OvirtHost *ovirt_vm_get_host(OvirtVm *vm);
3e596a
+OvirtCluster *ovirt_vm_get_cluster(OvirtVm *vm);
3e596a
 
3e596a
 G_END_DECLS
3e596a