diff --git a/os_cloud_config/cmd/utils/_clients.py b/os_cloud_config/cmd/utils/_clients.py index 210aa81..991928a 100644 --- a/os_cloud_config/cmd/utils/_clients.py +++ b/os_cloud_config/cmd/utils/_clients.py @@ -40,6 +40,13 @@ def get_keystone_client(): os.environ["OS_AUTH_URL"]) +def get_keystone_v3_client(): + return clients.get_keystone_v3_client(os.environ["OS_USERNAME"], + os.environ["OS_PASSWORD"], + os.environ["OS_TENANT_NAME"], + os.environ["OS_AUTH_URL"]) + + def get_neutron_client(): return clients.get_neutron_client(os.environ["OS_USERNAME"], os.environ["OS_PASSWORD"], diff --git a/os_cloud_config/cmd/utils/tests/test_clients.py b/os_cloud_config/cmd/utils/tests/test_clients.py index 1b8c3a2..a001d61 100644 --- a/os_cloud_config/cmd/utils/tests/test_clients.py +++ b/os_cloud_config/cmd/utils/tests/test_clients.py @@ -51,9 +51,19 @@ class CMDClientsTest(base.TestCase): auth_url=environ["OS_AUTH_URL"], tenant_name=environ["OS_TENANT_NAME"]) + @mock.patch('os.environ') + @mock.patch('keystoneclient.v3.client.Client') + def test_get_keystone_v3_client(self, client_mock, environ): + clients.get_keystone_v3_client() + client_mock.assert_called_once_with( + username=environ["OS_USERNAME"], + password=environ["OS_PASSWORD"], + auth_url=environ["OS_AUTH_URL"].replace('v2.0', 'v3'), + tenant_name=environ["OS_TENANT_NAME"]) + @mock.patch('os.environ') @mock.patch('neutronclient.neutron.client.Client') - def test_get_client(self, client_mock, environ): + def test_get_neutron_client(self, client_mock, environ): clients.get_neutron_client() client_mock.assert_called_once_with( '2.0', username=environ["OS_USERNAME"], diff --git a/os_cloud_config/utils/clients.py b/os_cloud_config/utils/clients.py index a1a9eee..81f0d04 100644 --- a/os_cloud_config/utils/clients.py +++ b/os_cloud_config/utils/clients.py @@ -17,6 +17,7 @@ import logging from ironicclient import client as ironicclient from keystoneclient.v2_0 import client as ksclient +from keystoneclient.v3 import client as ks3client from neutronclient.neutron import client as neutronclient from novaclient.extension import Extension from novaclient.v1_1 import client as novav11client @@ -53,6 +54,15 @@ def get_keystone_client(username, password, tenant_name, auth_url): return ksclient.Client(**kwargs) +def get_keystone_v3_client(username, password, tenant_name, auth_url): + LOG.debug('Creating keystone v3 client.') + kwargs = {'username': username, + 'password': password, + 'tenant_name': tenant_name, + 'auth_url': auth_url.replace('v2.0', 'v3')} + return ks3client.Client(**kwargs) + + def get_neutron_client(username, password, tenant_name, auth_url): LOG.debug('Creating neutron client.') kwargs = {'username': username, diff --git a/os_cloud_config/utils/tests/test_clients.py b/os_cloud_config/utils/tests/test_clients.py index d435fc9..ed52173 100644 --- a/os_cloud_config/utils/tests/test_clients.py +++ b/os_cloud_config/utils/tests/test_clients.py @@ -51,8 +51,28 @@ class ClientsTest(base.TestCase): auth_url='auth_url', tenant_name='tenant_name') + @mock.patch('keystoneclient.v3.client.Client') + def test_get_keystone_v3_client_with_v2_url(self, client_mock): + clients.get_keystone_v3_client('username', 'password', 'tenant_name', + 'auth_url/v2.0') + client_mock.assert_called_once_with( + username='username', + password='password', + auth_url='auth_url/v3', + tenant_name='tenant_name') + + @mock.patch('keystoneclient.v3.client.Client') + def test_get_keystone_v3_client_with_v3_url(self, client_mock): + clients.get_keystone_v3_client('username', 'password', 'tenant_name', + 'auth_url/v3') + client_mock.assert_called_once_with( + username='username', + password='password', + auth_url='auth_url/v3', + tenant_name='tenant_name') + @mock.patch('neutronclient.neutron.client.Client') - def test_get_client(self, client_mock): + def test_get_neutron_client(self, client_mock): clients.get_neutron_client('username', 'password', 'tenant_name', 'auth_url') client_mock.assert_called_once_with(