From 09421153f5d6f176870bda8bd5e6127cae5391ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 25 Jan 2018 13:11:56 +0200 Subject: [PATCH] Use OpsGenie API v2 --- hc/api/tests/test_notify.py | 17 ++++++++++++++- hc/api/transports.py | 21 ++++++++++++------- .../integrations/opsgenie_description.html | 4 ++++ 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 templates/integrations/opsgenie_description.html diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index dac66746..368d40c2 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -368,16 +368,31 @@ class NotifyTestCase(BaseTestCase): @patch("hc.api.transports.requests.request") def test_opsgenie(self, mock_post): self._setup_data("opsgenie", "123") - mock_post.return_value.status_code = 200 + mock_post.return_value.status_code = 202 self.channel.notify(self.check) n = Notification.objects.first() self.assertEqual(n.error, "") + self.assertEqual(mock_post.call_count, 1) args, kwargs = mock_post.call_args payload = kwargs["json"] self.assertIn("DOWN", payload["message"]) + @patch("hc.api.transports.requests.request") + def test_opsgenie_up(self, mock_post): + self._setup_data("opsgenie", "123", status="up") + mock_post.return_value.status_code = 202 + + self.channel.notify(self.check) + n = Notification.objects.first() + self.assertEqual(n.error, "") + + self.assertEqual(mock_post.call_count, 1) + args, kwargs = mock_post.call_args + method, url = args + self.assertTrue(str(self.check.code) in url) + @patch("hc.api.transports.requests.request") def test_pushover(self, mock_post): self._setup_data("po", "123|0") diff --git a/hc/api/transports.py b/hc/api/transports.py index 6d966ad1..75c49984 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -87,7 +87,7 @@ class HttpTransport(Transport): options["headers"]["User-Agent"] = "healthchecks.io" r = requests.request(method, url, **options) - if r.status_code not in (200, 201, 204): + if r.status_code not in (200, 201, 202, 204): return "Received status code %d" % r.status_code except requests.exceptions.Timeout: # Well, we tried @@ -205,22 +205,28 @@ class HipChat(HttpTransport): class OpsGenie(HttpTransport): def notify(self, check): + headers = { + "Conent-Type": "application/json", + "Authorization": "GenieKey %s" % self.channel.value + } + payload = { - "apiKey": self.channel.value, "alias": str(check.code), "source": settings.SITE_NAME } if check.status == "down": - payload["tags"] = ",".join(check.tags_list()) + payload["tags"] = check.tags_list() payload["message"] = tmpl("opsgenie_message.html", check=check) payload["note"] = tmpl("opsgenie_note.html", check=check) + payload["description"] = \ + tmpl("opsgenie_description.html", check=check) - url = "https://api.opsgenie.com/v1/json/alert" + url = "https://api.opsgenie.com/v2/alerts" if check.status == "up": - url += "/close" + url += "/%s/close?identifierType=alias" % check.code - return self.post(url, json=payload) + return self.post(url, json=payload, headers=headers) class PagerDuty(HttpTransport): @@ -240,6 +246,7 @@ class PagerDuty(HttpTransport): return self.post(self.URL, json=payload) + class PagerTree(HttpTransport): def notify(self, check): url = self.channel.value @@ -249,7 +256,7 @@ class PagerTree(HttpTransport): payload = { "incident_key": str(check.code), "event_type": "trigger" if check.status == "down" else "resolve", - "title": tmpl("pagertree_title.html", check=check), + "title": tmpl("pagertree_title.html", check=check), "description": tmpl("pagertree_description.html", check=check), "client": settings.SITE_NAME, "client_url": settings.SITE_ROOT, diff --git a/templates/integrations/opsgenie_description.html b/templates/integrations/opsgenie_description.html new file mode 100644 index 00000000..56904d89 --- /dev/null +++ b/templates/integrations/opsgenie_description.html @@ -0,0 +1,4 @@ +{% load hc_extras humanize %} +The check "{{ check.name_then_code }}" is DOWN. +{% if check.kind == "simple" %}Expecting to receive a ping every {{ check.timeout|hc_duration }}.{% endif %} +Last ping was {{ check.last_ping|naturaltime }}.