From 320ae068edf8e8b91fd904d2f23f387305e0f050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=A4mer?= Date: Tue, 6 Dec 2016 09:31:52 +1100 Subject: [PATCH] Allow Datadog monitors to be retrieved by id instead of name. (#3456) --- .../extras/monitoring/datadog_monitor.py | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/extras/monitoring/datadog_monitor.py b/lib/ansible/modules/extras/monitoring/datadog_monitor.py index 8fe9ded9be..d10dd70cff 100644 --- a/lib/ansible/modules/extras/monitoring/datadog_monitor.py +++ b/lib/ansible/modules/extras/monitoring/datadog_monitor.py @@ -105,6 +105,11 @@ options: required: false default: null version_added: "2.3" + id: + description: ["The id of the alert. If set, will be used instead of the name to locate the alert."] + required: false + default: null + version_added: "2.3" ''' EXAMPLES = ''' @@ -172,7 +177,8 @@ def main(): thresholds=dict(required=False, type='dict', default=None), tags=dict(required=False, type='list', default=None), locked=dict(required=False, default=False, type='bool'), - require_full_window=dict(required=False, default=None, type='bool') + require_full_window=dict(required=False, default=None, type='bool'), + id=dict(required=False) ) ) @@ -203,9 +209,16 @@ def _fix_template_vars(message): def _get_monitor(module): - for monitor in api.Monitor.get_all(): - if monitor['name'] == module.params['name']: - return monitor + if module.params['id'] is not None: + monitor = api.Monitor.get(module.params['id']) + if 'errors' in monitor: + module.fail_json(msg="Failed to retrieve monitor with id %s, errors are %s" % (module.params['id'], str(monitor['errors']))) + return monitor + else: + monitors = api.Monitor.get_all() + for monitor in monitors: + if monitor['name'] == module.params['name']: + return monitor return {}