diff --git a/lib/charms/prometheus_k8s/v0/prometheus_remote_write.py b/lib/charms/prometheus_k8s/v0/prometheus_remote_write.py index 07a379f3..910d7ca5 100644 --- a/lib/charms/prometheus_k8s/v0/prometheus_remote_write.py +++ b/lib/charms/prometheus_k8s/v0/prometheus_remote_write.py @@ -45,7 +45,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 10 +LIBPATCH = 11 logger = logging.getLogger(__name__) @@ -58,7 +58,11 @@ DEFAULT_ALERT_RULES_RELATIVE_PATH = "./src/prometheus_alert_rules" -class RelationNotFoundError(Exception): +class RemoteWriteError(Exception): + """Base class remote-write errors.""" + + +class RelationNotFoundError(RemoteWriteError): """Raised if there is no relation with the given name.""" def __init__(self, relation_name: str): @@ -68,7 +72,7 @@ def __init__(self, relation_name: str): super().__init__(self.message) -class RelationInterfaceMismatchError(Exception): +class RelationInterfaceMismatchError(RemoteWriteError): """Raised if the relation with the given name has a different interface.""" def __init__( @@ -89,7 +93,7 @@ def __init__( super().__init__(self.message) -class RelationRoleMismatchError(Exception): +class RelationRoleMismatchError(RemoteWriteError): """Raised if the relation with the given name has a different direction.""" def __init__( @@ -445,7 +449,7 @@ def restore(self, snapshot): self.relation_id = snapshot["relation_id"] -class InvalidAlertRulePathError(Exception): +class InvalidAlertRulePathError(RemoteWriteError): """Raised if the alert rules folder cannot be found or is otherwise invalid.""" def __init__( @@ -764,7 +768,7 @@ def __init__( self, charm: CharmBase, relation_name: str = DEFAULT_RELATION_NAME, - endpoint_schema: str = "http", + endpoint_schema: str = "http", # TODO: in v1, rename to 'scheme' endpoint_address: str = "", endpoint_port: Union[str, int] = 9090, endpoint_path: str = "/api/v1/write", @@ -802,7 +806,7 @@ def __init__( self._charm = charm self.tool = CosTool(self._charm) self._relation_name = relation_name - self._endpoint_schema = endpoint_schema + self._endpoint_scheme = endpoint_schema.strip().rstrip("://") self._endpoint_address = endpoint_address self._endpoint_port = int(endpoint_port) self._endpoint_path = endpoint_path @@ -851,8 +855,12 @@ def _set_endpoint_on_relation(self, relation: Relation) -> None: if path and not path.startswith("/"): path = "/{}".format(path) + if not re.match(r"^\w+$", self._endpoint_scheme): + msg = f"Invalid scheme 'f{self._endpoint_scheme}'; did you mean 'http'?" + raise ValueError(msg) + endpoint_url = "{}://{}:{}{}".format( - self._endpoint_schema, address, str(self._endpoint_port), path + self._endpoint_scheme, address, str(self._endpoint_port), path ) relation.data[self._charm.unit]["remote_write"] = json.dumps( diff --git a/tox.ini b/tox.ini index fd4e7c25..92825804 100644 --- a/tox.ini +++ b/tox.ini @@ -73,7 +73,7 @@ deps = integration: pytest-operator==1.0.0b1 commands = charm: mypy {[vars]src_path} {posargs} - lib: mypy --python-version 3.5 {[vars]lib_path} {posargs} + lib: mypy --python-version 3.8 {[vars]lib_path} {posargs} unit: mypy {[vars]tst_path}/unit {posargs} integration: mypy {[vars]tst_path}/integration {posargs}