Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check on custom labels #53

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wanna/core/models/base_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ class BaseInstanceModel(BaseModel, extra=Extra.ignore, validate_assignment=True)
_zone = validator("zone", allow_reuse=True)(validators.validate_zone)
_region = validator("region", allow_reuse=True)(validators.validate_region)
_network = validator("network", allow_reuse=True)(validators.validate_network_name)
_labels = validator("labels", allow_reuse=True)(validators.validate_labels)
1 change: 1 addition & 0 deletions src/wanna/core/models/gcp_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class GCPProfileModel(BaseModel, extra=Extra.forbid):

_ = validator("project_id", allow_reuse=True)(validators.validate_project_id)
_ = validator("zone", allow_reuse=True)(validators.validate_zone)
_ = validator("labels", allow_reuse=True)(validators.validate_labels)

@root_validator(pre=True)
def parse_region_from_zone(cls, values): # pylint: disable=no-self-argument,no-self-use
Expand Down
13 changes: 13 additions & 0 deletions src/wanna/core/utils/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re
from typing import Dict

from cron_validator import CronValidator
from google.api_core import exceptions
Expand Down Expand Up @@ -151,3 +152,15 @@ def validate_project_id(project_id: str) -> str:
"Cannot end with a hyphen."
)
return project_id


def validate_labels(labels: Dict[str, str]) -> Dict[str, str]:
for key, value in labels.items():
if not re.match(r"^[a-z]{1}[a-z0-9_-]{0,62}$", key) or not re.match(r"^[a-z0-9_-]{0,63}$", value):
raise ValueError(
"Invalid custom label!"
"Keys and values can contain only lowercase letters, numeric characters,"
"underscores, and dashes. Max length is 63 characters."
"https://cloud.google.com/compute/docs/labeling-resources#requirements"
)
return labels