main
py 48 lines 1.8 KB
Raw
1 from fastapi import HTTPException
2 from loguru import logger
3
4 from app.connectors.grafana.schema.folders import Folder
5 from app.connectors.grafana.schema.folders import FoldersResponse
6 from app.connectors.grafana.utils.universal import create_grafana_client
7
8
9 async def delete_folder(
10 organization_id: int,
11 folder_id: int,
12 ) -> dict:
13 """
14 Delete a folder for a given organization.
15
16 Args:
17 organization_id (int): The ID of the organization.
18 folder_id (int): The ID of the folder to be deleted.
19
20 Returns:
21 dict: The response from the Grafana client.
22 """
23 logger.info(
24 f"Updating dashboards for organization {organization_id} and folder {folder_id}",
25 )
26 try:
27 grafana_client = await create_grafana_client("Grafana")
28 # Switch to the newly created organization
29 grafana_client.user.switch_actual_user_organisation(organization_id)
30 logger.info(
31 f"Deleting folder {folder_id} for organization {organization_id}",
32 )
33 list_all_folders = grafana_client.folder.get_all_folders()
34
35 # Parse the response using the Pydantic model
36 folders_response = FoldersResponse(folders=[Folder(**folder) for folder in list_all_folders])
37 logger.info(f"Search for folder with ID {folder_id}")
38
39 # Ensure the folder_id is being compared correctly
40 folder_uid = next((folder.uid for folder in folders_response.folders if folder.id == folder_id), None)
41
42 if not folder_uid:
43 raise HTTPException(status_code=404, detail=f"Folder with ID {folder_id} not found")
44
45 return grafana_client.folder.delete_folder(folder_uid)
46 except Exception as e:
47 logger.error(f"Error deleting folder: {e}")
48 raise HTTPException(status_code=500, detail=f"Error deleting dashboard folder: {e}")