Merge pull request #474 from pb8226/feature/Upgrade-To-HipChat-V2-API

Changing from v1 to v2 hipchat api format.
This commit is contained in:
Brian Coca 2015-05-15 18:14:18 -04:00
commit d590de8c4e

View file

@ -58,13 +58,13 @@ options:
description:
- API url if using a self-hosted hipchat server
required: false
default: 'https://api.hipchat.com/v1/rooms/message'
default: 'https://api.hipchat.com/v1'
version_added: 1.6.0
# informational: requirements for nodes
requirements: [ urllib, urllib2 ]
author: "WAKAYAMA Shirou (@shirou)"
author: "WAKAYAMA Shirou (@shirou), BOURDEL Paul (@pb8226)"
'''
EXAMPLES = '''
@ -75,11 +75,17 @@ EXAMPLES = '''
# HipChat module specific support methods.
#
MSG_URI = "https://api.hipchat.com/v1/rooms/message"
DEFAULT_URI = "https://api.hipchat.com/v1"
def send_msg(module, token, room, msg_from, msg, msg_format='text',
color='yellow', notify=False, api=MSG_URI):
'''sending message to hipchat'''
MSG_URI_V1 = "/rooms/message"
MSG_URI_V2 = "/room/{id_or_name}/message"
NOTIFY_URI_V2 = "/room/{id_or_name}/notification"
def send_msg_v1(module, token, room, msg_from, msg, msg_format='text',
color='yellow', notify=False, api=MSG_URI_V1):
'''sending message to hipchat v1 server'''
print "Sending message to v1 server"
params = {}
params['room_id'] = room
@ -94,7 +100,7 @@ def send_msg(module, token, room, msg_from, msg, msg_format='text',
else:
params['notify'] = 0
url = api + "?auth_token=%s" % (token)
url = api + MSG_URI_V1 + "?auth_token=%s" % (token)
data = urllib.urlencode(params)
if module.check_mode:
@ -108,6 +114,37 @@ def send_msg(module, token, room, msg_from, msg, msg_format='text',
module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))
def send_msg_v2(module, token, room, msg_from, msg, msg_format='text',
color='yellow', notify=False, api=MSG_URI_V2):
'''sending message to hipchat v2 server'''
print "Sending message to v2 server"
headers = {'Authorization':'Bearer %s' % token, 'Content-Type':'application/json'}
body = dict()
body['message'] = msg
body['color'] = color
body['message_format'] = msg_format
if notify:
POST_URL = api + NOTIFY_URI_V2
else:
POST_URL = api + MSG_URI_V2
url = POST_URL.replace('{id_or_name}',room)
data = json.dumps(body)
if module.check_mode:
# In check mode, exit before actually sending the message
module.exit_json(changed=False)
response, info = fetch_url(module, url, data=data, headers=headers, method='POST')
if info['status'] == 200:
return response.read()
else:
module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))
# ===========================================
# Module execution.
#
@ -125,7 +162,7 @@ def main():
msg_format=dict(default="text", choices=["text", "html"]),
notify=dict(default=True, type='bool'),
validate_certs=dict(default='yes', type='bool'),
api=dict(default=MSG_URI),
api=dict(default=DEFAULT_URI),
),
supports_check_mode=True
)
@ -140,7 +177,10 @@ def main():
api = module.params["api"]
try:
send_msg(module, token, room, msg_from, msg, msg_format, color, notify, api)
if api.find('/v2') != -1:
send_msg_v2(module, token, room, msg_from, msg, msg_format, color, notify, api)
else:
send_msg_v1(module, token, room, msg_from, msg, msg_format, color, notify, api)
except Exception, e:
module.fail_json(msg="unable to send msg: %s" % e)