From: Sebastian Wagner Date: Wed, 22 Sep 2021 11:46:52 +0000 (+0200) Subject: python-common: HostSpec: add `validate()` X-Git-Tag: v17.1.0~253^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=7c6d922dead8480cd1f2cd05be7ccd1d8d5b7dd8;p=ceph.git python-common: HostSpec: add `validate()` Adjust HostSpec interface to ServiceSpec Signed-off-by: Sebastian Wagner --- diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index bbd37e80ab8..0daef476a88 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -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 diff --git a/src/python-common/ceph/deployment/hostspec.py b/src/python-common/ceph/deployment/hostspec.py index 2476e155fdf..1bf686f97cf 100644 --- a/src/python-common/ceph/deployment/hostspec.py +++ b/src/python-common/ceph/deployment/hostspec.py @@ -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, diff --git a/src/python-common/ceph/deployment/service_spec.py b/src/python-common/ceph/deployment/service_spec.py index 110d495af5b..0735bb6e30c 100644 --- a/src/python-common/ceph/deployment/service_spec.py +++ b/src/python-common/ceph/deployment/service_spec.py @@ -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: