Skip to content

Commit

Permalink
Allow managing dashboards in subfolders
Browse files Browse the repository at this point in the history
Since Grafana 11, it is possible to have dashboards in subfolders.
This enables users to manage dashboards in those.
  • Loading branch information
BenjaminSchubert committed Dec 10, 2024
1 parent 40d554b commit ba29eeb
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions plugins/modules/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
default: General
version_added: "1.0.0"
type: str
parent_uid:
description:
- The parent folder UID.
- Available with subfolder feature of Grafana 11.
- Allows creating dashboards in subfolders
version_added: "2.2.0"
type: str
state:
description:
- State of the dashboard.
Expand Down Expand Up @@ -112,6 +119,15 @@
folder: public
dashboard_url: https://grafana.com/api/dashboards/6098/revisions/1/download
- name: Import Grafana dashboard zabbix in a subfolder
community.grafana.grafana_dashboard:
grafana_url: http://grafana.company.com
grafana_api_key: "{{ grafana_api_key }}"
# Can be retrieved from `community.grafana.grafana_folder` as `folder.uid`
parent_uid: "{{ parent_uid }}"
folder: myteam
dashboard_url: https://grafana.com/api/dashboards/6098/revisions/1/download
- name: Export dashboard
community.grafana.grafana_dashboard:
grafana_url: http://grafana.company.com
Expand Down Expand Up @@ -226,15 +242,17 @@ def get_grafana_version(module, grafana_url, headers):
return int(grafana_version)


def grafana_folder_exists(module, grafana_url, folder_name, headers):
def grafana_folder_exists(module, grafana_url, folder_name, parent_uid, headers):
# the 'General' folder is a special case, it's ID is always '0'
if folder_name == "General":
return True, 0

try:
r, info = fetch_url(
module, "%s/api/folders" % grafana_url, headers=headers, method="GET"
)
url = "%s/api/folders" % grafana_url
if parent_uid:
url = "%s/parentUid=%s" % url, parent_uid

r, info = fetch_url(module, url, headers=headers, method="GET")

if info["status"] != 200:
raise GrafanaAPIException(
Expand Down Expand Up @@ -384,7 +402,7 @@ def grafana_create_dashboard(module, data):
folder_exists = False
if grafana_version >= 5:
folder_exists, folder_id = grafana_folder_exists(
module, data["url"], data["folder"], headers
module, data["url"], data["folder"], data["parent_uid"], headers
)
if folder_exists is False:
raise GrafanaAPIException(
Expand Down Expand Up @@ -612,6 +630,7 @@ def main():
org_id=dict(default=1, type="int"),
org_name=dict(type="str"),
folder=dict(type="str", default="General"),
parent_uid=dict(type="str"),
uid=dict(type="str"),
slug=dict(type="str"),
path=dict(aliases=["dashboard_url"], type="str"),
Expand Down

0 comments on commit ba29eeb

Please sign in to comment.