##
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]]:
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)