mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +00:00
* deps: update dependency hcloud to v1.27.1 * chore: update vendored files --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: jo <ljonas@riseup.net>
40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
|
|
class LabelValidator:
|
|
KEY_REGEX = re.compile(
|
|
r"^([a-z0-9A-Z]((?:[\-_.]|[a-z0-9A-Z]){0,253}[a-z0-9A-Z])?/)?[a-z0-9A-Z]((?:[\-_.]|[a-z0-9A-Z]|){0,61}[a-z0-9A-Z])?$"
|
|
)
|
|
VALUE_REGEX = re.compile(
|
|
r"^(([a-z0-9A-Z](?:[\-_.]|[a-z0-9A-Z]){0,61})?[a-z0-9A-Z]$|$)"
|
|
)
|
|
|
|
@staticmethod
|
|
def validate(labels: dict[str, str]) -> bool:
|
|
"""Validates Labels. If you want to know which key/value pair of the dict is not correctly formatted
|
|
use :func:`~hcloud.helpers.labels.validate_verbose`.
|
|
|
|
:return: bool
|
|
"""
|
|
for k, v in labels.items():
|
|
if LabelValidator.KEY_REGEX.match(k) is None:
|
|
return False
|
|
if LabelValidator.VALUE_REGEX.match(v) is None:
|
|
return False
|
|
return True
|
|
|
|
@staticmethod
|
|
def validate_verbose(labels: dict[str, str]) -> tuple[bool, str]:
|
|
"""Validates Labels and returns the corresponding error message if something is wrong. Returns True, <empty string>
|
|
if everything is fine.
|
|
|
|
:return: bool, str
|
|
"""
|
|
for k, v in labels.items():
|
|
if LabelValidator.KEY_REGEX.match(k) is None:
|
|
return False, f"label key {k} is not correctly formatted"
|
|
if LabelValidator.VALUE_REGEX.match(v) is None:
|
|
return False, f"label value {v} (key: {k}) is not correctly formatted"
|
|
return True, ""
|