X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=installers%2Fcharm%2Flcm%2Fsrc%2Fpod_spec.py;h=dc214537bca14d562b0dc60167370647d4369c99;hb=13722ca955cf2dfc9fd7b11a4c11ea5dc63f1e46;hp=c0e9624c72c5f2bcbcc348e40d73a710e549b68a;hpb=426d4938f27b200f359078258f41109b399d5e06;p=osm%2Fdevops.git diff --git a/installers/charm/lcm/src/pod_spec.py b/installers/charm/lcm/src/pod_spec.py index c0e9624c..dc214537 100644 --- a/installers/charm/lcm/src/pod_spec.py +++ b/installers/charm/lcm/src/pod_spec.py @@ -21,43 +21,60 @@ ## import logging -from pydantic import BaseModel, constr, PositiveInt, validator -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, NoReturn logger = logging.getLogger(__name__) -class ConfigData(BaseModel): - """Configuration data model.""" +def _validate_data( + config_data: Dict[str, Any], relation_data: Dict[str, Any] +) -> NoReturn: + """Validate input data. - database_commonkey: constr(min_length=1) - log_level: constr(regex=r"^(INFO|DEBUG)$") - vca_host: constr(min_length=1) - vca_port: PositiveInt - vca_user: constr(min_length=1) - vca_pubkey: constr(min_length=1) - vca_password: constr(min_length=1) - vca_cacert: str - vca_cloud: constr(min_length=1) - vca_k8s_cloud: constr(min_length=1) - vca_apiproxy: Optional[constr(min_length=1)] + Args: + config_data (Dict[str, Any]): configuration data. + relation_data (Dict[str, Any]): relation data. + """ + config_validators = { + "database_commonkey": lambda value, _: isinstance(value, str) + and len(value) > 1, + "log_level": lambda value, _: isinstance(value, str) + and value in ("INFO", "DEBUG"), + "vca_host": lambda value, _: isinstance(value, str) and len(value) > 1, + "vca_port": lambda value, _: isinstance(value, int) and value > 0, + "vca_user": lambda value, _: isinstance(value, str) and len(value) > 1, + "vca_pubkey": lambda value, _: isinstance(value, str) and len(value) > 1, + "vca_password": lambda value, _: isinstance(value, str) and len(value) > 1, + "vca_cacert": lambda value, _: isinstance(value, str), + "vca_cloud": lambda value, _: isinstance(value, str) and len(value) > 1, + "vca_k8s_cloud": lambda value, _: isinstance(value, str) and len(value) > 1, + "vca_apiproxy": lambda value, _: (isinstance(value, str) and len(value) > 1) + if value + else True, + } + relation_validators = { + "ro_host": lambda value, _: isinstance(value, str) and len(value) > 1, + "ro_port": lambda value, _: isinstance(value, int) and value > 0, + "message_host": lambda value, _: isinstance(value, str) and len(value) > 1, + "message_port": lambda value, _: isinstance(value, int) and value > 0, + "database_uri": lambda value, _: isinstance(value, str) and len(value) > 1, + } + problems = [] - @validator("vca_apiproxy", pre=True, always=True) - def validate_vca_apiproxy(cls, value, values, **kwargs): - if not value: - return None + for key, validator in config_validators.items(): + valid = validator(config_data.get(key), config_data) - return value + if not valid: + problems.append(key) + for key, validator in relation_validators.items(): + valid = validator(relation_data.get(key), relation_data) -class RelationData(BaseModel): - """Relation data model.""" + if not valid: + problems.append(key) - ro_host: constr(min_length=1) - ro_port: PositiveInt - message_host: constr(min_length=1) - message_port: PositiveInt - database_uri: constr(regex=r"^(mongodb://)") + if len(problems) > 0: + raise ValueError("Errors found in: {}".format(", ".join(problems))) def _make_pod_ports(port: int) -> List[Dict[str, Any]]: @@ -196,8 +213,7 @@ def make_pod_spec( if not image_info: return None - ConfigData(**(config)) - RelationData(**(relation_state)) + _validate_data(config, relation_state) ports = _make_pod_ports(port) env_config = _make_pod_envconfig(config, relation_state)