| 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.utils import AuthHandler |
| 7 | from app.db.db_session import get_db |
| 8 | from app.integrations.carbonblack.schema.provision import ProvisionCarbonBlackRequest |
| 9 | from app.integrations.carbonblack.schema.provision import ProvisionCarbonBlackResponse |
| 10 | from app.integrations.carbonblack.services.provision import provision_carbonblack |
| 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 | from app.schedulers.services.invoke_carbonblack import ( |
| 15 | invoke_carbonblack_integration_collect, |
| 16 | ) |
| 17 | |
| 18 | integration_carbonblack_provision_scheduler_router = APIRouter() |
| 19 | |
| 20 | |
| 21 | @integration_carbonblack_provision_scheduler_router.post( |
| 22 | "/provision", |
| 23 | response_model=ProvisionCarbonBlackResponse, |
| 24 | description="Provision a CarbonBlack integration.", |
| 25 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 26 | ) |
| 27 | async def provision_carbonblack_route( |
| 28 | provision_carbonblack_request: ProvisionCarbonBlackRequest, |
| 29 | session: AsyncSession = Depends(get_db), |
| 30 | ) -> ProvisionCarbonBlackResponse: |
| 31 | """ |
| 32 | Provisions a carbonblack integration. |
| 33 | |
| 34 | Args: |
| 35 | provision_carbonblack_request (ProvisionCarbonBlackRequest): The request object containing the necessary data for provisioning. |
| 36 | session (AsyncSession, optional): The database session. Defaults to Depends(get_db). |
| 37 | |
| 38 | Returns: |
| 39 | ProvisionCarbonBlackResponse: The response object indicating the success or failure of the provisioning process. |
| 40 | """ |
| 41 | # Check if the customer integration settings are available and can be provisioned |
| 42 | await get_customer_integration_response( |
| 43 | provision_carbonblack_request.customer_code, |
| 44 | session, |
| 45 | ) |
| 46 | await provision_carbonblack(provision_carbonblack_request, session) |
| 47 | await add_scheduler_jobs( |
| 48 | CreateSchedulerRequest( |
| 49 | function_name="invoke_carbonblack_integration_collection", |
| 50 | time_interval=provision_carbonblack_request.time_interval, |
| 51 | job_id="invoke_carbonblack_integration_collection", |
| 52 | ), |
| 53 | ) |
| 54 | return ProvisionCarbonBlackResponse( |
| 55 | success=True, |
| 56 | message="CarbonBlack provisioned successfully", |
| 57 | ) |
| 58 | |
| 59 | |
| 60 | @integration_carbonblack_provision_scheduler_router.get( |
| 61 | "/test", |
| 62 | response_model=ProvisionCarbonBlackResponse, |
| 63 | description="Invoke a CarbonBlack integration for testing", |
| 64 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 65 | ) |
| 66 | async def test() -> ProvisionCarbonBlackResponse: |
| 67 | """ |
| 68 | Invoke a CarbonBlack integration for testing. |
| 69 | """ |
| 70 | await invoke_carbonblack_integration_collect() |
| 71 | return ProvisionCarbonBlackResponse( |
| 72 | success=True, |
| 73 | message="CarbonBlack provisioned successfully", |
| 74 | ) |