Port campifre to fetch_url
This commit is contained in:
parent
588ff5f512
commit
bb0082a67d
1 changed files with 24 additions and 42 deletions
|
@ -42,7 +42,7 @@ options:
|
||||||
"vuvuzela", "what", "whoomp", "yeah", "yodel"]
|
"vuvuzela", "what", "whoomp", "yeah", "yodel"]
|
||||||
|
|
||||||
# informational: requirements for nodes
|
# informational: requirements for nodes
|
||||||
requirements: [ urllib2, cgi ]
|
requirements: [ ]
|
||||||
author: "Adam Garside (@fabulops)"
|
author: "Adam Garside (@fabulops)"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -53,19 +53,10 @@ EXAMPLES = '''
|
||||||
msg="Task completed ... with feeling."
|
msg="Task completed ... with feeling."
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import cgi
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
try:
|
|
||||||
import urllib2
|
|
||||||
except ImportError:
|
|
||||||
module.fail_json(msg="urllib2 is required")
|
|
||||||
|
|
||||||
try:
|
|
||||||
import cgi
|
|
||||||
except ImportError:
|
|
||||||
module.fail_json(msg="cgi is required")
|
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
subscription=dict(required=True),
|
subscription=dict(required=True),
|
||||||
|
@ -102,42 +93,33 @@ def main():
|
||||||
MSTR = "<message><body>%s</body></message>"
|
MSTR = "<message><body>%s</body></message>"
|
||||||
AGENT = "Ansible/1.2"
|
AGENT = "Ansible/1.2"
|
||||||
|
|
||||||
try:
|
# Hack to add basic auth username and password the way fetch_url expects
|
||||||
|
module.params['username'] = token
|
||||||
|
module.params['password'] = 'X'
|
||||||
|
|
||||||
# Setup basic auth using token as the username
|
target_url = '%s/room/%s/speak.xml' % (URI, room)
|
||||||
pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
|
headers = {'Content-Type': 'application/xml',
|
||||||
pm.add_password(None, URI, token, 'X')
|
'User-agent': AGENT}
|
||||||
|
|
||||||
# Setup Handler and define the opener for the request
|
# Send some audible notification if requested
|
||||||
handler = urllib2.HTTPBasicAuthHandler(pm)
|
if notify:
|
||||||
opener = urllib2.build_opener(handler)
|
response, info = fetch_url(module, target_url, data=NSTR % cgi.escape(notify), headers=headers)
|
||||||
|
if info['status'] != 200:
|
||||||
|
module.fail_json(msg="unable to send msg: '%s', campfire api"
|
||||||
|
" returned error code: '%s'" %
|
||||||
|
(notify, info['status']))
|
||||||
|
|
||||||
target_url = '%s/room/%s/speak.xml' % (URI, room)
|
# Send the message
|
||||||
|
response, info = fetch_url(module, target_url, data=MSTR %cgi.escape(msg), headers=headers)
|
||||||
# Send some audible notification if requested
|
if info['status'] != 200:
|
||||||
if notify:
|
module.fail_json(msg="unable to send msg: '%s', campfire api"
|
||||||
req = urllib2.Request(target_url, NSTR % cgi.escape(notify))
|
" returned error code: '%s'" %
|
||||||
req.add_header('Content-Type', 'application/xml')
|
(msg, info['status']))
|
||||||
req.add_header('User-agent', AGENT)
|
|
||||||
response = opener.open(req)
|
|
||||||
|
|
||||||
# Send the message
|
|
||||||
req = urllib2.Request(target_url, MSTR % cgi.escape(msg))
|
|
||||||
req.add_header('Content-Type', 'application/xml')
|
|
||||||
req.add_header('User-agent', AGENT)
|
|
||||||
response = opener.open(req)
|
|
||||||
|
|
||||||
except urllib2.HTTPError, e:
|
|
||||||
if not (200 <= e.code < 300):
|
|
||||||
module.fail_json(msg="unable to send msg: '%s', campfire api"
|
|
||||||
" returned error code: '%s'" %
|
|
||||||
(msg, e.code))
|
|
||||||
|
|
||||||
except Exception, e:
|
|
||||||
module.fail_json(msg="unable to send msg: %s" % msg)
|
|
||||||
|
|
||||||
module.exit_json(changed=True, room=room, msg=msg, notify=notify)
|
module.exit_json(changed=True, room=room, msg=msg, notify=notify)
|
||||||
|
|
||||||
# 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