From fb4cf9410b279313daa90c3d24d6102add338060 Mon Sep 17 00:00:00 2001 From: Ben Whaley Date: Wed, 23 Apr 2014 15:26:13 -0700 Subject: [PATCH] Updated to use fetch_url --- notification/stackdriver | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/notification/stackdriver b/notification/stackdriver index 331b8c5eb54..5ac889a212f 100644 --- a/notification/stackdriver +++ b/notification/stackdriver @@ -70,9 +70,9 @@ options: ''' EXAMPLES = ''' -- stackdriver: key=AAAAAA event=deploy deployed_to=production deployed_by=leroyjenkins repository=MyWebApp revision_id=abcd123 +- stackdriver: key=AAAAAA event=deploy deployed_to=production deployed_by=leeroyjenkins repository=MyWebApp revision_id=abcd123 -- stackdriver: key=AAAAAA event=annotation msg="Greetings from Ansible" annotated_by=leroyjenkins level=WARN instance_id=i-abcd1234 +- stackdriver: key=AAAAAA event=annotation msg="Greetings from Ansible" annotated_by=leeroyjenkins level=WARN instance_id=i-abcd1234 ''' # =========================================== @@ -96,12 +96,7 @@ def send_deploy_event(module, key, revision_id, deployed_by='Ansible', deployed_ if repository: params['repository'] = repository - data = json.dumps(params) - - req = urllib2.Request(deploy_api, data) - req.add_header('content-type', 'application/json') - req.add_header('x-stackdriver-apikey', key) - return urllib2.urlopen(req) + return do_send_request(module, deploy_api, params, key) def send_annotation_event(module, key, msg, annotated_by='Ansible', level=None, instance_id=None, event_epoch=None): """Send an annotation event to Stackdriver""" @@ -118,12 +113,18 @@ def send_annotation_event(module, key, msg, annotated_by='Ansible', level=None, if event_epoch: params['event_epoch'] = event_epoch + return do_send_request(module, annotation_api, params, key) + +def do_send_request(module, url, params, key): data = json.dumps(params) + headers = { + 'Content-Type': 'application/json', + 'x-stackdriver-apikey': key + } + response, info = fetch_url(module, url, headers=headers, data=data, method='POST') + if info['status'] != 200: + module.fail_json(msg="Unable to send msg: %s" % info['msg']) - req = urllib2.Request(annotation_api, data) - req.add_header('content-type', 'application/json') - req.add_header('x-stackdriver-apikey', key) - return urllib2.urlopen(req) # =========================================== # Module execution.