diff --git a/gso/api/v1/imports.py b/gso/api/v1/imports.py index fea1ca6289ca6e15aff816ac2e06e3748b0b4762..5ae848a47ef3b874c94ad1cd0d74aa665a497a47 100644 --- a/gso/api/v1/imports.py +++ b/gso/api/v1/imports.py @@ -15,8 +15,7 @@ from gso.products.product_blocks.router import RouterRole, RouterVendor from gso.products.product_blocks.site import SiteTier from gso.services import subscriptions from gso.services.crm import CustomerNotFoundError, get_customer_by_name -from gso.utils.helpers import LAGMember -from gso.workflows.site.base_site_model import BaseSiteValidatorModel +from gso.utils.helpers import BaseSiteValidatorModel, LAGMember router = APIRouter(prefix="/imports", tags=["Imports"], dependencies=[Depends(opa_security_default)]) diff --git a/gso/utils/helpers.py b/gso/utils/helpers.py index d41d19c47f3d80895cc5abc5663d2400d8830184..cbd56d1961ddeb65cca193e761422f34171d306f 100644 --- a/gso/utils/helpers.py +++ b/gso/utils/helpers.py @@ -8,11 +8,13 @@ from uuid import UUID import pycountry from orchestrator import step from orchestrator.types import State, UUIDstr -from pydantic import BaseModel +from pydantic import BaseModel, validator +from pydantic.fields import ModelField from pydantic_forms.validators import Choice from gso.products.product_blocks.iptrunk import IptrunkInterfaceBlock from gso.products.product_blocks.router import RouterVendor +from gso.products.product_blocks.site import SiteTier from gso.products.product_types.iptrunk import Iptrunk from gso.products.product_types.router import Router from gso.services import provisioning_proxy @@ -219,3 +221,45 @@ def validate_site_name(site_name: str) -> str: ) raise ValueError(msg) return site_name + + +class BaseSiteValidatorModel(BaseModel): + """A base site validator model extended by create site and by import site.""" + + site_bgp_community_id: int + site_internal_id: int + site_tier: SiteTier + site_ts_address: str + + @validator("site_ts_address", check_fields=False, allow_reuse=True) + def validate_ts_address(cls, site_ts_address: str) -> str: + """Validate that a terminal server address is valid.""" + validate_ipv4_or_ipv6(site_ts_address) + return site_ts_address + + @validator("site_country_code", check_fields=False, allow_reuse=True) + def country_code_must_exist(cls, country_code: str) -> str: + """Validate that the country code exists.""" + validate_country_code(country_code) + return country_code + + @validator( + "site_ts_address", + "site_internal_id", + "site_bgp_community_id", + "site_name", + check_fields=False, + allow_reuse=True, + ) + def validate_unique_fields(cls, value: str, field: ModelField) -> str | int: + """Validate that the internal and :term:`BGP` community IDs are unique.""" + return validate_site_fields_is_unique(field.name, value) + + @validator("site_name", check_fields=False, allow_reuse=True) + def site_name_must_be_valid(cls, site_name: str) -> str: + """Validate the site name. + + The site name must consist of three uppercase letters, followed by an optional single digit. + """ + validate_site_name(site_name) + return site_name diff --git a/gso/workflows/site/base_site_model.py b/gso/workflows/site/base_site_model.py deleted file mode 100644 index ff2696c73be5dc340de760e4a2cb672b03a69d64..0000000000000000000000000000000000000000 --- a/gso/workflows/site/base_site_model.py +++ /dev/null @@ -1,54 +0,0 @@ -"""A base site validator model extended by create site and by import site.""" - -from pydantic import BaseModel, validator -from pydantic.fields import ModelField - -from gso.products.product_blocks.site import SiteTier -from gso.utils.helpers import ( - validate_country_code, - validate_ipv4_or_ipv6, - validate_site_fields_is_unique, - validate_site_name, -) - - -class BaseSiteValidatorModel(BaseModel): - """A base site validator model extended by create site and by import site.""" - - site_bgp_community_id: int - site_internal_id: int - site_tier: SiteTier - site_ts_address: str - - @validator("site_ts_address", check_fields=False, allow_reuse=True) - def validate_ts_address(cls, site_ts_address: str) -> str: - """Validate that a terminal server address is valid.""" - validate_ipv4_or_ipv6(site_ts_address) - return site_ts_address - - @validator("site_country_code", check_fields=False, allow_reuse=True) - def country_code_must_exist(cls, country_code: str) -> str: - """Validate that the country code exists.""" - validate_country_code(country_code) - return country_code - - @validator( - "site_ts_address", - "site_internal_id", - "site_bgp_community_id", - "site_name", - check_fields=False, - allow_reuse=True, - ) - def validate_unique_fields(cls, value: str, field: ModelField) -> str | int: - """Validate that the internal and :term:`BGP` community IDs are unique.""" - return validate_site_fields_is_unique(field.name, value) - - @validator("site_name", check_fields=False, allow_reuse=True) - def site_name_must_be_valid(cls, site_name: str) -> str: - """Validate the site name. - - The site name must consist of three uppercase letters, followed by an optional single digit. - """ - validate_site_name(site_name) - return site_name diff --git a/gso/workflows/site/create_site.py b/gso/workflows/site/create_site.py index 2207d2dbf267b7d54d271e92d295c27b7bca831c..d94c119629f95070fc68d87f8161cdb7c79f5053 100644 --- a/gso/workflows/site/create_site.py +++ b/gso/workflows/site/create_site.py @@ -11,7 +11,7 @@ from gso.products.product_blocks import site as site_pb from gso.products.product_blocks.site import LatitudeCoordinate, LongitudeCoordinate from gso.products.product_types import site from gso.services.crm import customer_selector -from gso.workflows.site.base_site_model import BaseSiteValidatorModel +from gso.utils.helpers import BaseSiteValidatorModel def initial_input_form_generator(product_name: str) -> FormGenerator: