From 7cb4ddd1f4f5c165b178c0227bae4041b5b08462 Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Mon, 23 Jun 2014 10:36:21 +1000 Subject: [PATCH] Move client creation functions to utils._clients Potientally, other functions and scripts within os-cloud-config will also want to query running OpenStack services, and rather than duplicating code, let's move the functions that connect to nova, ironic and keystone as a user into utils._clients. Change-Id: Ic344de3d1550d4cb4d292882bf00f93cbb888931 --- os_cloud_config/nodes.py | 41 ++------------ os_cloud_config/tests/test_nodes.py | 30 ----------- .../{utils.py => utils/__init__.py} | 0 os_cloud_config/utils/_clients.py | 53 +++++++++++++++++++ os_cloud_config/utils/tests/test_clients.py | 52 ++++++++++++++++++ .../{ => utils}/tests/test_utils.py | 0 6 files changed, 109 insertions(+), 67 deletions(-) rename os_cloud_config/{utils.py => utils/__init__.py} (100%) create mode 100644 os_cloud_config/utils/_clients.py create mode 100644 os_cloud_config/utils/tests/test_clients.py rename os_cloud_config/{ => utils}/tests/test_utils.py (100%) diff --git a/os_cloud_config/nodes.py b/os_cloud_config/nodes.py index c9c042c..be324da 100644 --- a/os_cloud_config/nodes.py +++ b/os_cloud_config/nodes.py @@ -14,16 +14,11 @@ # limitations under the License. import logging -import os import time -from ironicclient import client as ironicclient from ironicclient.openstack.common.apiclient import exceptions as ironicexp -from keystoneclient.v2_0 import client as ksclient -from novaclient.extension import Extension from novaclient.openstack.common.apiclient import exceptions as novaexc -from novaclient.v1_1 import client as novav11client -from novaclient.v1_1.contrib import baremetal +from os_cloud_config.utils import _clients as clients LOG = logging.getLogger(__name__) @@ -106,43 +101,15 @@ def register_ironic_node(service_host, node, client=None): pass -def _get_nova_bm_client(): - 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"], - extensions=[baremetal_extension]) - - -def _get_ironic_client(): - 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']} - return ironicclient.get_client(1, **kwargs) - - -def _get_keystone_client(): - 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"]} - return ksclient.Client(**kwargs) - - def register_all_nodes(service_host, nodes_list, client=None): LOG.debug('Registering all nodes.') if using_ironic(keystone=None): if client is None: - client = _get_ironic_client() + client = clients.get_ironic_client() register_func = register_ironic_node else: if client is None: - client = _get_nova_bm_client() + client = clients.get_nova_bm_client() register_func = register_nova_bm_node for node in nodes_list: register_func(service_host, node, client=client) @@ -151,5 +118,5 @@ def register_all_nodes(service_host, nodes_list, client=None): def using_ironic(keystone=None): LOG.debug('Checking for usage of ironic.') if keystone is None: - keystone = _get_keystone_client() + keystone = clients.get_keystone_client() return 'ironic' in [service.name for service in keystone.services.list()] diff --git a/os_cloud_config/tests/test_nodes.py b/os_cloud_config/tests/test_nodes.py index a509c5e..6ef87f4 100644 --- a/os_cloud_config/tests/test_nodes.py +++ b/os_cloud_config/tests/test_nodes.py @@ -30,16 +30,6 @@ class NodesTest(base.TestCase): 'mac': ['aaa'], 'pm_addr': 'foo.bar', 'pm_user': 'test', 'pm_password': 'random', 'pm_type': 'pxe_ssh'} - @mock.patch('os.environ') - @mock.patch('novaclient.v1_1.client.Client') - def test_get_nova_bm_client(self, client_mock, environ): - nodes._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_cloud_config.nodes.using_ironic', return_value=False) def test_register_all_nodes_nova_bm(self, ironic_mock): node_list = [self._get_node(), self._get_node()] @@ -107,16 +97,6 @@ class NodesTest(base.TestCase): node["pm_password"] = "" self.assert_nova_bm_call_with_no_pm_password(node) - @mock.patch('os.environ') - @mock.patch('ironicclient.client.get_client') - def test_get_ironic_client(self, client_mock, environ): - nodes._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_cloud_config.nodes.using_ironic', return_value=True) def test_register_all_nodes_ironic(self, using_ironic): node_list = [self._get_node(), self._get_node()] @@ -170,16 +150,6 @@ class NodesTest(base.TestCase): nodes.register_ironic_node, None, self._get_node(), client=ironic) - @mock.patch('os.environ') - @mock.patch('keystoneclient.v2_0.client.Client') - def test_get_keystone_client(self, client_mock, environ): - nodes._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"]) - def test_using_ironic(self): keystone = mock.MagicMock() service = collections.namedtuple('servicelist', ['name']) diff --git a/os_cloud_config/utils.py b/os_cloud_config/utils/__init__.py similarity index 100% rename from os_cloud_config/utils.py rename to os_cloud_config/utils/__init__.py diff --git a/os_cloud_config/utils/_clients.py b/os_cloud_config/utils/_clients.py new file mode 100644 index 0000000..98ce774 --- /dev/null +++ b/os_cloud_config/utils/_clients.py @@ -0,0 +1,53 @@ +# 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 logging +import os + +from ironicclient import client as ironicclient +from keystoneclient.v2_0 import client as ksclient +from novaclient.extension import Extension +from novaclient.v1_1 import client as novav11client +from novaclient.v1_1.contrib import baremetal + +LOG = logging.getLogger(__name__) + + +def get_nova_bm_client(): + 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"], + extensions=[baremetal_extension]) + + +def get_ironic_client(): + 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']} + return ironicclient.get_client(1, **kwargs) + + +def get_keystone_client(): + 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"]} + return ksclient.Client(**kwargs) diff --git a/os_cloud_config/utils/tests/test_clients.py b/os_cloud_config/utils/tests/test_clients.py new file mode 100644 index 0000000..2f23b5c --- /dev/null +++ b/os_cloud_config/utils/tests/test_clients.py @@ -0,0 +1,52 @@ +# 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.tests import base +from os_cloud_config.utils import _clients as 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() + 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"]) diff --git a/os_cloud_config/tests/test_utils.py b/os_cloud_config/utils/tests/test_utils.py similarity index 100% rename from os_cloud_config/tests/test_utils.py rename to os_cloud_config/utils/tests/test_utils.py