Skip to content
Snippets Groups Projects
Verified Commit 4de070dd authored by Karel van Klink's avatar Karel van Klink :smiley_cat:
Browse files

Update type for site name

parent 42ba200f
No related branches found
No related tags found
1 merge request!265Feature/refactor validators
Pipeline #88972 passed
......@@ -5,6 +5,7 @@ from orchestrator.types import SubscriptionLifecycle, strEnum
from gso.types.coordinates import LatitudeCoordinate, LongitudeCoordinate
from gso.types.ip_address import IPAddress
from gso.types.site_name import SiteName
class SiteTier(strEnum):
......@@ -27,7 +28,7 @@ class SiteBlockInactive(
):
"""A site that's currently inactive, see :class:`SiteBlock`."""
site_name: str | None = None
site_name: SiteName | None = None
site_city: str | None = None
site_country: str | None = None
site_country_code: str | None = None
......@@ -42,7 +43,7 @@ class SiteBlockInactive(
class SiteBlockProvisioning(SiteBlockInactive, lifecycle=[SubscriptionLifecycle.PROVISIONING]):
"""A site that's currently being provisioned, see :class:`SiteBlock`."""
site_name: str
site_name: SiteName
site_city: str
site_country: str
site_country_code: str
......@@ -59,7 +60,7 @@ class SiteBlock(SiteBlockProvisioning, lifecycle=[SubscriptionLifecycle.ACTIVE])
#: The name of the site, that will dictate part of the :term:`FQDN` of routers that are hosted at this site. For
#: example: ``router.X.Y.geant.net``, where X denotes the name of the site.
site_name: str
site_name: SiteName
#: The city at which the site is located.
site_city: str
#: The country in which the site is located.
......
......@@ -6,7 +6,7 @@ from typing import Annotated
from pydantic import AfterValidator
def validate_site_name(site_name: str) -> None:
def validate_site_name(site_name: str) -> str:
"""Validate the site name.
The site name must consist of three uppercase letters, optionally followed by a single digit.
......@@ -18,6 +18,7 @@ def validate_site_name(site_name: str) -> None:
f"digit (0-9). Received: {site_name}"
)
raise ValueError(msg)
return site_name
SiteName = Annotated[str, AfterValidator(validate_site_name)]
......@@ -7,13 +7,15 @@ from pydantic_core.core_schema import ValidationInfo
from gso.services import subscriptions
T = TypeVar("T")
def validate_field_is_unique(value: str | int, info: ValidationInfo) -> None:
def validate_field_is_unique(value: T, info: ValidationInfo) -> T:
"""Validate that a field is unique."""
if len(subscriptions.get_active_subscriptions_by_field_and_value(str(info.field_name), str(value))) > 0:
msg = f"{info.field_name} must be unique"
raise ValueError(msg)
return value
T = TypeVar("T")
UniqueField = Annotated[T, str | int, AfterValidator(validate_field_is_unique)]
UniqueField = Annotated[T, AfterValidator(validate_field_is_unique)]
......@@ -65,7 +65,7 @@ def site_subscription_factory(faker, geant_partner):
partner = geant_partner
description = description or "Site Subscription"
site_name = site_name or faker.domain_word()
site_name = site_name or faker.site_name()
site_city = site_city or faker.city()
site_country = site_country or faker.country()
site_country_code = site_country_code or faker.country_code()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment