From 6a89b92cdaba2f98196bcdafefb9dbcee503e650 Mon Sep 17 00:00:00 2001 From: Bruce Pennypacker Date: Fri, 12 Jun 2015 18:36:23 +0000 Subject: [PATCH 1/3] Fixed results & 'Changed'. Added 'deleted' action. Added ability to specify multiple services. --- monitoring/pagerduty.py | 72 +++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/monitoring/pagerduty.py b/monitoring/pagerduty.py index 24c622c83a8..2ed7c0cc6bb 100644 --- a/monitoring/pagerduty.py +++ b/monitoring/pagerduty.py @@ -11,6 +11,7 @@ author: - "Andrew Newdigate (@suprememoocow)" - "Dylan Silva (@thaumos)" - "Justin Johns" + - "Bruce Pennypacker" requirements: - PagerDuty API access options: @@ -19,7 +20,7 @@ options: - Create a maintenance window or get a list of ongoing windows. required: true default: null - choices: [ "running", "started", "ongoing" ] + choices: [ "running", "started", "ongoing", "deleted" ] aliases: [] name: description: @@ -61,11 +62,11 @@ options: version_added: '1.8' service: description: - - PagerDuty service ID. + - A comma separated list of PagerDuty service IDs. required: false default: null choices: [] - aliases: [] + aliases: [ services ] hours: description: - Length of maintenance window in hours. @@ -96,9 +97,6 @@ options: default: 'yes' choices: ['yes', 'no'] version_added: 1.5.1 - -notes: - - This module does not yet have support to end maintenance windows. ''' EXAMPLES=''' @@ -132,6 +130,14 @@ EXAMPLES=''' service=FOO123 hours=4 desc=deployment + register: pd_window + +# Delete the previous maintenance window +- pagerduty: name=companyabc + user=example@example.com + passwd=password123 + state=deleted + service={{ pd_window.result.maintenance_window.id }} ''' import datetime @@ -152,7 +158,7 @@ def ongoing(module, name, user, passwd, token): if info['status'] != 200: module.fail_json(msg="failed to lookup the ongoing window: %s" % info['msg']) - return False, response.read() + return False, response.read(), False def create(module, name, user, passwd, token, requester_id, service, hours, minutes, desc): @@ -166,7 +172,8 @@ def create(module, name, user, passwd, token, requester_id, service, hours, minu 'Authorization': auth_header(user, passwd, token), '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: request_data['requester_id'] = requester_id else: @@ -178,19 +185,50 @@ def create(module, name, user, passwd, token, requester_id, service, hours, minu if info['status'] != 200: 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 delete(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(): module = AnsibleModule( argument_spec=dict( - state=dict(required=True, choices=['running', 'started', 'ongoing']), + state=dict(required=True, choices=['running', 'started', 'ongoing', 'deleted']), name=dict(required=True), user=dict(required=False), passwd=dict(required=False), token=dict(required=False), - service=dict(required=False), + service=dict(required=False, type='list', aliases=["services"]), requester_id=dict(required=False), hours=dict(default='1', required=False), minutes=dict(default='0', required=False), @@ -217,15 +255,21 @@ def main(): if state == "running" or state == "started": if not service: 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": - (rc, out) = ongoing(module, name, user, passwd, token) + (rc, out, changed) = ongoing(module, name, user, passwd, token) + + if state == "deleted": + (rc, out, changed) = delete(module, name, user, passwd, token, requester_id, service) if rc != 0: 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 from ansible.module_utils.basic import * From 51bba578b6fa6103400c45d9b7015299797aef4d Mon Sep 17 00:00:00 2001 From: Bruce Pennypacker Date: Fri, 12 Jun 2015 19:58:57 +0000 Subject: [PATCH 2/3] Updated 'ongoing' to also return properly formatted json --- monitoring/pagerduty.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/monitoring/pagerduty.py b/monitoring/pagerduty.py index 2ed7c0cc6bb..bd35fbb6003 100644 --- a/monitoring/pagerduty.py +++ b/monitoring/pagerduty.py @@ -158,7 +158,12 @@ def ongoing(module, name, user, passwd, token): if info['status'] != 200: module.fail_json(msg="failed to lookup the ongoing window: %s" % info['msg']) - return False, response.read(), False + 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): From 9285d0a1c75b21a412e70a0e432d1a125945e179 Mon Sep 17 00:00:00 2001 From: Bruce Pennypacker Date: Sun, 14 Jun 2015 20:20:36 +0000 Subject: [PATCH 3/3] changed 'deleted' to 'absent' --- monitoring/pagerduty.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/monitoring/pagerduty.py b/monitoring/pagerduty.py index bd35fbb6003..b35cfbf4992 100644 --- a/monitoring/pagerduty.py +++ b/monitoring/pagerduty.py @@ -20,7 +20,7 @@ options: - Create a maintenance window or get a list of ongoing windows. required: true default: null - choices: [ "running", "started", "ongoing", "deleted" ] + choices: [ "running", "started", "ongoing", "absent" ] aliases: [] name: description: @@ -136,7 +136,7 @@ EXAMPLES=''' - pagerduty: name=companyabc user=example@example.com passwd=password123 - state=deleted + state=absent service={{ pd_window.result.maintenance_window.id }} ''' @@ -197,7 +197,7 @@ def create(module, name, user, passwd, token, requester_id, service, hours, minu return False, json_out, True -def delete(module, name, user, passwd, token, requester_id, service): +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), @@ -228,7 +228,7 @@ def main(): module = AnsibleModule( argument_spec=dict( - state=dict(required=True, choices=['running', 'started', 'ongoing', 'deleted']), + state=dict(required=True, choices=['running', 'started', 'ongoing', 'absent']), name=dict(required=True), user=dict(required=False), passwd=dict(required=False), @@ -267,8 +267,8 @@ def main(): if state == "ongoing": (rc, out, changed) = ongoing(module, name, user, passwd, token) - if state == "deleted": - (rc, out, changed) = delete(module, name, user, passwd, token, requester_id, service) + if state == "absent": + (rc, out, changed) = absent(module, name, user, passwd, token, requester_id, service) if rc != 0: module.fail_json(msg="failed", result=out)