| 1 | from fastapi import APIRouter |
| 2 | from fastapi import Depends |
| 3 | from fastapi import Security |
| 4 | from sqlalchemy.ext.asyncio import AsyncSession |
| 5 | |
| 6 | from app.auth.routes.auth import AuthHandler |
| 7 | from app.db.db_session import get_db |
| 8 | from app.integrations.huntress.schema.provision import ProvisionHuntressRequest |
| 9 | from app.integrations.huntress.schema.provision import ProvisionHuntressResponse |
| 10 | from app.integrations.huntress.services.provision import provision_huntress |
| 11 | from app.integrations.utils.utils import get_customer_integration_response |
| 12 | from app.schedulers.models.scheduler import CreateSchedulerRequest |
| 13 | from app.schedulers.scheduler import add_scheduler_jobs |
| 14 | |
| 15 | integration_huntress_provision_scheduler_router = APIRouter() |
| 16 | |
| 17 | |
| 18 | @integration_huntress_provision_scheduler_router.post( |
| 19 | "/provision", |
| 20 | response_model=ProvisionHuntressResponse, |
| 21 | description="Provision a Huntress integration.", |
| 22 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 23 | ) |
| 24 | async def provision_huntress_route( |
| 25 | provision_huntress_request: ProvisionHuntressRequest, |
| 26 | session: AsyncSession = Depends(get_db), |
| 27 | ) -> ProvisionHuntressResponse: |
| 28 | """ |
| 29 | Provisions a huntress integration. |
| 30 | |
| 31 | Args: |
| 32 | provision_huntress_request (ProvisionHuntressRequest): The request object containing the necessary data for provisioning. |
| 33 | session (AsyncSession, optional): The database session. Defaults to Depends(get_db). |
| 34 | |
| 35 | Returns: |
| 36 | ProvisionHuntressResponse: The response object indicating the success or failure of the provisioning process. |
| 37 | """ |
| 38 | # Check if the customer integration settings are available and can be provisioned |
| 39 | await get_customer_integration_response( |
| 40 | provision_huntress_request.customer_code, |
| 41 | session, |
| 42 | ) |
| 43 | await provision_huntress(provision_huntress_request, session) |
| 44 | await add_scheduler_jobs( |
| 45 | CreateSchedulerRequest( |
| 46 | function_name="invoke_huntress_integration_collect", |
| 47 | time_interval=provision_huntress_request.time_interval, |
| 48 | job_id="invoke_huntress_integration_collect", |
| 49 | ), |
| 50 | ) |
| 51 | return ProvisionHuntressResponse( |
| 52 | success=True, |
| 53 | message="Huntress integration provisioned successfully.", |
| 54 | ) |