Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Add handler for health API. Thanks, @peekjef72!
* Add support for datasource proxying. Thanks, @peekjef72!
* Fix compatibility between `actions/checkout@v2` and `setuptools_scm`
* Fix `folderId` propagation for `update_dashboard`


## 2.0.2 (2022-02-03)
Expand Down
8 changes: 8 additions & 0 deletions grafana_client/elements/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def update_dashboard(self, dashboard):
:param dashboard:
:return:
"""

# When the "folderId" is not available within the dashboard payload,
# populate it from the nested "meta" object, if given.
if "folderId" not in dashboard:
if "meta" in dashboard and "folderId" in dashboard["meta"]:
dashboard = dashboard.copy()
dashboard["folderId"] = dashboard["meta"]["folderId"]

put_dashboard_path = "/dashboards/db"
r = self.client.POST(put_dashboard_path, json=dashboard)
return r
Expand Down
44 changes: 44 additions & 0 deletions test/elements/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def test_get_dashboard(self, m):

@requests_mock.Mocker()
def test_update_dashboard(self, m):
"""
Verify a general dashboard update.
"""
m.post(
"http://localhost/api/dashboards/db",
json={
Expand Down Expand Up @@ -65,6 +68,47 @@ def test_update_dashboard(self, m):
self.assertEqual(dashboard["uid"], "cIBgcSjkk")
self.assertEqual(dashboard["status"], "success")

@requests_mock.Mocker()
def test_update_dashboard_roundtrip_folder_1(self, m):
"""
Verify that a dashboard update will use the "folderId"
from the nested "meta" object.
This is important when roundtripping dashboard payloads.
"""
m.post("http://localhost/api/dashboards/db", json={})
self.grafana.dashboard.update_dashboard(
{
"meta": {
"folderId": 123,
},
"dashboard": {},
}
)

self.assertEqual(m.last_request.json()["folderId"], 123)

@requests_mock.Mocker()
def test_update_dashboard_roundtrip_folder_2(self, m):
"""
Verify that a dashboard update will use the "folderId"
from the toplevel dashboard payload, even if it is present
within the nested "meta" object.
This is important when roundtripping dashboard payloads and
intentionally wanting to move the dashboard to a different folder.
"""
m.post("http://localhost/api/dashboards/db", json={})
self.grafana.dashboard.update_dashboard(
{
"meta": {
"folderId": 123,
},
"dashboard": {},
"folderId": 456,
}
)

self.assertEqual(m.last_request.json()["folderId"], 456)

@requests_mock.Mocker()
def test_get_home_dashboard(self, m):
m.get(
Expand Down