grafana_api_sdk icon indicating copy to clipboard operation
grafana_api_sdk copied to clipboard

create_or_update in dashboard not working because of folders?

Open johannes-russek-sh opened this issue 8 months ago • 6 comments

Hi, it seems that I am unable to create a dashboard because the get_folder_id_by_dashboard_path() function is not finding the folder. It appears that the get_all_folder_ids_and_names() function returns an empty set in our grafana cloud instance.

Interestingly, if I change the get_folder_id_by_dashboard_path() function to use get_folders() instead, it works just fine.

Am I doing something incorrectly? What's the reason for using the search endpoint when finding the id of a dashboard?

I'd be happy to send a PR, it's literally one line of change - but I'd like to understand this better of course.

johannes-russek-sh avatar Apr 17 '25 20:04 johannes-russek-sh

Hi @johannes-russek-sh,

can you please share the folder structure on your side? Is the dashboard inside the general folder?

FYI: Inside the function get_folder_id_by_dashboard_path the get_all_folder_ids_and_names is called, so I understand why the result is empty.

What's the reason for using the search endpoint when finding the id of a dashboard? The function call is related to identify the folder id, if you place the dashboard in a custom folder. I'm expecting that you don't know the id/ uid of the custom folder. Normally, you only know the path/ name of the folder.

ZPascal avatar Apr 17 '25 20:04 ZPascal

Hi @johannes-russek-sh,

can you please share the folder structure on your side? Is the dashboard inside the general folder?

The folder that I'm using is indeed in the general folder, yes.

FYI: Inside the function get_folder_id_by_dashboard_path the get_all_folder_ids_and_names is called, so I understand why the result is empty.

yes, just to clarify - the result of get_all_folder_ids_and_names is empty, it

What's the reason for using the search endpoint when finding the id of a dashboard? The function call is related to identify the folder id, if you place the dashboard in a custom folder. I'm expecting that you don't know the id/ uid of the custom folder. Normally, you only know the path/ name of the folder.

Ah yes that I understand - what I mean is why use get_all_folder_ids_and_names which calls the search endpoint instead of get_folders() which calls the folders endpoint. The latter works just fine and returns a list of all folders as expected.

I changed get_folder_id_by_dashboard_path to use get_folders and it works as expected:

diff --git a/grafana_api/folder.py b/grafana_api/folder.py
index cc5826b..3c7eed0 100644
--- a/grafana_api/folder.py
+++ b/grafana_api/folder.py
@@ -373,7 +373,7 @@ class Folder:
             return 0

         if len(dashboard_path) != 0:
-            folders: list = self.get_all_folder_ids_and_names()
+            folders: list = self.get_folders()
             folder_id: int = 0

             for f in folders:

johannes-russek-sh avatar Apr 18 '25 14:04 johannes-russek-sh

@johannes-russek-sh

Ah yes that I understand - what I mean is why use get_all_folder_ids_and_names which calls the search endpoint instead of get_folders() which calls the folders endpoint. The latter works just fine and returns a list of all folders as expected.

The 'get_folders()' function only returns the first level organization folders, and the search endpoint returns all folders associated with the organization by default (deeper search).

In general, I have reproduced the case and found the issue. There is a breaking change inside the Grafana API related to the folderId search parameter and I'm currently preparing a fix for it.

ZPascal avatar May 07 '25 04:05 ZPascal

Hi @ZPascal

it's been quite a while since this issue was posted, any updates when can we expect fix for this?

Instead of switching to get_folders like @johannes-russek-sh mentioned why not simple replace query filter to a different one? From f"{APIEndpoints.SEARCH.value}?folderIds=0" to f"{APIEndpoints.SEARCH.value}?type=dash-folder"? It will get all folders for us and that's what we want here, don't we? :)

matlos89 avatar Jun 16 '25 09:06 matlos89

Hi @matlos89,

I've already prepared the fix in local a feature branch and I'm currently adapting the integration tests/ unit tests. The fix should be available in the next few hours.

ZPascal avatar Jun 16 '25 09:06 ZPascal

Hey - just checking if there's any update for this? We can update / create dashboards in the root folder at the moment.

mcmarkj avatar Aug 07 '25 13:08 mcmarkj