Changing from v1 to v2 hipchat api format.

This commit is contained in:
Paul Bourdel 2015-05-10 19:39:26 -05:00 committed by Matt Clay
parent 9ccc869180
commit ca28acd07a

View file

@ -58,13 +58,13 @@ options:
description: description:
- API url if using a self-hosted hipchat server - API url if using a self-hosted hipchat server
required: false required: false
default: 'https://api.hipchat.com/v1/rooms/message' default: 'https://api.hipchat.com/v2/room/{id_or_name}/message'
version_added: 1.6.0 version_added: 1.6.0
# informational: requirements for nodes # informational: requirements for nodes
requirements: [ urllib, urllib2 ] requirements: [ urllib, urllib2, requests, json ]
author: WAKAYAMA Shirou author: WAKAYAMA Shirou, BOURDEL Paul
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -75,32 +75,27 @@ EXAMPLES = '''
# HipChat module specific support methods. # HipChat module specific support methods.
# #
MSG_URI = "https://api.hipchat.com/v1/rooms/message" MSG_URI = "https://api.hipchat.com/v2/room/{id_or_name}/message"
NOTIFY_URI = "https://api.hipchat.com/v2/room/{id_or_name}/notification"
def send_msg(module, token, room, msg_from, msg, msg_format='text', def send_msg(module, token, room, msg_from, msg, msg_format='text',
color='yellow', notify=False, api=MSG_URI): color='yellow', notify=False, api=MSG_URI):
'''sending message to hipchat''' '''sending message to hipchat'''
params = {}
params['room_id'] = room payload = {'message': msg, 'color': color}
params['from'] = msg_from[:15] # max length is 15 url_params = {'auth_token': token}
params['message'] = msg
params['message_format'] = msg_format
params['color'] = color
params['api'] = api
if notify: if notify:
params['notify'] = 1 POST_URL = NOTIFY_URI
else: else:
params['notify'] = 0 POST_URL = MSG_URI
url = api + "?auth_token=%s" % (token) response = requests.post(POST_URL.replace('{id_or_name}',room), json=payload, params=url_params)
data = urllib.urlencode(params)
response, info = fetch_url(module, url, data=data) if response.status_code == 201 or response.status_code == 204:
if info['status'] == 200: return response.json
return response.read()
else: else:
module.fail_json(msg="failed to send message, return status=%s" % str(info['status'])) module.fail_json(msg="failed to send message, return status=%s" % str(response.status_code))
# =========================================== # ===========================================
@ -137,7 +132,7 @@ def main():
try: try:
send_msg(module, token, room, msg_from, msg, msg_format, color, notify, api) send_msg(module, token, room, msg_from, msg, msg_format, color, notify, api)
except Exception, e: except Exception, e:
module.fail_json(msg="unable to send msg: %s" % e) module.fail_json(msg="unable to sent msg: %s" % e)
changed = True changed = True
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg) module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
@ -145,5 +140,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.urls import * from ansible.module_utils.urls import *
import requests, json
main() main()