diff --git a/gso/api/api_v1/endpoints/import_site.py b/gso/api/api_v1/endpoints/import_site.py index 5c5ef1a0a586f48e3d6c09cdda1722ac8a0020f5..340c89b0fe65d2c74c9c5becc1e331bf7dce7c49 100644 --- a/gso/api/api_v1/endpoints/import_site.py +++ b/gso/api/api_v1/endpoints/import_site.py @@ -1,8 +1,8 @@ from typing import Dict, Any -from fastapi import HTTPException +from fastapi import HTTPException, status from fastapi.routing import APIRouter -from orchestrator.services import processes +from orchestrator.services import processes, subscriptions from pydantic import BaseModel from gso.products.product_blocks.site import SiteTier @@ -20,19 +20,31 @@ class SiteImport(BaseModel): site_bgp_community_id: int site_internal_id: int site_tier: SiteTier + site_ts_address: str -@router.post("/", tags=["Import"]) +@router.post("/", status_code=status.HTTP_201_CREATED, tags=["Import"]) def import_site(site: SiteImport) -> Dict[str, Any]: """ Import site by running the import_site workflow. + response: + - pid: The process id of the started process. + Raises: + HTTPException: If the site already exists or if there's an error in the process. """ + subscription = subscriptions.retrieve_subscription_by_subscription_instance_value( + resource_type="site_name", value=site.site_name, sub_status=("provisioning", "active")) + if subscription: + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Site already exists.") + pid = processes.start_process("import_site", [site.dict()]) if pid is None: - raise HTTPException(status_code=500, detail="Failed to start the process.") + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to start the process.") process = processes._get_process(pid) # pylint: disable=protected-access if process.last_status == "failed": raise HTTPException( - status_code=500, detail=f"Process {pid} failed because of an internal error.") + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail=f"Process {pid} failed because of an internal error.") + return {"pid": str(pid)} diff --git a/gso/workflows/tasks/import_site.py b/gso/workflows/tasks/import_site.py index 43f3686c4a8ca4405601efa7a8caae09e3988dfb..2dbc2a8a232589ffbb6b3115bf39d3be9ecf9cd5 100644 --- a/gso/workflows/tasks/import_site.py +++ b/gso/workflows/tasks/import_site.py @@ -38,6 +38,7 @@ def generate_initial_input_form() -> FormGenerator: site_bgp_community_id: int site_internal_id: int site_tier: SiteTier + site_ts_address: str user_input = yield ImportSite return user_input.dict()