[openstack-dev] Not able to handle : 'novaclient.exceptions.Unauthorized' exception

Arun Sharma arunain at gmail.com
Fri Dec 28 05:58:51 UTC 2012


Thanks Vish, Even i tried that too but still the same since the class name
remains the absolute path.

(Pdb) p exc.Unauthorized
<class 'lib.openstack.novaclient.exceptions.Unauthorized'>

Finally got solution here.
http://effbot.org/zone/import-string.htm

- Arun Sharma

On Thu, Dec 27, 2012 at 11:07 PM, Vishvananda Ishaya
<vishvananda at gmail.com>wrote:

> answer inline
>
> Vish
>
> On Dec 24, 2012, at 1:11 AM, Arun Sharma <arunain at gmail.com> wrote:
>
> Hello
>
> I am trying to write a wrapper class which will use python-novaclient api.
> But in wrapper class, i am not able to handle
>  'novaclient.exceptions.Unauthorized' exception class .Could anyone pl help
> me out on this.?
>
> -------------- Details -------------
>
> /root/work/ # ls -l lib/openstack/
> -rw-r--r-- 1 root root     0 Dec 24 00:44 __init__.py
> drwxr-xr-x 4 root root  4096 Dec 23 22:01 novaclient                 <<<
> copied from python-novaclient/novaclient>>>
> -rw-r--r-- 1 root root  1065 Dec 24 01:00 test.py
> -rw-r--r-- 1 root root   248 Dec 24 00:45 test_sample.py
>
> # echo $PYTHONPATH
> /root/work
>
> /root/work/# python lib/openstack/test_sample.py
> Traceback (most recent call last):
>   File "lib/openstack/test_sample.py", line 8, in <module>
>     cloud = test.Openstack(auth_url, username, password, project_id)
>   File "/root/work/lib/openstack/test.py", line 41, in __init__
>     self._authenticate()
>   File "/root/work/lib/openstack/test.py", line 54, in _authenticate
>     raise Exception("Wrong exception")
> Exception: Wrong exception
>
>
> ------------------ Here is the debug output
> ----------------------------------------------------------------------------------------
> # python -m pdb lib/openstack/test_sample.py
> > /root/work/lib/openstack/test_sample.py(1)<module>()
> -> from lib.openstack import test
> (Pdb) b lib/openstack/test.py:25
> Breakpoint 1 at /root/work/lib/openstack/test.py:25
> (Pdb) c
> > /root/work/lib/openstack/test.py(25)_authenticate()
> -> self._cs.authenticate()
> (Pdb) n
> Unauthorized: Unauthorized()
> > /root/work/lib/openstack/test.py(25)_authenticate()
> -> self._cs.authenticate()
> (Pdb) n
> > /root/work/lib/openstack/test.py(26)_authenticate()
> -> except exc.Unauthorized:
>  [MISSED TO HANDLE HERE]
> (Pdb) n
> > /root/work/lib/openstack/test.py(29)_authenticate()
> -> except exc.AuthorizationFailure:
> (Pdb) l
>  24             try:
>  25 B               self._cs.authenticate()
>  26             except exc.Unauthorized:
>  27                 print ("Gotcha: Invalid credentials for user %s" %
>  28                                          self._username)
>  29  ->         except exc.AuthorizationFailure:
>  30                 print ("Gotcha: user %s not authorized" %
> self._username)
>  31             except Exception as e:
>  32                 raise Exception("Wrong exception")
> [EOF]
> (Pdb) n
> > /root/work/lib/openstack/test.py(31)_authenticate()
> -> except Exception as e:
> (Pdb) n
> > /root/work/lib/openstack/test.py(32)_authenticate()
> -> raise Exception("Wrong exception")
> (Pdb) l
>  27                 print ("Gotcha: Invalid credentials for user %s" %
>  28                                          self._username)
>  29             except exc.AuthorizationFailure:
>  30                 print ("Gotcha: user %s not authorized" %
> self._username)
>  31             except Exception as e:
>  32  ->             raise Exception("Wrong exception")
>                        [DEFAULT EXCEPTION HANDLED WHICH I DON"T WANT]
> [EOF]
> (Pdb) p e
> Unauthorized()
> (Pdb) p type(e)
> <class 'novaclient.exceptions.Unauthorized'>
> (Pdb) p exc.Unauthorized
> <class 'lib.openstack.novaclient.exceptions.Unauthorized'>
> (Pdb)
>
>
>
> --------------------------------------------------------------------------  lib/openstack/test.py
> ----------------------------------------------------------------------------
>
> #!/usr/bin/python
>
> from novaclient import client
> from novaclient import utils
> from novaclient import exceptions as exc
>
>
> from lib.openstack.novaclient import exceptions as exc
>
>
>
> DEFAULT_OS_COMPUTE_API_VERSION = "1.1"
>
> class Openstack(object):
>     def __init__(self, auth_url=None, username=None, apikey=None,
>                  project_id=None):
>
>         self._auth_url = auth_url
>         self._username = username
>         self._apikey = apikey
>         self._project_id = project_id
>         self._cs = client.Client(DEFAULT_OS_COMPUTE_API_VERSION,
>                                  self._username, self._apikey,
>                                  self._project_id, self._auth_url)
>         self._authenticate()
>
>
>     def _authenticate(self):
>         try:
>             self._cs.authenticate()
>         except exc.Unauthorized:
>             print ("Gotcha: Invalid credentials for user %s" %
>                                      self._username)
>         except exc.AuthorizationFailure:
>             print ("Gotcha: user %s not authorized" % self._username)
>         except Exception as e:
>             raise Exception("Wrong exception")
>
>
> --------------------------------------- lib/openstack/test_sample.py
> ---------------------------------------------------------
>
> from lib.openstack import test
>
> if __name__ == '__main__':
>     auth_url = "http://xx.xx.xx.xx:5000/v2.0"
>     username = "admin"
>     password = "passwor"
>     project_id = "admin"
>     cloud = test.Openstack(auth_url, username, password, project_id)
>
>
>
>
>
>
> - What could be the cause of this problem?
>
>  _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>
>
>
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>
>


-- 
Regards
Arun Sharma
09987668884
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20121228/bb8f4c2c/attachment.html>


More information about the OpenStack-dev mailing list