Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ nosetests.xml
coverage.xml
*.cover
.hypothesis/
test-reports/

# Translations
*.mo
Expand Down Expand Up @@ -108,3 +109,5 @@ venv.bak/
# mypy
.mypy_cache/

# Editors
.idea/
8 changes: 8 additions & 0 deletions grafana_api/api/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.api.POST(put_dashboard_path, json=dashboard)
return r
Expand Down
50 changes: 50 additions & 0 deletions test/api/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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,53 @@ 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.cli.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.cli.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