summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <toshio@fedoraproject.org>2015-07-20 23:33:05 -0700
committerToshio Kuratomi <toshio@fedoraproject.org>2015-07-20 23:36:49 -0700
commitb8df0d32a2e2c8f2b2069c6170cc1a0f9a923d3e (patch)
tree2d9e1cad9cfbe51abd023b1240649689a7914f9a
parent79173ac18de66bcdb6c25729fb4561c772d37cab (diff)
downloadansible-modules-extras-b8df0d32a2e2c8f2b2069c6170cc1a0f9a923d3e.tar.gz
Port sendgrid to fetch_urlfetch_url-sendgrid
-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()