2014-12-01 10:13:00 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2014-12-01 10:15:13 -07:00
|
|
|
Runner Module for Firing Events via PagerDuty
|
2014-12-01 10:13:00 -07:00
|
|
|
|
|
|
|
.. versionadded:: 2014.1.0
|
|
|
|
|
|
|
|
:configuration: This module can be used by specifying the name of a
|
2014-12-01 10:15:13 -07:00
|
|
|
configuration profile in the master config.
|
2014-12-01 10:13:00 -07:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
my-pagerduty-account:
|
|
|
|
pagerduty.api_key: F3Rbyjbve43rfFWf2214
|
|
|
|
pagerduty.subdomain: mysubdomain
|
|
|
|
'''
|
2017-12-14 19:21:00 -06:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2014-12-01 10:13:00 -07:00
|
|
|
|
|
|
|
# Import salt libs
|
2017-10-13 20:52:56 -05:00
|
|
|
import salt.utils.functools
|
2017-12-22 13:54:53 -06:00
|
|
|
import salt.utils.json
|
2014-12-01 10:13:00 -07:00
|
|
|
import salt.utils.pagerduty
|
2017-12-27 22:31:50 -06:00
|
|
|
import salt.utils.yaml
|
2017-12-14 19:21:00 -06:00
|
|
|
from salt.ext import six
|
2014-12-01 10:13:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
def __virtual__():
|
|
|
|
'''
|
|
|
|
No dependencies outside of what Salt itself requires
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def list_services(profile=None, api_key=None):
|
|
|
|
'''
|
|
|
|
List services belonging to this account
|
|
|
|
|
|
|
|
CLI Example:
|
|
|
|
|
|
|
|
salt-run pagerduty.list_services my-pagerduty-account
|
|
|
|
'''
|
|
|
|
return salt.utils.pagerduty.list_items(
|
2015-02-18 13:13:58 -06:00
|
|
|
'services',
|
|
|
|
'name',
|
|
|
|
__salt__['config.option'](profile),
|
|
|
|
api_key,
|
|
|
|
opts=__opts__
|
2014-12-01 10:13:00 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def list_incidents(profile=None, api_key=None):
|
|
|
|
'''
|
|
|
|
List incidents belonging to this account
|
|
|
|
|
|
|
|
CLI Example:
|
|
|
|
|
|
|
|
salt-run pagerduty.list_incidents my-pagerduty-account
|
|
|
|
'''
|
|
|
|
return salt.utils.pagerduty.list_items(
|
2015-02-18 13:13:58 -06:00
|
|
|
'incidents',
|
|
|
|
'id',
|
|
|
|
__salt__['config.option'](profile),
|
|
|
|
api_key,
|
|
|
|
opts=__opts__
|
2014-12-01 10:13:00 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def list_users(profile=None, api_key=None):
|
|
|
|
'''
|
|
|
|
List users belonging to this account
|
|
|
|
|
|
|
|
CLI Example:
|
|
|
|
|
|
|
|
salt-run pagerduty.list_users my-pagerduty-account
|
|
|
|
'''
|
|
|
|
return salt.utils.pagerduty.list_items(
|
2015-02-18 13:13:58 -06:00
|
|
|
'users',
|
|
|
|
'id',
|
|
|
|
__salt__['config.option'](profile),
|
|
|
|
api_key,
|
|
|
|
opts=__opts__
|
2014-12-01 10:13:00 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def list_schedules(profile=None, api_key=None):
|
|
|
|
'''
|
|
|
|
List schedules belonging to this account
|
|
|
|
|
|
|
|
CLI Example:
|
|
|
|
|
|
|
|
salt-run pagerduty.list_schedules my-pagerduty-account
|
|
|
|
'''
|
|
|
|
return salt.utils.pagerduty.list_items(
|
2015-02-18 13:13:58 -06:00
|
|
|
'schedules',
|
|
|
|
'id',
|
|
|
|
__salt__['config.option'](profile),
|
|
|
|
api_key,
|
|
|
|
opts=__opts__
|
2014-12-01 10:13:00 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def list_windows(profile=None, api_key=None):
|
|
|
|
'''
|
|
|
|
List maintenance windows belonging to this account
|
|
|
|
|
|
|
|
CLI Example:
|
|
|
|
|
|
|
|
salt-run pagerduty.list_windows my-pagerduty-account
|
|
|
|
salt-run pagerduty.list_maintenance_windows my-pagerduty-account
|
|
|
|
'''
|
|
|
|
return salt.utils.pagerduty.list_items(
|
2015-02-18 13:13:58 -06:00
|
|
|
'maintenance_windows',
|
|
|
|
'id',
|
|
|
|
__salt__['config.option'](profile),
|
|
|
|
api_key,
|
|
|
|
opts=__opts__
|
2014-12-01 10:13:00 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# The long version, added for consistency
|
2017-10-13 20:52:56 -05:00
|
|
|
list_maintenance_windows = salt.utils.functools.alias_function(list_windows, 'list_maintenance_windows')
|
2014-12-01 10:13:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
def list_policies(profile=None, api_key=None):
|
|
|
|
'''
|
|
|
|
List escalation policies belonging to this account
|
|
|
|
|
|
|
|
CLI Example:
|
|
|
|
|
|
|
|
salt-run pagerduty.list_policies my-pagerduty-account
|
|
|
|
salt-run pagerduty.list_escalation_policies my-pagerduty-account
|
|
|
|
'''
|
|
|
|
return salt.utils.pagerduty.list_items(
|
2015-02-18 13:13:58 -06:00
|
|
|
'escalation_policies',
|
|
|
|
'id',
|
|
|
|
__salt__['config.option'](profile),
|
|
|
|
api_key,
|
|
|
|
opts=__opts__
|
2014-12-01 10:13:00 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# The long version, added for consistency
|
2017-10-13 20:52:56 -05:00
|
|
|
list_escalation_policies = salt.utils.functools.alias_function(list_policies, 'list_escalation_policies')
|
2014-12-01 10:13:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
def create_event(service_key=None, description=None, details=None,
|
|
|
|
incident_key=None, profile=None):
|
|
|
|
'''
|
|
|
|
Create an event in PagerDuty. Designed for use in states.
|
|
|
|
|
|
|
|
CLI Example:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
salt-run pagerduty.create_event <service_key> <description> <details> \
|
|
|
|
profile=my-pagerduty-account
|
|
|
|
|
|
|
|
The following parameters are required:
|
|
|
|
|
|
|
|
service_key
|
|
|
|
This key can be found by using pagerduty.list_services.
|
|
|
|
|
|
|
|
description
|
|
|
|
This is a short description of the event.
|
|
|
|
|
|
|
|
details
|
|
|
|
This can be a more detailed description of the event.
|
|
|
|
|
|
|
|
profile
|
|
|
|
This refers to the configuration profile to use to connect to the
|
|
|
|
PagerDuty service.
|
|
|
|
'''
|
|
|
|
trigger_url = 'https://events.pagerduty.com/generic/2010-04-15/create_event.json'
|
|
|
|
|
2017-12-14 19:21:00 -06:00
|
|
|
if isinstance(details, six.string_types):
|
2017-12-27 22:31:50 -06:00
|
|
|
details = salt.utils.yaml.safe_load(details)
|
2017-12-14 19:21:00 -06:00
|
|
|
if isinstance(details, six.string_types):
|
2014-12-01 10:13:00 -07:00
|
|
|
details = {'details': details}
|
|
|
|
|
2017-12-22 13:54:53 -06:00
|
|
|
ret = salt.utils.json.loads(salt.utils.pagerduty.query(
|
2014-12-01 10:13:00 -07:00
|
|
|
method='POST',
|
2015-02-18 13:13:58 -06:00
|
|
|
profile_dict=__salt__['config.option'](profile),
|
2014-12-01 10:13:00 -07:00
|
|
|
api_key=service_key,
|
|
|
|
data={
|
|
|
|
'service_key': service_key,
|
|
|
|
'incident_key': incident_key,
|
|
|
|
'event_type': 'trigger',
|
|
|
|
'description': description,
|
|
|
|
'details': details,
|
|
|
|
},
|
|
|
|
url=trigger_url,
|
|
|
|
opts=__opts__
|
|
|
|
))
|
|
|
|
return ret
|