From b405d71a5f9562414ce6b08f7eb4556f534dd273 Mon Sep 17 00:00:00 2001 From: Mikhail Nikolaenko Date: Thu, 7 Jul 2016 19:30:59 +0300 Subject: [PATCH] Fix missing service_catalog parameter in Client object Return None if service_catalog parameter does not exist. service_catalog is the property and it is not initialized on object creation. service_catalog tries to get value from auth_ref and raises AttributeError exception if auth_ref is not initialized. It worked before we introduced sessions, because authentication happened on client instantiation. Now, when sessions are used, auth_ref is not initialized until the first request. This change adds try-catch block in service_catalog property to catch this error. Change-Id: I58eb888f0989241f9e5626564bd48d901b324d36 Closes-Bug: #1508374 --- keystoneclient/httpclient.py | 5 ++++- keystoneclient/tests/unit/v2_0/test_client.py | 8 ++++++++ keystoneclient/tests/unit/v3/test_client.py | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/keystoneclient/httpclient.py b/keystoneclient/httpclient.py index e7c2f2444..98e958fcb 100644 --- a/keystoneclient/httpclient.py +++ b/keystoneclient/httpclient.py @@ -442,7 +442,10 @@ class HTTPClient(baseclient.Client, base.BaseAuthPlugin): @property def service_catalog(self): """Return this client's service catalog.""" - return self.auth_ref.service_catalog + try: + return self.auth_ref.service_catalog + except AttributeError: + return None def has_service_catalog(self): """Return True if this client provides a service catalog.""" diff --git a/keystoneclient/tests/unit/v2_0/test_client.py b/keystoneclient/tests/unit/v2_0/test_client.py index dbd673c2c..88855880d 100644 --- a/keystoneclient/tests/unit/v2_0/test_client.py +++ b/keystoneclient/tests/unit/v2_0/test_client.py @@ -15,6 +15,7 @@ import uuid import six +from keystoneauth1 import session as auth_session from keystoneclient.auth import token_endpoint from keystoneclient import exceptions from keystoneclient import fixture @@ -211,3 +212,10 @@ class KeystoneClientTest(utils.TestCase): self.assertEqual('identity', cl._adapter.service_type) self.assertEqual((2, 0), cl._adapter.version) + + def test_empty_service_catalog_param(self): + # Client().service_catalog should return None if the client is not + # authenticated + sess = auth_session.Session() + cl = client.Client(session=sess) + self.assertEqual(None, cl.service_catalog) diff --git a/keystoneclient/tests/unit/v3/test_client.py b/keystoneclient/tests/unit/v3/test_client.py index c401ba680..42004ff10 100644 --- a/keystoneclient/tests/unit/v3/test_client.py +++ b/keystoneclient/tests/unit/v3/test_client.py @@ -16,6 +16,7 @@ import uuid import six +from keystoneauth1 import session as auth_session from keystoneclient.auth import token_endpoint from keystoneclient import exceptions from keystoneclient import session @@ -261,3 +262,10 @@ class KeystoneClientTest(utils.TestCase): self.assertEqual('identity', cl._adapter.service_type) self.assertEqual((3, 0), cl._adapter.version) + + def test_empty_service_catalog_param(self): + # Client().service_catalog should return None if the client is not + # authenticated + sess = auth_session.Session() + cl = client.Client(session=sess) + self.assertEqual(None, cl.service_catalog)