mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-10 22:15:05 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -244,6 +244,7 @@ import traceback
|
|||
DATADOG_IMP_ERR = None
|
||||
try:
|
||||
from datadog import initialize, api
|
||||
|
||||
HAS_DATADOG = True
|
||||
except Exception:
|
||||
DATADOG_IMP_ERR = traceback.format_exc()
|
||||
|
|
@ -259,43 +260,54 @@ def main():
|
|||
api_key=dict(required=True, no_log=True),
|
||||
api_host=dict(),
|
||||
app_key=dict(required=True, no_log=True),
|
||||
state=dict(required=True, choices=['present', 'absent', 'mute', 'unmute']),
|
||||
type=dict(choices=['metric alert', 'service check', 'event alert', 'event-v2 alert', 'process alert',
|
||||
'log alert', 'query alert', 'trace-analytics alert',
|
||||
'rum alert', 'composite']),
|
||||
state=dict(required=True, choices=["present", "absent", "mute", "unmute"]),
|
||||
type=dict(
|
||||
choices=[
|
||||
"metric alert",
|
||||
"service check",
|
||||
"event alert",
|
||||
"event-v2 alert",
|
||||
"process alert",
|
||||
"log alert",
|
||||
"query alert",
|
||||
"trace-analytics alert",
|
||||
"rum alert",
|
||||
"composite",
|
||||
]
|
||||
),
|
||||
name=dict(required=True),
|
||||
query=dict(),
|
||||
notification_message=dict(no_log=True),
|
||||
silenced=dict(type='dict'),
|
||||
notify_no_data=dict(default=False, type='bool'),
|
||||
silenced=dict(type="dict"),
|
||||
notify_no_data=dict(default=False, type="bool"),
|
||||
no_data_timeframe=dict(),
|
||||
timeout_h=dict(),
|
||||
renotify_interval=dict(),
|
||||
escalation_message=dict(),
|
||||
notify_audit=dict(default=False, type='bool'),
|
||||
thresholds=dict(type='dict'),
|
||||
tags=dict(type='list', elements='str'),
|
||||
locked=dict(default=False, type='bool'),
|
||||
require_full_window=dict(type='bool'),
|
||||
notify_audit=dict(default=False, type="bool"),
|
||||
thresholds=dict(type="dict"),
|
||||
tags=dict(type="list", elements="str"),
|
||||
locked=dict(default=False, type="bool"),
|
||||
require_full_window=dict(type="bool"),
|
||||
new_host_delay=dict(),
|
||||
evaluation_delay=dict(),
|
||||
id=dict(),
|
||||
include_tags=dict(default=True, type='bool'),
|
||||
priority=dict(type='int'),
|
||||
notification_preset_name=dict(choices=['show_all', 'hide_query', 'hide_handles', 'hide_all']),
|
||||
renotify_occurrences=dict(type='int'),
|
||||
renotify_statuses=dict(type='list', elements='str', choices=['alert', 'warn', 'no data']),
|
||||
include_tags=dict(default=True, type="bool"),
|
||||
priority=dict(type="int"),
|
||||
notification_preset_name=dict(choices=["show_all", "hide_query", "hide_handles", "hide_all"]),
|
||||
renotify_occurrences=dict(type="int"),
|
||||
renotify_statuses=dict(type="list", elements="str", choices=["alert", "warn", "no data"]),
|
||||
)
|
||||
)
|
||||
|
||||
# Prepare Datadog
|
||||
if not HAS_DATADOG:
|
||||
module.fail_json(msg=missing_required_lib('datadogpy'), exception=DATADOG_IMP_ERR)
|
||||
module.fail_json(msg=missing_required_lib("datadogpy"), exception=DATADOG_IMP_ERR)
|
||||
|
||||
options = {
|
||||
'api_key': module.params['api_key'],
|
||||
'api_host': module.params['api_host'],
|
||||
'app_key': module.params['app_key']
|
||||
"api_key": module.params["api_key"],
|
||||
"api_host": module.params["api_host"],
|
||||
"app_key": module.params["app_key"],
|
||||
}
|
||||
|
||||
initialize(**options)
|
||||
|
|
@ -304,53 +316,58 @@ def main():
|
|||
# if not, then fail here.
|
||||
response = api.Monitor.get_all()
|
||||
if isinstance(response, dict):
|
||||
msg = response.get('errors', None)
|
||||
msg = response.get("errors", None)
|
||||
if msg:
|
||||
module.fail_json(msg=f"Failed to connect Datadog server using given app_key and api_key : {msg[0]}")
|
||||
|
||||
if module.params['state'] == 'present':
|
||||
if module.params["state"] == "present":
|
||||
install_monitor(module)
|
||||
elif module.params['state'] == 'absent':
|
||||
elif module.params["state"] == "absent":
|
||||
delete_monitor(module)
|
||||
elif module.params['state'] == 'mute':
|
||||
elif module.params["state"] == "mute":
|
||||
mute_monitor(module)
|
||||
elif module.params['state'] == 'unmute':
|
||||
elif module.params["state"] == "unmute":
|
||||
unmute_monitor(module)
|
||||
|
||||
|
||||
def _fix_template_vars(message):
|
||||
if message:
|
||||
return message.replace('[[', '{{').replace(']]', '}}')
|
||||
return message.replace("[[", "{{").replace("]]", "}}")
|
||||
return message
|
||||
|
||||
|
||||
def _get_monitor(module):
|
||||
if module.params['id'] is not None:
|
||||
monitor = api.Monitor.get(module.params['id'])
|
||||
if 'errors' in monitor:
|
||||
module.fail_json(msg=f"Failed to retrieve monitor with id {module.params['id']}, errors are {monitor['errors']}")
|
||||
if module.params["id"] is not None:
|
||||
monitor = api.Monitor.get(module.params["id"])
|
||||
if "errors" in monitor:
|
||||
module.fail_json(
|
||||
msg=f"Failed to retrieve monitor with id {module.params['id']}, errors are {monitor['errors']}"
|
||||
)
|
||||
return monitor
|
||||
else:
|
||||
monitors = api.Monitor.get_all()
|
||||
for monitor in monitors:
|
||||
if monitor['name'] == _fix_template_vars(module.params['name']):
|
||||
if monitor["name"] == _fix_template_vars(module.params["name"]):
|
||||
return monitor
|
||||
return {}
|
||||
|
||||
|
||||
def _post_monitor(module, options):
|
||||
try:
|
||||
kwargs = dict(type=module.params['type'], query=module.params['query'],
|
||||
name=_fix_template_vars(module.params['name']),
|
||||
message=_fix_template_vars(module.params['notification_message']),
|
||||
escalation_message=_fix_template_vars(module.params['escalation_message']),
|
||||
priority=module.params['priority'],
|
||||
options=options)
|
||||
if module.params['tags'] is not None:
|
||||
kwargs['tags'] = module.params['tags']
|
||||
kwargs = dict(
|
||||
type=module.params["type"],
|
||||
query=module.params["query"],
|
||||
name=_fix_template_vars(module.params["name"]),
|
||||
message=_fix_template_vars(module.params["notification_message"]),
|
||||
escalation_message=_fix_template_vars(module.params["escalation_message"]),
|
||||
priority=module.params["priority"],
|
||||
options=options,
|
||||
)
|
||||
if module.params["tags"] is not None:
|
||||
kwargs["tags"] = module.params["tags"]
|
||||
msg = api.Monitor.create(**kwargs)
|
||||
if 'errors' in msg:
|
||||
module.fail_json(msg=str(msg['errors']))
|
||||
if "errors" in msg:
|
||||
module.fail_json(msg=str(msg["errors"]))
|
||||
else:
|
||||
module.exit_json(changed=True, msg=msg)
|
||||
except Exception as e:
|
||||
|
|
@ -365,19 +382,24 @@ def _equal_dicts(a, b, ignore_keys):
|
|||
|
||||
def _update_monitor(module, monitor, options):
|
||||
try:
|
||||
kwargs = dict(id=monitor['id'], query=module.params['query'],
|
||||
name=_fix_template_vars(module.params['name']),
|
||||
message=_fix_template_vars(module.params['notification_message']),
|
||||
escalation_message=_fix_template_vars(module.params['escalation_message']),
|
||||
priority=module.params['priority'],
|
||||
options=options)
|
||||
if module.params['tags'] is not None:
|
||||
kwargs['tags'] = module.params['tags']
|
||||
kwargs = dict(
|
||||
id=monitor["id"],
|
||||
query=module.params["query"],
|
||||
name=_fix_template_vars(module.params["name"]),
|
||||
message=_fix_template_vars(module.params["notification_message"]),
|
||||
escalation_message=_fix_template_vars(module.params["escalation_message"]),
|
||||
priority=module.params["priority"],
|
||||
options=options,
|
||||
)
|
||||
if module.params["tags"] is not None:
|
||||
kwargs["tags"] = module.params["tags"]
|
||||
msg = api.Monitor.update(**kwargs)
|
||||
|
||||
if 'errors' in msg:
|
||||
module.fail_json(msg=str(msg['errors']))
|
||||
elif _equal_dicts(msg, monitor, ['creator', 'overall_state', 'modified', 'matching_downtimes', 'overall_state_modified']):
|
||||
if "errors" in msg:
|
||||
module.fail_json(msg=str(msg["errors"]))
|
||||
elif _equal_dicts(
|
||||
msg, monitor, ["creator", "overall_state", "modified", "matching_downtimes", "overall_state_modified"]
|
||||
):
|
||||
module.exit_json(changed=False, msg=msg)
|
||||
else:
|
||||
module.exit_json(changed=True, msg=msg)
|
||||
|
|
@ -387,27 +409,30 @@ def _update_monitor(module, monitor, options):
|
|||
|
||||
def install_monitor(module):
|
||||
options = {
|
||||
"silenced": module.params['silenced'],
|
||||
"notify_no_data": module.boolean(module.params['notify_no_data']),
|
||||
"no_data_timeframe": module.params['no_data_timeframe'],
|
||||
"timeout_h": module.params['timeout_h'],
|
||||
"renotify_interval": module.params['renotify_interval'],
|
||||
"escalation_message": module.params['escalation_message'],
|
||||
"notify_audit": module.boolean(module.params['notify_audit']),
|
||||
"locked": module.boolean(module.params['locked']),
|
||||
"require_full_window": module.params['require_full_window'],
|
||||
"new_host_delay": module.params['new_host_delay'],
|
||||
"evaluation_delay": module.params['evaluation_delay'],
|
||||
"include_tags": module.params['include_tags'],
|
||||
"notification_preset_name": module.params['notification_preset_name'],
|
||||
"renotify_occurrences": module.params['renotify_occurrences'],
|
||||
"renotify_statuses": module.params['renotify_statuses'],
|
||||
"silenced": module.params["silenced"],
|
||||
"notify_no_data": module.boolean(module.params["notify_no_data"]),
|
||||
"no_data_timeframe": module.params["no_data_timeframe"],
|
||||
"timeout_h": module.params["timeout_h"],
|
||||
"renotify_interval": module.params["renotify_interval"],
|
||||
"escalation_message": module.params["escalation_message"],
|
||||
"notify_audit": module.boolean(module.params["notify_audit"]),
|
||||
"locked": module.boolean(module.params["locked"]),
|
||||
"require_full_window": module.params["require_full_window"],
|
||||
"new_host_delay": module.params["new_host_delay"],
|
||||
"evaluation_delay": module.params["evaluation_delay"],
|
||||
"include_tags": module.params["include_tags"],
|
||||
"notification_preset_name": module.params["notification_preset_name"],
|
||||
"renotify_occurrences": module.params["renotify_occurrences"],
|
||||
"renotify_statuses": module.params["renotify_statuses"],
|
||||
}
|
||||
|
||||
if module.params['type'] == "service check":
|
||||
options["thresholds"] = module.params['thresholds'] or {'ok': 1, 'critical': 1, 'warning': 1}
|
||||
if module.params['type'] in ["metric alert", "log alert", "query alert", "trace-analytics alert", "rum alert"] and module.params['thresholds'] is not None:
|
||||
options["thresholds"] = module.params['thresholds']
|
||||
if module.params["type"] == "service check":
|
||||
options["thresholds"] = module.params["thresholds"] or {"ok": 1, "critical": 1, "warning": 1}
|
||||
if (
|
||||
module.params["type"] in ["metric alert", "log alert", "query alert", "trace-analytics alert", "rum alert"]
|
||||
and module.params["thresholds"] is not None
|
||||
):
|
||||
options["thresholds"] = module.params["thresholds"]
|
||||
|
||||
monitor = _get_monitor(module)
|
||||
if not monitor:
|
||||
|
|
@ -421,7 +446,7 @@ def delete_monitor(module):
|
|||
if not monitor:
|
||||
module.exit_json(changed=False)
|
||||
try:
|
||||
msg = api.Monitor.delete(monitor['id'])
|
||||
msg = api.Monitor.delete(monitor["id"])
|
||||
module.exit_json(changed=True, msg=msg)
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||
|
|
@ -431,15 +456,20 @@ def mute_monitor(module):
|
|||
monitor = _get_monitor(module)
|
||||
if not monitor:
|
||||
module.fail_json(msg=f"Monitor {module.params['name']} not found!")
|
||||
elif monitor['options']['silenced']:
|
||||
module.fail_json(msg="Monitor is already muted. Datadog does not allow to modify muted alerts, consider unmuting it first.")
|
||||
elif module.params['silenced'] is not None and len(set(monitor['options']['silenced']) ^ set(module.params['silenced'])) == 0:
|
||||
elif monitor["options"]["silenced"]:
|
||||
module.fail_json(
|
||||
msg="Monitor is already muted. Datadog does not allow to modify muted alerts, consider unmuting it first."
|
||||
)
|
||||
elif (
|
||||
module.params["silenced"] is not None
|
||||
and len(set(monitor["options"]["silenced"]) ^ set(module.params["silenced"])) == 0
|
||||
):
|
||||
module.exit_json(changed=False)
|
||||
try:
|
||||
if module.params['silenced'] is None or module.params['silenced'] == "":
|
||||
msg = api.Monitor.mute(id=monitor['id'])
|
||||
if module.params["silenced"] is None or module.params["silenced"] == "":
|
||||
msg = api.Monitor.mute(id=monitor["id"])
|
||||
else:
|
||||
msg = api.Monitor.mute(id=monitor['id'], silenced=module.params['silenced'])
|
||||
msg = api.Monitor.mute(id=monitor["id"], silenced=module.params["silenced"])
|
||||
module.exit_json(changed=True, msg=msg)
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||
|
|
@ -449,14 +479,14 @@ def unmute_monitor(module):
|
|||
monitor = _get_monitor(module)
|
||||
if not monitor:
|
||||
module.fail_json(msg=f"Monitor {module.params['name']} not found!")
|
||||
elif not monitor['options']['silenced']:
|
||||
elif not monitor["options"]["silenced"]:
|
||||
module.exit_json(changed=False)
|
||||
try:
|
||||
msg = api.Monitor.unmute(monitor['id'])
|
||||
msg = api.Monitor.unmute(monitor["id"])
|
||||
module.exit_json(changed=True, msg=msg)
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue