Skip to content
Snippets Groups Projects

Draft: Nat 219 import trunks

Closed Mohammad Torkashvand requested to merge NAT-219-import-trunks into develop
19 files
+ 756
117
Compare changes
  • Side-by-side
  • Inline
Files
19
+ 30
42
import ipaddress
from typing import Any, Dict, Optional
from typing import Any, Dict
from uuid import UUID
from fastapi import Depends, HTTPException, status
from fastapi.routing import APIRouter
from orchestrator.security import opa_security_default
from orchestrator.services import processes, subscriptions
from pydantic import BaseModel
from sqlalchemy.exc import MultipleResultsFound
from gso.products.product_blocks.router import RouterRole, RouterVendor
from gso.products.product_blocks.site import SiteTier
from gso.schemas.imports import IptrunkImportModel, RouterImportModel, SiteImportModel
router = APIRouter(prefix="/imports", tags=["Import"], dependencies=[Depends(opa_security_default)])
router = APIRouter(prefix="/imports", tags=["Imports"], dependencies=[Depends(opa_security_default)])
def start_process(process_name: str, data: dict) -> UUID:
def _start_process(process_name: str, data: dict) -> UUID:
"""Start a process and handle common exceptions."""
pid: UUID = processes.start_process(process_name, [data])
@@ -32,27 +29,13 @@ def start_process(process_name: str, data: dict) -> UUID:
return pid
class SiteImport(BaseModel):
site_name: str
site_city: str
site_country: str
site_country_code: str
site_latitude: float
site_longitude: float
site_bgp_community_id: int
site_internal_id: int
site_tier: SiteTier
site_ts_address: str
customer: str
@router.post("/sites", status_code=status.HTTP_201_CREATED)
def import_site(site: SiteImport) -> Dict[str, Any]:
"""Import site by running the import_site workflow.
def import_site(site: SiteImportModel) -> Dict[str, Any]:
"""Import a site by running the import_site workflow.
Args:
----
site (SiteImport): The site information to be imported.
site (SiteImportModel): The site information to be imported.
Returns:
-------
@@ -71,26 +54,10 @@ def import_site(site: SiteImport) -> Dict[str, Any]:
except MultipleResultsFound:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Multiple subscriptions found.")
pid = start_process("import_site", site.dict())
pid = _start_process("import_site", site.dict())
return {"detail": "Site added successfully.", "pid": pid}
class RouterImportModel(BaseModel):
customer: str
router_site: str
hostname: str
ts_port: int
router_vendor: RouterVendor
router_role: RouterRole
is_ias_connected: bool
router_lo_ipv4_address: ipaddress.IPv4Address
router_lo_ipv6_address: ipaddress.IPv6Address
router_lo_iso_address: str
router_si_ipv4_network: Optional[ipaddress.IPv4Network] = None
router_ias_lt_ipv4_network: Optional[ipaddress.IPv4Network] = None
router_ias_lt_ipv6_network: Optional[ipaddress.IPv6Network] = None
@router.post("/routers", status_code=status.HTTP_201_CREATED)
def import_router(router_data: RouterImportModel) -> Dict[str, Any]:
"""Import a router by running the import_router workflow.
@@ -108,5 +75,26 @@ def import_router(router_data: RouterImportModel) -> Dict[str, Any]:
HTTPException: If there's an error in the process.
"""
pid = start_process("import_router", router_data.dict())
pid = _start_process("import_router", router_data.dict())
return {"detail": "Router added successfully", "pid": pid}
@router.post("/iptrunks", status_code=status.HTTP_201_CREATED)
def import_iptrunk(iptrunk_data: IptrunkImportModel) -> Dict[str, Any]:
"""Import an iptrunk by running the import_iptrunk workflow.
Args:
----
iptrunk_data (IptrunkImportModel): The iptrunk information to be imported.
Returns:
-------
dict: A dictionary containing the process id of the started process and detail message.
Raises:
------
HTTPException: If there's an error in the process.
"""
pid = _start_process("import_iptrunk", iptrunk_data.dict())
return {"detail": "Iptrunk added successfully", "pid": pid}
Loading