]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
python-common: HostSpec: add `validate()`
authorSebastian Wagner <sewagner@redhat.com>
Wed, 22 Sep 2021 11:46:52 +0000 (13:46 +0200)
committerSebastian Wagner <sewagner@redhat.com>
Wed, 1 Dec 2021 08:57:31 +0000 (09:57 +0100)
Adjust HostSpec interface to ServiceSpec

Signed-off-by: Sebastian Wagner <sewagner@redhat.com>
src/pybind/mgr/cephadm/module.py
src/python-common/ceph/deployment/hostspec.py
src/python-common/ceph/deployment/service_spec.py

index bbd37e80ab8ddaf0f8de5a10a2fca1340109622d..0daef476a881126c5f348b9361a22ef5c1b23fe4 100644 (file)
@@ -23,7 +23,7 @@ from prettytable import PrettyTable
 from ceph.deployment import inventory
 from ceph.deployment.drive_group import DriveGroupSpec
 from ceph.deployment.service_spec import \
-    ServiceSpec, PlacementSpec, assert_valid_host, \
+    ServiceSpec, PlacementSpec, \
     HostPlacementSpec, IngressSpec
 from ceph.utils import str_to_datetime, datetime_to_str, datetime_now
 from cephadm.serve import CephadmServe
@@ -1344,7 +1344,7 @@ Then run the following:
 
         :param host: host name
         """
-        assert_valid_host(spec.hostname)
+        spec.validate()
         ip_addr = self._check_valid_addr(spec.hostname, spec.addr)
         if spec.addr == spec.hostname and ip_addr:
             spec.addr = ip_addr
index 2476e155fdf12e87e986d7b715b07992f6d6a224..1bf686f97cf6b9e516db9568609f552f1fed3da6 100644 (file)
@@ -1,9 +1,19 @@
 from collections import OrderedDict
 import errno
-try:
-    from typing import Optional, List, Any, Dict
-except ImportError:
-    pass  # just for type checking
+import re
+from typing import Optional, List, Any, Dict
+
+
+def assert_valid_host(name: str) -> None:
+    p = re.compile('^[a-zA-Z0-9-]+$')
+    try:
+        assert len(name) <= 250, 'name is too long (max 250 chars)'
+        for part in name.split('.'):
+            assert len(part) > 0, '.-delimited name component must not be empty'
+            assert len(part) <= 63, '.-delimited name component must not be more than 63 chars'
+            assert p.match(part), 'name component must include only a-z, 0-9, and -'
+    except AssertionError as e:
+        raise SpecValidationError(str(e))
 
 
 class SpecValidationError(Exception):
@@ -45,6 +55,9 @@ class HostSpec(object):
 
         self.location = location
 
+    def validate(self) -> None:
+        assert_valid_host(self.hostname)
+
     def to_json(self) -> Dict[str, Any]:
         r: Dict[str, Any] = {
             'hostname': self.hostname,
index 110d495af5b8405adebc818130d565bf804be147..0735bb6e30c1474bb8fc5aaf280996ab9f026d92 100644 (file)
@@ -8,25 +8,13 @@ from typing import Optional, Dict, Any, List, Union, Callable, Iterable, Type, T
 
 import yaml
 
-from ceph.deployment.hostspec import HostSpec, SpecValidationError
+from ceph.deployment.hostspec import HostSpec, SpecValidationError, assert_valid_host
 from ceph.deployment.utils import unwrap_ipv6
 
 ServiceSpecT = TypeVar('ServiceSpecT', bound='ServiceSpec')
 FuncT = TypeVar('FuncT', bound=Callable)
 
 
-def assert_valid_host(name: str) -> None:
-    p = re.compile('^[a-zA-Z0-9-]+$')
-    try:
-        assert len(name) <= 250, 'name is too long (max 250 chars)'
-        for part in name.split('.'):
-            assert len(part) > 0, '.-delimited name component must not be empty'
-            assert len(part) <= 63, '.-delimited name component must not be more than 63 chars'
-            assert p.match(part), 'name component must include only a-z, 0-9, and -'
-    except AssertionError as e:
-        raise SpecValidationError(str(e))
-
-
 def handle_type_error(method: FuncT) -> FuncT:
     @wraps(method)
     def inner(cls: Any, *args: Any, **kwargs: Any) -> Any: