From 7434c48d9d749a60f7c33179080c3ce390b298f0 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Tue, 28 Jul 2015 12:23:54 -0700 Subject: [PATCH] Switch from httplib to fetch_url --- .../modules/extras/notification/pushover.py | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/ansible/modules/extras/notification/pushover.py b/lib/ansible/modules/extras/notification/pushover.py index 505917189e..0c1d6e94ab 100644 --- a/lib/ansible/modules/extras/notification/pushover.py +++ b/lib/ansible/modules/extras/notification/pushover.py @@ -57,24 +57,36 @@ EXAMPLES = ''' ''' import urllib -import httplib -class pushover(object): +class Pushover(object): ''' Instantiates a pushover object, use it to send notifications ''' + base_uri = 'https://api.pushover.net' + port = 443 - def __init__(self): - self.host, self.port = 'api.pushover.net', 443 + def __init__(self, module, user, token): + self.module = module + self.user = user + self.token = token - def run(self): + def run(self, priority, msg): ''' Do, whatever it is, we do. ''' + + url = '%s:%s/1/messages.json' % (self.base_uri, self.port) + # parse config - conn = httplib.HTTPSConnection(self.host, self.port) - conn.request("POST", "/1/messages.json", - urllib.urlencode(self.options), - {"Content-type": "application/x-www-form-urlencoded"}) - conn.getresponse() - return + options = dict(user=self.user, + token=self.token, + priority=priority, + message=msg) + data = urllib.urlencode(options) + + headers = { "Content-type": "application/x-www-form-urlencoded"} + r, info = fetch_url(self.module, url, method='POST', data=data, headers=headers) + if info['status'] != 200: + raise Exception(info) + + return r.read() def main(): @@ -88,14 +100,9 @@ def main(): ), ) - msg_object = pushover() - msg_object.options = {} - msg_object.options['user'] = module.params['user_key'] - msg_object.options['token'] = module.params['app_token'] - msg_object.options['priority'] = module.params['pri'] - msg_object.options['message'] = module.params['msg'] + msg_object = Pushover(module, module.params['user_key'], module.params['app_token']) try: - msg_object.run() + msg_object.run(module.params['pri'], module.params['msg']) except: module.fail_json(msg='Unable to send msg via pushover') @@ -103,4 +110,6 @@ def main(): # import module snippets from ansible.module_utils.basic import * -main() +from ansible.module_utils.urls import * +if __name__ == '__main__': + main()