Merge "Move client creation functions to utils._clients"

This commit is contained in:
Jenkins 2014-07-24 14:23:58 +00:00 committed by Gerrit Code Review
commit 48af308683
6 changed files with 109 additions and 67 deletions

View File

@ -14,16 +14,11 @@
# limitations under the License. # limitations under the License.
import logging import logging
import os
import time import time
from ironicclient import client as ironicclient
from ironicclient.openstack.common.apiclient import exceptions as ironicexp 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.openstack.common.apiclient import exceptions as novaexc
from novaclient.v1_1 import client as novav11client from os_cloud_config.utils import _clients as clients
from novaclient.v1_1.contrib import baremetal
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -106,43 +101,15 @@ def register_ironic_node(service_host, node, client=None):
pass 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): def register_all_nodes(service_host, nodes_list, client=None):
LOG.debug('Registering all nodes.') LOG.debug('Registering all nodes.')
if using_ironic(keystone=None): if using_ironic(keystone=None):
if client is None: if client is None:
client = _get_ironic_client() client = clients.get_ironic_client()
register_func = register_ironic_node register_func = register_ironic_node
else: else:
if client is None: if client is None:
client = _get_nova_bm_client() client = clients.get_nova_bm_client()
register_func = register_nova_bm_node register_func = register_nova_bm_node
for node in nodes_list: for node in nodes_list:
register_func(service_host, node, client=client) 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): def using_ironic(keystone=None):
LOG.debug('Checking for usage of ironic.') LOG.debug('Checking for usage of ironic.')
if keystone is None: if keystone is None:
keystone = _get_keystone_client() keystone = clients.get_keystone_client()
return 'ironic' in [service.name for service in keystone.services.list()] return 'ironic' in [service.name for service in keystone.services.list()]

View File

@ -30,16 +30,6 @@ class NodesTest(base.TestCase):
'mac': ['aaa'], 'pm_addr': 'foo.bar', 'pm_user': 'test', 'mac': ['aaa'], 'pm_addr': 'foo.bar', 'pm_user': 'test',
'pm_password': 'random', 'pm_type': 'pxe_ssh'} '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) @mock.patch('os_cloud_config.nodes.using_ironic', return_value=False)
def test_register_all_nodes_nova_bm(self, ironic_mock): def test_register_all_nodes_nova_bm(self, ironic_mock):
node_list = [self._get_node(), self._get_node()] node_list = [self._get_node(), self._get_node()]
@ -107,16 +97,6 @@ class NodesTest(base.TestCase):
node["pm_password"] = "" node["pm_password"] = ""
self.assert_nova_bm_call_with_no_pm_password(node) 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) @mock.patch('os_cloud_config.nodes.using_ironic', return_value=True)
def test_register_all_nodes_ironic(self, using_ironic): def test_register_all_nodes_ironic(self, using_ironic):
node_list = [self._get_node(), self._get_node()] node_list = [self._get_node(), self._get_node()]
@ -170,16 +150,6 @@ class NodesTest(base.TestCase):
nodes.register_ironic_node, None, self._get_node(), nodes.register_ironic_node, None, self._get_node(),
client=ironic) 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): def test_using_ironic(self):
keystone = mock.MagicMock() keystone = mock.MagicMock()
service = collections.namedtuple('servicelist', ['name']) service = collections.namedtuple('servicelist', ['name'])

View File

@ -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)

View File

@ -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"])