Merge charm-grafana:dashboard-json into charm-grafana:master

Proposed by Joe Guo
Status: Merged
Approved by: Adam Dyess
Approved revision: 5c5a449244ae995995dc6c4814d3e2e187647f62
Merged at revision: 239f891810ed9f7afee1c28de74967aa1b4c6bd8
Proposed branch: charm-grafana:dashboard-json
Merge into: charm-grafana:master
Diff against target: 63 lines (+27/-8)
2 files modified
src/lib/charms/layer/grafana.py (+26/-8)
src/wheelhouse.txt (+1/-0)
Reviewer Review Type Date Requested Status
Adam Dyess (community) Approve
James Hebden (community) Approve
Review via email: mp+391653@code.launchpad.net

Commit message

Fix LP1897843: support non-wrapped JSON data and keep backward compatibility

Currently this charm requires dashboard JSON data be wrapped like this:

    {
        "dashbaord": {<real dashboard JSON data>},
        "overwrite": true,
        "folderId": 1
    }

Actually this is a misuse of the grafana rest api structure.
Dashboard JSON data should include the real data directly.
Refer to LP1897843 for details.

This patch make the code support both formats.
Since most of our dashboard JSON file followed the wrong pattern, we
have to keep backward compatibility until grafana charm upgraded in all clouds.

LP: #1897843

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
James Hebden (ec0) wrote :

See comments inline

review: Needs Fixing
Revision history for this message
Joe Guo (guoqiao) wrote :

log removed and re-pushed.

Revision history for this message
James Hebden (ec0) wrote :

LGTM

review: Approve
Revision history for this message
Adam Dyess (addyess) wrote :

LGTM2

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 239f891810ed9f7afee1c28de74967aa1b4c6bd8

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/charms/layer/grafana.py b/src/lib/charms/layer/grafana.py
2index ecd8c69..009c812 100644
3--- a/src/lib/charms/layer/grafana.py
4+++ b/src/lib/charms/layer/grafana.py
5@@ -132,7 +132,7 @@ def post_dashboard(name, dashboard):
6 )
7
8
9-def import_dashboard(dashboard, remote_app=None, name=None):
10+def import_dashboard(dashboard, remote_app=None, name=None, overwrite=True):
11 """Process and import a dashboard.
12
13 Will update dashboard title if the dashboard is provided over a remote
14@@ -146,16 +146,34 @@ def import_dashboard(dashboard, remote_app=None, name=None):
15 :param remote_app: name of remote app for dashes provided over relation
16 :param remote_model: if dash is provided over CMR, name of remote model
17 :param name: dash internal name
18+ :param overwrite: if True, overwrite existing dashboard with same id
19 :return: None
20 """
21- dashboard["dashboard"]["title"] = compute_dash_title(
22- dashboard["dashboard"].get("title"), remote_app
23- )
24+ if "dashboard" in dashboard:
25+ # bug LP1897843: dashboard json data is wrapped in {"dashboard": {<data>}}
26+ dashboard["dashboard"]["title"] = compute_dash_title(
27+ dashboard["dashboard"].get("title"), remote_app
28+ )
29
30- dashboard["folderId"] = ensure_and_get_dash_folder(dashboard.get("source_model"))
31- if name is None:
32- name = dashboard["dashboard"].get("title") or "Untitled"
33- return post_dashboard(name, dashboard)
34+ dashboard["folderId"] = ensure_and_get_dash_folder(
35+ dashboard.get("source_model")
36+ )
37+ if name is None:
38+ name = dashboard["dashboard"].get("title") or "Untitled"
39+ return post_dashboard(name, dashboard)
40+ else:
41+ # correct data
42+ dashboard["title"] = compute_dash_title(dashboard.get("title"), remote_app)
43+
44+ folder_id = ensure_and_get_dash_folder(dashboard.get("source_model"))
45+ data = {
46+ "dashboard": dashboard,
47+ "folderId": folder_id,
48+ "overwrite": overwrite,
49+ }
50+ if name is None:
51+ name = dashboard.get("title") or "Untitled"
52+ return post_dashboard(name, data)
53
54
55 def get_cmd_output(cmd, shell=False):
56diff --git a/src/wheelhouse.txt b/src/wheelhouse.txt
57index ffdadef..ebe0ec6 100644
58--- a/src/wheelhouse.txt
59+++ b/src/wheelhouse.txt
60@@ -1,2 +1,3 @@
61+setuptools
62 requests
63 jsondiff

Subscribers

No one subscribed via source and target branches