Skip to content
Snippets Groups Projects
Commit 875c0df0 authored by JORGE SASIAIN's avatar JORGE SASIAIN
Browse files

NAT-356: move BaseSiteValidatorModel to utils/helpers.py

parent 96c56efd
No related branches found
No related tags found
1 merge request!112Address SonarQube code smells
Pipeline #84803 failed
This commit is part of merge request !112. Comments created here will be created in the context of that merge request.
...@@ -15,8 +15,7 @@ from gso.products.product_blocks.router import RouterRole, RouterVendor ...@@ -15,8 +15,7 @@ from gso.products.product_blocks.router import RouterRole, RouterVendor
from gso.products.product_blocks.site import SiteTier from gso.products.product_blocks.site import SiteTier
from gso.services import subscriptions from gso.services import subscriptions
from gso.services.crm import CustomerNotFoundError, get_customer_by_name from gso.services.crm import CustomerNotFoundError, get_customer_by_name
from gso.utils.helpers import LAGMember from gso.utils.helpers import BaseSiteValidatorModel, LAGMember
from gso.workflows.site.base_site_model import BaseSiteValidatorModel
router = APIRouter(prefix="/imports", tags=["Imports"], dependencies=[Depends(opa_security_default)]) router = APIRouter(prefix="/imports", tags=["Imports"], dependencies=[Depends(opa_security_default)])
......
...@@ -8,11 +8,13 @@ from uuid import UUID ...@@ -8,11 +8,13 @@ from uuid import UUID
import pycountry import pycountry
from orchestrator import step from orchestrator import step
from orchestrator.types import State, UUIDstr 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 pydantic_forms.validators import Choice
from gso.products.product_blocks.iptrunk import IptrunkInterfaceBlock from gso.products.product_blocks.iptrunk import IptrunkInterfaceBlock
from gso.products.product_blocks.router import RouterVendor 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.iptrunk import Iptrunk
from gso.products.product_types.router import Router from gso.products.product_types.router import Router
from gso.services import provisioning_proxy from gso.services import provisioning_proxy
...@@ -219,3 +221,45 @@ def validate_site_name(site_name: str) -> str: ...@@ -219,3 +221,45 @@ def validate_site_name(site_name: str) -> str:
) )
raise ValueError(msg) raise ValueError(msg)
return site_name 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
"""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
...@@ -11,7 +11,7 @@ from gso.products.product_blocks import site as site_pb ...@@ -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_blocks.site import LatitudeCoordinate, LongitudeCoordinate
from gso.products.product_types import site from gso.products.product_types import site
from gso.services.crm import customer_selector 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: def initial_input_form_generator(product_name: str) -> FormGenerator:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment