Merge charm-grafana:dashboards_joined into charm-grafana:master

Proposed by Joe Guo
Status: Merged
Approved by: Joe Guo
Approved revision: 6ab6bed6082347fbc5f92fa703b3118f21da60a0
Merged at revision: c632b1fde22240963c81f338208d4d226638045a
Proposed branch: charm-grafana:dashboards_joined
Merge into: charm-grafana:master
Diff against target: 36 lines (+25/-0)
1 file modified
src/reactive/grafana.py (+25/-0)
Reviewer Review Type Date Requested Status
James Hebden (community) Approve
Xav Paice (community) Approve
Review via email: mp+391854@code.launchpad.net

Commit message

add dashboards-joined hook

when relate an operator based charm, e.g.: cloudstats, to grafana, this hook won't trigger:

    @when("grafana.started", "endpoint.dashboards.has_requests")
    @when_not("grafana.change-block")
    def import_dashboards(dashboards):
        ...

So dashboard will not be imported and there is no error.

Add a normal `joined` hook, query relation data directly with key, so operator based
charm can relate to grafana and import dashboard.

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
Xav Paice (xavpaice) wrote :

We need to add in a @reactive.when_not("is-update-status-hook") otherwise this will fire for every update-status.

Otherwise, +1 from me.

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

change `when` decorators to `@hook("dashboards-relation-joined")`
So it will only be triggered when relation joined.

Revision history for this message
Xav Paice (xavpaice) wrote :

lgtm

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

tested in local juju env, working as expected.

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

NOTE:
the `dashboards` arg is required by the `reactive.hook` decorator, otherwise it raised an error for missing args.
Although it's not used in code and not recommended by doc.

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

+1

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

Change successfully merged at revision c632b1fde22240963c81f338208d4d226638045a

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/reactive/grafana.py b/src/reactive/grafana.py
2index 553238c..b0a3bff 100644
3--- a/src/reactive/grafana.py
4+++ b/src/reactive/grafana.py
5@@ -1157,6 +1157,31 @@ def ensure_dash_deserialized(dash):
6 return dash
7
8
9+@hook("dashboards-relation-joined")
10+def dashboards_joined(dashboards):
11+ hookenv.log("hook %s triggered" % hookenv.hook_name(), "INFO")
12+
13+ remote_unit = os.environ.get('JUJU_REMOTE_UNIT', None)
14+ if not remote_unit:
15+ hookenv.log("remote_unit is None, return", "WARNING")
16+ return
17+
18+ dashboard_str = hookenv.relation_get('dashboard', remote_unit) # json str
19+ if not dashboard_str:
20+ hookenv.log("dashboard field is empty, return", "WARNING")
21+ return
22+
23+ dashboard = ensure_dash_deserialized(dashboard_str)
24+ name = hookenv.relation_get('name', remote_unit) # dashboard name
25+ remote_app = hookenv.remote_service_name()
26+
27+ success, reason = import_dashboard(dashboard, remote_app=remote_app, name=name)
28+ if success:
29+ hookenv.log("import_dashboard {} success".format(name), "INFO")
30+ else:
31+ hookenv.log("import_dashboard {} failed: {}".format(name, reason), "ERROR")
32+
33+
34 @when("grafana.started", "endpoint.dashboards.has_requests")
35 @when_not("grafana.change-block")
36 def import_dashboards(dashboards):

Subscribers

No one subscribed via source and target branches