Commit 98b2414e authored by Tetsuro Nakamura's avatar Tetsuro Nakamura

Catch exceptions for session client

Exceptions from the API were not properly caught when it uses
keystone session client path. As a result, error messages in the
response body from blazar REST APIs were not displayed.

This patch fixes it creating the new SessionClient class, which
is basically the same as its parent class, but it can raise
BlazarClientException if requests to the blazar server failed.

Change-Id: I98ba3949742f2a987beb7e16400608b8ebb60065
Closes-Bug: #1777548
parent f7a8b570
......@@ -110,6 +110,25 @@ class RequestManager(object):
return resp, body
class SessionClient(adapter.LegacyJsonAdapter):
"""Manager to create request with keystoneauth1 session."""
def request(self, url, method, **kwargs):
resp, body = super(SessionClient, self).request(
url, method, raise_exc=False, **kwargs)
if resp.status_code >= 400:
if body is not None:
error_message = body.get('error_message', body)
else:
error_message = resp.text
msg = _("ERROR: {0}").format(error_message)
raise exception.BlazarClientException(msg, code=resp.status_code)
return resp, body
class BaseClientManager(object):
"""Base class for managing resources of Blazar."""
......@@ -121,7 +140,7 @@ class BaseClientManager(object):
self.session = session
if self.session:
self.request_manager = adapter.LegacyJsonAdapter(
self.request_manager = SessionClient(
session=self.session,
user_agent=self.user_agent,
**kwargs
......
......@@ -16,8 +16,6 @@
import mock
from keystoneauth1 import adapter
from blazarclient import base
from blazarclient import exception
from blazarclient import tests
......@@ -130,6 +128,36 @@ class RequestManagerTestCase(tests.TestCase):
self.manager.request, url, "POST", **kwargs)
class SessionClientTestCase(tests.TestCase):
def setUp(self):
super(SessionClientTestCase, self).setUp()
self.manager = base.SessionClient(user_agent="python-blazarclient",
session=mock.MagicMock())
@mock.patch('blazarclient.base.adapter.LegacyJsonAdapter.request')
def test_request_ok(self, m):
mock_resp = mock.Mock()
mock_resp.status_code = 200
mock_body = {"resp_key": "resp_value"}
m.return_value = (mock_resp, mock_body)
url = '/leases'
kwargs = {"body": {"req_key": "req_value"}}
resp, body = self.manager.request(url, "POST", **kwargs)
self.assertEqual((resp, body), (mock_resp, mock_body))
@mock.patch('blazarclient.base.adapter.LegacyJsonAdapter.request')
def test_request_fail(self, m):
resp = mock.Mock()
resp.status_code = 400
body = {"error message": "error"}
m.return_value = (resp, body)
url = '/leases'
kwargs = {"body": {"req_key": "req_value"}}
self.assertRaises(exception.BlazarClientException,
self.manager.request, url, "POST", **kwargs)
class BaseClientManagerTestCase(tests.TestCase):
def setUp(self):
......@@ -145,7 +173,7 @@ class BaseClientManagerTestCase(tests.TestCase):
auth_token=None,
session=self.session)
self.assertIsInstance(manager.request_manager,
adapter.LegacyJsonAdapter)
base.SessionClient)
def test_init_with_url_and_token(self):
manager = base.BaseClientManager(blazar_url=self.blazar_url,
......
---
fixes:
- |
When the blazar CLI client got an error code from the blazar server,
it didn't display error messages created in the blazar server.
Instead, it displayed `messages created in keystoneauth`_ with poor
information. See the `bug report`_ for example.
It was fixed to display original error messages which include useful
information.
.. _messages created in keystoneauth: https://github.com/openstack/keystoneauth/blob/master/keystoneauth1/exceptions/http.py
.. _bug report: https://bugs.launchpad.net/blazar/+bug/1777548
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment