]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr: move build_url from dashboard to mgr_util
authorSebastian Wagner <sewagner@redhat.com>
Tue, 17 Aug 2021 10:06:12 +0000 (12:06 +0200)
committerSebastian Wagner <sewagner@redhat.com>
Wed, 18 Aug 2021 10:08:50 +0000 (12:08 +0200)
Signed-off-by: Sebastian Wagner <sewagner@redhat.com>
src/pybind/mgr/dashboard/rest_client.py
src/pybind/mgr/dashboard/services/rgw_client.py
src/pybind/mgr/dashboard/tools.py
src/pybind/mgr/mgr_util.py

index e35ceb5474ee2c86ab668fc77edc7a2ba6ecfdee..bd03dc7067f9efb719ba0b639cab909383ebbc69 100644 (file)
@@ -17,21 +17,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')
 
@@ -330,7 +328,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
index 7fd7ae236745eb8c3131e5b2b4f8dd07cac29161..2caf6d66aac3adeb15d4b7a3728e3a71079c1323 100644 (file)
@@ -8,12 +8,14 @@ import xml.etree.ElementTree as ET  # noqa: N814
 from distutils.util import strtobool
 from subprocess import SubprocessError
 
+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
@@ -385,10 +387,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:
index a882a30cc005f7fa3dcb42f3f457790275484816..1092534ea1864dd0d9e4534aaf3384ff6685fc19 100644 (file)
@@ -12,7 +12,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
@@ -672,41 +672,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.
index a810d519ae09f66ece19bc8a1e44393740d6de6b..982d69a79848180af600009e6f1e50a878d984fc 100644 (file)
@@ -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