From ffdc62ad81b727bcc3359bbb394ceabf7867c1ab Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Tue, 17 Aug 2021 12:06:12 +0200 Subject: [PATCH] pybind/mgr: move build_url from dashboard to mgr_util Signed-off-by: Sebastian Wagner (cherry picked from commit 29883d68ef696a361b4fec0af22505835170a74d) --- src/pybind/mgr/dashboard/rest_client.py | 16 +++++--- .../mgr/dashboard/services/rgw_client.py | 12 +++--- src/pybind/mgr/dashboard/tools.py | 37 +----------------- src/pybind/mgr/mgr_util.py | 39 +++++++++++++++++++ 4 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/pybind/mgr/dashboard/rest_client.py b/src/pybind/mgr/dashboard/rest_client.py index 90cfd41ad77eb..f33c13432ff47 100644 --- a/src/pybind/mgr/dashboard/rest_client.py +++ b/src/pybind/mgr/dashboard/rest_client.py @@ -18,21 +18,19 @@ import logging import re import requests +from requests.auth import AuthBase from requests.exceptions import ConnectionError, InvalidURL, Timeout from .settings import Settings -from .tools import build_url try: from requests.packages.urllib3.exceptions import SSLError except ImportError: from urllib3.exceptions import SSLError # type: ignore -try: - from typing import List -except ImportError: - pass # Just for type checking +from typing import List, Optional +from mgr_util import build_url logger = logging.getLogger('rest_client') @@ -331,7 +329,13 @@ class _Request(object): class RestClient(object): - def __init__(self, host, port, client_name=None, ssl=False, auth=None, ssl_verify=True): + def __init__(self, + host: str, + port: int, + client_name: Optional[str] = None, + ssl: bool = False, + auth: Optional[AuthBase] = None, + ssl_verify: bool = True) -> None: super(RestClient, self).__init__() self.client_name = client_name if client_name else '' self.host = host diff --git a/src/pybind/mgr/dashboard/services/rgw_client.py b/src/pybind/mgr/dashboard/services/rgw_client.py index 25d1ed5ddac10..76b115dcc54e2 100644 --- a/src/pybind/mgr/dashboard/services/rgw_client.py +++ b/src/pybind/mgr/dashboard/services/rgw_client.py @@ -7,12 +7,14 @@ import re import xml.etree.ElementTree as ET # noqa: N814 from distutils.util import strtobool +from mgr_util import build_url + from .. import mgr from ..awsauth import S3Auth from ..exceptions import DashboardException from ..rest_client import RequestException, RestClient from ..settings import Settings -from ..tools import build_url, dict_contains_path, dict_get, json_str_to_object +from ..tools import dict_contains_path, dict_get, json_str_to_object try: from typing import Any, Dict, List, Optional, Tuple, Union @@ -324,10 +326,10 @@ class RgwClient(RestClient): service_url=self.service_url) def __init__(self, - access_key, - secret_key, - daemon_name, - user_id=None): + access_key: str, + secret_key: str, + daemon_name: str, + user_id: Optional[str] = None) -> None: try: daemon = RgwClient._daemons[daemon_name] except KeyError as error: diff --git a/src/pybind/mgr/dashboard/tools.py b/src/pybind/mgr/dashboard/tools.py index a6f14e82d0084..7e22ec91bec3e 100644 --- a/src/pybind/mgr/dashboard/tools.py +++ b/src/pybind/mgr/dashboard/tools.py @@ -13,7 +13,7 @@ from datetime import datetime, timedelta from distutils.util import strtobool import cherrypy -from ceph.deployment.utils import wrap_ipv6 +from mgr_util import build_url from . import mgr from .exceptions import ViewCacheNoDataException @@ -673,41 +673,6 @@ class Task(object): return self._begin_time -def build_url(host, scheme=None, port=None): - """ - Build a valid URL. IPv6 addresses specified in host will be enclosed in brackets - automatically. - - >>> build_url('example.com', 'https', 443) - 'https://example.com:443' - - >>> build_url(host='example.com', port=443) - '//example.com:443' - - >>> build_url('fce:9af7:a667:7286:4917:b8d3:34df:8373', port=80, scheme='http') - 'http://[fce:9af7:a667:7286:4917:b8d3:34df:8373]:80' - - :param scheme: The scheme, e.g. http, https or ftp. - :type scheme: str - :param host: Consisting of either a registered name (including but not limited to - a hostname) or an IP address. - :type host: str - :type port: int - :rtype: str - """ - netloc = wrap_ipv6(host) - if port: - netloc += ':{}'.format(port) - pr = urllib.parse.ParseResult( - scheme=scheme if scheme else '', - netloc=netloc, - path='', - params='', - query='', - fragment='') - return pr.geturl() - - def prepare_url_prefix(url_prefix): """ return '' if no prefix, or '/prefix' without slash in the end. diff --git a/src/pybind/mgr/mgr_util.py b/src/pybind/mgr/mgr_util.py index a810d519ae09f..982d69a798481 100644 --- a/src/pybind/mgr/mgr_util.py +++ b/src/pybind/mgr/mgr_util.py @@ -13,6 +13,7 @@ import logging import sys from threading import Lock, Condition, Event from typing import no_type_check +import urllib from functools import wraps if sys.version_info >= (3, 3): from threading import Timer @@ -20,6 +21,9 @@ else: from threading import _Timer as Timer from typing import Tuple, Any, Callable, Optional, Dict, TYPE_CHECKING, TypeVar, List, Iterable, Generator, Generic, Iterator + +from ceph.deployment.utils import wrap_ipv6 + T = TypeVar('T') if TYPE_CHECKING: @@ -431,6 +435,41 @@ def get_default_addr(): return result +def build_url(host: str, scheme: Optional[str] = None, port: Optional[int] = None) -> str: + """ + Build a valid URL. IPv6 addresses specified in host will be enclosed in brackets + automatically. + + >>> build_url('example.com', 'https', 443) + 'https://example.com:443' + + >>> build_url(host='example.com', port=443) + '//example.com:443' + + >>> build_url('fce:9af7:a667:7286:4917:b8d3:34df:8373', port=80, scheme='http') + 'http://[fce:9af7:a667:7286:4917:b8d3:34df:8373]:80' + + :param scheme: The scheme, e.g. http, https or ftp. + :type scheme: str + :param host: Consisting of either a registered name (including but not limited to + a hostname) or an IP address. + :type host: str + :type port: int + :rtype: str + """ + netloc = wrap_ipv6(host) + if port: + netloc += ':{}'.format(port) + pr = urllib.parse.ParseResult( + scheme=scheme if scheme else '', + netloc=netloc, + path='', + params='', + query='', + fragment='') + return pr.geturl() + + class ServerConfigException(Exception): pass -- 2.39.5