Browse Source

Use OpsGenie API v2

pull/154/head
Pēteris Caune 7 years ago
parent
commit
09421153f5
3 changed files with 34 additions and 8 deletions
  1. +16
    -1
      hc/api/tests/test_notify.py
  2. +14
    -7
      hc/api/transports.py
  3. +4
    -0
      templates/integrations/opsgenie_description.html

+ 16
- 1
hc/api/tests/test_notify.py View File

@ -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")


+ 14
- 7
hc/api/transports.py View File

@ -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,


+ 4
- 0
templates/integrations/opsgenie_description.html View File

@ -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 }}.

Loading…
Cancel
Save