Merge ~lihuiguo/charm-nagios:bug/1945723 into charm-nagios:master

Proposed by Linda Guo
Status: Merged
Approved by: James Troup
Approved revision: d31671415a99a6d923190d00f83f3a3347d4866f
Merged at revision: 9ad5dc8672b4d2fec83d92e6fdbd2c97eb5b0d9d
Proposed branch: ~lihuiguo/charm-nagios:bug/1945723
Merge into: charm-nagios:master
Diff against target: 188 lines (+125/-0)
7 files modified
config.yaml (+4/-0)
hooks/application-dashboard-relation-changed (+1/-0)
hooks/application-dashboard-relation-joined (+1/-0)
hooks/application_dashboard_relation.py (+75/-0)
hooks/upgrade_charm.py (+3/-0)
metadata.yaml (+2/-0)
tests/unit/test_registration_relation_joined.py (+39/-0)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
James Troup (community) Needs Fixing
BootStack Reviewers Pending
Review via email: mp+409472@code.launchpad.net

Commit message

add registration relation to register nagios to Homer dashboard[1]

[1] https://launchpad.net/charm-homer-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
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
James Troup (elmo) wrote :

As discussed, let's please come up with clearer relation/interface names (e.g. 'application-dashboard' and 'register-application').

Other comments inline.

review: Needs Fixing
Revision history for this message
Linda Guo (lihuiguo) wrote :

updated the code according to the review comments

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

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

Change successfully merged at revision 9ad5dc8672b4d2fec83d92e6fdbd2c97eb5b0d9d

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/config.yaml b/config.yaml
index 765d70b..0739d64 100644
--- a/config.yaml
+++ b/config.yaml
@@ -355,3 +355,7 @@ options:
355 - name: snmp_collector355 - name: snmp_collector
356 host: /path/to/snmp/helper ....356 host: /path/to/snmp/helper ....
357 service: /path/to/snmp/helper ....357 service: /path/to/snmp/helper ....
358 site_name:
359 type: string
360 default: ''
361 description: An unique site name for Nagios deployment
358\ No newline at end of file362\ No newline at end of file
diff --git a/hooks/application-dashboard-relation-changed b/hooks/application-dashboard-relation-changed
359new file mode 120000363new file mode 120000
index 0000000..a13f386
--- /dev/null
+++ b/hooks/application-dashboard-relation-changed
@@ -0,0 +1 @@
1application_dashboard_relation.py
0\ No newline at end of file2\ No newline at end of file
diff --git a/hooks/application-dashboard-relation-joined b/hooks/application-dashboard-relation-joined
1new file mode 1200003new file mode 120000
index 0000000..a13f386
--- /dev/null
+++ b/hooks/application-dashboard-relation-joined
@@ -0,0 +1 @@
1application_dashboard_relation.py
0\ No newline at end of file2\ No newline at end of file
diff --git a/hooks/application_dashboard_relation.py b/hooks/application_dashboard_relation.py
1new file mode 1007553new file mode 100755
index 0000000..54a9596
--- /dev/null
+++ b/hooks/application_dashboard_relation.py
@@ -0,0 +1,75 @@
1#!/usr/bin/python
2# Copyright Canonical 2021 Canonical Ltd. All Rights Reserved
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16import os
17import sys
18
19from charmhelpers.core.hookenv import (
20 Hooks,
21 UnregisteredHookError,
22 config,
23 is_leader,
24 log,
25 relation_ids,
26 relation_set,
27 unit_public_ip,
28)
29
30hooks = Hooks()
31
32
33@hooks.hook("config-changed")
34@hooks.hook("application-dashboard-relation-joined")
35@hooks.hook("application-dashboard-relation-changed")
36def application_dashboard_relation_changed(relation_id=None, remote_unit=None):
37 """Register Nagios URL in dashboard charm such as Homer."""
38 if not is_leader():
39 return
40 relations = relation_ids("application-dashboard")
41 if not relations:
42 return
43 tls_configured = config("ssl_key")
44 scheme = "https://" if tls_configured else "http://"
45 url = scheme + unit_public_ip()
46 if config("site_name"):
47 subtitle = "[{}] Monitoring and alerting".format(config("site_name"))
48 group = "[{}] LMA".format(config("site_name"))
49 else:
50 subtitle = "Monitoring and alerting"
51 group = "LMA"
52 icon_file = os.environ.get("JUJU_CHARM_DIR", None) + "/icon.svg"
53 icon_data = None
54 if os.path.exists(icon_file):
55 with open(icon_file) as f:
56 icon_data = f.read()
57 for rid in relations:
58 relation_set(
59 rid,
60 app=True,
61 relation_settings={
62 "name": "Nagios",
63 "url": url,
64 "subtitle": subtitle,
65 "icon": icon_data,
66 "group": group,
67 },
68 )
69
70
71if __name__ == "__main__":
72 try:
73 hooks.execute(sys.argv)
74 except UnregisteredHookError as e:
75 log("Unknown hook {} - skipping.".format(e))
diff --git a/hooks/upgrade_charm.py b/hooks/upgrade_charm.py
index 32f6613..816d41d 100755
--- a/hooks/upgrade_charm.py
+++ b/hooks/upgrade_charm.py
@@ -20,6 +20,8 @@ except ImportError:
20 "DEBIAN_FRONTEND=noninteractive apt-get -qy install python-enum34", shell=True20 "DEBIAN_FRONTEND=noninteractive apt-get -qy install python-enum34", shell=True
21 )21 )
2222
23from application_dashboard_relation import application_dashboard_relation_changed
24
23from charmhelpers import fetch25from charmhelpers import fetch
24from charmhelpers.contrib import ssl26from charmhelpers.contrib import ssl
25from charmhelpers.core import hookenv, host27from charmhelpers.core import hookenv, host
@@ -678,6 +680,7 @@ update_cgi_config()
678update_contacts()680update_contacts()
679update_password("nagiosro", ro_password)681update_password("nagiosro", ro_password)
680configure_livestatus_xinetd()682configure_livestatus_xinetd()
683application_dashboard_relation_changed()
681684
682if password:685if password:
683 update_password(nagiosadmin, password)686 update_password(nagiosadmin, password)
diff --git a/metadata.yaml b/metadata.yaml
index 21ceda1..e89c497 100644
--- a/metadata.yaml
+++ b/metadata.yaml
@@ -16,5 +16,7 @@ requires:
16 interface: juju-info16 interface: juju-info
17 monitors:17 monitors:
18 interface: monitors18 interface: monitors
19 application-dashboard:
20 interface: register-application
19extra-bindings:21extra-bindings:
20 public:22 public:
diff --git a/tests/unit/test_registration_relation_joined.py b/tests/unit/test_registration_relation_joined.py
21new file mode 10064423new file mode 100644
index 0000000..0161f47
--- /dev/null
+++ b/tests/unit/test_registration_relation_joined.py
@@ -0,0 +1,39 @@
1import application_dashboard_relation
2
3import mock
4
5
6@mock.patch("application_dashboard_relation.relation_set")
7@mock.patch("application_dashboard_relation.unit_public_ip")
8@mock.patch("application_dashboard_relation.is_leader")
9@mock.patch("application_dashboard_relation.relation_ids")
10@mock.patch("application_dashboard_relation.config")
11@mock.patch("os.environ.get")
12def test_main(
13 environ_get, config, relation_ids, is_leader, unit_public_ip, relation_set
14):
15 hostname = "nagios.com"
16 unit_public_ip.return_value = hostname
17 relation_id = "application-dashboard:0"
18 environ_get.return_value = ""
19 relation_ids.return_value = relation_id
20 site_name = "test"
21 config.return_value = site_name
22 application_dashboard_relation.application_dashboard_relation_changed()
23 subtitle = "[{}] Monitoring and alerting".format(site_name)
24 group = "[{}] LMA".format(site_name)
25 name = "Nagios"
26 tls_configured = config("ssl_key")
27 scheme = "https" if tls_configured else "http"
28
29 relation_set.assert_called_with(
30 "0",
31 app=True,
32 relation_settings={
33 "name": name,
34 "url": "{}://{}".format(scheme, hostname),
35 "subtitle": subtitle,
36 "icon": None,
37 "group": group,
38 },
39 )

Subscribers

People subscribed via source and target branches

to all changes: