| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: pagerduty |
| 11 |
short_description: Create PagerDuty maintenance windows |
| 12 |
description: |
| 13 |
- This module lets you create PagerDuty maintenance windows. |
| 14 |
author: |
| 15 |
- "Andrew Newdigate (@suprememoocow)" |
| 16 |
- "Dylan Silva (@thaumos)" |
| 17 |
- "Justin Johns (!UNKNOWN)" |
| 18 |
- "Bruce Pennypacker (@bpennypacker)" |
| 19 |
requirements: |
| 20 |
- PagerDuty API access |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: none |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
options: |
| 29 |
state: |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- Create a maintenance window or get a list of ongoing windows. |
| 33 |
required: true |
| 34 |
choices: ["running", "started", "ongoing", "absent"] |
| 35 |
name: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- PagerDuty unique subdomain. Obsolete. It is not used with PagerDuty REST v2 API. |
| 39 |
user: |
| 40 |
type: str |
| 41 |
description: |
| 42 |
- PagerDuty user ID. Obsolete. Please, use O(token) for authorization. |
| 43 |
token: |
| 44 |
type: str |
| 45 |
description: |
| 46 |
- A pagerduty token, generated on the pagerduty site. It is used for authorization. |
| 47 |
required: true |
| 48 |
requester_id: |
| 49 |
type: str |
| 50 |
description: |
| 51 |
- ID of user making the request. Only needed when creating a maintenance_window. |
| 52 |
service: |
| 53 |
type: list |
| 54 |
elements: str |
| 55 |
description: |
| 56 |
- A comma separated list of PagerDuty service IDs. |
| 57 |
aliases: [services] |
| 58 |
window_id: |
| 59 |
type: str |
| 60 |
description: |
| 61 |
- ID of maintenance window. Only needed when absent a maintenance_window. |
| 62 |
hours: |
| 63 |
type: str |
| 64 |
description: |
| 65 |
- Length of maintenance window in hours. |
| 66 |
default: '1' |
| 67 |
minutes: |
| 68 |
type: str |
| 69 |
description: |
| 70 |
- Maintenance window in minutes (this is added to the hours). |
| 71 |
default: '0' |
| 72 |
desc: |
| 73 |
type: str |
| 74 |
description: |
| 75 |
- Short description of maintenance window. |
| 76 |
default: Created by Ansible |
| 77 |
validate_certs: |
| 78 |
description: |
| 79 |
- If V(false), SSL certificates are not validated. This should only be used on personally controlled sites using self-signed |
| 80 |
certificates. |
| 81 |
type: bool |
| 82 |
default: true |
| 83 |
""" |
| 84 |
|
| 85 |
EXAMPLES = r""" |
| 86 |
- name: List ongoing maintenance windows using a token |
| 87 |
community.general.pagerduty: |
| 88 |
name: companyabc |
| 89 |
token: xxxxxxxxxxxxxx |
| 90 |
state: ongoing |
| 91 |
|
| 92 |
- name: Create a 1 hour maintenance window for service FOO123 |
| 93 |
community.general.pagerduty: |
| 94 |
name: companyabc |
| 95 |
user: [email protected] |
| 96 |
token: yourtoken |
| 97 |
state: running |
| 98 |
service: FOO123 |
| 99 |
|
| 100 |
- name: Create a 5 minute maintenance window for service FOO123 |
| 101 |
community.general.pagerduty: |
| 102 |
name: companyabc |
| 103 |
token: xxxxxxxxxxxxxx |
| 104 |
hours: 0 |
| 105 |
minutes: 5 |
| 106 |
state: running |
| 107 |
service: FOO123 |
| 108 |
|
| 109 |
|
| 110 |
- name: Create a 4 hour maintenance window for service FOO123 with the description "deployment" |
| 111 |
community.general.pagerduty: |
| 112 |
name: companyabc |
| 113 |
user: [email protected] |
| 114 |
state: running |
| 115 |
service: FOO123 |
| 116 |
hours: 4 |
| 117 |
desc: deployment |
| 118 |
register: pd_window |
| 119 |
|
| 120 |
- name: Delete the previous maintenance window |
| 121 |
community.general.pagerduty: |
| 122 |
name: companyabc |
| 123 |
user: [email protected] |
| 124 |
state: absent |
| 125 |
window_id: '{{ pd_window.result.maintenance_window.id }}' |
| 126 |
|
| 127 |
# Delete a maintenance window from a separate playbook than its creation, |
| 128 |
# and if it is the only existing maintenance window |
| 129 |
- name: Check |
| 130 |
community.general.pagerduty: |
| 131 |
requester_id: XXXXXXX |
| 132 |
token: yourtoken |
| 133 |
state: ongoing |
| 134 |
register: pd_window |
| 135 |
|
| 136 |
- name: Delete |
| 137 |
community.general.pagerduty: |
| 138 |
requester_id: XXXXXXX |
| 139 |
token: yourtoken |
| 140 |
state: absent |
| 141 |
window_id: "{{ pd_window.result.maintenance_windows[0].id }}" |
| 142 |
""" |
| 143 |
|
| 144 |
import datetime |
| 145 |
import json |
| 146 |
|
| 147 |
from ansible.module_utils.basic import AnsibleModule |
| 148 |
from ansible.module_utils.urls import fetch_url |
| 149 |
|
| 150 |
from ansible_collections.community.general.plugins.module_utils._datetime import ( |
| 151 |
now, |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
class PagerDutyRequest: |
| 156 |
def __init__(self, module, name, user, token): |
| 157 |
self.module = module |
| 158 |
self.name = name |
| 159 |
self.user = user |
| 160 |
self.token = token |
| 161 |
self.headers = { |
| 162 |
"Content-Type": "application/json", |
| 163 |
"Authorization": self._auth_header(), |
| 164 |
"Accept": "application/vnd.pagerduty+json;version=2", |
| 165 |
} |
| 166 |
|
| 167 |
def ongoing(self, http_call=fetch_url): |
| 168 |
url = "https://api.pagerduty.com/maintenance_windows?filter=ongoing" |
| 169 |
headers = dict(self.headers) |
| 170 |
|
| 171 |
response, info = http_call(self.module, url, headers=headers) |
| 172 |
if info["status"] != 200: |
| 173 |
self.module.fail_json(msg=f"failed to lookup the ongoing window: {info['msg']}") |
| 174 |
|
| 175 |
json_out = self._read_response(response) |
| 176 |
|
| 177 |
return False, json_out, False |
| 178 |
|
| 179 |
def create(self, requester_id, service, hours, minutes, desc, http_call=fetch_url): |
| 180 |
if not requester_id: |
| 181 |
self.module.fail_json(msg="requester_id is required when maintenance window should be created") |
| 182 |
|
| 183 |
url = "https://api.pagerduty.com/maintenance_windows" |
| 184 |
|
| 185 |
headers = dict(self.headers) |
| 186 |
headers.update({"From": requester_id}) |
| 187 |
|
| 188 |
start, end = self._compute_start_end_time(hours, minutes) |
| 189 |
services = self._create_services_payload(service) |
| 190 |
|
| 191 |
request_data = { |
| 192 |
"maintenance_window": {"start_time": start, "end_time": end, "description": desc, "services": services} |
| 193 |
} |
| 194 |
|
| 195 |
data = json.dumps(request_data) |
| 196 |
response, info = http_call(self.module, url, data=data, headers=headers, method="POST") |
| 197 |
if info["status"] != 201: |
| 198 |
self.module.fail_json(msg=f"failed to create the window: {info['msg']}") |
| 199 |
|
| 200 |
json_out = self._read_response(response) |
| 201 |
|
| 202 |
return False, json_out, True |
| 203 |
|
| 204 |
def _create_services_payload(self, service): |
| 205 |
if isinstance(service, list): |
| 206 |
return [{"id": s, "type": "service_reference"} for s in service] |
| 207 |
else: |
| 208 |
return [{"id": service, "type": "service_reference"}] |
| 209 |
|
| 210 |
def _compute_start_end_time(self, hours, minutes): |
| 211 |
now_t = now() |
| 212 |
later = now_t + datetime.timedelta(hours=int(hours), minutes=int(minutes)) |
| 213 |
start = now_t.strftime("%Y-%m-%dT%H:%M:%SZ") |
| 214 |
end = later.strftime("%Y-%m-%dT%H:%M:%SZ") |
| 215 |
return start, end |
| 216 |
|
| 217 |
def absent(self, window_id, http_call=fetch_url): |
| 218 |
url = f"https://api.pagerduty.com/maintenance_windows/{window_id}" |
| 219 |
headers = dict(self.headers) |
| 220 |
|
| 221 |
response, info = http_call(self.module, url, headers=headers, method="DELETE") |
| 222 |
if info["status"] != 204: |
| 223 |
self.module.fail_json(msg=f"failed to delete the window: {info['msg']}") |
| 224 |
|
| 225 |
json_out = self._read_response(response) |
| 226 |
|
| 227 |
return False, json_out, True |
| 228 |
|
| 229 |
def _auth_header(self): |
| 230 |
return f"Token token={self.token}" |
| 231 |
|
| 232 |
def _read_response(self, response): |
| 233 |
try: |
| 234 |
return json.loads(response.read()) |
| 235 |
except Exception: |
| 236 |
return "" |
| 237 |
|
| 238 |
|
| 239 |
def main(): |
| 240 |
module = AnsibleModule( |
| 241 |
argument_spec=dict( |
| 242 |
state=dict(required=True, choices=["running", "started", "ongoing", "absent"]), |
| 243 |
name=dict(), |
| 244 |
user=dict(), |
| 245 |
token=dict(required=True, no_log=True), |
| 246 |
service=dict(type="list", elements="str", aliases=["services"]), |
| 247 |
window_id=dict(), |
| 248 |
requester_id=dict(), |
| 249 |
hours=dict(default="1"), # @TODO change to int? |
| 250 |
minutes=dict(default="0"), # @TODO change to int? |
| 251 |
desc=dict(default="Created by Ansible"), |
| 252 |
validate_certs=dict(default=True, type="bool"), |
| 253 |
) |
| 254 |
) |
| 255 |
|
| 256 |
state = module.params["state"] |
| 257 |
name = module.params["name"] |
| 258 |
user = module.params["user"] |
| 259 |
service = module.params["service"] |
| 260 |
window_id = module.params["window_id"] |
| 261 |
hours = module.params["hours"] |
| 262 |
minutes = module.params["minutes"] |
| 263 |
token = module.params["token"] |
| 264 |
desc = module.params["desc"] |
| 265 |
requester_id = module.params["requester_id"] |
| 266 |
|
| 267 |
pd = PagerDutyRequest(module, name, user, token) |
| 268 |
|
| 269 |
if state == "running" or state == "started": |
| 270 |
if not service: |
| 271 |
module.fail_json(msg="service not specified") |
| 272 |
(rc, out, changed) = pd.create(requester_id, service, hours, minutes, desc) |
| 273 |
if rc == 0: |
| 274 |
changed = True |
| 275 |
|
| 276 |
if state == "ongoing": |
| 277 |
(rc, out, changed) = pd.ongoing() |
| 278 |
|
| 279 |
if state == "absent": |
| 280 |
(rc, out, changed) = pd.absent(window_id) |
| 281 |
|
| 282 |
if rc != 0: |
| 283 |
module.fail_json(msg="failed", result=out) |
| 284 |
|
| 285 |
module.exit_json(msg="success", result=out, changed=changed) |
| 286 |
|
| 287 |
|
| 288 |
if __name__ == "__main__": |
| 289 |
main() |