Skip to content

fix: Adjust the dashboard and folder issue #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions docs/content/grafana_api/folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<a id="folder"></a>

Expand Down Expand Up @@ -327,17 +328,42 @@ The method includes a functionality to extract the folder id specified inside mo

- `folder_id` _int_ - Returns the folder id

<a id="folder.Folder.get_all_folder_ids_and_names"></a>
<a id="folder.Folder.get_folder_uid_by_dashboard_path"></a>

#### 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

<a id="folder.Folder.get_all_folder_ids_uids_and_names"></a>

#### 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

16 changes: 10 additions & 6 deletions grafana_api/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down
50 changes: 44 additions & 6 deletions grafana_api/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
5 changes: 5 additions & 0 deletions tests/integrationtest/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
46 changes: 7 additions & 39 deletions tests/integrationtest/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(),
)