mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Switch from httplib to fetch_url
This commit is contained in:
parent
eaf6d6fb4d
commit
7434c48d9d
1 changed files with 28 additions and 19 deletions
|
@ -57,24 +57,36 @@ EXAMPLES = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import urllib
|
import urllib
|
||||||
import httplib
|
|
||||||
|
|
||||||
|
|
||||||
class pushover(object):
|
class Pushover(object):
|
||||||
''' Instantiates a pushover object, use it to send notifications '''
|
''' Instantiates a pushover object, use it to send notifications '''
|
||||||
|
base_uri = 'https://api.pushover.net'
|
||||||
|
port = 443
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, module, user, token):
|
||||||
self.host, self.port = 'api.pushover.net', 443
|
self.module = module
|
||||||
|
self.user = user
|
||||||
|
self.token = token
|
||||||
|
|
||||||
def run(self):
|
def run(self, priority, msg):
|
||||||
''' Do, whatever it is, we do. '''
|
''' Do, whatever it is, we do. '''
|
||||||
|
|
||||||
|
url = '%s:%s/1/messages.json' % (self.base_uri, self.port)
|
||||||
|
|
||||||
# parse config
|
# parse config
|
||||||
conn = httplib.HTTPSConnection(self.host, self.port)
|
options = dict(user=self.user,
|
||||||
conn.request("POST", "/1/messages.json",
|
token=self.token,
|
||||||
urllib.urlencode(self.options),
|
priority=priority,
|
||||||
{"Content-type": "application/x-www-form-urlencoded"})
|
message=msg)
|
||||||
conn.getresponse()
|
data = urllib.urlencode(options)
|
||||||
return
|
|
||||||
|
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():
|
def main():
|
||||||
|
@ -88,14 +100,9 @@ def main():
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
msg_object = pushover()
|
msg_object = Pushover(module, module.params['user_key'], module.params['app_token'])
|
||||||
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']
|
|
||||||
try:
|
try:
|
||||||
msg_object.run()
|
msg_object.run(module.params['pri'], module.params['msg'])
|
||||||
except:
|
except:
|
||||||
module.fail_json(msg='Unable to send msg via pushover')
|
module.fail_json(msg='Unable to send msg via pushover')
|
||||||
|
|
||||||
|
@ -103,4 +110,6 @@ def main():
|
||||||
|
|
||||||
# import module snippets
|
# import module snippets
|
||||||
from ansible.module_utils.basic import *
|
from ansible.module_utils.basic import *
|
||||||
main()
|
from ansible.module_utils.urls import *
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue