Skip to content

Commit

Permalink
Merge pull request #401 from alonm-totango/master
Browse files Browse the repository at this point in the history
  • Loading branch information
liiight committed Aug 23, 2021
2 parents 6084847 + 7e54c34 commit 68a0f9f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 1 deletion.
2 changes: 2 additions & 0 deletions notifiers/providers/__init__.py
Expand Up @@ -12,6 +12,7 @@
from . import statuspage
from . import telegram
from . import twilio
from . import victorops
from . import zulip

_all_providers = {
Expand All @@ -30,4 +31,5 @@
"mailgun": mailgun.MailGun,
"popcornnotify": popcornnotify.PopcornNotify,
"statuspage": statuspage.Statuspage,
"victorops": victorops.VictorOps,
}
93 changes: 93 additions & 0 deletions notifiers/providers/victorops.py
@@ -0,0 +1,93 @@
from ..core import Provider
from ..core import Response
from ..utils import requests


class VictorOps(Provider):
"""Send VictorOps webhook notifications"""

base_url = "https://portal.victorops.com/ui/{ORGANIZATION_ID}/incidents"
site_url = "https://portal.victorops.com/dash/{ORGANIZATION_ID}#/advanced/rest"
name = "victorops"

_required = {
"required": [
"rest_url",
"message_type",
"entity_id",
"entity_display_name",
"message",
]
}
_schema = {
"type": "object",
"properties": {
"rest_url": {
"type": "string",
"format": "uri",
"title": "the REST URL to use with routing_key. create one in victorops `integrations` tab.",
},
"message_type": {
"type": "string",
"title": "severity level can be: "
"- critical or warning: Triggers an incident "
"- acknowledgement: sends Acknowledgment to an incident "
"- info: Creates a timeline event but doesn't trigger an incident "
"- recovery or ok: Resolves an incident",
"enum": [
"critical",
"warning",
"acknowledgement",
"info",
"recovery",
"ok",
],
},
"entity_id": {
"type": "string",
"title": "Unique id for the incident for aggregation ,Acknowledging, or resolving.",
},
"entity_display_name": {
"type": "string",
"title": "Display Name in the UI and Notifications.",
},
"message": {
"type": "string",
"title": "This is the description that will be posted in the incident.",
},
"annotations": {
"type": "object",
"patternProperties": {
"^vo_annotate.u.": {"type": "string"},
"^vo_annotate.s.": {"type": "string"},
"^vo_annotate.i.": {"type": "string"},
},
"minProperties": 1,
"title": "annotations can be of three types: "
"vo_annotate.u.{custom_name}, "
"vo_annotate.s.{custom_name}, "
"vo_annotate.i.{custom_name} .",
"additionalProperties": False,
},
"additional_keys": {
"type": "object",
"title": "any additional keys that can be passed in the body",
},
},
"additionalProperties": False,
}

def _prepare_data(self, data: dict) -> dict:
annotations = data.pop("annotations", {})
for annotation, value in annotations.items():
data[annotation] = value

additional_keys = data.pop("additional_keys", {})
for additional_key, value in additional_keys.items():
data[additional_key] = value
return data

def _send_notification(self, data: dict) -> Response:
url = data.pop("rest_url")
response, errors = requests.post(url, json=data)
return self.create_response(data, response, errors)
3 changes: 2 additions & 1 deletion source/CLI.rst
Expand Up @@ -33,14 +33,15 @@ To view the main help just enter ``notifiers`` or ``notifiers --help``:
slack Options for 'slack'
telegram Options for 'telegram'
zulip Options for 'zulip'
victorops Options for 'victorops'
To view all providers use the ``providers`` command like so:

.. code-block:: console
$ notifiers providers
pushover, simplepush, slack, email, gmail, telegram, gitter, pushbullet, join, hipchat, zulip
pushover, simplepush, slack, email, gmail, telegram, gitter, pushbullet, join, hipchat, zulip, victorops
This will return all available provider names

Expand Down
5 changes: 5 additions & 0 deletions source/api/providers.rst
Expand Up @@ -66,3 +66,8 @@ API documentation for the different providers.
:members:
:undoc-members:

.. automodule:: notifiers.providers.victorops
:members:
:undoc-members:


72 changes: 72 additions & 0 deletions source/providers/victorops.rst
@@ -0,0 +1,72 @@
VictorOps (REST)
--------------------

Send `VictorOps <https://alert.victorops.com/integrations/generic>`_ rest integration notifications.

Minimal example:

.. code-block:: python
>>> from notifiers import get_notifier
>>> victorops = get_notifier('victorops')
>>> victorops.notify(rest_url='https://alert.victorops.com/integrations/generic/20104876/alert/f7dc2eeb-ms9k-43b8-kd89-0f00000f4ec2/$routing_key',
message_type='CRITICAL',
entity_id='foo testing',
entity_display_name="bla test title text",
message="bla message description")
Full schema:

.. code-block:: yaml
additionalProperties: false
properties:
rest_url:
type: string
format: uri
title: the REST URL to use with routing_key, create one in victorops integrations tab.
message_type:
type: string
title: severity level can be:
- critical or warning: Triggers an incident
- acknowledgement: Acks an incident
- info: Creates a timeline event but doesn't trigger an incident
- recovery or ok: Resolves an incident
entity_id:
type: string
title: Unique id for the incident for aggregation acking or resolving.
entity_display_name:
type: string
title: Display Name in the UI and Notifications.
message:
type: string
title: This is the description that will be posted in the incident.
annotations:
type: object
format:
vo_annotate.s.{custom_name}: annotation
vo_annotate.u.{custom_name}: annotation
vo_annotate.i.{custom_name}: annotation
title: annotations can be of three types vo_annotate.u.{custom_name} vo_annotate.s.{custom_name} vo_annotate.i.{custom_name}.
additional_keys:
type: object
format:
key: value
key: value
key: value
title: any additional keys that ca be passed in the body
required:
- rest_url
- message_type
- entity_id
- entity_display_name
- message
type: object
36 changes: 36 additions & 0 deletions tests/providers/test_victorops.py
@@ -0,0 +1,36 @@
import os

import pytest

provider = "victorops"


class TestVicrotops:
"""
Victorops rest alert tests
Online test rely on setting the env variable VICTOROPS_REST_URL
"""

@pytest.mark.online
def test_all_options(self, provider):
VICTOROPS_REST_URL = os.getenv("VICTOROPS_REST_URL")
data = {
"rest_url": VICTOROPS_REST_URL,
"message_type": "info",
"entity_id": "BA tesing",
"entity_display_name": "message test header",
"message": "text in body",
"annotations": {
"vo_annotate.i.Graph": "https://shorturl.at/dAQ28",
"vo_annotate.s.Note": "'You can't have everything. Where would you put it?' Steven Wright",
"vo_annotate.u.Runbook": "https://giphy.com/gifs/win-xNBcChLQt7s9a/fullscreen",
},
"additional_keys": {
"foo": "this is a custom fields",
"monitoring_tool": "this is an official victorops fields",
},
}
rsp = provider.notify(**data)
assert rsp.ok
assert rsp.status == "Success"
assert rsp.errors is None

0 comments on commit 68a0f9f

Please sign in to comment.