Skip to content

Commit 642722f

Browse files
committed
Deprecate get_dashboard_by_name on Grafana 8 and higher
It no longer supports getting dashboards by slug.
1 parent 37ec223 commit 642722f

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## unreleased
44

5+
* Deprecate `get_dashboard_by_name` on Grafana 8 and higher, as it no longer
6+
supports getting dashboards by slug. Thanks, @oz123.
7+
58

69
## 3.6.0 (2023-07-30)
710

grafana_client/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(
6161
self.admin = Admin(self.client)
6262
self.alerting = Alerting(self.client)
6363
self.alertingprovisioning = AlertingProvisioning(self.client)
64-
self.dashboard = Dashboard(self.client)
64+
self.dashboard = Dashboard(self.client, self)
6565
self.dashboard_versions = DashboardVersions(self.client)
6666
self.datasource = Datasource(self.client, self)
6767
self.folder = Folder(self.client)

grafana_client/elements/dashboard.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from distutils.version import LooseVersion
2+
13
from .base import Base
24

35

46
class Dashboard(Base):
5-
def __init__(self, client):
7+
def __init__(self, client, api):
68
super(Dashboard, self).__init__(client)
79
self.client = client
10+
self.api = api
811

912
def get_dashboard(self, dashboard_uid):
1013
"""
@@ -22,6 +25,8 @@ def get_dashboard_by_name(self, dashboard_name):
2225
:param dashboard_name:
2326
:return:
2427
"""
28+
if self.api.version and LooseVersion(self.api.version) >= LooseVersion("8"):
29+
raise DeprecationWarning("Grafana 8 and higher does not support getting dashboards by slug")
2530
get_dashboard_path = "/dashboards/db/%s" % dashboard_name
2631
r = self.client.GET(get_dashboard_path)
2732
return r

test/elements/test_dashboard.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ def test_get_dashboard(self, m):
3434
self.assertEqual(dashboard["dashboard"]["uid"], "cIBgcSjkk")
3535

3636
@requests_mock.Mocker()
37-
def test_get_dashboard_by_name(self, m):
37+
def test_get_dashboard_by_name_grafana7(self, m):
38+
m.get(
39+
"http://localhost/api/health",
40+
json={"commit": "6f8c1d9fe4", "database": "ok", "version": "7.5.11"},
41+
)
3842
m.get(
3943
"http://localhost/api/dashboards/db/Production Overview",
4044
json={
@@ -57,6 +61,19 @@ def test_get_dashboard_by_name(self, m):
5761
dashboard = self.grafana.dashboard.get_dashboard_by_name("Production Overview")
5862
self.assertEqual(dashboard["dashboard"]["title"], "Production Overview")
5963

64+
@requests_mock.Mocker()
65+
def test_get_dashboard_by_name_grafana8(self, m):
66+
m.get(
67+
"http://localhost/api/health",
68+
json={"commit": "unknown", "database": "ok", "version": "8.0.2"},
69+
)
70+
with self.assertRaises(DeprecationWarning) as ex:
71+
self.grafana.dashboard.get_dashboard_by_name("foobar")
72+
self.assertEqual(
73+
"Grafana 8 and higher does not support getting dashboards by slug",
74+
str(ex.exception),
75+
)
76+
6077
@requests_mock.Mocker()
6178
def test_update_dashboard(self, m):
6279
"""

0 commit comments

Comments
 (0)