diff --git a/docs/content/grafana_api/folder.md b/docs/content/grafana_api/folder.md index c847bb4..1e2c52c 100644 --- a/docs/content/grafana_api/folder.md +++ b/docs/content/grafana_api/folder.md @@ -12,7 +12,8 @@ * [get\_folder\_permissions](#folder.Folder.get_folder_permissions) * [update\_folder\_permissions](#folder.Folder.update_folder_permissions) * [get\_folder\_id\_by\_dashboard\_path](#folder.Folder.get_folder_id_by_dashboard_path) - * [get\_all\_folder\_ids\_and\_names](#folder.Folder.get_all_folder_ids_and_names) + * [get\_folder\_uid\_by\_dashboard\_path](#folder.Folder.get_folder_uid_by_dashboard_path) + * [get\_all\_folder\_ids\_uids\_and\_names](#folder.Folder.get_all_folder_ids_uids_and_names) @@ -327,17 +328,42 @@ The method includes a functionality to extract the folder id specified inside mo - `folder_id` _int_ - Returns the folder id - + -#### get\_all\_folder\_ids\_and\_names +#### get\_folder\_uid\_by\_dashboard\_path ```python -def get_all_folder_ids_and_names() -> list +def get_folder_uid_by_dashboard_path(dashboard_path: str) -> str | None ``` -The method extract all folder id and names inside the complete organisation +The method includes a functionality to extract the folder uid specified inside model dashboard path + +**Arguments**: + +- `dashboard_path` _str_ - Specify the dashboard path + + +**Raises**: + +- `ValueError` - Missed specifying a necessary value +- `Exception` - Unspecified error by executing the API call + + +**Returns**: + +- `folder_uid` _str_ - Returns the folder uid + + + +#### get\_all\_folder\_ids\_uids\_and\_names + +```python +def get_all_folder_ids_uids_and_names() -> list +``` + +The method extract all folder id, uid and names inside the complete organisation **Returns**: -- `folders` _list_ - Returns a list of dicts with folder ids and the corresponding names +- `folders` _list_ - Returns a list of dicts with folder ids, uids and the corresponding names diff --git a/grafana_api/dashboard.py b/grafana_api/dashboard.py index 03cfb12..8dcc7a6 100644 --- a/grafana_api/dashboard.py +++ b/grafana_api/dashboard.py @@ -43,13 +43,13 @@ def create_or_update_dashboard( """ if len(dashboard_path) != 0 and dashboard_json != dict() and len(message) != 0: - folder_id: int = Folder( + folder_uid: str = Folder( self.grafana_api_model - ).get_folder_id_by_dashboard_path(dashboard_path) + ).get_folder_uid_by_dashboard_path(dashboard_path) dashboard_json_complete: dict = { "dashboard": dashboard_json, - "folderId": folder_id, + "folderUID": folder_uid, "message": message, "overwrite": overwrite, } @@ -197,12 +197,16 @@ def get_dashboard_uid_and_id_by_name_and_folder( """ if len(dashboard_name) != 0 and len(dashboard_path) != 0: - folder_id: int = Folder( + folder_uid: str = Folder( self.grafana_api_model - ).get_folder_id_by_dashboard_path(dashboard_path) + ).get_folder_uid_by_dashboard_path(dashboard_path) + + folder_query_parameter: str = f"folderUIDs={folder_uid}" + if folder_uid is None: + folder_query_parameter = "" search_query: str = ( - f"{APIEndpoints.SEARCH.value}?folderIds={folder_id}&query={dashboard_name}" + f"{APIEndpoints.SEARCH.value}?{folder_query_parameter}&query={dashboard_name}" ) dashboard_meta: list = Api(self.grafana_api_model).call_the_api( search_query diff --git a/grafana_api/folder.py b/grafana_api/folder.py index cc5826b..cbfe910 100644 --- a/grafana_api/folder.py +++ b/grafana_api/folder.py @@ -373,7 +373,7 @@ def get_folder_id_by_dashboard_path(self, dashboard_path: str) -> int: return 0 if len(dashboard_path) != 0: - folders: list = self.get_all_folder_ids_and_names() + folders: list = self.get_all_folder_ids_uids_and_names() folder_id: int = 0 for f in folders: @@ -391,22 +391,60 @@ def get_folder_id_by_dashboard_path(self, dashboard_path: str) -> int: logging.error("There is no dashboard_path defined.") raise ValueError - def get_all_folder_ids_and_names(self) -> list: - """The method extract all folder id and names inside the complete organisation + def get_folder_uid_by_dashboard_path(self, dashboard_path: str) -> str | None: + """The method includes a functionality to extract the folder uid specified inside model dashboard path + + Args: + dashboard_path (str): Specify the dashboard path + + Raises: + ValueError: Missed specifying a necessary value + Exception: Unspecified error by executing the API call + + Returns: + folder_uid (str): Returns the folder uid + """ + + # TODO Check the general folder uid + + if dashboard_path.lower() == "general": + return None + + if len(dashboard_path) != 0: + folders: list = self.get_all_folder_ids_uids_and_names() + folder_uid: str | None = None + + for f in folders: + if dashboard_path == f.get("title"): + folder_uid = f.get("uid") + + if folder_uid is None: + logging.error( + f"There's no folder_uid for the dashboard named {dashboard_path} available." + ) + raise Exception + + return folder_uid + else: + logging.error("There is no dashboard_path defined.") + raise ValueError + + def get_all_folder_ids_uids_and_names(self) -> list: + """The method extract all folder id, uid and names inside the complete organisation Returns: - folders (list): Returns a list of dicts with folder ids and the corresponding names + folders (list): Returns a list of dicts with folder ids, uids and the corresponding names """ folders_raw: list = Api(self.grafana_api_model).call_the_api( - f"{APIEndpoints.SEARCH.value}?folderIds=0" + f"{APIEndpoints.SEARCH.value}?folderUIDs" ) folders_raw_len: int = len(folders_raw) folders: list = list() for i in range(0, folders_raw_len): folders.append( - {"title": folders_raw[i].get("title"), "id": folders_raw[i].get("id")} + {"title": folders_raw[i].get("title"), "id": folders_raw[i].get("id"), "uid": folders_raw[i].get("uid")} ) return folders diff --git a/tests/integrationtest/test_dashboard.py b/tests/integrationtest/test_dashboard.py index e2fba7b..13a9f64 100644 --- a/tests/integrationtest/test_dashboard.py +++ b/tests/integrationtest/test_dashboard.py @@ -76,6 +76,11 @@ def test_d_dashboard_creation_general_folder(self): overwrite=True, ) + + # print(self.dashboard.get_dashboard_uid_and_id_by_name_and_folder( + # dashboard_path="General", + # dashboard_name=os.environ["GRAFANA_DASHBOARD_NAME"], + # )) self.assertEqual( "test1", self.dashboard.get_dashboard_uid_and_id_by_name_and_folder( diff --git a/tests/integrationtest/test_folder.py b/tests/integrationtest/test_folder.py index e899dfb..6b0be7b 100644 --- a/tests/integrationtest/test_folder.py +++ b/tests/integrationtest/test_folder.py @@ -22,46 +22,14 @@ def test_get_folders(self): def test_get_folder_by_uids(self): self.assertEqual( - { - "canAdmin": True, - "canDelete": True, - "canEdit": True, - "canSave": True, - "created": "2022-01-10T00:24:58+01:00", - "createdBy": "Anonymous", - "hasAcl": False, - "id": 72, - "orgId": 4, - "title": "Github Integrationtest", - "uid": "6U_QdWJnz", - "updated": "2022-01-10T00:24:58+01:00", - "updatedBy": "Anonymous", - "url": "/dashboards/f/6U_QdWJnz/github-integrationtest", - "version": 1, - }, - self.folder.get_folder_by_uid("6U_QdWJnz"), + "Github Integrationtest", + self.folder.get_folder_by_uid("6U_QdWJnz").get("title"), ) def test_get_folder_by_id(self): self.assertEqual( - { - "canAdmin": True, - "canDelete": True, - "canEdit": True, - "canSave": True, - "created": "2022-01-10T00:24:58+01:00", - "createdBy": "Anonymous", - "hasAcl": False, - "id": 72, - "orgId": 4, - "title": "Github Integrationtest", - "uid": "6U_QdWJnz", - "updated": "2022-01-10T00:24:58+01:00", - "updatedBy": "Anonymous", - "url": "/dashboards/f/6U_QdWJnz/github-integrationtest", - "version": 1, - }, - self.folder.get_folder_by_id(72), + "Github Integrationtest", + self.folder.get_folder_by_id(72).get("title"), ) def test_a_create_folder(self): @@ -138,8 +106,8 @@ def test_get_folder_id_by_dashboard_path_general_folder(self): 0, self.folder.get_folder_id_by_dashboard_path("General") ) - def test_get_all_folder_ids_and_names(self): + def test_get_all_folder_ids_uids_and_names(self): self.assertEqual( - list([{"id": 72, "title": "Github Integrationtest"}]), - self.folder.get_all_folder_ids_and_names(), + list([{"id": 72, "uid": "6U_QdWJnz", "title": "Github Integrationtest"}]), + self.folder.get_all_folder_ids_uids_and_names(), )