summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2015-07-28 10:55:30 -0700
committerToshio Kuratomi <a.badger@gmail.com>2015-07-28 10:55:30 -0700
commit5a2869bc10062075186f6cafba93771326e7f7e6 (patch)
tree92eb3934081cd5b03d1418c8ec8653a37691a0c2
parent4a43f52437b5568837925c6cbbc43927365e7d3e (diff)
parentb8df0d32a2e2c8f2b2069c6170cc1a0f9a923d3e (diff)
downloadansible-modules-extras-5a2869bc10062075186f6cafba93771326e7f7e6.tar.gz
Merge pull request #736 from ansible/fetch_url-sendgrid
Port sendgrid to fetch_url
-rw-r--r--notification/sendgrid.py27
1 files changed, 13 insertions, 14 deletions
diff --git a/notification/sendgrid.py b/notification/sendgrid.py
index e1ae7b77..2655b424 100644
--- a/notification/sendgrid.py
+++ b/notification/sendgrid.py
@@ -85,9 +85,6 @@ EXAMPLES = '''
# sendgrid module support methods
#
import urllib
-import urllib2
-
-import base64
def post_sendgrid_api(module, username, password, from_address, to_addresses,
subject, body):
@@ -102,11 +99,11 @@ def post_sendgrid_api(module, username, password, from_address, to_addresses,
recipient = recipient.encode('utf-8')
to_addresses_api += '&to[]=%s' % recipient
encoded_data += to_addresses_api
- request = urllib2.Request(SENDGRID_URI)
- request.add_header('User-Agent', AGENT)
- request.add_header('Content-type', 'application/x-www-form-urlencoded')
- request.add_header('Accept', 'application/json')
- return urllib2.urlopen(request, encoded_data)
+
+ headers = { 'User-Agent': AGENT,
+ 'Content-type': 'application/x-www-form-urlencoded',
+ 'Accept': 'application/json'}
+ return fetch_url(module, SENDGRID_URI, data=encoded_data, headers=headers, method='POST')
# =======================================
@@ -133,14 +130,16 @@ def main():
subject = module.params['subject']
body = module.params['body']
- try:
- response = post_sendgrid_api(module, username, password,
- from_address, to_addresses, subject, body)
- except Exception:
- module.fail_json(msg="unable to send email through SendGrid API")
+ response, info = post_sendgrid_api(module, username, password,
+ from_address, to_addresses, subject, body)
+ if info['status'] != 200:
+ module.fail_json(msg="unable to send email through SendGrid API: %s" % info['msg'])
+
module.exit_json(msg=subject, changed=False)
# import module snippets
from ansible.module_utils.basic import *
-main()
+from ansible.module_utils.urls import *
+if __name__ == '__main__':
+ main()