| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2020, Datadog, Inc |
| 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: datadog_downtime |
| 11 |
short_description: Manages Datadog downtimes |
| 12 |
version_added: 2.0.0 |
| 13 |
description: |
| 14 |
- Manages downtimes within Datadog. |
| 15 |
- Options as described on U(https://docs.datadoghq.com/api/v1/downtimes/). |
| 16 |
author: |
| 17 |
- Datadog (@Datadog) |
| 18 |
requirements: |
| 19 |
- datadog-api-client |
| 20 |
- Python 3.6+ |
| 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 |
api_key: |
| 30 |
description: |
| 31 |
- Your Datadog API key. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
api_host: |
| 35 |
description: |
| 36 |
- The URL to the Datadog API. |
| 37 |
- This value can also be set with the E(DATADOG_HOST) environment variable. |
| 38 |
default: https://api.datadoghq.com |
| 39 |
type: str |
| 40 |
app_key: |
| 41 |
description: |
| 42 |
- Your Datadog app key. |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
state: |
| 46 |
description: |
| 47 |
- The designated state of the downtime. |
| 48 |
choices: ["present", "absent"] |
| 49 |
default: present |
| 50 |
type: str |
| 51 |
id: |
| 52 |
description: |
| 53 |
- The identifier of the downtime. |
| 54 |
- If empty, a new downtime gets created, otherwise it is either updated or deleted depending of the O(state). |
| 55 |
- To keep your playbook idempotent, you should save the identifier in a file and read it in a lookup. |
| 56 |
type: int |
| 57 |
monitor_tags: |
| 58 |
description: |
| 59 |
- A list of monitor tags to which the downtime applies. |
| 60 |
- The resulting downtime applies to monitors that match ALL provided monitor tags. |
| 61 |
type: list |
| 62 |
elements: str |
| 63 |
scope: |
| 64 |
description: |
| 65 |
- A list of scopes to which the downtime applies. |
| 66 |
- The resulting downtime applies to sources that matches ALL provided scopes. |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
monitor_id: |
| 70 |
description: |
| 71 |
- The ID of the monitor to mute. If not provided, the downtime applies to all monitors. |
| 72 |
type: int |
| 73 |
downtime_message: |
| 74 |
description: |
| 75 |
- A message to include with notifications for this downtime. |
| 76 |
- Email notifications can be sent to specific users by using the same "@username" notation as events. |
| 77 |
type: str |
| 78 |
start: |
| 79 |
type: int |
| 80 |
description: |
| 81 |
- POSIX timestamp to start the downtime. If not provided, the downtime starts the moment it is created. |
| 82 |
end: |
| 83 |
type: int |
| 84 |
description: |
| 85 |
- POSIX timestamp to end the downtime. If not provided, the downtime is in effect until you cancel it. |
| 86 |
timezone: |
| 87 |
description: |
| 88 |
- The timezone for the downtime. |
| 89 |
type: str |
| 90 |
rrule: |
| 91 |
description: |
| 92 |
- The C(RRULE) standard for defining recurring events. |
| 93 |
- For example, to have a recurring event on the first day of each month, select a type of rrule and set the C(FREQ) |
| 94 |
to C(MONTHLY) and C(BYMONTHDAY) to C(1). |
| 95 |
- Most common rrule options from the iCalendar Spec are supported. |
| 96 |
- Attributes specifying the duration in C(RRULE) are not supported (for example C(DTSTART), C(DTEND), C(DURATION)). |
| 97 |
type: str |
| 98 |
""" |
| 99 |
|
| 100 |
EXAMPLES = r""" |
| 101 |
- name: Create a downtime |
| 102 |
register: downtime_var |
| 103 |
community.general.datadog_downtime: |
| 104 |
state: present |
| 105 |
monitor_tags: |
| 106 |
- "foo:bar" |
| 107 |
downtime_message: "Downtime for foo:bar" |
| 108 |
scope: "test" |
| 109 |
api_key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 110 |
app_key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 111 |
# Lookup the id in the file and ignore errors if the file doesn't exits, so downtime gets created |
| 112 |
id: "{{ lookup('file', inventory_hostname ~ '_downtime_id.txt', errors='ignore') }}" |
| 113 |
- name: Save downtime id to file for later updates and idempotence |
| 114 |
delegate_to: localhost |
| 115 |
copy: |
| 116 |
content: "{{ downtime.downtime.id }}" |
| 117 |
dest: "{{ inventory_hostname ~ '_downtime_id.txt' }}" |
| 118 |
""" |
| 119 |
|
| 120 |
RETURN = r""" |
| 121 |
# Returns the downtime JSON dictionary from the API response under the C(downtime) key. |
| 122 |
# See https://docs.datadoghq.com/api/v1/downtimes/#schedule-a-downtime for more details. |
| 123 |
downtime: |
| 124 |
description: The downtime returned by the API. |
| 125 |
type: dict |
| 126 |
returned: always |
| 127 |
sample: |
| 128 |
{ |
| 129 |
"active": true, |
| 130 |
"canceled": null, |
| 131 |
"creator_id": 1445416, |
| 132 |
"disabled": false, |
| 133 |
"downtime_type": 2, |
| 134 |
"end": null, |
| 135 |
"id": 1055751000, |
| 136 |
"message": "Downtime for foo:bar", |
| 137 |
"monitor_id": null, |
| 138 |
"monitor_tags": [ |
| 139 |
"foo:bar" |
| 140 |
], |
| 141 |
"parent_id": null, |
| 142 |
"recurrence": null, |
| 143 |
"scope": [ |
| 144 |
"test" |
| 145 |
], |
| 146 |
"start": 1607015009, |
| 147 |
"timezone": "UTC", |
| 148 |
"updater_id": null |
| 149 |
} |
| 150 |
""" |
| 151 |
|
| 152 |
import traceback |
| 153 |
|
| 154 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 155 |
|
| 156 |
# Import Datadog |
| 157 |
|
| 158 |
DATADOG_IMP_ERR = None |
| 159 |
HAS_DATADOG = True |
| 160 |
try: |
| 161 |
from datadog_api_client.v1 import ApiClient, ApiException, Configuration |
| 162 |
from datadog_api_client.v1.api.downtimes_api import DowntimesApi |
| 163 |
from datadog_api_client.v1.model.downtime import Downtime |
| 164 |
from datadog_api_client.v1.model.downtime_recurrence import DowntimeRecurrence |
| 165 |
except ImportError: |
| 166 |
DATADOG_IMP_ERR = traceback.format_exc() |
| 167 |
HAS_DATADOG = False |
| 168 |
|
| 169 |
|
| 170 |
def main(): |
| 171 |
module = AnsibleModule( |
| 172 |
argument_spec=dict( |
| 173 |
api_key=dict(required=True, no_log=True), |
| 174 |
api_host=dict(default="https://api.datadoghq.com"), |
| 175 |
app_key=dict(required=True, no_log=True), |
| 176 |
state=dict(choices=["present", "absent"], default="present"), |
| 177 |
monitor_tags=dict(type="list", elements="str"), |
| 178 |
scope=dict(type="list", elements="str"), |
| 179 |
monitor_id=dict(type="int"), |
| 180 |
downtime_message=dict(no_log=True), |
| 181 |
start=dict(type="int"), |
| 182 |
end=dict(type="int"), |
| 183 |
timezone=dict(type="str"), |
| 184 |
rrule=dict(type="str"), |
| 185 |
id=dict(type="int"), |
| 186 |
) |
| 187 |
) |
| 188 |
|
| 189 |
# Prepare Datadog |
| 190 |
if not HAS_DATADOG: |
| 191 |
module.fail_json(msg=missing_required_lib("datadog-api-client"), exception=DATADOG_IMP_ERR) |
| 192 |
|
| 193 |
configuration = Configuration( |
| 194 |
host=module.params["api_host"], |
| 195 |
api_key={"apiKeyAuth": module.params["api_key"], "appKeyAuth": module.params["app_key"]}, |
| 196 |
) |
| 197 |
with ApiClient(configuration) as api_client: |
| 198 |
api_client.user_agent = ( |
| 199 |
f"ansible_collection/community_general (module_name datadog_downtime) {api_client.user_agent}" |
| 200 |
) |
| 201 |
api_instance = DowntimesApi(api_client) |
| 202 |
|
| 203 |
# Validate api and app keys |
| 204 |
try: |
| 205 |
api_instance.list_downtimes(current_only=True) |
| 206 |
except ApiException as e: |
| 207 |
module.fail_json(msg=f"Failed to connect Datadog server using given app_key and api_key: {e}") |
| 208 |
|
| 209 |
if module.params["state"] == "present": |
| 210 |
schedule_downtime(module, api_client) |
| 211 |
elif module.params["state"] == "absent": |
| 212 |
cancel_downtime(module, api_client) |
| 213 |
|
| 214 |
|
| 215 |
def _get_downtime(module, api_client): |
| 216 |
api = DowntimesApi(api_client) |
| 217 |
downtime = None |
| 218 |
if module.params["id"]: |
| 219 |
try: |
| 220 |
downtime = api.get_downtime(module.params["id"]) |
| 221 |
except ApiException as e: |
| 222 |
module.fail_json(msg=f"Failed to retrieve downtime with id {module.params['id']}: {e}") |
| 223 |
return downtime |
| 224 |
|
| 225 |
|
| 226 |
def build_downtime(module): |
| 227 |
downtime = Downtime() |
| 228 |
if module.params["monitor_tags"]: |
| 229 |
downtime.monitor_tags = module.params["monitor_tags"] |
| 230 |
if module.params["scope"]: |
| 231 |
downtime.scope = module.params["scope"] |
| 232 |
if module.params["monitor_id"]: |
| 233 |
downtime.monitor_id = module.params["monitor_id"] |
| 234 |
if module.params["downtime_message"]: |
| 235 |
downtime.message = module.params["downtime_message"] |
| 236 |
if module.params["start"]: |
| 237 |
downtime.start = module.params["start"] |
| 238 |
if module.params["end"]: |
| 239 |
downtime.end = module.params["end"] |
| 240 |
if module.params["timezone"]: |
| 241 |
downtime.timezone = module.params["timezone"] |
| 242 |
if module.params["rrule"]: |
| 243 |
downtime.recurrence = DowntimeRecurrence( |
| 244 |
rrule=module.params["rrule"], |
| 245 |
type="rrule", |
| 246 |
) |
| 247 |
return downtime |
| 248 |
|
| 249 |
|
| 250 |
def _resp_to_dict(resp): |
| 251 |
d = resp.to_dict() |
| 252 |
if "uuid" in d: |
| 253 |
d["uuid"] = str(d["uuid"]) |
| 254 |
return d |
| 255 |
|
| 256 |
|
| 257 |
def _post_downtime(module, api_client): |
| 258 |
api = DowntimesApi(api_client) |
| 259 |
downtime = build_downtime(module) |
| 260 |
try: |
| 261 |
resp = api.create_downtime(downtime) |
| 262 |
module.params["id"] = resp.id |
| 263 |
module.exit_json(changed=True, downtime=_resp_to_dict(resp)) |
| 264 |
except ApiException as e: |
| 265 |
module.fail_json(msg=f"Failed to create downtime: {e}") |
| 266 |
|
| 267 |
|
| 268 |
def _equal_dicts(a, b, ignore_keys): |
| 269 |
ka = set(a).difference(ignore_keys) |
| 270 |
kb = set(b).difference(ignore_keys) |
| 271 |
return ka == kb and all(a[k] == b[k] for k in ka) |
| 272 |
|
| 273 |
|
| 274 |
def _update_downtime(module, current_downtime, api_client): |
| 275 |
api = DowntimesApi(api_client) |
| 276 |
downtime = build_downtime(module) |
| 277 |
try: |
| 278 |
if current_downtime.disabled: |
| 279 |
resp = api.create_downtime(downtime) |
| 280 |
else: |
| 281 |
resp = api.update_downtime(module.params["id"], downtime) |
| 282 |
if _equal_dicts(resp.to_dict(), current_downtime.to_dict(), ["active", "creator_id", "updater_id"]): |
| 283 |
module.exit_json(changed=False, downtime=_resp_to_dict(resp)) |
| 284 |
else: |
| 285 |
module.exit_json(changed=True, downtime=_resp_to_dict(resp)) |
| 286 |
except ApiException as e: |
| 287 |
module.fail_json(msg=f"Failed to update downtime: {e}") |
| 288 |
|
| 289 |
|
| 290 |
def schedule_downtime(module, api_client): |
| 291 |
downtime = _get_downtime(module, api_client) |
| 292 |
if downtime is None: |
| 293 |
_post_downtime(module, api_client) |
| 294 |
else: |
| 295 |
_update_downtime(module, downtime, api_client) |
| 296 |
|
| 297 |
|
| 298 |
def cancel_downtime(module, api_client): |
| 299 |
downtime = _get_downtime(module, api_client) |
| 300 |
api = DowntimesApi(api_client) |
| 301 |
if downtime is None: |
| 302 |
module.exit_json(changed=False) |
| 303 |
try: |
| 304 |
api.cancel_downtime(downtime["id"]) |
| 305 |
except ApiException as e: |
| 306 |
module.fail_json(msg=f"Failed to create downtime: {e}") |
| 307 |
|
| 308 |
module.exit_json(changed=True) |
| 309 |
|
| 310 |
|
| 311 |
if __name__ == "__main__": |
| 312 |
main() |