Rerouting Escalations using VictorOps and StackStorm

Welcome, today we will be talking about one of VictorOps latest API calls released last week that allows you to reroute an incident to another escalation policy.

This is a very powerful addition to the VictorOps API as it will allow you to escalate incidents from Tier 1 to Tier 2 support using automation for example.

File: /opt/stackstorm/packs/victorops/actions/reroute_incident.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
import json
import requests

from lib.reroute_action import VictorOpsRerouteAction

class RerouteIncident(VictorOpsRerouteAction):
def run(self, entity, username, slug):
headers = {'X-VO-Api-Id': self.reroute_id,
'X-VO-Api-Key': self.reroute_key,
'content-type': 'application/json'}

r = requests.get(self.rerouteurl, headers=headers)
incidents = r.json()

for incident in incidents["incidents"]:
if incident["entityId"] == entity:
incidentID = incident["incidentNumber"]
break

if not 'incidentID' in vars():
message = "Incident %s not found." % entity
return (False, message)

reroute = {'userName': username,
'reroutes': [{'targets': [{'type': 'EscalationPolicy',
'slug': slug}], 'incidentNumber': incidentID }]}

post_data = json.dumps(reroute)
response = requests.post(self.rerouteurl + "/reroute",
data=post_data, headers=headers)

return True

File: /opt/stackstorm/packs/victorops/actions/reroute_incident.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
name: reroute_incident
runner_type: python-script
description: Triggers the event on VictorOps with the given parameters
enabled: true
entry_point: reroute_incident.py

parameters:
slug:
type: string
description: "Enter the slug of the escalation policy to reroute to."
required: true
entity:
type: string
description: "Provide entity id, This would be used by victorops to reroute the incident."
required: true
username:
type: string
description: "Provide the username this action will be commited by."
required: true

File: /opt/stackstorm/packs/victorops/actions/lib/reroute_action.py

1
2
3
4
5
6
7
8
9
from st2common.runners.base_action import Action


class VictorOpsRerouteAction(Action):
def __init__(self, config):
super(VictorOpsRerouteAction, self).__init__(config)
self.rerouteurl = "{0}".format(self.config['reroute_url'])
self.reroute_key = self.config['routing_key']
self.reroute_id = self.config['routing_id']