Updated to use fetch_url

This commit is contained in:
Ben Whaley 2014-04-23 15:26:13 -07:00
parent 6c10297b2d
commit 1c996085ff

View file

@ -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
data = json.dumps(params)
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.