Re: [PATCH v2 9/9] admin: Usage example of the new server listing API (not to be pushed)

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

 



On Fri, Aug 21, 2015 at 08:04:10PM +0200, Erik Skultety wrote:
Not to be actually pushed since majority of this example will be merged
into virt-admin once it's ready, i.e. virsh splitting series is merged,
but might be good to just see the API's working.
---
.gitignore                   |  1 +
Makefile.am                  |  2 +-
configure.ac                 |  1 +
examples/admin/Makefile.am   | 25 +++++++++++++++++
examples/admin/listservers.c | 65 ++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 93 insertions(+), 1 deletion(-)
create mode 100644 examples/admin/Makefile.am
create mode 100644 examples/admin/listservers.c

diff --git a/.gitignore b/.gitignore
index 6bd41be..d77c13e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -76,6 +76,7 @@
/docs/libvirt-refs.xml
/docs/search.php
/docs/todo.html.in
+/examples/admin/listservers
/examples/object-events/event-test
/examples/dominfo/info1
/examples/domsuspend/suspend
diff --git a/Makefile.am b/Makefile.am
index 91b943b..c14229e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,7 +24,7 @@ SUBDIRS = . gnulib/lib include src daemon tools docs gnulib/tests \
  examples/dominfo examples/domsuspend examples/apparmor \
  examples/xml/nwfilter examples/openauth examples/systemtap \
  tools/wireshark examples/dommigrate \
-  examples/lxcconvert examples/domtop
+  examples/lxcconvert examples/domtop examples/admin

ACLOCAL_AMFLAGS = -I m4

diff --git a/configure.ac b/configure.ac
index 08a0f93..1c2f6e8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2809,6 +2809,7 @@ AC_CONFIG_FILES([\
        examples/systemtap/Makefile \
        examples/xml/nwfilter/Makefile \
        examples/lxcconvert/Makefile \
+        examples/admin/Makefile \
        tools/wireshark/Makefile \
        tools/wireshark/src/Makefile])
AC_OUTPUT
diff --git a/examples/admin/Makefile.am b/examples/admin/Makefile.am
new file mode 100644
index 0000000..8373132
--- /dev/null
+++ b/examples/admin/Makefile.am
@@ -0,0 +1,25 @@
+## Copyright (C) 2005-2015 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/>.
+
+INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include
+LDADDS = $(STATIC_BINARIES) $(WARN_CFLAGS) \
+		 $(top_builddir)/src/libvirt-admin.la $(COVERAGE_LDFLAGS)
+
+noinst_PROGRAMS=listservers
+
+listservers_SOURCES=listservers.c
+listservers_LDFLAGS=
+listservers_LDADD= $(LDADDS)
diff --git a/examples/admin/listservers.c b/examples/admin/listservers.c
new file mode 100644
index 0000000..34c1f3a
--- /dev/null
+++ b/examples/admin/listservers.c
@@ -0,0 +1,65 @@
+/*
+ * listservers.c: Demo program to show listing of available servers
+ *
+ * Copyright (C) 2015 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: Erik Skultety <eskultet@xxxxxxxxxx>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <libvirt/libvirt-admin.h>
+
+int main(void)
+{
+    int ret = -1;
+    virAdmConnectPtr conn = NULL;
+    virAdmServerPtr *srvs = NULL;
+    int nsrvs = 0;
+    size_t i;
+
+    /* Connect to an admin server on a specific daemon, NULL in this case means
+     * connect to libvirtd UNIX socket
+     */
+    if (!(conn = virAdmConnectOpen(NULL, 0)))
+        goto cleanup;
+
+    /* Obtain a list of available servers on the daemon */
+    if ((nsrvs = virAdmConnectListServers(conn, &srvs, 0)) < 0) {
+        fprintf(stderr, "Failed to obtain list of available servers\n");
+        goto cleanup;
+    }
+
+    printf(" %-5s %-15s\n", "Id", "Name");
+    printf("---------------\n");
+    for (i = 0; i < nsrvs; i++)
+        printf(" %-5d %-15s\n", virAdmGetServerID(srvs[i]),
+               virAdmGetServerName(srvs[i]));
+
+    ret = nsrvs;
+
+ cleanup:
+    if (conn)
+        virAdmConnectClose(conn);
+    if (srvs) {
+        for (i = 0; i < nsrvs; i++)
+            virAdmServerFree(srvs[i]);
+        free(srvs);
+    }
+
+    return ret;
+}
--
2.4.3


Looks ok, but with the following squashed in, it's more fun to test ;)

diff --git a/examples/admin/listservers.c b/examples/admin/listservers.c
index 34c1f3adc8d1..3e96d85ace05 100644
--- a/examples/admin/listservers.c
+++ b/examples/admin/listservers.c
@@ -24,18 +24,19 @@
#include <stdlib.h>
#include <libvirt/libvirt-admin.h>

-int main(void)
+int main(int argc, char **argv)
{
    int ret = -1;
    virAdmConnectPtr conn = NULL;
    virAdmServerPtr *srvs = NULL;
    int nsrvs = 0;
+    const char *name = NULL;
    size_t i;

-    /* Connect to an admin server on a specific daemon, NULL in this case means
-     * connect to libvirtd UNIX socket
-     */
-    if (!(conn = virAdmConnectOpen(NULL, 0)))
+    if (argc > 1)
+        name = argv[1];
+
+    if (!(conn = virAdmConnectOpen(name, 0)))
        goto cleanup;

    /* Obtain a list of available servers on the daemon */
--

Martin

Attachment: signature.asc
Description: PGP signature

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]