From 44bcad7d8ae03b41e7c059b113b3ef66ec15dafa Mon Sep 17 00:00:00 2001 From: Naoya Nakazawa Date: Thu, 11 Aug 2016 14:35:52 +0900 Subject: [PATCH] ready_for_review datadog_event module: Datadog API http status code 202 is ok. Ref: http://docs.datadoghq.com/api/ (#2117) * Use official datadog create event API. * Fix exception --- .../extras/monitoring/datadog_event.py | 86 ++++++++++--------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/lib/ansible/modules/extras/monitoring/datadog_event.py b/lib/ansible/modules/extras/monitoring/datadog_event.py index de3b73c210..88d921bf91 100644 --- a/lib/ansible/modules/extras/monitoring/datadog_event.py +++ b/lib/ansible/modules/extras/monitoring/datadog_event.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- # # Author: Artūras 'arturaz' Šlajus +# Author: Naoya Nakazawa # # This module is proudly sponsored by iGeolise (www.igeolise.com) and # Tiny Lab Productions (www.tinylabproductions.com). @@ -21,6 +22,12 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . +# Import Datadog +try: + from datadog import initialize, api + HAS_DATADOG = True +except: + HAS_DATADOG = False DOCUMENTATION = ''' --- @@ -30,7 +37,9 @@ description: - "Allows to post events to DataDog (www.datadoghq.com) service." - "Uses http://docs.datadoghq.com/api/#events API." version_added: "1.3" -author: "Artūras `arturaz` Šlajus (@arturaz)" +author: +- "Artūras `arturaz` Šlajus (@arturaz)" +- "Naoya Nakazawa (@n0ts)" notes: [] requirements: [] options: @@ -38,6 +47,10 @@ options: description: ["Your DataDog API key."] required: true default: null + app_key: + description: ["Your DataDog app key."] + required: true + version_added: "2.2" title: description: ["The event title."] required: true @@ -83,17 +96,21 @@ options: EXAMPLES = ''' # Post an event with low priority datadog_event: title="Testing from ansible" text="Test!" priority="low" - api_key="6873258723457823548234234234" + api_key: "9775a026f1ca7d1c6c5af9d94d9595a4" + app_key: "j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN" # Post an event with several tags datadog_event: title="Testing from ansible" text="Test!" - api_key="6873258723457823548234234234" + api_key: "9775a026f1ca7d1c6c5af9d94d9595a4" + app_key: "j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN" tags=aa,bb,#host:{{ inventory_hostname }} ''' +# Import Datadog def main(): module = AnsibleModule( argument_spec=dict( api_key=dict(required=True, no_log=True), + app_key=dict(required=True, no_log=True), title=dict(required=True), text=dict(required=True), date_happened=dict(required=False, default=None, type='int'), @@ -106,51 +123,42 @@ def main(): choices=['error', 'warning', 'info', 'success'] ), aggregation_key=dict(required=False, default=None), - source_type_name=dict( - required=False, default='my apps', - choices=['nagios', 'hudson', 'jenkins', 'user', 'my apps', - 'feed', 'chef', 'puppet', 'git', 'bitbucket', 'fabric', - 'capistrano'] - ), validate_certs = dict(default='yes', type='bool'), ) ) - post_event(module) + # Prepare Datadog + if not HAS_DATADOG: + module.fail_json(msg='datadogpy required for this module') -def post_event(module): - uri = "https://app.datadoghq.com/api/v1/events?api_key=%s" % module.params['api_key'] + options = { + 'api_key': module.params['api_key'], + 'app_key': module.params['app_key'] + } - body = dict( - title=module.params['title'], - text=module.params['text'], - priority=module.params['priority'], - alert_type=module.params['alert_type'] - ) - if module.params['date_happened'] != None: - body['date_happened'] = module.params['date_happened'] - if module.params['tags'] != None: - body['tags'] = module.params['tags'] - if module.params['aggregation_key'] != None: - body['aggregation_key'] = module.params['aggregation_key'] - if module.params['source_type_name'] != None: - body['source_type_name'] = module.params['source_type_name'] + initialize(**options) - json_body = module.jsonify(body) - headers = {"Content-Type": "application/json"} + _post_event(module) + + +def _post_event(module): + try: + msg = api.Event.create(title=module.params['title'], + text=module.params['text'], + tags=module.params['tags'], + priority=module.params['priority'], + alert_type=module.params['alert_type'], + aggregation_key=module.params['aggregation_key'], + source_type_name='ansible') + if msg['status'] != 'ok': + module.fail_json(msg=msg) + + module.exit_json(changed=True, msg=msg) + except Exception: + e = get_exception() + module.fail_json(msg=str(e)) - (response, info) = fetch_url(module, uri, data=json_body, headers=headers) - if info['status'] == 202: - response_body = response.read() - response_json = module.from_json(response_body) - if response_json['status'] == 'ok': - module.exit_json(changed=True) - else: - module.fail_json(msg=response) - else: - module.fail_json(**info) -# import module snippets from ansible.module_utils.basic import * from ansible.module_utils.urls import * if __name__ == '__main__':