Port campifre to fetch_url

pull/18777/head
Toshio Kuratomi 9 years ago committed by Matt Clay
parent 588ff5f512
commit bb0082a67d

@ -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
# Setup basic auth using token as the username module.params['password'] = 'X'
pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
pm.add_password(None, URI, token, 'X') target_url = '%s/room/%s/speak.xml' % (URI, room)
headers = {'Content-Type': 'application/xml',
# Setup Handler and define the opener for the request 'User-agent': AGENT}
handler = urllib2.HTTPBasicAuthHandler(pm)
opener = urllib2.build_opener(handler) # Send some audible notification if requested
if notify:
target_url = '%s/room/%s/speak.xml' % (URI, room) response, info = fetch_url(module, target_url, data=NSTR % cgi.escape(notify), headers=headers)
if info['status'] != 200:
# Send some audible notification if requested module.fail_json(msg="unable to send msg: '%s', campfire api"
if notify: " returned error code: '%s'" %
req = urllib2.Request(target_url, NSTR % cgi.escape(notify)) (notify, info['status']))
req.add_header('Content-Type', 'application/xml')
req.add_header('User-agent', AGENT) # Send the message
response = opener.open(req) response, info = fetch_url(module, target_url, data=MSTR %cgi.escape(msg), headers=headers)
if info['status'] != 200:
# Send the message module.fail_json(msg="unable to send msg: '%s', campfire api"
req = urllib2.Request(target_url, MSTR % cgi.escape(msg)) " returned error code: '%s'" %
req.add_header('Content-Type', 'application/xml') (msg, info['status']))
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…
Cancel
Save