From 8d8af3c360b360c982b44ba343a97cf5949c8c20 Mon Sep 17 00:00:00 2001 From: Linda Wang Date: Thu, 18 May 2017 01:53:18 +0000 Subject: Add the function of getting endpoint Functest is trying to leverage SNAPS-OO library to replace some utility functions, in which def get_endpoint is called by testcases onos, odl, multisite and cloudify_ims. JIRA: SNAPS-78 Change-Id: Icb2778e0337a5d4246762ac3037773b39d5d554d Signed-off-by: Linda Wang --- snaps/openstack/utils/keystone_utils.py | 43 ++++++++++++++++++---- .../openstack/utils/tests/keystone_utils_tests.py | 26 +++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/snaps/openstack/utils/keystone_utils.py b/snaps/openstack/utils/keystone_utils.py index 8175b9a..622481c 100644 --- a/snaps/openstack/utils/keystone_utils.py +++ b/snaps/openstack/utils/keystone_utils.py @@ -24,6 +24,27 @@ logger = logging.getLogger('keystone_utils') V2_VERSION = 'v2.0' +def get_session_auth(os_creds): + """ + Return the session auth for keystone session + :param os_creds: the OpenStack credentials (OSCreds) object + :return: the auth + """ + if os_creds.identity_api_version == 3: + auth = v3.Password(auth_url=os_creds.auth_url, + username=os_creds.username, + password=os_creds.password, + project_name=os_creds.project_name, + user_domain_id=os_creds.user_domain_id, + project_domain_id=os_creds.project_domain_id) + else: + auth = v2.Password(auth_url=os_creds.auth_url, + username=os_creds.username, + password=os_creds.password, + tenant_name=os_creds.project_name) + return auth + + def keystone_session(os_creds): """ Creates a keystone session used for authenticating OpenStack clients @@ -32,13 +53,7 @@ def keystone_session(os_creds): """ logger.debug('Retrieving Keystone Session') - if os_creds.identity_api_version == 3: - auth = v3.Password(auth_url=os_creds.auth_url, username=os_creds.username, password=os_creds.password, - project_name=os_creds.project_name, user_domain_id=os_creds.user_domain_id, - project_domain_id=os_creds.project_domain_id) - else: - auth = v2.Password(auth_url=os_creds.auth_url, username=os_creds.username, password=os_creds.password, - tenant_name=os_creds.project_name) + auth = get_session_auth(os_creds) req_session = None if os_creds.proxy_settings: @@ -56,6 +71,20 @@ def keystone_client(os_creds): return Client(version=os_creds.identity_api_version, session=keystone_session(os_creds)) +def get_endpoint(os_creds, service_type, endpoint_type='publicURL'): + """ + Returns the endpoint of specific service + :param os_creds: the OpenStack credentials (OSCreds) object + :param service_type: the type of specific service + :param endpoint_type: the type of endpoint + :return: the endpoint url + """ + auth = get_session_auth(os_creds) + return keystone_session(os_creds).get_endpoint(auth=auth, + service_type=service_type, + endpoint_type=endpoint_type) + + def get_project(keystone=None, os_creds=None, project_name=None): """ Returns the first project object or None if not found diff --git a/snaps/openstack/utils/tests/keystone_utils_tests.py b/snaps/openstack/utils/tests/keystone_utils_tests.py index c072fd3..7bd7f5a 100644 --- a/snaps/openstack/utils/tests/keystone_utils_tests.py +++ b/snaps/openstack/utils/tests/keystone_utils_tests.py @@ -46,6 +46,32 @@ class KeystoneSmokeTests(OSComponentTestCase): keystone = keystone_utils.keystone_client(OSCreds('user', 'pass', 'url', 'project')) keystone.users.list() + def test_get_endpoint_success(self): + """ + Tests to ensure that proper credentials and proper service type can succeed. + """ + endpoint = keystone_utils.get_endpoint(self.os_creds, + service_type="identity") + self.assertIsNotNone(endpoint) + + def test_get_endpoint_fail_without_proper_service(self): + """ + Tests to ensure that proper credentials and improper service type cannot succeed. + """ + with self.assertRaises(Exception): + keystone_utils.get_endpoint(self.os_creds, service_type="glance") + + def test_get_endpoint_fail_without_proper_credentials(self): + """ + Tests to ensure that improper credentials and proper service type cannot succeed. + """ + from snaps.openstack.os_credentials import OSCreds + + with self.assertRaises(Exception): + keystone_utils.get_endpoint( + OSCreds('user', 'pass', 'url', 'project'), + service_type="image") + class KeystoneUtilsTests(OSComponentTestCase): """ -- cgit 1.2.3-korg