Skip to content

Commit 68a0f9f

Browse files
authored
Merge pull request #401 from alonm-totango/master
2 parents 6084847 + 7e54c34 commit 68a0f9f

File tree

6 files changed

+210
-1
lines changed

6 files changed

+210
-1
lines changed

notifiers/providers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from . import statuspage
1313
from . import telegram
1414
from . import twilio
15+
from . import victorops
1516
from . import zulip
1617

1718
_all_providers = {
@@ -30,4 +31,5 @@
3031
"mailgun": mailgun.MailGun,
3132
"popcornnotify": popcornnotify.PopcornNotify,
3233
"statuspage": statuspage.Statuspage,
34+
"victorops": victorops.VictorOps,
3335
}

notifiers/providers/victorops.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from ..core import Provider
2+
from ..core import Response
3+
from ..utils import requests
4+
5+
6+
class VictorOps(Provider):
7+
"""Send VictorOps webhook notifications"""
8+
9+
base_url = "https://portal.victorops.com/ui/{ORGANIZATION_ID}/incidents"
10+
site_url = "https://portal.victorops.com/dash/{ORGANIZATION_ID}#/advanced/rest"
11+
name = "victorops"
12+
13+
_required = {
14+
"required": [
15+
"rest_url",
16+
"message_type",
17+
"entity_id",
18+
"entity_display_name",
19+
"message",
20+
]
21+
}
22+
_schema = {
23+
"type": "object",
24+
"properties": {
25+
"rest_url": {
26+
"type": "string",
27+
"format": "uri",
28+
"title": "the REST URL to use with routing_key. create one in victorops `integrations` tab.",
29+
},
30+
"message_type": {
31+
"type": "string",
32+
"title": "severity level can be: "
33+
"- critical or warning: Triggers an incident "
34+
"- acknowledgement: sends Acknowledgment to an incident "
35+
"- info: Creates a timeline event but doesn't trigger an incident "
36+
"- recovery or ok: Resolves an incident",
37+
"enum": [
38+
"critical",
39+
"warning",
40+
"acknowledgement",
41+
"info",
42+
"recovery",
43+
"ok",
44+
],
45+
},
46+
"entity_id": {
47+
"type": "string",
48+
"title": "Unique id for the incident for aggregation ,Acknowledging, or resolving.",
49+
},
50+
"entity_display_name": {
51+
"type": "string",
52+
"title": "Display Name in the UI and Notifications.",
53+
},
54+
"message": {
55+
"type": "string",
56+
"title": "This is the description that will be posted in the incident.",
57+
},
58+
"annotations": {
59+
"type": "object",
60+
"patternProperties": {
61+
"^vo_annotate.u.": {"type": "string"},
62+
"^vo_annotate.s.": {"type": "string"},
63+
"^vo_annotate.i.": {"type": "string"},
64+
},
65+
"minProperties": 1,
66+
"title": "annotations can be of three types: "
67+
"vo_annotate.u.{custom_name}, "
68+
"vo_annotate.s.{custom_name}, "
69+
"vo_annotate.i.{custom_name} .",
70+
"additionalProperties": False,
71+
},
72+
"additional_keys": {
73+
"type": "object",
74+
"title": "any additional keys that can be passed in the body",
75+
},
76+
},
77+
"additionalProperties": False,
78+
}
79+
80+
def _prepare_data(self, data: dict) -> dict:
81+
annotations = data.pop("annotations", {})
82+
for annotation, value in annotations.items():
83+
data[annotation] = value
84+
85+
additional_keys = data.pop("additional_keys", {})
86+
for additional_key, value in additional_keys.items():
87+
data[additional_key] = value
88+
return data
89+
90+
def _send_notification(self, data: dict) -> Response:
91+
url = data.pop("rest_url")
92+
response, errors = requests.post(url, json=data)
93+
return self.create_response(data, response, errors)

source/CLI.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ To view the main help just enter ``notifiers`` or ``notifiers --help``:
3333
slack Options for 'slack'
3434
telegram Options for 'telegram'
3535
zulip Options for 'zulip'
36+
victorops Options for 'victorops'
3637
3738
3839
To view all providers use the ``providers`` command like so:
3940

4041
.. code-block:: console
4142
4243
$ notifiers providers
43-
pushover, simplepush, slack, email, gmail, telegram, gitter, pushbullet, join, hipchat, zulip
44+
pushover, simplepush, slack, email, gmail, telegram, gitter, pushbullet, join, hipchat, zulip, victorops
4445
4546
This will return all available provider names
4647

source/api/providers.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ API documentation for the different providers.
6666
:members:
6767
:undoc-members:
6868

69+
.. automodule:: notifiers.providers.victorops
70+
:members:
71+
:undoc-members:
72+
73+

source/providers/victorops.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
VictorOps (REST)
2+
--------------------
3+
4+
Send `VictorOps <https://alert.victorops.com/integrations/generic>`_ rest integration notifications.
5+
6+
Minimal example:
7+
8+
.. code-block:: python
9+
10+
>>> from notifiers import get_notifier
11+
>>> victorops = get_notifier('victorops')
12+
>>> victorops.notify(rest_url='https://alert.victorops.com/integrations/generic/20104876/alert/f7dc2eeb-ms9k-43b8-kd89-0f00000f4ec2/$routing_key',
13+
message_type='CRITICAL',
14+
entity_id='foo testing',
15+
entity_display_name="bla test title text",
16+
message="bla message description")
17+
18+
Full schema:
19+
20+
.. code-block:: yaml
21+
22+
additionalProperties: false
23+
properties:
24+
rest_url:
25+
type: string
26+
format: uri
27+
title: the REST URL to use with routing_key, create one in victorops integrations tab.
28+
29+
message_type:
30+
type: string
31+
title: severity level can be:
32+
- critical or warning: Triggers an incident
33+
- acknowledgement: Acks an incident
34+
- info: Creates a timeline event but doesn't trigger an incident
35+
- recovery or ok: Resolves an incident
36+
37+
entity_id:
38+
type: string
39+
title: Unique id for the incident for aggregation acking or resolving.
40+
41+
entity_display_name:
42+
type: string
43+
title: Display Name in the UI and Notifications.
44+
45+
message:
46+
type: string
47+
title: This is the description that will be posted in the incident.
48+
49+
annotations:
50+
type: object
51+
format:
52+
vo_annotate.s.{custom_name}: annotation
53+
vo_annotate.u.{custom_name}: annotation
54+
vo_annotate.i.{custom_name}: annotation
55+
title: annotations can be of three types vo_annotate.u.{custom_name} vo_annotate.s.{custom_name} vo_annotate.i.{custom_name}.
56+
57+
additional_keys:
58+
type: object
59+
format:
60+
key: value
61+
key: value
62+
key: value
63+
title: any additional keys that ca be passed in the body
64+
65+
required:
66+
- rest_url
67+
- message_type
68+
- entity_id
69+
- entity_display_name
70+
- message
71+
type: object
72+

tests/providers/test_victorops.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
import pytest
4+
5+
provider = "victorops"
6+
7+
8+
class TestVicrotops:
9+
"""
10+
Victorops rest alert tests
11+
Online test rely on setting the env variable VICTOROPS_REST_URL
12+
"""
13+
14+
@pytest.mark.online
15+
def test_all_options(self, provider):
16+
VICTOROPS_REST_URL = os.getenv("VICTOROPS_REST_URL")
17+
data = {
18+
"rest_url": VICTOROPS_REST_URL,
19+
"message_type": "info",
20+
"entity_id": "BA tesing",
21+
"entity_display_name": "message test header",
22+
"message": "text in body",
23+
"annotations": {
24+
"vo_annotate.i.Graph": "https://shorturl.at/dAQ28",
25+
"vo_annotate.s.Note": "'You can't have everything. Where would you put it?' Steven Wright",
26+
"vo_annotate.u.Runbook": "https://giphy.com/gifs/win-xNBcChLQt7s9a/fullscreen",
27+
},
28+
"additional_keys": {
29+
"foo": "this is a custom fields",
30+
"monitoring_tool": "this is an official victorops fields",
31+
},
32+
}
33+
rsp = provider.notify(**data)
34+
assert rsp.ok
35+
assert rsp.status == "Success"
36+
assert rsp.errors is None

0 commit comments

Comments
 (0)