ccf8fb
From 91089ba30f2345ee14be676ca8b7465d6f29871a Mon Sep 17 00:00:00 2001
ccf8fb
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
ccf8fb
Date: Tue, 16 Jun 2020 13:37:36 -0300
ccf8fb
Subject: [PATCH] Initial support for Disks
ccf8fb
ccf8fb
Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
ccf8fb
---
ccf8fb
 govirt/Makefile.am          |   3 +
ccf8fb
 govirt/govirt-private.h     |   1 +
ccf8fb
 govirt/govirt.h             |   1 +
ccf8fb
 govirt/govirt.sym           |   6 ++
ccf8fb
 govirt/meson.build          |   3 +
ccf8fb
 govirt/ovirt-disk-private.h |  37 +++++++++
ccf8fb
 govirt/ovirt-disk.c         | 144 ++++++++++++++++++++++++++++++++++++
ccf8fb
 govirt/ovirt-disk.h         |  76 +++++++++++++++++++
ccf8fb
 govirt/ovirt-types.h        |   1 +
ccf8fb
 9 files changed, 272 insertions(+)
ccf8fb
 create mode 100644 govirt/ovirt-disk-private.h
ccf8fb
 create mode 100644 govirt/ovirt-disk.c
ccf8fb
 create mode 100644 govirt/ovirt-disk.h
ccf8fb
ccf8fb
diff --git a/govirt/Makefile.am b/govirt/Makefile.am
ccf8fb
index 0ce276d..f7ab835 100644
ccf8fb
--- a/govirt/Makefile.am
ccf8fb
+++ b/govirt/Makefile.am
ccf8fb
@@ -21,6 +21,7 @@ libgovirt_la_HEADERS =						\
ccf8fb
 	ovirt-cluster.h						\
ccf8fb
 	ovirt-collection.h					\
ccf8fb
 	ovirt-data-center.h					\
ccf8fb
+	ovirt-disk.h						\
ccf8fb
 	ovirt-error.h						\
ccf8fb
 	ovirt-host.h						\
ccf8fb
 	ovirt-options.h						\
ccf8fb
@@ -41,6 +42,7 @@ noinst_HEADERS =						\
ccf8fb
 	ovirt-cluster-private.h					\
ccf8fb
 	ovirt-collection-private.h				\
ccf8fb
 	ovirt-data-center-private.h				\
ccf8fb
+	ovirt-disk-private.h					\
ccf8fb
 	ovirt-host-private.h					\
ccf8fb
 	ovirt-proxy-private.h					\
ccf8fb
 	ovirt-resource-private.h				\
ccf8fb
@@ -58,6 +60,7 @@ libgovirt_la_SOURCES =						\
ccf8fb
 	ovirt-cluster.c						\
ccf8fb
 	ovirt-collection.c					\
ccf8fb
 	ovirt-data-center.c					\
ccf8fb
+	ovirt-disk.c						\
ccf8fb
 	ovirt-error.c						\
ccf8fb
 	ovirt-host.c						\
ccf8fb
 	ovirt-options.c						\
ccf8fb
diff --git a/govirt/govirt-private.h b/govirt/govirt-private.h
ccf8fb
index 2c0e30f..f5cf721 100644
ccf8fb
--- a/govirt/govirt-private.h
ccf8fb
+++ b/govirt/govirt-private.h
ccf8fb
@@ -27,6 +27,7 @@
ccf8fb
 #include <govirt/ovirt-cluster-private.h>
ccf8fb
 #include <govirt/ovirt-collection-private.h>
ccf8fb
 #include <govirt/ovirt-data-center-private.h>
ccf8fb
+#include <govirt/ovirt-disk-private.h>
ccf8fb
 #include <govirt/ovirt-enum-types-private.h>
ccf8fb
 #include <govirt/ovirt-host-private.h>
ccf8fb
 #include <govirt/ovirt-proxy-private.h>
ccf8fb
diff --git a/govirt/govirt.h b/govirt/govirt.h
ccf8fb
index 9c92318..8eab984 100644
ccf8fb
--- a/govirt/govirt.h
ccf8fb
+++ b/govirt/govirt.h
ccf8fb
@@ -28,6 +28,7 @@
ccf8fb
 #include <govirt/ovirt-cluster.h>
ccf8fb
 #include <govirt/ovirt-collection.h>
ccf8fb
 #include <govirt/ovirt-data-center.h>
ccf8fb
+#include <govirt/ovirt-disk.h>
ccf8fb
 #include <govirt/ovirt-error.h>
ccf8fb
 #include <govirt/ovirt-host.h>
ccf8fb
 #include <govirt/ovirt-options.h>
ccf8fb
diff --git a/govirt/govirt.sym b/govirt/govirt.sym
ccf8fb
index 0d488e6..1cc2a8e 100644
ccf8fb
--- a/govirt/govirt.sym
ccf8fb
+++ b/govirt/govirt.sym
ccf8fb
@@ -141,4 +141,10 @@ GOVIRT_0.4.0 {
ccf8fb
         ovirt_vm_get_host;
ccf8fb
         ovirt_vm_get_cluster;
ccf8fb
 } GOVIRT_0.3.4;
ccf8fb
+
ccf8fb
+GOVIRT_0.4.1 {
ccf8fb
+        ovirt_disk_get_type;
ccf8fb
+        ovirt_disk_content_type_get_type;
ccf8fb
+        ovirt_disk_new;
ccf8fb
+} GOVIRT_0.4.0;
ccf8fb
 # .... define new API here using predicted next version number ....
ccf8fb
diff --git a/govirt/meson.build b/govirt/meson.build
ccf8fb
index bec7781..778e5c0 100644
ccf8fb
--- a/govirt/meson.build
ccf8fb
+++ b/govirt/meson.build
ccf8fb
@@ -7,6 +7,7 @@ govirt_headers = [
ccf8fb
   'ovirt-cluster.h',
ccf8fb
   'ovirt-collection.h',
ccf8fb
   'ovirt-data-center.h',
ccf8fb
+  'ovirt-disk.h',
ccf8fb
   'ovirt-error.h',
ccf8fb
   'ovirt-host.h',
ccf8fb
   'ovirt-options.h',
ccf8fb
@@ -34,6 +35,7 @@ govirt_private_headers = [
ccf8fb
   'ovirt-cluster-private.h',
ccf8fb
   'ovirt-collection-private.h',
ccf8fb
   'ovirt-data-center-private.h',
ccf8fb
+  'ovirt-disk-private.h',
ccf8fb
   'ovirt-host-private.h',
ccf8fb
   'ovirt-proxy-private.h',
ccf8fb
   'ovirt-resource-private.h',
ccf8fb
@@ -58,6 +60,7 @@ govirt_sources = [
ccf8fb
   'ovirt-cluster.c',
ccf8fb
   'ovirt-collection.c',
ccf8fb
   'ovirt-data-center.c',
ccf8fb
+  'ovirt-disk.c',
ccf8fb
   'ovirt-error.c',
ccf8fb
   'ovirt-host.c',
ccf8fb
   'ovirt-options.c',
ccf8fb
diff --git a/govirt/ovirt-disk-private.h b/govirt/ovirt-disk-private.h
ccf8fb
new file mode 100644
ccf8fb
index 0000000..d9fff3f
ccf8fb
--- /dev/null
ccf8fb
+++ b/govirt/ovirt-disk-private.h
ccf8fb
@@ -0,0 +1,37 @@
ccf8fb
+/*
ccf8fb
+ * ovirt-disk-private.h: oVirt disk resource
ccf8fb
+ *
ccf8fb
+ * Copyright (C) 2020 Red Hat, Inc.
ccf8fb
+ *
ccf8fb
+ * This library is free software; you can redistribute it and/or
ccf8fb
+ * modify it under the terms of the GNU Lesser General Public
ccf8fb
+ * License as published by the Free Software Foundation; either
ccf8fb
+ * version 2.1 of the License, or (at your option) any later version.
ccf8fb
+ *
ccf8fb
+ * This library is distributed in the hope that it will be useful,
ccf8fb
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ccf8fb
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ccf8fb
+ * Lesser General Public License for more details.
ccf8fb
+ *
ccf8fb
+ * You should have received a copy of the GNU Lesser General Public
ccf8fb
+ * License along with this library. If not, see
ccf8fb
+ * <http://www.gnu.org/licenses/>.
ccf8fb
+ *
ccf8fb
+ * Author: Eduardo Lima (Etrunko) <etrunko@redhat.com>
ccf8fb
+ */
ccf8fb
+#ifndef __OVIRT_DISK_PRIVATE_H__
ccf8fb
+#define __OVIRT_DISK_PRIVATE_H__
ccf8fb
+
ccf8fb
+#include <govirt/ovirt-disk.h>
ccf8fb
+#include <rest/rest-xml-node.h>
ccf8fb
+
ccf8fb
+G_BEGIN_DECLS
ccf8fb
+
ccf8fb
+OvirtDisk *ovirt_disk_new_from_id(const char *id,
ccf8fb
+                                  const char *href);
ccf8fb
+OvirtDisk *ovirt_disk_new_from_xml(RestXmlNode *node,
ccf8fb
+                                   GError **error);
ccf8fb
+
ccf8fb
+G_END_DECLS
ccf8fb
+
ccf8fb
+#endif /* __OVIRT_DISK_PRIVATE_H__ */
ccf8fb
diff --git a/govirt/ovirt-disk.c b/govirt/ovirt-disk.c
ccf8fb
new file mode 100644
ccf8fb
index 0000000..9242c4c
ccf8fb
--- /dev/null
ccf8fb
+++ b/govirt/ovirt-disk.c
ccf8fb
@@ -0,0 +1,144 @@
ccf8fb
+/*
ccf8fb
+ * ovirt-disk.c: oVirt disk handling
ccf8fb
+ *
ccf8fb
+ * Copyright (C) 2020 Red Hat, Inc.
ccf8fb
+ *
ccf8fb
+ * This library is free software; you can redistribute it and/or
ccf8fb
+ * modify it under the terms of the GNU Lesser General Public
ccf8fb
+ * License as published by the Free Software Foundation; either
ccf8fb
+ * version 2.1 of the License, or (at your option) any later version.
ccf8fb
+ *
ccf8fb
+ * This library is distributed in the hope that it will be useful,
ccf8fb
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ccf8fb
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ccf8fb
+ * Lesser General Public License for more details.
ccf8fb
+ *
ccf8fb
+ * You should have received a copy of the GNU Lesser General Public
ccf8fb
+ * License along with this library. If not, see
ccf8fb
+ * <http://www.gnu.org/licenses/>.
ccf8fb
+ *
ccf8fb
+ * Author: Eduardo Lima (Etrunko) <etrunko@redhat.com>
ccf8fb
+ */
ccf8fb
+
ccf8fb
+#include <config.h>
ccf8fb
+#include "ovirt-enum-types.h"
ccf8fb
+#include "ovirt-disk.h"
ccf8fb
+#include "govirt-private.h"
ccf8fb
+
ccf8fb
+struct _OvirtDiskPrivate {
ccf8fb
+    OvirtDiskContentType content_type;
ccf8fb
+};
ccf8fb
+
ccf8fb
+G_DEFINE_TYPE_WITH_PRIVATE(OvirtDisk, ovirt_disk, OVIRT_TYPE_RESOURCE);
ccf8fb
+
ccf8fb
+enum {
ccf8fb
+    PROP_0,
ccf8fb
+    PROP_CONTENT_TYPE,
ccf8fb
+};
ccf8fb
+
ccf8fb
+static void ovirt_disk_get_property(GObject *object,
ccf8fb
+                                    guint prop_id,
ccf8fb
+                                    GValue *value,
ccf8fb
+                                    GParamSpec *pspec)
ccf8fb
+{
ccf8fb
+    OvirtDisk *disk = OVIRT_DISK(object);
ccf8fb
+
ccf8fb
+    switch (prop_id) {
ccf8fb
+    case PROP_CONTENT_TYPE:
ccf8fb
+        g_value_set_enum(value, disk->priv->content_type);
ccf8fb
+        break;
ccf8fb
+    default:
ccf8fb
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
ccf8fb
+        break;
ccf8fb
+    }
ccf8fb
+}
ccf8fb
+
ccf8fb
+static void ovirt_disk_set_property(GObject *object,
ccf8fb
+                                    guint prop_id,
ccf8fb
+                                    const GValue *value,
ccf8fb
+                                    GParamSpec *pspec)
ccf8fb
+{
ccf8fb
+    OvirtDisk *disk = OVIRT_DISK(object);
ccf8fb
+
ccf8fb
+    switch (prop_id) {
ccf8fb
+    case PROP_CONTENT_TYPE:
ccf8fb
+        disk->priv->content_type = g_value_get_enum(value);
ccf8fb
+        break;
ccf8fb
+    default:
ccf8fb
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
ccf8fb
+        break;
ccf8fb
+    }
ccf8fb
+}
ccf8fb
+
ccf8fb
+static gboolean ovirt_disk_init_from_xml(OvirtResource *resource,
ccf8fb
+                                         RestXmlNode *node,
ccf8fb
+                                         GError **error)
ccf8fb
+{
ccf8fb
+    gboolean parsed_ok;
ccf8fb
+    OvirtResourceClass *parent_class;
ccf8fb
+    OvirtXmlElement disk_elements[] = {
ccf8fb
+        { .prop_name = "content_type",
ccf8fb
+          .xml_path = "content_type",
ccf8fb
+        },
ccf8fb
+        { NULL , }
ccf8fb
+    };
ccf8fb
+
ccf8fb
+    parsed_ok = ovirt_rest_xml_node_parse(node, G_OBJECT(resource), disk_elements);
ccf8fb
+    if (!parsed_ok) {
ccf8fb
+        return FALSE;
ccf8fb
+    }
ccf8fb
+    parent_class = OVIRT_RESOURCE_CLASS(ovirt_disk_parent_class);
ccf8fb
+
ccf8fb
+    return parent_class->init_from_xml(resource, node, error);
ccf8fb
+}
ccf8fb
+
ccf8fb
+static void ovirt_disk_class_init(OvirtDiskClass *klass)
ccf8fb
+{
ccf8fb
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
ccf8fb
+    OvirtResourceClass *resource_class = OVIRT_RESOURCE_CLASS(klass);
ccf8fb
+    GParamSpec *param_spec;
ccf8fb
+
ccf8fb
+    resource_class->init_from_xml = ovirt_disk_init_from_xml;
ccf8fb
+
ccf8fb
+    object_class->get_property = ovirt_disk_get_property;
ccf8fb
+    object_class->set_property = ovirt_disk_set_property;
ccf8fb
+
ccf8fb
+    param_spec = g_param_spec_enum("type",
ccf8fb
+                                   "Content Type",
ccf8fb
+                                   "The actual content residing on the disk",
ccf8fb
+                                   OVIRT_TYPE_DISK_CONTENT_TYPE,
ccf8fb
+                                   OVIRT_DISK_CONTENT_TYPE_DATA,
ccf8fb
+                                   G_PARAM_READWRITE |
ccf8fb
+                                   G_PARAM_STATIC_STRINGS);
ccf8fb
+    g_object_class_install_property(object_class,
ccf8fb
+                                    PROP_CONTENT_TYPE,
ccf8fb
+                                    param_spec);
ccf8fb
+}
ccf8fb
+
ccf8fb
+
ccf8fb
+static void ovirt_disk_init(OvirtDisk *disk)
ccf8fb
+{
ccf8fb
+    disk->priv = ovirt_disk_get_instance_private(disk);
ccf8fb
+}
ccf8fb
+
ccf8fb
+G_GNUC_INTERNAL
ccf8fb
+OvirtDisk *ovirt_disk_new_from_id(const char *id,
ccf8fb
+                                  const char *href)
ccf8fb
+{
ccf8fb
+    OvirtResource *disk = ovirt_resource_new_from_id(OVIRT_TYPE_DISK, id, href);
ccf8fb
+    return OVIRT_DISK(disk);
ccf8fb
+}
ccf8fb
+
ccf8fb
+G_GNUC_INTERNAL
ccf8fb
+OvirtDisk *ovirt_disk_new_from_xml(RestXmlNode *node,
ccf8fb
+                                   GError **error)
ccf8fb
+{
ccf8fb
+    OvirtResource *disk = ovirt_resource_new_from_xml(OVIRT_TYPE_DISK, node, error);
ccf8fb
+    return OVIRT_DISK(disk);
ccf8fb
+}
ccf8fb
+
ccf8fb
+OvirtDisk *ovirt_disk_new(void)
ccf8fb
+{
ccf8fb
+    OvirtResource *disk = ovirt_resource_new(OVIRT_TYPE_DISK);
ccf8fb
+    return OVIRT_DISK(disk);
ccf8fb
+}
ccf8fb
diff --git a/govirt/ovirt-disk.h b/govirt/ovirt-disk.h
ccf8fb
new file mode 100644
ccf8fb
index 0000000..a0bdd5a
ccf8fb
--- /dev/null
ccf8fb
+++ b/govirt/ovirt-disk.h
ccf8fb
@@ -0,0 +1,76 @@
ccf8fb
+/*
ccf8fb
+ * ovirt-disk.h: oVirt disk resource
ccf8fb
+ *
ccf8fb
+ * Copyright (C) 2020 Red Hat, Inc.
ccf8fb
+ *
ccf8fb
+ * This library is free software; you can redistribute it and/or
ccf8fb
+ * modify it under the terms of the GNU Lesser General Public
ccf8fb
+ * License as published by the Free Software Foundation; either
ccf8fb
+ * version 2.1 of the License, or (at your option) any later version.
ccf8fb
+ *
ccf8fb
+ * This library is distributed in the hope that it will be useful,
ccf8fb
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ccf8fb
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ccf8fb
+ * Lesser General Public License for more details.
ccf8fb
+ *
ccf8fb
+ * You should have received a copy of the GNU Lesser General Public
ccf8fb
+ * License along with this library. If not, see
ccf8fb
+ * <http://www.gnu.org/licenses/>.
ccf8fb
+ *
ccf8fb
+ * Author: Eduardo Lima (Etrunko) <etrunko@redhat.com>
ccf8fb
+ */
ccf8fb
+#ifndef __OVIRT_DISK_H__
ccf8fb
+#define __OVIRT_DISK_H__
ccf8fb
+
ccf8fb
+#include <gio/gio.h>
ccf8fb
+#include <glib-object.h>
ccf8fb
+#include <govirt/ovirt-resource.h>
ccf8fb
+#include <govirt/ovirt-types.h>
ccf8fb
+
ccf8fb
+G_BEGIN_DECLS
ccf8fb
+
ccf8fb
+#define OVIRT_TYPE_DISK            (ovirt_disk_get_type ())
ccf8fb
+#define OVIRT_DISK(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), OVIRT_TYPE_DISK, OvirtDisk))
ccf8fb
+#define OVIRT_DISK_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), OVIRT_TYPE_DISK, OvirtDiskClass))
ccf8fb
+#define OVIRT_IS_DISK(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), OVIRT_TYPE_DISK))
ccf8fb
+#define OVIRT_IS_DISK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), OVIRT_TYPE_DISK))
ccf8fb
+#define OVIRT_DISK_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), OVIRT_TYPE_DISK, OvirtDiskClass))
ccf8fb
+
ccf8fb
+typedef enum {
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_DATA,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_HOSTED_ENGINE,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_HOSTED_ENGINE_CONFIGURATION,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_HOSTED_ENGINE_METADATA,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_HOSTED_ENGINE_SANLOCK,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_ISO,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_MEMORY_DUMP_VOLUME,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_METADATA_VOLUME,
ccf8fb
+    OVIRT_DISK_CONTENT_TYPE_OVF_STORE,
ccf8fb
+} OvirtDiskContentType;
ccf8fb
+
ccf8fb
+typedef struct _OvirtDiskPrivate OvirtDiskPrivate;
ccf8fb
+typedef struct _OvirtDiskClass OvirtDiskClass;
ccf8fb
+
ccf8fb
+struct _OvirtDisk
ccf8fb
+{
ccf8fb
+    OvirtResource parent;
ccf8fb
+
ccf8fb
+    OvirtDiskPrivate *priv;
ccf8fb
+
ccf8fb
+    /* Do not add fields to this struct */
ccf8fb
+};
ccf8fb
+
ccf8fb
+struct _OvirtDiskClass
ccf8fb
+{
ccf8fb
+    OvirtResourceClass parent_class;
ccf8fb
+
ccf8fb
+    gpointer padding[20];
ccf8fb
+};
ccf8fb
+
ccf8fb
+GType ovirt_disk_get_type(void);
ccf8fb
+
ccf8fb
+OvirtDisk *ovirt_disk_new(void);
ccf8fb
+
ccf8fb
+G_END_DECLS
ccf8fb
+
ccf8fb
+#endif /* __OVIRT_DISK_H__ */
ccf8fb
diff --git a/govirt/ovirt-types.h b/govirt/ovirt-types.h
ccf8fb
index eb85fd6..3c05d36 100644
ccf8fb
--- a/govirt/ovirt-types.h
ccf8fb
+++ b/govirt/ovirt-types.h
ccf8fb
@@ -29,6 +29,7 @@ typedef struct _OvirtApi OvirtApi;
ccf8fb
 typedef struct _OvirtCdrom OvirtCdrom;
ccf8fb
 typedef struct _OvirtCluster OvirtCluster;
ccf8fb
 typedef struct _OvirtCollection OvirtCollection;
ccf8fb
+typedef struct _OvirtDisk OvirtDisk;
ccf8fb
 typedef struct _OvirtDataCenter OvirtDataCenter;
ccf8fb
 typedef struct _OvirtHost OvirtHost;
ccf8fb
 typedef struct _OvirtProxy OvirtProxy;
ccf8fb
-- 
ccf8fb
2.26.2
ccf8fb