From fa2b68ed4e5dad285af1ec9fddc6ee1527c2004e Mon Sep 17 00:00:00 2001 From: Rakesh H S Date: Thu, 11 Sep 2014 20:31:46 +0530 Subject: [PATCH] ironic client to use os_region_name if provided Currently ironicclient does not pass OS_REGION_NAME to Keystone client. That will cause problems in multiregion deployments. Modified ironicclient to consider using OS_REGION_NAME if provided. Closes-Bug: #1314159 Change-Id: I885e57973ccec64471fe2ba5fecfab2224829787 --- ironicclient/client.py | 8 +++++ ironicclient/tests/test_client.py | 59 +++++++++++++++++++++++++++++++ ironicclient/tests/utils.py | 8 +++-- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/ironicclient/client.py b/ironicclient/client.py index 73dfe6978..d32ed2b77 100644 --- a/ironicclient/client.py +++ b/ironicclient/client.py @@ -41,8 +41,15 @@ def _get_ksclient(**kwargs): def _get_endpoint(client, **kwargs): """Get an endpoint using the provided keystone client.""" + attr = None + filter_value = None + if kwargs.get('region_name'): + attr = 'region' + filter_value = kwargs.get('region_name') return client.service_catalog.url_for( service_type=kwargs.get('service_type') or 'baremetal', + attr=attr, + filter_value=filter_value, endpoint_type=kwargs.get('endpoint_type') or 'publicURL') @@ -86,6 +93,7 @@ def get_client(api_version, **kwargs): if kwargs.get('os_auth_token') else _ksclient.auth_token) + ks_kwargs['region_name'] = kwargs.get('os_region_name') endpoint = kwargs.get('ironic_url') or \ _get_endpoint(_ksclient, **ks_kwargs) diff --git a/ironicclient/tests/test_client.py b/ironicclient/tests/test_client.py index 983324524..792389203 100644 --- a/ironicclient/tests/test_client.py +++ b/ironicclient/tests/test_client.py @@ -25,6 +25,19 @@ def fake_get_ksclient(**kwargs): class ClientTest(utils.BaseTestCase): + def test_get_client_with_auth_token_ironic_url(self): + self.useFixture(fixtures.MonkeyPatch( + 'ironicclient.client._get_ksclient', fake_get_ksclient)) + kwargs = { + 'ironic_url': 'http://ironic.example.org:6385/', + 'os_auth_token': 'USER_AUTH_TOKEN', + } + client = get_client('1', **kwargs) + + self.assertEqual('USER_AUTH_TOKEN', client.http_client.auth_token) + self.assertEqual('http://ironic.example.org:6385/', + client.http_client.endpoint) + def test_get_client_no_auth_token(self): self.useFixture(fixtures.MonkeyPatch( 'ironicclient.client._get_ksclient', fake_get_ksclient)) @@ -38,6 +51,29 @@ class ClientTest(utils.BaseTestCase): client = get_client('1', **kwargs) self.assertEqual('KSCLIENT_AUTH_TOKEN', client.http_client.auth_token) + self.assertEqual('http://localhost:6385/v1/f14b41234', + client.http_client.endpoint) + self.assertEqual(fake_get_ksclient().auth_ref, + client.http_client.auth_ref) + + def test_get_client_with_region_no_auth_token(self): + self.useFixture(fixtures.MonkeyPatch( + 'ironicclient.client._get_ksclient', fake_get_ksclient)) + kwargs = { + 'os_tenant_name': 'TENANT_NAME', + 'os_username': 'USERNAME', + 'os_password': 'PASSWORD', + 'os_region_name': 'REGIONONE', + 'os_auth_url': 'http://localhost:35357/v2.0', + 'os_auth_token': '', + } + client = get_client('1', **kwargs) + + self.assertEqual('KSCLIENT_AUTH_TOKEN', client.http_client.auth_token) + self.assertEqual('http://regionhost:6385/v1/f14b41234', + client.http_client.endpoint) + self.assertEqual(fake_get_ksclient().auth_ref, + client.http_client.auth_ref) def test_get_client_with_auth_token(self): self.useFixture(fixtures.MonkeyPatch( @@ -52,6 +88,29 @@ class ClientTest(utils.BaseTestCase): client = get_client('1', **kwargs) self.assertEqual('USER_AUTH_TOKEN', client.http_client.auth_token) + self.assertEqual('http://localhost:6385/v1/f14b41234', + client.http_client.endpoint) + self.assertEqual(fake_get_ksclient().auth_ref, + client.http_client.auth_ref) + + def test_get_client_with_region_name_auth_token(self): + self.useFixture(fixtures.MonkeyPatch( + 'ironicclient.client._get_ksclient', fake_get_ksclient)) + kwargs = { + 'os_tenant_name': 'TENANT_NAME', + 'os_username': 'USERNAME', + 'os_password': 'PASSWORD', + 'os_auth_url': 'http://localhost:35357/v2.0', + 'os_region_name': 'REGIONONE', + 'os_auth_token': 'USER_AUTH_TOKEN', + } + client = get_client('1', **kwargs) + + self.assertEqual('USER_AUTH_TOKEN', client.http_client.auth_token) + self.assertEqual('http://regionhost:6385/v1/f14b41234', + client.http_client.endpoint) + self.assertEqual(fake_get_ksclient().auth_ref, + client.http_client.auth_ref) def test_get_client_no_url_and_no_token(self): self.useFixture(fixtures.MonkeyPatch( diff --git a/ironicclient/tests/utils.py b/ironicclient/tests/utils.py index 4df3767d6..7c4dacc9f 100644 --- a/ironicclient/tests/utils.py +++ b/ironicclient/tests/utils.py @@ -89,8 +89,12 @@ class FakeResponse(object): class FakeServiceCatalog(): - def url_for(self, endpoint_type, service_type): - return 'http://localhost:6385/v1/f14b41234' + def url_for(self, endpoint_type, service_type, attr=None, + filter_value=None): + if attr == 'region' and filter_value: + return 'http://regionhost:6385/v1/f14b41234' + else: + return 'http://localhost:6385/v1/f14b41234' class FakeKeystone():