From e3abc1c269581e40c08440e1c2b508affcbf1b63 Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Tue, 13 Jan 2015 15:53:10 +1300 Subject: [PATCH] Add support for creating a Glance client os-cloud-config can almost do everything to set up a nascent cloud for deploying baremetal machines except uploading the kernel and ramdisk using Glance, which is still performed by setup-baremetal. To enact the plan to put setup-baremetal on a radical diet, add support for creating a Glance client. Change-Id: I765e0ac89d1bf196255ea9217f877952b3617bad --- os_cloud_config/cmd/utils/_clients.py | 4 ++++ .../cmd/utils/tests/test_clients.py | 21 +++++++++++++++++++ os_cloud_config/utils/clients.py | 18 ++++++++++++++++ os_cloud_config/utils/tests/test_clients.py | 19 +++++++++++++++++ requirements.txt | 1 + 5 files changed, 63 insertions(+) diff --git a/os_cloud_config/cmd/utils/_clients.py b/os_cloud_config/cmd/utils/_clients.py index bdb5c6c..0a78855 100644 --- a/os_cloud_config/cmd/utils/_clients.py +++ b/os_cloud_config/cmd/utils/_clients.py @@ -45,3 +45,7 @@ def get_keystone_v3_client(): def get_neutron_client(): return clients.get_neutron_client(*_get_client_args()) + + +def get_glance_client(): + return clients.get_glance_client(*_get_client_args()) diff --git a/os_cloud_config/cmd/utils/tests/test_clients.py b/os_cloud_config/cmd/utils/tests/test_clients.py index a76a2ee..340b703 100644 --- a/os_cloud_config/cmd/utils/tests/test_clients.py +++ b/os_cloud_config/cmd/utils/tests/test_clients.py @@ -85,3 +85,24 @@ class CMDClientsTest(base.TestCase): auth_url=environ["OS_AUTH_URL"], tenant_name=environ["OS_TENANT_NAME"], ca_cert=environ.get("OS_CACERT")) + + @mock.patch('os.environ') + @mock.patch('keystoneclient.session.Session') + @mock.patch('keystoneclient.auth.identity.v2.Password') + @mock.patch('glanceclient.Client') + def test_get_glance_client(self, client_mock, password_mock, session_mock, + environ): + clients.get_glance_client() + tenant_name = environ["OS_TENANT_NAME"] + password_mock.assert_called_once_with(auth_url=environ["OS_AUTH_URL"], + username=environ["OS_USERNAME"], + password=environ["OS_PASSWORD"], + tenant_name=tenant_name) + session_mock.assert_called_once_with(auth=password_mock.return_value) + session_mock.return_value.get_endpoint.assert_called_once_with( + service_type='image', interface='public', region_name='regionOne') + session_mock.return_value.get_token.assert_called_once_with() + client_mock.assert_called_once_with( + '1', endpoint=session_mock.return_value.get_endpoint.return_value, + token=session_mock.return_value.get_token.return_value, + cacert=environ.get('OS_CACERT')) diff --git a/os_cloud_config/utils/clients.py b/os_cloud_config/utils/clients.py index a7cfb3b..21c3a7d 100644 --- a/os_cloud_config/utils/clients.py +++ b/os_cloud_config/utils/clients.py @@ -15,7 +15,10 @@ import logging +import glanceclient from ironicclient import client as ironicclient +from keystoneclient.auth.identity import v2 +from keystoneclient import session from keystoneclient.v2_0 import client as ksclient from keystoneclient.v3 import client as ks3client from neutronclient.neutron import client as neutronclient @@ -95,3 +98,18 @@ def get_neutron_client(username, neutron = neutronclient.Client('2.0', **kwargs) neutron.format = 'json' return neutron + + +def get_glance_client(username, password, tenant_name, auth_url, cacert=None, + region_name='regionOne'): + LOG.debug('Creating Keystone session to fetch Glance endpoint.') + auth = v2.Password(auth_url=auth_url, username=username, password=password, + tenant_name=tenant_name) + ks_session = session.Session(auth=auth) + endpoint = ks_session.get_endpoint(service_type='image', + interface='public', + region_name=region_name) + token = ks_session.get_token() + LOG.debug('Creating glance client.') + return glanceclient.Client('1', endpoint=endpoint, token=token, + cacert=cacert) diff --git a/os_cloud_config/utils/tests/test_clients.py b/os_cloud_config/utils/tests/test_clients.py index 85c9048..225582d 100644 --- a/os_cloud_config/utils/tests/test_clients.py +++ b/os_cloud_config/utils/tests/test_clients.py @@ -86,3 +86,22 @@ class ClientsTest(base.TestCase): auth_url='auth_url', tenant_name='tenant_name', ca_cert=None) + + @mock.patch('keystoneclient.session.Session') + @mock.patch('keystoneclient.auth.identity.v2.Password') + @mock.patch('glanceclient.Client') + def test_get_glance_client(self, client_mock, password_mock, session_mock): + clients.get_glance_client('username', 'password', 'tenant_name', + 'auth_url') + password_mock.assert_called_once_with(auth_url='auth_url', + username='username', + password='password', + tenant_name='tenant_name') + session_mock.assert_called_once_with(auth=password_mock.return_value) + session_mock.return_value.get_endpoint.assert_called_once_with( + service_type='image', interface='public', region_name='regionOne') + session_mock.return_value.get_token.assert_called_once_with() + client_mock.assert_called_once_with( + '1', endpoint=session_mock.return_value.get_endpoint.return_value, + token=session_mock.return_value.get_token.return_value, + cacert=None) diff --git a/requirements.txt b/requirements.txt index c44d008..99670a7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ pbr>=0.6,!=0.7,<1.0 argparse Babel>=1.3 +python-glanceclient>=0.15.0 python-ironicclient>=0.2.1 python-keystoneclient>=0.11.1 python-neutronclient>=2.3.6,<3