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
This commit is contained in:
Martin Kopec 2018-08-13 11:57:14 +02:00
parent 83e6ecb079
commit 0b4a78daa8
2 changed files with 37 additions and 18 deletions

View File

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

33
config_tempest/utils.py Normal file
View File

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