[libgovirt] Add OvirtResourceRestCall class



commit 7d3dd05f2ce6f4052040cf59fdc37923ccbfe882
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Thu Aug 29 20:56:04 2013 +0200

    Add OvirtResourceRestCall class
    
    This is needed to generate the body of the XML REST call when
    we want to update an existing resource as this is quite different
    from how we generate the body content for actions.

 govirt/Makefile.am                |    2 +
 govirt/govirt-private.h           |    1 +
 govirt/ovirt-resource-rest-call.c |  162 +++++++++++++++++++++++++++++++++++++
 govirt/ovirt-resource-rest-call.h |   63 ++++++++++++++
 4 files changed, 228 insertions(+), 0 deletions(-)
---
diff --git a/govirt/Makefile.am b/govirt/Makefile.am
index 0c89ec1..7a84fbf 100644
--- a/govirt/Makefile.am
+++ b/govirt/Makefile.am
@@ -36,6 +36,7 @@ noinst_HEADERS =                                              \
        ovirt-proxy-private.h                                   \
        ovirt-resource-private.h                                \
        ovirt-rest-call.h                                       \
+       ovirt-resource-rest-call.h                              \
        ovirt-storage-domain-private.h                          \
        ovirt-utils.h                                           \
        ovirt-vm-private.h                                      \
@@ -51,6 +52,7 @@ libgovirt_la_SOURCES =                                                \
        ovirt-proxy-deprecated.c                                \
        ovirt-resource.c                                        \
        ovirt-rest-call.c                                       \
+       ovirt-resource-rest-call.c                              \
        ovirt-storage-domain.c                                  \
        ovirt-utils.c                                           \
        ovirt-vm.c                                              \
diff --git a/govirt/govirt-private.h b/govirt/govirt-private.h
index cd6c3f7..07b5606 100644
--- a/govirt/govirt-private.h
+++ b/govirt/govirt-private.h
@@ -27,6 +27,7 @@
 #include <govirt/ovirt-enum-types-private.h>
 #include <govirt/ovirt-proxy-private.h>
 #include <govirt/ovirt-resource-private.h>
+#include <govirt/ovirt-resource-rest-call.h>
 #include <govirt/ovirt-rest-call.h>
 #include <govirt/ovirt-storage-domain-private.h>
 #include <govirt/ovirt-utils.h>
diff --git a/govirt/ovirt-resource-rest-call.c b/govirt/ovirt-resource-rest-call.c
new file mode 100644
index 0000000..5f25e35
--- /dev/null
+++ b/govirt/ovirt-resource-rest-call.c
@@ -0,0 +1,162 @@
+/*
+ * ovirt-rest-call.c: oVirt librest call proxy
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Christophe Fergeau <cfergeau redhat com>
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include "ovirt-proxy.h"
+#include "ovirt-resource-private.h"
+#include "ovirt-resource-rest-call.h"
+#include "ovirt-rest-call-error.h"
+#include <rest/rest-params.h>
+
+#define OVIRT_RESOURCE_REST_CALL_GET_PRIVATE(obj)                         \
+        (G_TYPE_INSTANCE_GET_PRIVATE((obj), OVIRT_TYPE_RESOURCE_REST_CALL, OvirtResourceRestCallPrivate))
+
+struct _OvirtResourceRestCallPrivate {
+    OvirtResource *resource;
+} ;
+G_DEFINE_TYPE(OvirtResourceRestCall, ovirt_resource_rest_call, REST_TYPE_PROXY_CALL);
+
+enum {
+    PROP_0,
+    PROP_RESOURCE,
+};
+
+
+static void ovirt_resource_rest_call_get_property(GObject *object,
+                                  guint prop_id,
+                                  GValue *value,
+                                  GParamSpec *pspec)
+{
+    OvirtResourceRestCall *call = OVIRT_RESOURCE_REST_CALL(object);
+
+    switch (prop_id) {
+    case PROP_RESOURCE:
+        g_value_set_object(value, call->priv->resource);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+
+static void ovirt_resource_rest_call_set_property(GObject *object,
+                                                  guint prop_id,
+                                                  const GValue *value,
+                                                  GParamSpec *pspec)
+{
+    OvirtResourceRestCall *call = OVIRT_RESOURCE_REST_CALL(object);
+
+    switch (prop_id) {
+    case PROP_RESOURCE:
+        call->priv->resource = g_value_dup_object(value);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+static gboolean ovirt_resource_rest_call_class_serialize_params(RestProxyCall *call,
+                                                                gchar **content_type,
+                                                                gchar **content,
+                                                                gsize *content_len,
+                                                                GError **error)
+{
+    OvirtResourceRestCall *self;
+
+    g_return_val_if_fail(OVIRT_IS_RESOURCE_REST_CALL(call), FALSE);
+    g_return_val_if_fail(content_type != NULL, FALSE);
+    g_return_val_if_fail(content != NULL, FALSE);
+    g_return_val_if_fail(content_len != NULL, FALSE);
+
+    self = OVIRT_RESOURCE_REST_CALL(call);
+
+    *content_type = g_strdup("application/xml");
+    *content = ovirt_resource_to_xml(self->priv->resource);
+    *content_len = strlen(*content);
+
+    return TRUE;
+}
+
+
+static void ovirt_resource_rest_call_dispose(GObject *object)
+{
+    OvirtResourceRestCall *call = OVIRT_RESOURCE_REST_CALL(object);
+
+    g_clear_object(&call->priv->resource);
+
+    G_OBJECT_CLASS(ovirt_resource_rest_call_parent_class)->dispose(object);
+}
+
+
+static void ovirt_resource_rest_call_class_init(OvirtResourceRestCallClass *klass)
+{
+    GParamSpec *param_spec;
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof(OvirtResourceRestCallPrivate));
+
+    object_class->dispose = ovirt_resource_rest_call_dispose;
+    object_class->get_property = ovirt_resource_rest_call_get_property;
+    object_class->set_property = ovirt_resource_rest_call_set_property;
+    REST_PROXY_CALL_CLASS(klass)->serialize_params = ovirt_resource_rest_call_class_serialize_params;
+
+    param_spec = g_param_spec_object("resource",
+                                     "Resource",
+                                     "Resource being manipulated through this REST call",
+                                     OVIRT_TYPE_RESOURCE,
+                                     G_PARAM_READWRITE |
+                                     G_PARAM_CONSTRUCT_ONLY |
+                                     G_PARAM_STATIC_STRINGS);
+    g_object_class_install_property(object_class, PROP_RESOURCE, param_spec);
+}
+
+
+static void ovirt_resource_rest_call_init(OvirtResourceRestCall *call)
+{
+    call->priv = OVIRT_RESOURCE_REST_CALL_GET_PRIVATE(call);
+}
+
+OvirtResourceRestCall *ovirt_resource_rest_call_new(RestProxy *proxy,
+                                                    OvirtResource *resource)
+{
+    OvirtResourceRestCall *call;
+    gboolean admin;
+
+    g_return_val_if_fail(OVIRT_IS_PROXY(proxy), NULL);
+    call = OVIRT_RESOURCE_REST_CALL(g_object_new(OVIRT_TYPE_RESOURCE_REST_CALL,
+                                                 "proxy", proxy,
+                                                 "resource", resource,
+                                                 NULL));
+    g_return_val_if_fail(call != NULL, NULL);
+    g_object_get(G_OBJECT(proxy), "admin", &admin, NULL);
+    if (admin) {
+        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "false");
+    } else {
+        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "true");
+    }
+
+
+    return call;
+}
diff --git a/govirt/ovirt-resource-rest-call.h b/govirt/ovirt-resource-rest-call.h
new file mode 100644
index 0000000..290d7e6
--- /dev/null
+++ b/govirt/ovirt-resource-rest-call.h
@@ -0,0 +1,63 @@
+/*
+ * ovirt-rest-call.h: oVirt librest call proxy
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Christophe Fergeau <cfergeau redhat com>
+ */
+#ifndef __OVIRT_RESOURCE_REST_CALL_H__
+#define __OVIRT_RESOURCE_REST_CALL_H__
+
+#include <govirt/ovirt-resource.h>
+#include <rest/rest-proxy.h>
+
+G_BEGIN_DECLS
+
+#define OVIRT_TYPE_RESOURCE_REST_CALL            (ovirt_resource_rest_call_get_type ())
+#define OVIRT_RESOURCE_REST_CALL(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
OVIRT_TYPE_RESOURCE_REST_CALL, OvirtResourceRestCall))
+#define OVIRT_RESOURCE_REST_CALL_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), 
OVIRT_TYPE_RESOURCE_REST_CALL, OvirtResourceRestCallClass))
+#define OVIRT_IS_RESOURCE_REST_CALL(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
OVIRT_TYPE_RESOURCE_REST_CALL))
+#define OVIRT_IS_RESOURCE_REST_CALL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), 
OVIRT_TYPE_RESOURCE_REST_CALL))
+#define OVIRT_RESOURCE_REST_CALL_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), 
OVIRT_TYPE_RESOURCE_REST_CALL, OvirtResourceRestCallClass))
+
+typedef struct _OvirtResourceRestCall OvirtResourceRestCall;
+typedef struct _OvirtResourceRestCallPrivate OvirtResourceRestCallPrivate;
+typedef struct _OvirtResourceRestCallClass OvirtResourceRestCallClass;
+
+struct _OvirtResourceRestCall
+{
+    RestProxyCall parent;
+
+    OvirtResourceRestCallPrivate *priv;
+
+    /* Do not add fields to this struct */
+};
+
+struct _OvirtResourceRestCallClass
+{
+    RestProxyCallClass parent_class;
+
+    gpointer padding[20];
+};
+
+G_GNUC_INTERNAL GType ovirt_resource_rest_call_get_type(void);
+G_GNUC_INTERNAL OvirtResourceRestCall *ovirt_resource_rest_call_new(RestProxy *proxy,
+                                                                    OvirtResource *resource);
+
+G_END_DECLS
+
+#endif /* __OVIRT_RESOURCE_REST_CALL_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]