[PATCH libvirt-python] examples: Fix connection error handling

Philipp Hahn posted 1 patch 4 years ago
Failed in applying to current master (apply log)
examples/dominfo.py    | 5 +++--
examples/domrestore.py | 5 +++--
examples/domsave.py    | 5 +++--
examples/domstart.py   | 5 +++--
examples/esxlist.py    | 6 +++---
5 files changed, 15 insertions(+), 11 deletions(-)
[PATCH libvirt-python] examples: Fix connection error handling
Posted by Philipp Hahn 4 years ago
libvirt.open*() does not return None but raises an exception

Signed-off-by: Philipp Hahn <hahn@univention.de>
---
 examples/dominfo.py    | 5 +++--
 examples/domrestore.py | 5 +++--
 examples/domsave.py    | 5 +++--
 examples/domstart.py   | 5 +++--
 examples/esxlist.py    | 6 +++---
 5 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/examples/dominfo.py b/examples/dominfo.py
index 0a39f4c..fcfb8ed 100755
--- a/examples/dominfo.py
+++ b/examples/dominfo.py
@@ -34,8 +34,9 @@ if len(sys.argv) != 2:
 name = sys.argv[1]
 
 # Connect to libvirt
-conn = libvirt.openReadOnly(None)
-if conn is None:
+try:
+    conn = libvirt.openReadOnly(None)
+except libvirt.libvirtError:
     print('Failed to open connection to the hypervisor')
     sys.exit(1)
 
diff --git a/examples/domrestore.py b/examples/domrestore.py
index 96f4955..7fd5c8e 100755
--- a/examples/domrestore.py
+++ b/examples/domrestore.py
@@ -20,8 +20,9 @@ if len(sys.argv) != 2:
 dir = sys.argv[1]
 imgs = os.listdir(dir)
 
-conn = libvirt.open(None)
-if conn is None:
+try:
+    conn = libvirt.open(None)
+except libvirt.libvirtError:
     print('Failed to open connection to the hypervisor')
     sys.exit(1)
 
diff --git a/examples/domsave.py b/examples/domsave.py
index 4940cce..f8922d6 100755
--- a/examples/domsave.py
+++ b/examples/domsave.py
@@ -18,8 +18,9 @@ if len(sys.argv) != 2:
 
 dir = sys.argv[1]
 
-conn = libvirt.open(None)
-if conn is None:
+try:
+    conn = libvirt.open(None)
+except libvirt.libvirtError:
     print('Failed to open connection to the hypervisor')
     sys.exit(1)
 
diff --git a/examples/domstart.py b/examples/domstart.py
index 7ff6cb9..f342e95 100755
--- a/examples/domstart.py
+++ b/examples/domstart.py
@@ -32,8 +32,9 @@ if len(sys.argv) != 2:
 
 (name, xmldesc) = read_domain(sys.argv[1])
 
-conn = libvirt.open(None)
-if conn is None:
+try:
+    conn = libvirt.open(None)
+except libvirt.libvirtError:
     print('Failed to open connection to the hypervisor')
     sys.exit(1)
 
diff --git a/examples/esxlist.py b/examples/esxlist.py
index d86e064..caeced1 100755
--- a/examples/esxlist.py
+++ b/examples/esxlist.py
@@ -97,9 +97,9 @@ uri = "esx://%s/?no_verify=1" % hostname
 # in order to log into the vCenter
 auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
         request_credentials, None]
-conn = libvirt.openAuth(uri, auth, 0)
-
-if conn is None:
+try:
+    conn = libvirt.openAuth(uri, auth, 0)
+except libvirt.libvirtError:
     print("Failed to open connection to %s" % hostname)
     sys.exit(1)
 
-- 
2.20.1


Re: [PATCH libvirt-python] examples: Fix connection error handling
Posted by Daniel P. Berrangé 4 years ago
On Mon, Apr 20, 2020 at 08:05:00AM +0200, Philipp Hahn wrote:
> libvirt.open*() does not return None but raises an exception
> 
> Signed-off-by: Philipp Hahn <hahn@univention.de>
> ---
>  examples/dominfo.py    | 5 +++--
>  examples/domrestore.py | 5 +++--
>  examples/domsave.py    | 5 +++--
>  examples/domstart.py   | 5 +++--
>  examples/esxlist.py    | 6 +++---
>  5 files changed, 15 insertions(+), 11 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


and pushed to git

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|