2013-05-27 20:55:29 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
|
|
|
|
module: pagerduty
|
|
|
|
short_description: Create PagerDuty maintenance windows
|
|
|
|
description:
|
|
|
|
- This module will let you create PagerDuty maintenance windows
|
2013-05-30 00:56:34 +02:00
|
|
|
version_added: "1.2"
|
2013-05-27 20:55:29 +02:00
|
|
|
author: Justin Johns
|
|
|
|
requirements:
|
|
|
|
- PagerDuty API access
|
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Create a maintenance window or get a list of ongoing windows.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
choices: [ "running", "started", "ongoing" ]
|
|
|
|
aliases: []
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- PagerDuty unique subdomain.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- PagerDuty user ID.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
|
|
|
passwd:
|
|
|
|
description:
|
|
|
|
- PagerDuty user password.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
2014-03-05 23:22:56 +01:00
|
|
|
requester_id:
|
|
|
|
description:
|
|
|
|
- ID of user making the request. Only needed when using a token and creating a maintenance_window.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
2013-05-27 20:55:29 +02:00
|
|
|
service:
|
|
|
|
description:
|
|
|
|
- PagerDuty service ID.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
|
|
|
hours:
|
|
|
|
description:
|
|
|
|
- Length of maintenance window in hours.
|
|
|
|
required: false
|
|
|
|
default: 1
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
2014-03-05 23:22:56 +01:00
|
|
|
minutes:
|
|
|
|
description:
|
|
|
|
- Maintenance window in minutes (this is added to the hours).
|
|
|
|
required: false
|
|
|
|
default: 0
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
2013-05-27 20:55:29 +02:00
|
|
|
desc:
|
|
|
|
description:
|
|
|
|
- Short description of maintenance window.
|
|
|
|
required: false
|
|
|
|
default: Created by Ansible
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
|
|
|
notes:
|
|
|
|
- This module does not yet have support to end maintenance windows.
|
|
|
|
'''
|
|
|
|
|
2013-05-30 00:34:38 +02:00
|
|
|
EXAMPLES='''
|
|
|
|
# List ongoing maintenance windows.
|
2013-06-17 08:53:46 +02:00
|
|
|
- pagerduty: name=companyabc user=example@example.com passwd=password123 state=ongoing
|
2013-05-30 00:34:38 +02:00
|
|
|
|
|
|
|
# Create a 1 hour maintenance window for service FOO123.
|
2013-06-17 08:53:46 +02:00
|
|
|
- pagerduty: name=companyabc
|
|
|
|
user=example@example.com
|
|
|
|
passwd=password123
|
|
|
|
state=running
|
|
|
|
service=FOO123
|
2013-05-30 00:34:38 +02:00
|
|
|
|
|
|
|
# Create a 4 hour maintenance window for service FOO123 with the description "deployment".
|
2013-06-17 08:53:46 +02:00
|
|
|
- pagerduty: name=companyabc
|
|
|
|
user=example@example.com
|
|
|
|
passwd=password123
|
|
|
|
state=running
|
|
|
|
service=FOO123
|
|
|
|
hours=4
|
|
|
|
desc=deployment
|
2013-05-30 00:34:38 +02:00
|
|
|
'''
|
|
|
|
|
2013-05-27 20:55:29 +02:00
|
|
|
import json
|
|
|
|
import datetime
|
|
|
|
import urllib2
|
|
|
|
import base64
|
|
|
|
|
2014-03-05 23:22:56 +01:00
|
|
|
def create_req(url, data, name, user, passwd, token):
|
|
|
|
req = urllib2.Request(url, data)
|
|
|
|
if token:
|
|
|
|
req.add_header("Authorization", "Token token=%s" % token)
|
|
|
|
else:
|
|
|
|
auth = base64.encodestring('%s:%s' % (user, passwd)).replace('\n', '')
|
|
|
|
req.add_header("Authorization", "Basic %s" % auth)
|
2013-05-27 20:55:29 +02:00
|
|
|
|
2014-03-05 23:22:56 +01:00
|
|
|
return req
|
2013-05-27 20:55:29 +02:00
|
|
|
|
2014-03-05 23:22:56 +01:00
|
|
|
def ongoing(name, user, passwd, token):
|
2013-05-27 20:55:29 +02:00
|
|
|
url = "https://" + name + ".pagerduty.com/api/v1/maintenance_windows/ongoing"
|
2014-03-05 23:22:56 +01:00
|
|
|
req = create_req(url, None, name, user, passwd, token)
|
2013-05-27 20:55:29 +02:00
|
|
|
res = urllib2.urlopen(req)
|
|
|
|
out = res.read()
|
|
|
|
|
|
|
|
return False, out
|
|
|
|
|
|
|
|
|
2014-03-05 23:22:56 +01:00
|
|
|
def create(name, user, passwd, token, requester_id, service, hours, minutes, desc):
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
now = datetime.datetime.utcnow()
|
2014-03-05 23:22:56 +01:00
|
|
|
later = now + datetime.timedelta(hours=int(hours), minutes=int(minutes))
|
2013-05-27 20:55:29 +02:00
|
|
|
start = now.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
end = later.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
|
|
|
|
url = "https://" + name + ".pagerduty.com/api/v1/maintenance_windows"
|
2014-03-05 23:22:56 +01:00
|
|
|
request_data = {'maintenance_window': {'start_time': start, 'end_time': end, 'description': desc, 'service_ids': [service]}}
|
|
|
|
if requester_id:
|
|
|
|
request_data['requester_id'] = requester_id
|
2013-05-27 20:55:29 +02:00
|
|
|
|
2014-03-05 23:22:56 +01:00
|
|
|
data = json.dumps(request_data)
|
|
|
|
|
|
|
|
req = create_req(url, data, name, user, passwd, token)
|
2013-05-27 20:55:29 +02:00
|
|
|
req.add_header('Content-Type', 'application/json')
|
2014-03-05 23:22:56 +01:00
|
|
|
|
2013-05-27 20:55:29 +02:00
|
|
|
res = urllib2.urlopen(req)
|
|
|
|
out = res.read()
|
|
|
|
|
|
|
|
return False, out
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
state=dict(required=True, choices=['running', 'started', 'ongoing']),
|
|
|
|
name=dict(required=True),
|
2014-03-05 23:22:56 +01:00
|
|
|
user=dict(required=False),
|
|
|
|
passwd=dict(required=False),
|
|
|
|
token=dict(required=False),
|
2013-05-27 20:55:29 +02:00
|
|
|
service=dict(required=False),
|
2014-03-05 23:22:56 +01:00
|
|
|
requester_id=dict(required=False),
|
2013-05-27 20:55:29 +02:00
|
|
|
hours=dict(default='1', required=False),
|
2014-03-05 23:22:56 +01:00
|
|
|
minutes=dict(default='0', required=False),
|
2013-05-27 20:55:29 +02:00
|
|
|
desc=dict(default='Created by Ansible', required=False)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
name = module.params['name']
|
|
|
|
user = module.params['user']
|
|
|
|
passwd = module.params['passwd']
|
2014-03-05 23:22:56 +01:00
|
|
|
token = module.params['token']
|
2013-05-27 20:55:29 +02:00
|
|
|
service = module.params['service']
|
|
|
|
hours = module.params['hours']
|
2014-03-05 23:22:56 +01:00
|
|
|
minutes = module.params['minutes']
|
|
|
|
token = module.params['token']
|
2013-05-27 20:55:29 +02:00
|
|
|
desc = module.params['desc']
|
2014-03-05 23:22:56 +01:00
|
|
|
requester_id = module.params['requester_id']
|
|
|
|
|
|
|
|
if not token and not (user or passwd):
|
|
|
|
module.fail_json(msg="neither user and passwd nor token specified")
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
if state == "running" or state == "started":
|
|
|
|
if not service:
|
|
|
|
module.fail_json(msg="service not specified")
|
2014-03-05 23:22:56 +01:00
|
|
|
(rc, out) = create(name, user, passwd, token, requester_id, service, hours, minutes, desc)
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
if state == "ongoing":
|
2014-03-05 23:22:56 +01:00
|
|
|
(rc, out) = ongoing(name, user, passwd, token)
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="failed", result=out)
|
|
|
|
|
|
|
|
module.exit_json(msg="success", result=out)
|
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-05-27 20:55:29 +02:00
|
|
|
main()
|