Skip to content

Commit fe7f52b

Browse files
committed
feat: Add the first version
1 parent b250ddc commit fe7f52b

File tree

4 files changed

+66
-51
lines changed

4 files changed

+66
-51
lines changed

grafana_api/dashboard.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def create_or_update_dashboard(
4343
"""
4444

4545
if len(dashboard_path) != 0 and dashboard_json != dict() and len(message) != 0:
46-
folder_id: int = Folder(
46+
folder_uid: str = Folder(
4747
self.grafana_api_model
48-
).get_folder_id_by_dashboard_path(dashboard_path)
48+
).get_folder_uid_by_dashboard_path(dashboard_path)
4949

5050
dashboard_json_complete: dict = {
5151
"dashboard": dashboard_json,
52-
"folderId": folder_id,
52+
"folderUID": folder_uid,
5353
"message": message,
5454
"overwrite": overwrite,
5555
}
@@ -197,12 +197,16 @@ def get_dashboard_uid_and_id_by_name_and_folder(
197197
"""
198198

199199
if len(dashboard_name) != 0 and len(dashboard_path) != 0:
200-
folder_id: int = Folder(
200+
folder_uid: str = Folder(
201201
self.grafana_api_model
202-
).get_folder_id_by_dashboard_path(dashboard_path)
202+
).get_folder_uid_by_dashboard_path(dashboard_path)
203+
204+
folder_query_parameter: str = f"folderUIDs={folder_uid}"
205+
if folder_uid is None:
206+
folder_query_parameter = ""
203207

204208
search_query: str = (
205-
f"{APIEndpoints.SEARCH.value}?folderIds={folder_id}&query={dashboard_name}"
209+
f"{APIEndpoints.SEARCH.value}?{folder_query_parameter}&query={dashboard_name}"
206210
)
207211
dashboard_meta: list = Api(self.grafana_api_model).call_the_api(
208212
search_query

grafana_api/folder.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def get_folder_id_by_dashboard_path(self, dashboard_path: str) -> int:
373373
return 0
374374

375375
if len(dashboard_path) != 0:
376-
folders: list = self.get_all_folder_ids_and_names()
376+
folders: list = self.get_all_folder_ids_uids_and_names()
377377
folder_id: int = 0
378378

379379
for f in folders:
@@ -391,22 +391,60 @@ def get_folder_id_by_dashboard_path(self, dashboard_path: str) -> int:
391391
logging.error("There is no dashboard_path defined.")
392392
raise ValueError
393393

394-
def get_all_folder_ids_and_names(self) -> list:
395-
"""The method extract all folder id and names inside the complete organisation
394+
def get_folder_uid_by_dashboard_path(self, dashboard_path: str) -> str | None:
395+
"""The method includes a functionality to extract the folder uid specified inside model dashboard path
396+
397+
Args:
398+
dashboard_path (str): Specify the dashboard path
399+
400+
Raises:
401+
ValueError: Missed specifying a necessary value
402+
Exception: Unspecified error by executing the API call
403+
404+
Returns:
405+
folder_uid (str): Returns the folder uid
406+
"""
407+
408+
# TODO Check the general folder uid
409+
410+
if dashboard_path.lower() == "general":
411+
return None
412+
413+
if len(dashboard_path) != 0:
414+
folders: list = self.get_all_folder_ids_uids_and_names()
415+
folder_uid: str | None = None
416+
417+
for f in folders:
418+
if dashboard_path == f.get("title"):
419+
folder_uid = f.get("uid")
420+
421+
if folder_uid is None:
422+
logging.error(
423+
f"There's no folder_uid for the dashboard named {dashboard_path} available."
424+
)
425+
raise Exception
426+
427+
return folder_uid
428+
else:
429+
logging.error("There is no dashboard_path defined.")
430+
raise ValueError
431+
432+
def get_all_folder_ids_uids_and_names(self) -> list:
433+
"""The method extract all folder id, uid and names inside the complete organisation
396434
397435
Returns:
398-
folders (list): Returns a list of dicts with folder ids and the corresponding names
436+
folders (list): Returns a list of dicts with folder ids, uids and the corresponding names
399437
"""
400438

401439
folders_raw: list = Api(self.grafana_api_model).call_the_api(
402-
f"{APIEndpoints.SEARCH.value}?folderIds=0"
440+
f"{APIEndpoints.SEARCH.value}?folderUIDs"
403441
)
404442
folders_raw_len: int = len(folders_raw)
405443
folders: list = list()
406444

407445
for i in range(0, folders_raw_len):
408446
folders.append(
409-
{"title": folders_raw[i].get("title"), "id": folders_raw[i].get("id")}
447+
{"title": folders_raw[i].get("title"), "id": folders_raw[i].get("id"), "uid": folders_raw[i].get("uid")}
410448
)
411449

412450
return folders

tests/integrationtest/test_dashboard.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def test_d_dashboard_creation_general_folder(self):
7676
overwrite=True,
7777
)
7878

79+
80+
# print(self.dashboard.get_dashboard_uid_and_id_by_name_and_folder(
81+
# dashboard_path="General",
82+
# dashboard_name=os.environ["GRAFANA_DASHBOARD_NAME"],
83+
# ))
7984
self.assertEqual(
8085
"test1",
8186
self.dashboard.get_dashboard_uid_and_id_by_name_and_folder(

tests/integrationtest/test_folder.py

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,14 @@ def test_get_folders(self):
2222

2323
def test_get_folder_by_uids(self):
2424
self.assertEqual(
25-
{
26-
"canAdmin": True,
27-
"canDelete": True,
28-
"canEdit": True,
29-
"canSave": True,
30-
"created": "2022-01-10T00:24:58+01:00",
31-
"createdBy": "Anonymous",
32-
"hasAcl": False,
33-
"id": 72,
34-
"orgId": 4,
35-
"title": "Github Integrationtest",
36-
"uid": "6U_QdWJnz",
37-
"updated": "2022-01-10T00:24:58+01:00",
38-
"updatedBy": "Anonymous",
39-
"url": "/dashboards/f/6U_QdWJnz/github-integrationtest",
40-
"version": 1,
41-
},
42-
self.folder.get_folder_by_uid("6U_QdWJnz"),
25+
"Github Integrationtest",
26+
self.folder.get_folder_by_uid("6U_QdWJnz").get("title"),
4327
)
4428

4529
def test_get_folder_by_id(self):
4630
self.assertEqual(
47-
{
48-
"canAdmin": True,
49-
"canDelete": True,
50-
"canEdit": True,
51-
"canSave": True,
52-
"created": "2022-01-10T00:24:58+01:00",
53-
"createdBy": "Anonymous",
54-
"hasAcl": False,
55-
"id": 72,
56-
"orgId": 4,
57-
"title": "Github Integrationtest",
58-
"uid": "6U_QdWJnz",
59-
"updated": "2022-01-10T00:24:58+01:00",
60-
"updatedBy": "Anonymous",
61-
"url": "/dashboards/f/6U_QdWJnz/github-integrationtest",
62-
"version": 1,
63-
},
64-
self.folder.get_folder_by_id(72),
31+
"Github Integrationtest",
32+
self.folder.get_folder_by_id(72).get("title"),
6533
)
6634

6735
def test_a_create_folder(self):
@@ -138,8 +106,8 @@ def test_get_folder_id_by_dashboard_path_general_folder(self):
138106
0, self.folder.get_folder_id_by_dashboard_path("General")
139107
)
140108

141-
def test_get_all_folder_ids_and_names(self):
109+
def test_get_all_folder_ids_uids_and_names(self):
142110
self.assertEqual(
143-
list([{"id": 72, "title": "Github Integrationtest"}]),
144-
self.folder.get_all_folder_ids_and_names(),
111+
list([{"id": 72, "uid": "6U_QdWJnz", "title": "Github Integrationtest"}]),
112+
self.folder.get_all_folder_ids_uids_and_names(),
145113
)

0 commit comments

Comments
 (0)