ansible-collection-hetzner-.../plugins/module_utils/vendor/hcloud/helpers/labels.py
Jonas L 8a6157e8b2
feat: vendor hcloud python dependency (#244)
* chore: ignore venv directories

* chore: ignore integration test generated inventory

* feat: vendor hcloud package

* import https://github.com/hetznercloud/hcloud-python

* use vendored hcloud in modules

* update integration test requirements

* make vendor script self contained

* chore: add  check-hcloud-vendor pre-commit hook

* pin hcloud version to v.1.24.0

* move vendored __version__.py file to _version.py

* update comment about galaxy-importer filename lint
2023-07-11 11:15:08 +02:00

39 lines
1.4 KiB
Python

import re
from typing import Dict
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]) -> (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, ""