Check if api_key and app_key before proceeding (#24336)

Fix adds check if app_key and api_key provided by
user is correct or not. If this combination is wrong
then fail with appropriate error message given by
Datadog server

Fixes https://github.com/ansible/ansible/issues/24325

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-05-09 17:38:25 +05:30 committed by John R Barker
parent d999d613cb
commit e342b281d8
2 changed files with 13 additions and 4 deletions

View file

@ -210,6 +210,14 @@ def main():
initialize(**options) initialize(**options)
# Check if api_key and app_key is correct or not
# if not, then fail here.
response = api.Monitor.get_all()
if isinstance(response, dict):
msg = response.get('errors', None)
if msg:
module.fail_json(msg="Failed to connect Datadog server using given app_key and api_key : {0}".format(msg[0]))
if module.params['state'] == 'present': if module.params['state'] == 'present':
install_monitor(module) install_monitor(module)
elif module.params['state'] == 'absent': elif module.params['state'] == 'absent':
@ -219,6 +227,7 @@ def main():
elif module.params['state'] == 'unmute': elif module.params['state'] == 'unmute':
unmute_monitor(module) unmute_monitor(module)
def _fix_template_vars(message): def _fix_template_vars(message):
if message: if message:
return message.replace('[[', '{{').replace(']]', '}}') return message.replace('[[', '{{').replace(']]', '}}')
@ -255,11 +264,13 @@ def _post_monitor(module, options):
e = get_exception() e = get_exception()
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
def _equal_dicts(a, b, ignore_keys): def _equal_dicts(a, b, ignore_keys):
ka = set(a).difference(ignore_keys) ka = set(a).difference(ignore_keys)
kb = set(b).difference(ignore_keys) kb = set(b).difference(ignore_keys)
return ka == kb and all(a[k] == b[k] for k in ka) return ka == kb and all(a[k] == b[k] for k in ka)
def _update_monitor(module, monitor, options): def _update_monitor(module, monitor, options):
try: try:
kwargs = dict(id=monitor['id'], query=module.params['query'], kwargs = dict(id=monitor['id'], query=module.params['query'],
@ -290,7 +301,7 @@ def install_monitor(module):
"escalation_message": module.params['escalation_message'], "escalation_message": module.params['escalation_message'],
"notify_audit": module.boolean(module.params['notify_audit']), "notify_audit": module.boolean(module.params['notify_audit']),
"locked": module.boolean(module.params['locked']), "locked": module.boolean(module.params['locked']),
"require_full_window" : module.params['require_full_window'] "require_full_window": module.params['require_full_window']
} }
if module.params['type'] == "service check": if module.params['type'] == "service check":
@ -323,8 +334,7 @@ def mute_monitor(module):
module.fail_json(msg="Monitor %s not found!" % module.params['name']) module.fail_json(msg="Monitor %s not found!" % module.params['name'])
elif monitor['options']['silenced']: elif monitor['options']['silenced']:
module.fail_json(msg="Monitor is already muted. Datadog does not allow to modify muted alerts, consider unmuting it first.") 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 elif (module.params['silenced'] is not None and len(set(monitor['options']['silenced']) - set(module.params['silenced'])) == 0):
and len(set(monitor['options']['silenced']) - set(module.params['silenced'])) == 0):
module.exit_json(changed=False) module.exit_json(changed=False)
try: try:
if module.params['silenced'] is None or module.params['silenced'] == "": if module.params['silenced'] is None or module.params['silenced'] == "":

View file

@ -422,7 +422,6 @@ lib/ansible/modules/monitoring/bigpanda.py
lib/ansible/modules/monitoring/boundary_meter.py lib/ansible/modules/monitoring/boundary_meter.py
lib/ansible/modules/monitoring/circonus_annotation.py lib/ansible/modules/monitoring/circonus_annotation.py
lib/ansible/modules/monitoring/datadog_event.py lib/ansible/modules/monitoring/datadog_event.py
lib/ansible/modules/monitoring/datadog_monitor.py
lib/ansible/modules/monitoring/honeybadger_deployment.py lib/ansible/modules/monitoring/honeybadger_deployment.py
lib/ansible/modules/monitoring/icinga2_feature.py lib/ansible/modules/monitoring/icinga2_feature.py
lib/ansible/modules/monitoring/librato_annotation.py lib/ansible/modules/monitoring/librato_annotation.py