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
This commit is contained in:
Ladislav Smola 2014-08-26 10:52:49 +02:00
parent c56fe96eed
commit db8317c6aa
9 changed files with 165 additions and 57 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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',

View File

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

View File

View File

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