From ec945cc31b942f04aa0f7f9b7aecec0b694fb1e5 Mon Sep 17 00:00:00 2001 From: Modulos Date: Wed, 10 May 2017 00:39:41 +0200 Subject: [PATCH] Add ConnectionRefusedError exception --- opennebula_api/models.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 53fbb6c5..21151dc8 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -30,6 +30,7 @@ class OpenNebulaManager(): settings.OPENNEBULA_USERNAME, settings.OPENNEBULA_PASSWORD ) + if not create_user: return @@ -60,15 +61,25 @@ class OpenNebulaManager(): def _get_or_create_user(self, email, password): try: - user_pool = oca.UserPool(self.oneadmin_client) - user_pool.info() + user_pool = self._get_user_pool() opennebula_user = user_pool.get_by_name(email) return opennebula_user except WrongNameError as wrong_name_err: opennebula_user = self.oneadmin_client.call(oca.User.METHODS['allocate'], email, password, 'core') + except ConnectionRefusedError: + print('Could not connect to host: {host} via protocol {protocol}'.format( + host=settings.OPENNEBULA_DOMAIN, + protocol=settings.OPENNEBULA_PROTOCOL) + ) return opennebula_user def _get_user_pool(self): - user_pool = oca.UserPool(self.oneadmin_client) - user_pool.info() + try: + user_pool = oca.UserPool(self.oneadmin_client) + user_pool.info() + except ConnectionRefusedError: + print('Could not connect to host: {host} via protocol {protocol}'.format( + host=settings.OPENNEBULA_DOMAIN, + protocol=settings.OPENNEBULA_PROTOCOL) + ) return user_pool