Merge pull request #605 from bpennypacker/pagerduty_updates

monitoring/pagerduty module improvements, including specifying multiple services, fixing task output, adding 'delete'' action
reviewable/pr18780/r1
Greg DeKoenigsberg 9 years ago
commit d640fd95e9

@ -11,6 +11,7 @@ author:
- "Andrew Newdigate (@suprememoocow)" - "Andrew Newdigate (@suprememoocow)"
- "Dylan Silva (@thaumos)" - "Dylan Silva (@thaumos)"
- "Justin Johns" - "Justin Johns"
- "Bruce Pennypacker"
requirements: requirements:
- PagerDuty API access - PagerDuty API access
options: options:
@ -19,7 +20,7 @@ options:
- Create a maintenance window or get a list of ongoing windows. - Create a maintenance window or get a list of ongoing windows.
required: true required: true
default: null default: null
choices: [ "running", "started", "ongoing" ] choices: [ "running", "started", "ongoing", "absent" ]
aliases: [] aliases: []
name: name:
description: description:
@ -61,11 +62,11 @@ options:
version_added: '1.8' version_added: '1.8'
service: service:
description: description:
- PagerDuty service ID. - A comma separated list of PagerDuty service IDs.
required: false required: false
default: null default: null
choices: [] choices: []
aliases: [] aliases: [ services ]
hours: hours:
description: description:
- Length of maintenance window in hours. - Length of maintenance window in hours.
@ -96,9 +97,6 @@ options:
default: 'yes' default: 'yes'
choices: ['yes', 'no'] choices: ['yes', 'no']
version_added: 1.5.1 version_added: 1.5.1
notes:
- This module does not yet have support to end maintenance windows.
''' '''
EXAMPLES=''' EXAMPLES='''
@ -132,6 +130,14 @@ EXAMPLES='''
service=FOO123 service=FOO123
hours=4 hours=4
desc=deployment desc=deployment
register: pd_window
# Delete the previous maintenance window
- pagerduty: name=companyabc
user=example@example.com
passwd=password123
state=absent
service={{ pd_window.result.maintenance_window.id }}
''' '''
import datetime import datetime
@ -152,7 +158,12 @@ def ongoing(module, name, user, passwd, token):
if info['status'] != 200: if info['status'] != 200:
module.fail_json(msg="failed to lookup the ongoing window: %s" % info['msg']) module.fail_json(msg="failed to lookup the ongoing window: %s" % info['msg'])
return False, response.read() try:
json_out = json.loads(response.read())
except:
json_out = ""
return False, json_out, False
def create(module, name, user, passwd, token, requester_id, service, hours, minutes, desc): def create(module, name, user, passwd, token, requester_id, service, hours, minutes, desc):
@ -166,7 +177,8 @@ def create(module, name, user, passwd, token, requester_id, service, hours, minu
'Authorization': auth_header(user, passwd, token), 'Authorization': auth_header(user, passwd, token),
'Content-Type' : 'application/json', 'Content-Type' : 'application/json',
} }
request_data = {'maintenance_window': {'start_time': start, 'end_time': end, 'description': desc, 'service_ids': [service]}} request_data = {'maintenance_window': {'start_time': start, 'end_time': end, 'description': desc, 'service_ids': service}}
if requester_id: if requester_id:
request_data['requester_id'] = requester_id request_data['requester_id'] = requester_id
else: else:
@ -178,19 +190,50 @@ def create(module, name, user, passwd, token, requester_id, service, hours, minu
if info['status'] != 200: if info['status'] != 200:
module.fail_json(msg="failed to create the window: %s" % info['msg']) module.fail_json(msg="failed to create the window: %s" % info['msg'])
return False, response.read() try:
json_out = json.loads(response.read())
except:
json_out = ""
return False, json_out, True
def absent(module, name, user, passwd, token, requester_id, service):
url = "https://" + name + ".pagerduty.com/api/v1/maintenance_windows/" + service[0]
headers = {
'Authorization': auth_header(user, passwd, token),
'Content-Type' : 'application/json',
}
request_data = {}
if requester_id:
request_data['requester_id'] = requester_id
else:
if token:
module.fail_json(msg="requester_id is required when using a token")
data = json.dumps(request_data)
response, info = fetch_url(module, url, data=data, headers=headers, method='DELETE')
if info['status'] != 200:
module.fail_json(msg="failed to delete the window: %s" % info['msg'])
try:
json_out = json.loads(response.read())
except:
json_out = ""
return False, json_out, True
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
state=dict(required=True, choices=['running', 'started', 'ongoing']), state=dict(required=True, choices=['running', 'started', 'ongoing', 'absent']),
name=dict(required=True), name=dict(required=True),
user=dict(required=False), user=dict(required=False),
passwd=dict(required=False), passwd=dict(required=False),
token=dict(required=False), token=dict(required=False),
service=dict(required=False), service=dict(required=False, type='list', aliases=["services"]),
requester_id=dict(required=False), requester_id=dict(required=False),
hours=dict(default='1', required=False), hours=dict(default='1', required=False),
minutes=dict(default='0', required=False), minutes=dict(default='0', required=False),
@ -217,15 +260,21 @@ def main():
if state == "running" or state == "started": if state == "running" or state == "started":
if not service: if not service:
module.fail_json(msg="service not specified") module.fail_json(msg="service not specified")
(rc, out) = create(module, name, user, passwd, token, requester_id, service, hours, minutes, desc) (rc, out, changed) = create(module, name, user, passwd, token, requester_id, service, hours, minutes, desc)
if rc == 0:
changed=True
if state == "ongoing": if state == "ongoing":
(rc, out) = ongoing(module, name, user, passwd, token) (rc, out, changed) = ongoing(module, name, user, passwd, token)
if state == "absent":
(rc, out, changed) = absent(module, name, user, passwd, token, requester_id, service)
if rc != 0: if rc != 0:
module.fail_json(msg="failed", result=out) module.fail_json(msg="failed", result=out)
module.exit_json(msg="success", result=out)
module.exit_json(msg="success", result=out, changed=changed)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

Loading…
Cancel
Save