From 0b4a78daa8cb847a4954d2caffbbf513c094eb3c Mon Sep 17 00:00:00 2001 From: Martin Kopec Date: Mon, 13 Aug 2018 11:57:14 +0200 Subject: [PATCH] Create utils.py The patch create utils.py for methods which might be used elsewhere in the project, with no matter on context. Move get_base_url method from credentials.py to util.py so that the method is reusable. Change-Id: Ic8cb83bb8859b4fddcd57faa3e23992941d0010b --- config_tempest/credentials.py | 22 ++++------------------ config_tempest/utils.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 config_tempest/utils.py diff --git a/config_tempest/credentials.py b/config_tempest/credentials.py index a6dda255..33e0db0b 100644 --- a/config_tempest/credentials.py +++ b/config_tempest/credentials.py @@ -13,11 +13,11 @@ # License for the specific language governing permissions and limitations # under the License. -import re import requests -from six.moves import urllib from tempest.lib import auth +from config_tempest import utils + class Credentials(object): """Class contains all needed credentials. @@ -65,20 +65,6 @@ class Credentials(object): # tool keeps them in identity section for further usage return self._conf.get_defaulted('identity', key) - def _get_base_url(self, endpoint): - """Return the base url. - - :param key: endpoint - :type key: string - :returns: base_url - :rtype: string - """ - url = urllib.parse.urlsplit(endpoint) - new_path = re.split(r'(^|/)+v\d+(\.\d+)?', url.path)[0] - url = list(url) - url[2] = new_path + '/' - return urllib.parse.urlunsplit(url) - def _list_versions(self, base_url): resp = requests.get(base_url) data = resp.json() @@ -90,7 +76,7 @@ class Credentials(object): :returns: identity version :rtype: string """ - base_url = self._get_base_url(self._conf.get("identity", "uri")) + base_url = utils.get_base_url(self._conf.get("identity", "uri")) versions = self._list_versions(base_url) for version in versions: if version["status"] == "stable" and "v3" in version["id"]: @@ -138,7 +124,7 @@ class Credentials(object): # We set uri and uri_v3 to /v3 here because if the endpoint on the # rc file don't set the /v3 it will fail with a error 404 uri = self._conf.get_defaulted('identity', 'uri_v3') - uri = self._get_base_url(uri) + 'v3' + uri = utils.get_base_url(uri) + 'v3' self._conf.set('identity', 'uri_v3', uri) return auth.KeystoneV3AuthProvider( self.tempest_creds, diff --git a/config_tempest/utils.py b/config_tempest/utils.py new file mode 100644 index 00000000..651a4ea4 --- /dev/null +++ b/config_tempest/utils.py @@ -0,0 +1,33 @@ +# Copyright 2018 Red Hat, Inc. +# All Rights Reserved. +# +# 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 re +from six.moves import urllib + + +def get_base_url(endpoint): + """Return the base url. + + :param endpoint: endpoint + :type endpoint: string + :returns: base_url - without version + :rtype: string + """ + url = urllib.parse.urlsplit(endpoint) + # the regex matches a version in the url path + url_path_without_version = re.split(r'(^|/)+v\d+(\.\d+)?', url.path)[0] + url = list(url) + url[2] = url_path_without_version + '/' + return urllib.parse.urlunsplit(url)