summaryrefslogtreecommitdiffstats
path: root/snaps
diff options
context:
space:
mode:
authorLinda Wang <wangwulin@huawei.com>2017-05-18 01:53:18 +0000
committerLinda Wang <wangwulin@huawei.com>2017-05-19 07:16:25 +0000
commit8d8af3c360b360c982b44ba343a97cf5949c8c20 (patch)
tree6b34aa85679d88c635404657d8ac03f9247ec687 /snaps
parent956acc7da293b931c3e21e64fa0a9011f995c11c (diff)
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 <wangwulin@huawei.com>
Diffstat (limited to 'snaps')
-rw-r--r--snaps/openstack/utils/keystone_utils.py43
-rw-r--r--snaps/openstack/utils/tests/keystone_utils_tests.py26
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):
"""