From db8317c6aa2c02ad6b0cbee08eda93f986aa869b Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Tue, 26 Aug 2014 10:52:49 +0200 Subject: [PATCH] Move _clients to cmd dir Make clients public and accepting parameters. Python functions should be not connected to environ, cause that is there only for CLI use. This lib will be called e.g. from UI. Making clients public unifies creating clients in all places, it should help with future version changes of the clients. Move _clients to CMD dir, where it is defaulted to os.environ values and call public clients with those default values. Change-Id: Ic490d9cfb4069809c8b3142f62f3dd40644a0cf7 --- os_cloud_config/cmd/utils/_clients.py | 47 ++++++++++++++ .../cmd/utils/tests/test_clients.py | 62 +++++++++++++++++++ .../cmd/utils/tests/test_environment.py | 2 +- os_cloud_config/neutron.py | 2 +- os_cloud_config/nodes.py | 2 +- os_cloud_config/tests/test_neutron.py | 8 +-- .../utils/{_clients.py => clients.py} | 41 ++++++------ os_cloud_config/utils/tests/__init__.py | 0 os_cloud_config/utils/tests/test_clients.py | 58 ++++++++--------- 9 files changed, 165 insertions(+), 57 deletions(-) create mode 100644 os_cloud_config/cmd/utils/_clients.py create mode 100644 os_cloud_config/cmd/utils/tests/test_clients.py rename os_cloud_config/utils/{_clients.py => clients.py} (58%) create mode 100644 os_cloud_config/utils/tests/__init__.py diff --git a/os_cloud_config/cmd/utils/_clients.py b/os_cloud_config/cmd/utils/_clients.py new file mode 100644 index 0000000..210aa81 --- /dev/null +++ b/os_cloud_config/cmd/utils/_clients.py @@ -0,0 +1,47 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import os + +from os_cloud_config.utils import clients + +LOG = logging.getLogger(__name__) + + +def get_nova_bm_client(): + return clients.get_nova_bm_client(os.environ["OS_USERNAME"], + os.environ["OS_PASSWORD"], + os.environ["OS_TENANT_NAME"], + os.environ["OS_AUTH_URL"]) + + +def get_ironic_client(): + return clients.get_ironic_client(os.environ["OS_USERNAME"], + os.environ["OS_PASSWORD"], + os.environ["OS_TENANT_NAME"], + os.environ["OS_AUTH_URL"]) + + +def get_keystone_client(): + return clients.get_keystone_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"], + os.environ["OS_TENANT_NAME"], + os.environ["OS_AUTH_URL"]) diff --git a/os_cloud_config/cmd/utils/tests/test_clients.py b/os_cloud_config/cmd/utils/tests/test_clients.py new file mode 100644 index 0000000..1b8c3a2 --- /dev/null +++ b/os_cloud_config/cmd/utils/tests/test_clients.py @@ -0,0 +1,62 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import mock + +from os_cloud_config.cmd.utils import _clients as clients +from os_cloud_config.tests import base + + +class CMDClientsTest(base.TestCase): + + @mock.patch('os.environ') + @mock.patch('ironicclient.client.get_client') + def test_get_ironic_client(self, client_mock, environ): + clients.get_ironic_client() + client_mock.assert_called_once_with( + 1, os_username=environ["OS_USERNAME"], + os_password=environ["OS_PASSWORD"], + os_auth_url=environ["OS_AUTH_URL"], + os_tenant_name=environ["OS_TENANT_NAME"]) + + @mock.patch('os.environ') + @mock.patch('novaclient.v1_1.client.Client') + def test_get_nova_bm_client(self, client_mock, environ): + clients.get_nova_bm_client() + client_mock.assert_called_once_with(environ["OS_USERNAME"], + environ["OS_PASSWORD"], + environ["OS_AUTH_URL"], + environ["OS_TENANT_NAME"], + extensions=[mock.ANY]) + + @mock.patch('os.environ') + @mock.patch('keystoneclient.v2_0.client.Client') + def test_get_keystone_client(self, client_mock, environ): + clients.get_keystone_client() + client_mock.assert_called_once_with( + username=environ["OS_USERNAME"], + password=environ["OS_PASSWORD"], + auth_url=environ["OS_AUTH_URL"], + tenant_name=environ["OS_TENANT_NAME"]) + + @mock.patch('os.environ') + @mock.patch('neutronclient.neutron.client.Client') + def test_get_client(self, client_mock, environ): + clients.get_neutron_client() + client_mock.assert_called_once_with( + '2.0', username=environ["OS_USERNAME"], + password=environ["OS_PASSWORD"], + auth_url=environ["OS_AUTH_URL"], + tenant_name=environ["OS_TENANT_NAME"]) diff --git a/os_cloud_config/cmd/utils/tests/test_environment.py b/os_cloud_config/cmd/utils/tests/test_environment.py index df7d813..b69e427 100644 --- a/os_cloud_config/cmd/utils/tests/test_environment.py +++ b/os_cloud_config/cmd/utils/tests/test_environment.py @@ -21,7 +21,7 @@ from os_cloud_config import exception from os_cloud_config.tests import base -class UtilsTest(base.TestCase): +class CMDEnviromentTest(base.TestCase): @mock.patch.dict('os.environ', {}) def test_ensure_environment_missing_all(self): diff --git a/os_cloud_config/neutron.py b/os_cloud_config/neutron.py index 2149d72..55b0253 100644 --- a/os_cloud_config/neutron.py +++ b/os_cloud_config/neutron.py @@ -15,7 +15,7 @@ import logging -from os_cloud_config.utils import _clients as clients +from os_cloud_config.cmd.utils import _clients as clients LOG = logging.getLogger(__name__) diff --git a/os_cloud_config/nodes.py b/os_cloud_config/nodes.py index be324da..58c0054 100644 --- a/os_cloud_config/nodes.py +++ b/os_cloud_config/nodes.py @@ -18,7 +18,7 @@ import time from ironicclient.openstack.common.apiclient import exceptions as ironicexp from novaclient.openstack.common.apiclient import exceptions as novaexc -from os_cloud_config.utils import _clients as clients +from os_cloud_config.cmd.utils import _clients as clients LOG = logging.getLogger(__name__) diff --git a/os_cloud_config/tests/test_neutron.py b/os_cloud_config/tests/test_neutron.py index fa36559..17195e2 100644 --- a/os_cloud_config/tests/test_neutron.py +++ b/os_cloud_config/tests/test_neutron.py @@ -140,8 +140,8 @@ class NeutronTest(base.TestCase): 'end': '172.16.5.40'}]}} client.create_subnet.assert_called_once_with(float_call) - @mock.patch('os_cloud_config.utils._clients.get_neutron_client') - @mock.patch('os_cloud_config.utils._clients.get_keystone_client') + @mock.patch('os_cloud_config.cmd.utils._clients.get_neutron_client') + @mock.patch('os_cloud_config.cmd.utils._clients.get_keystone_client') def test_initialize_neutron_physical(self, keystoneclient, neutronclient): network_desc = {'physical': {'name': 'ctlplane', 'cidr': '10.0.0.0/24', @@ -165,8 +165,8 @@ class NeutronTest(base.TestCase): neutronclient().create_network.assert_called_once_with(network_call) neutronclient().create_subnet.assert_called_once_with(subnet_call) - @mock.patch('os_cloud_config.utils._clients.get_neutron_client') - @mock.patch('os_cloud_config.utils._clients.get_keystone_client') + @mock.patch('os_cloud_config.cmd.utils._clients.get_neutron_client') + @mock.patch('os_cloud_config.cmd.utils._clients.get_keystone_client') def test_initialize_neutron_float_and_external(self, keystoneclient, neutronclient): network_desc = {'float': {'name': 'default-net', diff --git a/os_cloud_config/utils/_clients.py b/os_cloud_config/utils/clients.py similarity index 58% rename from os_cloud_config/utils/_clients.py rename to os_cloud_config/utils/clients.py index 56e889c..a1a9eee 100644 --- a/os_cloud_config/utils/_clients.py +++ b/os_cloud_config/utils/clients.py @@ -14,7 +14,6 @@ # limitations under the License. import logging -import os from ironicclient import client as ironicclient from keystoneclient.v2_0 import client as ksclient @@ -26,40 +25,40 @@ from novaclient.v1_1.contrib import baremetal LOG = logging.getLogger(__name__) -def get_nova_bm_client(): +def get_nova_bm_client(username, password, tenant_name, auth_url): LOG.debug('Creating nova client.') baremetal_extension = Extension('baremetal', baremetal) - return novav11client.Client(os.environ["OS_USERNAME"], - os.environ["OS_PASSWORD"], - os.environ["OS_TENANT_NAME"], - os.environ["OS_AUTH_URL"], + return novav11client.Client(username, + password, + tenant_name, + auth_url, extensions=[baremetal_extension]) -def get_ironic_client(): +def get_ironic_client(username, password, tenant_name, auth_url): LOG.debug('Creating ironic client.') - kwargs = {'os_username': os.environ['OS_USERNAME'], - 'os_password': os.environ['OS_PASSWORD'], - 'os_auth_url': os.environ['OS_AUTH_URL'], - 'os_tenant_name': os.environ['OS_TENANT_NAME']} + kwargs = {'os_username': username, + 'os_password': password, + 'os_auth_url': auth_url, + 'os_tenant_name': tenant_name} return ironicclient.get_client(1, **kwargs) -def get_keystone_client(): +def get_keystone_client(username, password, tenant_name, auth_url): LOG.debug('Creating keystone client.') - kwargs = {'username': os.environ["OS_USERNAME"], - 'password': os.environ["OS_PASSWORD"], - 'tenant_name': os.environ["OS_TENANT_NAME"], - 'auth_url': os.environ["OS_AUTH_URL"]} + kwargs = {'username': username, + 'password': password, + 'tenant_name': tenant_name, + 'auth_url': auth_url} return ksclient.Client(**kwargs) -def get_neutron_client(): +def get_neutron_client(username, password, tenant_name, auth_url): LOG.debug('Creating neutron client.') - kwargs = {'username': os.environ["OS_USERNAME"], - 'password': os.environ["OS_PASSWORD"], - 'tenant_name': os.environ["OS_TENANT_NAME"], - 'auth_url': os.environ["OS_AUTH_URL"]} + kwargs = {'username': username, + 'password': password, + 'tenant_name': tenant_name, + 'auth_url': auth_url} neutron = neutronclient.Client('2.0', **kwargs) neutron.format = 'json' return neutron diff --git a/os_cloud_config/utils/tests/__init__.py b/os_cloud_config/utils/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/os_cloud_config/utils/tests/test_clients.py b/os_cloud_config/utils/tests/test_clients.py index a76dc36..d435fc9 100644 --- a/os_cloud_config/utils/tests/test_clients.py +++ b/os_cloud_config/utils/tests/test_clients.py @@ -16,47 +16,47 @@ import mock from os_cloud_config.tests import base -from os_cloud_config.utils import _clients as clients +from os_cloud_config.utils import clients class ClientsTest(base.TestCase): - @mock.patch('os.environ') @mock.patch('ironicclient.client.get_client') - def test_get_ironic_client(self, client_mock, environ): - clients.get_ironic_client() + def test_get_ironic_client(self, client_mock): + clients.get_ironic_client('username', 'password', 'tenant_name', + 'auth_url') client_mock.assert_called_once_with( - 1, os_username=environ["OS_USERNAME"], - os_password=environ["OS_PASSWORD"], - os_auth_url=environ["OS_AUTH_URL"], - os_tenant_name=environ["OS_TENANT_NAME"]) + 1, os_username='username', + os_password='password', + os_auth_url='auth_url', + os_tenant_name='tenant_name') - @mock.patch('os.environ') @mock.patch('novaclient.v1_1.client.Client') - def test_get_nova_bm_client(self, client_mock, environ): - clients.get_nova_bm_client() - client_mock.assert_called_once_with(environ["OS_USERNAME"], - environ["OS_PASSWORD"], - environ["OS_AUTH_URL"], - environ["OS_TENANT_NAME"], + def test_get_nova_bm_client(self, client_mock): + clients.get_nova_bm_client('username', 'password', 'tenant_name', + 'auth_url') + client_mock.assert_called_once_with('username', + 'password', + 'tenant_name', + 'auth_url', extensions=[mock.ANY]) - @mock.patch('os.environ') @mock.patch('keystoneclient.v2_0.client.Client') - def test_get_keystone_client(self, client_mock, environ): - clients.get_keystone_client() + def test_get_keystone_client(self, client_mock): + clients.get_keystone_client('username', 'password', 'tenant_name', + 'auth_url') client_mock.assert_called_once_with( - username=environ["OS_USERNAME"], - password=environ["OS_PASSWORD"], - auth_url=environ["OS_AUTH_URL"], - tenant_name=environ["OS_TENANT_NAME"]) + username='username', + password='password', + auth_url='auth_url', + tenant_name='tenant_name') - @mock.patch('os.environ') @mock.patch('neutronclient.neutron.client.Client') - def test_get_client(self, client_mock, environ): - clients.get_neutron_client() + def test_get_client(self, client_mock): + clients.get_neutron_client('username', 'password', 'tenant_name', + 'auth_url') client_mock.assert_called_once_with( - '2.0', username=environ["OS_USERNAME"], - password=environ["OS_PASSWORD"], - auth_url=environ["OS_AUTH_URL"], - tenant_name=environ["OS_TENANT_NAME"]) + '2.0', username='username', + password='password', + auth_url='auth_url', + tenant_name='tenant_name')