| 1 | from fastapi import HTTPException |
| 2 | |
| 3 | import app.agents.wazuh.services.agents as wazuh_services |
| 4 | from app.db.db_session import session |
| 5 | from app.db.universal_models import Agents |
| 6 | |
| 7 | |
| 8 | def delete_agent_db(agent_id: str): |
| 9 | """ |
| 10 | Delete agent from database. |
| 11 | |
| 12 | Args: |
| 13 | agent_id (str): The ID of the agent to be deleted. |
| 14 | |
| 15 | Returns: |
| 16 | dict: A dictionary indicating the success of the operation and a message. |
| 17 | """ |
| 18 | agent = session.query(Agents).filter(Agents.agent_id == agent_id).first() |
| 19 | if not agent: |
| 20 | raise HTTPException( |
| 21 | status_code=404, |
| 22 | detail=f"Agent with agent_id {agent_id} not found", |
| 23 | ) |
| 24 | session.delete(agent) |
| 25 | session.commit() |
| 26 | return {"success": True, "message": f"Agent {agent_id} deleted from database"} |
| 27 | |
| 28 | |
| 29 | def delete_agent_wazuh(agent_id: str): |
| 30 | """ |
| 31 | Delete agent from Wazuh service. |
| 32 | |
| 33 | Args: |
| 34 | agent_id (str): The ID of the agent to be deleted. |
| 35 | |
| 36 | Returns: |
| 37 | dict: A dictionary containing the success status and a message. |
| 38 | |
| 39 | Raises: |
| 40 | HTTPException: If there is an error while deleting the agent from Wazuh. |
| 41 | """ |
| 42 | try: |
| 43 | wazuh_services.delete_agent(agent_id) |
| 44 | return {"success": True, "message": f"Agent {agent_id} deleted from Wazuh"} |
| 45 | except Exception as e: |
| 46 | raise HTTPException( |
| 47 | status_code=500, |
| 48 | detail=f"Failed to delete agent {agent_id} from Wazuh: {e}", |
| 49 | ) |