[libgovirt] Add sample code which lists VMs/VM pools



commit 7c6c26cf0609205b1b3756dee4f0932c7b6f82a7
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Wed Dec 4 15:33:45 2013 +0100

    Add sample code which lists VMs/VM pools
    
    There are both sync and async tests showing how to get a list of VMs/VM
    pools from an oVirt instance.

 examples/Makefile.am      |   26 ++++++-
 examples/list-vms-async.c |  210 +++++++++++++++++++++++++++++++++++++++++++++
 examples/list-vms.c       |  147 +++++++++++++++++++++++++++++++
 3 files changed, 382 insertions(+), 1 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index e47f15e..959a4da 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,4 +1,4 @@
-noinst_PROGRAMS = async-test sync-test
+noinst_PROGRAMS = async-test sync-test list-vms list-vms-async
 
 EXTRA_DIST =
 
@@ -25,3 +25,27 @@ async_test_LDADD = \
                ../govirt/libgovirt.la \
                $(GLIB_LIBS) \
                $(REST_LIBS)
+
+list_vms_CFLAGS = \
+               $(WARNINGFLAGS_C) \
+               $(GLIB_CFLAGS) \
+               $(REST_CFLAGS) \
+               -I$(top_srcdir)
+list_vms_SOURCES = \
+               list-vms.c
+list_vms_LDADD = \
+               ../govirt/libgovirt.la \
+               $(GLIB_LIBS) \
+               $(REST_LIBS)
+
+list_vms_async_CFLAGS = \
+               $(WARNINGFLAGS_C) \
+               $(GLIB_CFLAGS) \
+               $(REST_CFLAGS) \
+               -I$(top_srcdir)
+list_vms_async_SOURCES = \
+               list-vms-async.c
+list_vms_async_LDADD = \
+               ../govirt/libgovirt.la \
+               $(GLIB_LIBS) \
+               $(REST_LIBS)
diff --git a/examples/list-vms-async.c b/examples/list-vms-async.c
new file mode 100644
index 0000000..717f734
--- /dev/null
+++ b/examples/list-vms-async.c
@@ -0,0 +1,210 @@
+/* Copyright 2013 Red Hat, Inc. and/or its affiliates.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include <stdlib.h>
+
+#include <govirt/govirt.h>
+#include "govirt/glib-compat.h"
+
+static GMainLoop *main_loop;
+
+typedef struct {
+    const char *uri;
+    OvirtProxy *proxy;
+    OvirtApi *api;
+} AsyncData;
+
+
+static gboolean
+authenticate_cb(RestProxy *proxy, G_GNUC_UNUSED RestProxyAuth *auth,
+                gboolean retrying, gpointer user_data)
+{
+    if (retrying)
+        return FALSE;
+
+    g_debug("setting username to %s", g_getenv("LIBGOVIRT_TEST_USERNAME"));
+    g_object_set(G_OBJECT(proxy),
+                 "username", g_getenv("LIBGOVIRT_TEST_USERNAME"),
+                 "password", g_getenv("LIBGOVIRT_TEST_PASSWORD"),
+                 NULL);
+    return TRUE;
+}
+
+
+static void dump_resource(gpointer key, gpointer value, gpointer user_data)
+{
+    g_assert(OVIRT_IS_RESOURCE(value));
+
+    g_print("\t%s\n", (char *)key);
+}
+
+
+static void dump_collection(OvirtCollection *collection)
+{
+    GHashTable *resources;
+
+    resources = ovirt_collection_get_resources(collection);
+    g_hash_table_foreach(resources, dump_resource, NULL);
+}
+
+
+
+static void pools_fetched_cb(GObject *source_object,
+                             GAsyncResult *result,
+                             gpointer user_data)
+{
+    GError *error = NULL;
+    OvirtCollection *pools;
+
+    g_debug("Fetched pools");
+    pools = OVIRT_COLLECTION(source_object);
+    ovirt_collection_fetch_finish(pools, result, &error);
+    if (error != NULL) {
+        g_debug("failed to fetch pools: %s", error->message);
+        g_main_loop_quit(main_loop);
+        return;
+    }
+    g_print("Pools:\n");
+    dump_collection(pools);
+    g_main_loop_quit(main_loop);
+}
+
+
+static void vms_fetched_cb(GObject *source_object,
+                           GAsyncResult *result,
+                           gpointer user_data)
+{
+    GError *error = NULL;
+    OvirtCollection *vms;
+    OvirtCollection *pools;
+    AsyncData *data = (AsyncData *)user_data;
+
+    g_debug("Fetched VMs");
+    vms = OVIRT_COLLECTION(source_object);
+    ovirt_collection_fetch_finish(vms, result, &error);
+    if (error != NULL) {
+        g_debug("failed to fetch VMs: %s", error->message);
+        g_main_loop_quit(main_loop);
+        return;
+    }
+    g_print("VMs:\n");
+    dump_collection(vms);
+
+    pools = ovirt_api_get_vm_pools(data->api);
+    ovirt_collection_fetch_async(pools, data->proxy, NULL, pools_fetched_cb, user_data);
+}
+
+
+static void api_fetched_cb(GObject *source_object,
+                           GAsyncResult *result,
+                           gpointer user_data)
+{
+    GError *error = NULL;
+    OvirtProxy *proxy;
+    OvirtCollection *vms;
+    AsyncData *data = (AsyncData *)user_data;
+
+    g_debug("Fetched API");
+    proxy = OVIRT_PROXY(source_object);
+    data->api = ovirt_proxy_fetch_api_finish(proxy, result, &error);
+    if (error != NULL) {
+        g_debug("failed to fetch api: %s", error->message);
+        g_main_loop_quit(main_loop);
+        return;
+    }
+
+    vms = ovirt_api_get_vms(data->api);
+    g_assert(vms != NULL);
+
+    ovirt_collection_fetch_async(vms, proxy, NULL, vms_fetched_cb, user_data);
+}
+
+static void fetched_ca_cert_cb(GObject *source_object,
+                               GAsyncResult *result,
+                               gpointer user_data)
+{
+    GError *error = NULL;
+    GByteArray *ca_cert;
+    OvirtProxy *proxy;
+
+    g_debug("Fetched CA certificate");
+    proxy = OVIRT_PROXY(source_object);
+    ca_cert = ovirt_proxy_fetch_ca_certificate_finish(proxy, result, &error);
+    if (error != NULL) {
+        g_debug("failed to get CA certificate: %s", error->message);
+        g_main_loop_quit(main_loop);
+        return;
+    }
+    if (ca_cert == NULL) {
+        g_debug("failed to get CA certificate");
+        g_main_loop_quit(main_loop);
+        return;
+    }
+    g_print("\tCA certificate: %p\n", ca_cert);
+    ovirt_proxy_fetch_api_async(proxy, NULL, api_fetched_cb, user_data);
+}
+
+static gboolean start(gpointer user_data)
+{
+    AsyncData *data = (AsyncData *)user_data;
+
+    data->proxy = ovirt_proxy_new (data->uri);
+    if (data->proxy == NULL)
+        return -1;
+
+    g_signal_connect(G_OBJECT(data->proxy), "authenticate",
+                     G_CALLBACK(authenticate_cb), NULL);
+
+    ovirt_proxy_fetch_ca_certificate_async(data->proxy, NULL,
+                                           fetched_ca_cert_cb,
+                                           data);
+
+    return G_SOURCE_REMOVE;
+}
+
+int main(int argc, char **argv)
+{
+    AsyncData *data;
+    g_type_init();
+
+    if (argc != 2) {
+        g_print("Usage: %s URI\n", argv[0]);
+        exit(1);
+    }
+
+    data = g_new0(AsyncData, 1);
+    data->uri = argv[1];
+
+    g_idle_add(start, data);
+    main_loop = g_main_loop_new(NULL, FALSE);
+    g_main_loop_run(main_loop);
+
+    if (data->api != NULL) {
+        g_object_unref(data->api);
+    }
+    if (data->proxy != NULL) {
+        g_object_unref(data->proxy);
+    }
+    g_free(data);
+
+    return 0;
+}
diff --git a/examples/list-vms.c b/examples/list-vms.c
new file mode 100644
index 0000000..58f7722
--- /dev/null
+++ b/examples/list-vms.c
@@ -0,0 +1,147 @@
+/* Copyright 2013 Red Hat, Inc. and/or its affiliates.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include <stdlib.h>
+
+#include <govirt/govirt.h>
+#include "govirt/glib-compat.h"
+
+static gboolean
+authenticate_cb(RestProxy *proxy, G_GNUC_UNUSED RestProxyAuth *auth,
+                G_GNUC_UNUSED gboolean retrying, gpointer user_data)
+{
+    if (retrying)
+        return FALSE;
+
+    g_object_set(G_OBJECT(proxy),
+                 "username", g_getenv("LIBGOVIRT_TEST_USERNAME"),
+                 "password", g_getenv("LIBGOVIRT_TEST_PASSWORD"),
+                 NULL);
+    return TRUE;
+}
+
+static void dump_resource(gpointer key, gpointer value, gpointer user_data)
+{
+    g_assert(OVIRT_IS_RESOURCE(value));
+
+    g_print("\t%s\n", (char *)key);
+}
+
+
+static void dump_collection(OvirtCollection *collection)
+{
+    GHashTable *resources;
+
+    resources = ovirt_collection_get_resources(collection);
+    g_hash_table_foreach(resources, dump_resource, NULL);
+}
+
+
+static void dump_vms(OvirtApi *api, OvirtProxy *proxy)
+{
+    OvirtCollection *vms;
+    GError *error = NULL;
+
+    vms = ovirt_api_get_vms(api);
+    g_assert(vms != NULL);
+    ovirt_collection_fetch(vms, proxy, &error);
+    if (error != NULL) {
+        g_debug("failed to fetch VMs: %s", error->message);
+        return;
+    }
+
+    g_print("VMs:\n");
+    dump_collection(vms);
+}
+
+
+static void dump_vm_pools(OvirtApi *api, OvirtProxy *proxy)
+{
+    OvirtCollection *pools;
+    GError *error = NULL;
+
+    pools = ovirt_api_get_vm_pools(api);
+    g_assert(pools != NULL);
+    ovirt_collection_fetch(pools, proxy, &error);
+    if (error != NULL) {
+        g_debug("failed to fetch VM pools: %s", error->message);
+        return;
+    }
+
+    g_print("VM pools:\n");
+    dump_collection(pools);
+}
+
+
+int main(int argc, char **argv)
+{
+    OvirtApi *api;
+    OvirtProxy *proxy = NULL;
+    GError *error = NULL;
+    GByteArray *ca_cert = NULL;
+
+    g_type_init();
+
+    if (argc != 2) {
+        g_print("Usage: %s URI\n", argv[0]);
+        exit(1);
+    }
+
+    proxy = ovirt_proxy_new(argv[1]);
+    if (proxy == NULL)
+        goto error;
+
+    g_signal_connect(G_OBJECT(proxy), "authenticate",
+                     G_CALLBACK(authenticate_cb), NULL);
+
+    /* Should be using ovirt_get_option_group/ovirt_set_proxy_options
+     * instead as ovirt_proxy_fetch_ca_certificate is not checking
+     * properly the validity of https certificates
+     */
+    ovirt_proxy_fetch_ca_certificate(proxy, &error);
+    if (error != NULL) {
+        g_debug("failed to get CA certificate: %s", error->message);
+        goto error;
+    }
+    g_object_get(G_OBJECT(proxy), "ca-cert", &ca_cert, NULL);
+
+    api = ovirt_proxy_fetch_api(proxy, &error);
+    if (error != NULL) {
+        g_debug("failed to lookup %s: %s", argv[2], error->message);
+        goto error;
+    }
+    g_assert(api != NULL);
+
+    dump_vms(api, proxy);
+    dump_vm_pools(api, proxy);
+
+
+error:
+    if (ca_cert != NULL)
+        g_byte_array_unref(ca_cert);
+    if (error != NULL)
+        g_error_free(error);
+    if (proxy != NULL)
+        g_object_unref(proxy);
+
+    return 0;
+}


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