2013-05-27 20:55:29 +02:00
|
|
|
#!/usr/bin/python
|
2015-07-30 20:48:59 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
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"
|
2015-05-13 21:48:15 +02:00
|
|
|
author:
|
2015-05-13 21:51:34 +02:00
|
|
|
- "Andrew Newdigate (@suprememoocow)"
|
|
|
|
- "Dylan Silva (@thaumos)"
|
|
|
|
- "Justin Johns"
|
2015-06-12 20:36:23 +02:00
|
|
|
- "Bruce Pennypacker"
|
2013-05-27 20:55:29 +02:00
|
|
|
requirements:
|
|
|
|
- PagerDuty API access
|
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Create a maintenance window or get a list of ongoing windows.
|
|
|
|
required: true
|
|
|
|
default: null
|
2015-06-14 22:20:36 +02:00
|
|
|
choices: [ "running", "started", "ongoing", "absent" ]
|
2013-05-27 20:55:29 +02:00
|
|
|
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-06 10:18:38 +01:00
|
|
|
token:
|
|
|
|
description:
|
|
|
|
- A pagerduty token, generated on the pagerduty site. Can be used instead of
|
|
|
|
user/passwd combination.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
2014-08-08 19:38:59 +02:00
|
|
|
version_added: '1.8'
|
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: []
|
2014-08-08 19:38:59 +02:00
|
|
|
version_added: '1.8'
|
2013-05-27 20:55:29 +02:00
|
|
|
service:
|
|
|
|
description:
|
2015-06-12 20:36:23 +02:00
|
|
|
- A comma separated list of PagerDuty service IDs.
|
2013-05-27 20:55:29 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
2015-06-12 20:36:23 +02:00
|
|
|
aliases: [ services ]
|
2013-05-27 20:55:29 +02:00
|
|
|
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: []
|
2014-08-08 19:38:59 +02:00
|
|
|
version_added: '1.8'
|
2013-05-27 20:55:29 +02:00
|
|
|
desc:
|
|
|
|
description:
|
|
|
|
- Short description of maintenance window.
|
|
|
|
required: false
|
|
|
|
default: Created by Ansible
|
|
|
|
choices: []
|
|
|
|
aliases: []
|
2014-03-12 16:19:22 +01:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
version_added: 1.5.1
|
2013-05-27 20:55:29 +02:00
|
|
|
'''
|
|
|
|
|
2013-05-30 00:34:38 +02:00
|
|
|
EXAMPLES='''
|
2014-03-06 10:18:38 +01:00
|
|
|
# List ongoing maintenance windows using a user/passwd
|
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
|
|
|
|
2014-03-06 10:18:38 +01:00
|
|
|
# List ongoing maintenance windows using a token
|
|
|
|
- pagerduty: name=companyabc token=xxxxxxxxxxxxxx state=ongoing
|
|
|
|
|
|
|
|
# Create a 1 hour maintenance window for service FOO123, using a user/passwd
|
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
|
|
|
|
2014-03-06 10:18:38 +01:00
|
|
|
# Create a 5 minute maintenance window for service FOO123, using a token
|
|
|
|
- pagerduty: name=companyabc
|
|
|
|
token=xxxxxxxxxxxxxx
|
|
|
|
hours=0
|
|
|
|
minutes=5
|
|
|
|
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
|
2015-06-12 20:36:23 +02:00
|
|
|
register: pd_window
|
|
|
|
|
|
|
|
# Delete the previous maintenance window
|
|
|
|
- pagerduty: name=companyabc
|
|
|
|
user=example@example.com
|
|
|
|
passwd=password123
|
2015-06-14 22:20:36 +02:00
|
|
|
state=absent
|
2015-06-12 20:36:23 +02:00
|
|
|
service={{ pd_window.result.maintenance_window.id }}
|
2013-05-30 00:34:38 +02:00
|
|
|
'''
|
|
|
|
|
2013-05-27 20:55:29 +02:00
|
|
|
import datetime
|
|
|
|
import base64
|
|
|
|
|
2014-04-16 23:43:01 +02:00
|
|
|
def auth_header(user, passwd, token):
|
|
|
|
if token:
|
|
|
|
return "Token token=%s" % token
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
auth = base64.encodestring('%s:%s' % (user, passwd)).replace('\n', '')
|
2014-04-16 23:43:01 +02:00
|
|
|
return "Basic %s" % auth
|
|
|
|
|
|
|
|
def ongoing(module, name, user, passwd, token):
|
2013-05-27 20:55:29 +02:00
|
|
|
url = "https://" + name + ".pagerduty.com/api/v1/maintenance_windows/ongoing"
|
2014-04-16 23:43:01 +02:00
|
|
|
headers = {"Authorization": auth_header(user, passwd, token)}
|
2013-05-27 20:55:29 +02:00
|
|
|
|
2014-03-10 22:06:52 +01:00
|
|
|
response, info = fetch_url(module, url, headers=headers)
|
|
|
|
if info['status'] != 200:
|
|
|
|
module.fail_json(msg="failed to lookup the ongoing window: %s" % info['msg'])
|
2013-05-27 20:55:29 +02:00
|
|
|
|
2015-06-12 21:58:57 +02:00
|
|
|
try:
|
|
|
|
json_out = json.loads(response.read())
|
|
|
|
except:
|
|
|
|
json_out = ""
|
|
|
|
|
|
|
|
return False, json_out, False
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
|
2014-04-16 23:43:01 +02:00
|
|
|
def create(module, 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-10 22:06:52 +01:00
|
|
|
headers = {
|
2014-04-16 23:43:01 +02:00
|
|
|
'Authorization': auth_header(user, passwd, token),
|
2014-03-10 22:06:52 +01:00
|
|
|
'Content-Type' : 'application/json',
|
|
|
|
}
|
2015-06-12 20:36:23 +02:00
|
|
|
request_data = {'maintenance_window': {'start_time': start, 'end_time': end, 'description': desc, 'service_ids': service}}
|
|
|
|
|
2014-03-05 23:22:56 +01:00
|
|
|
if requester_id:
|
|
|
|
request_data['requester_id'] = requester_id
|
2014-04-17 00:00:01 +02:00
|
|
|
else:
|
|
|
|
if token:
|
|
|
|
module.fail_json(msg="requester_id is required when using a token")
|
2013-05-27 20:55:29 +02:00
|
|
|
|
2014-03-05 23:22:56 +01:00
|
|
|
data = json.dumps(request_data)
|
2014-03-10 22:06:52 +01:00
|
|
|
response, info = fetch_url(module, url, data=data, headers=headers, method='POST')
|
|
|
|
if info['status'] != 200:
|
|
|
|
module.fail_json(msg="failed to create the window: %s" % info['msg'])
|
2014-03-05 23:22:56 +01:00
|
|
|
|
2015-06-12 20:36:23 +02:00
|
|
|
try:
|
|
|
|
json_out = json.loads(response.read())
|
|
|
|
except:
|
|
|
|
json_out = ""
|
|
|
|
|
|
|
|
return False, json_out, True
|
|
|
|
|
2015-06-14 22:20:36 +02:00
|
|
|
def absent(module, name, user, passwd, token, requester_id, service):
|
2015-06-12 20:36:23 +02:00
|
|
|
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
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2015-06-14 22:20:36 +02:00
|
|
|
state=dict(required=True, choices=['running', 'started', 'ongoing', 'absent']),
|
2013-05-27 20:55:29 +02:00
|
|
|
name=dict(required=True),
|
2014-03-05 23:22:56 +01:00
|
|
|
user=dict(required=False),
|
|
|
|
passwd=dict(required=False),
|
|
|
|
token=dict(required=False),
|
2015-06-12 20:36:23 +02:00
|
|
|
service=dict(required=False, type='list', aliases=["services"]),
|
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),
|
2014-03-12 16:19:22 +01:00
|
|
|
desc=dict(default='Created by Ansible', required=False),
|
|
|
|
validate_certs = dict(default='yes', type='bool'),
|
2013-05-27 20:55:29 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2015-06-12 20:36:23 +02:00
|
|
|
(rc, out, changed) = create(module, name, user, passwd, token, requester_id, service, hours, minutes, desc)
|
|
|
|
if rc == 0:
|
|
|
|
changed=True
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
if state == "ongoing":
|
2015-06-12 20:36:23 +02:00
|
|
|
(rc, out, changed) = ongoing(module, name, user, passwd, token)
|
|
|
|
|
2015-06-14 22:20:36 +02:00
|
|
|
if state == "absent":
|
|
|
|
(rc, out, changed) = absent(module, name, user, passwd, token, requester_id, service)
|
2013-05-27 20:55:29 +02:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="failed", result=out)
|
|
|
|
|
2015-06-12 20:36:23 +02:00
|
|
|
|
|
|
|
module.exit_json(msg="success", result=out, changed=changed)
|
2013-05-27 20:55:29 +02:00
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2014-03-10 22:06:52 +01:00
|
|
|
from ansible.module_utils.urls import *
|
|
|
|
|
2013-05-27 20:55:29 +02:00
|
|
|
main()
|