Skip to content

Commit 4a99b1e

Browse files
committed
Fix "folderId" propagation for "update_dashboard"
When the "folderId" is not available within the dashboard payload, populate it from the nested "meta" object, if given. On the other hand, if it *is* given within the dashboard payload, it should take precedence.
1 parent aed1ffb commit 4a99b1e

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Add handler for health API. Thanks, @peekjef72!
55
* Add support for datasource proxying. Thanks, @peekjef72!
66
* Fix compatibility between `actions/checkout@v2` and `setuptools_scm`
7+
* Fix `folderId` propagation for `update_dashboard`
78

89

910
## 2.0.2 (2022-02-03)

grafana_client/elements/dashboard.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ def update_dashboard(self, dashboard):
2222
:param dashboard:
2323
:return:
2424
"""
25+
26+
# When the "folderId" is not available within the dashboard payload,
27+
# populate it from the nested "meta" object, if given.
28+
if "folderId" not in dashboard:
29+
if "meta" in dashboard and "folderId" in dashboard["meta"]:
30+
dashboard = dashboard.copy()
31+
dashboard["folderId"] = dashboard["meta"]["folderId"]
32+
2533
put_dashboard_path = "/dashboards/db"
2634
r = self.client.POST(put_dashboard_path, json=dashboard)
2735
return r

test/elements/test_dashboard.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def test_get_dashboard(self, m):
3535

3636
@requests_mock.Mocker()
3737
def test_update_dashboard(self, m):
38+
"""
39+
Verify a general dashboard update.
40+
"""
3841
m.post(
3942
"http://localhost/api/dashboards/db",
4043
json={
@@ -65,6 +68,47 @@ def test_update_dashboard(self, m):
6568
self.assertEqual(dashboard["uid"], "cIBgcSjkk")
6669
self.assertEqual(dashboard["status"], "success")
6770

71+
@requests_mock.Mocker()
72+
def test_update_dashboard_roundtrip_folder_1(self, m):
73+
"""
74+
Verify that a dashboard update will use the "folderId"
75+
from the nested "meta" object.
76+
This is important when roundtripping dashboard payloads.
77+
"""
78+
m.post("http://localhost/api/dashboards/db", json={})
79+
self.grafana.dashboard.update_dashboard(
80+
{
81+
"meta": {
82+
"folderId": 123,
83+
},
84+
"dashboard": {},
85+
}
86+
)
87+
88+
self.assertEqual(m.last_request.json()["folderId"], 123)
89+
90+
@requests_mock.Mocker()
91+
def test_update_dashboard_roundtrip_folder_2(self, m):
92+
"""
93+
Verify that a dashboard update will use the "folderId"
94+
from the toplevel dashboard payload, even if it is present
95+
within the nested "meta" object.
96+
This is important when roundtripping dashboard payloads and
97+
intentionally wanting to move the dashboard to a different folder.
98+
"""
99+
m.post("http://localhost/api/dashboards/db", json={})
100+
self.grafana.dashboard.update_dashboard(
101+
{
102+
"meta": {
103+
"folderId": 123,
104+
},
105+
"dashboard": {},
106+
"folderId": 456,
107+
}
108+
)
109+
110+
self.assertEqual(m.last_request.json()["folderId"], 456)
111+
68112
@requests_mock.Mocker()
69113
def test_get_home_dashboard(self, m):
70114
m.get(

0 commit comments

Comments
 (0)