2015-04-25 13:56:35 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2015-07-30 20:48:59 +02:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-06 11:35:25 +01:00
|
|
|
ANSIBLE_METADATA = {'status': ['stableinterface'],
|
|
|
|
'supported_by': 'community',
|
|
|
|
'version': '1.0'}
|
|
|
|
|
2015-04-25 13:56:35 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: hipchat
|
|
|
|
version_added: "1.2"
|
2015-07-02 13:17:56 +02:00
|
|
|
short_description: Send a message to hipchat.
|
2015-04-25 13:56:35 +02:00
|
|
|
description:
|
|
|
|
- Send a message to hipchat
|
|
|
|
options:
|
|
|
|
token:
|
|
|
|
description:
|
|
|
|
- API token.
|
|
|
|
required: true
|
|
|
|
room:
|
|
|
|
description:
|
|
|
|
- ID or name of the room.
|
|
|
|
required: true
|
|
|
|
from:
|
|
|
|
description:
|
|
|
|
- Name the message will appear be sent from. max 15 characters.
|
|
|
|
Over 15, will be shorten.
|
|
|
|
required: false
|
|
|
|
default: Ansible
|
|
|
|
msg:
|
|
|
|
description:
|
|
|
|
- The message body.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
color:
|
|
|
|
description:
|
|
|
|
- Background color for the message. Default is yellow.
|
|
|
|
required: false
|
|
|
|
default: yellow
|
|
|
|
choices: [ "yellow", "red", "green", "purple", "gray", "random" ]
|
|
|
|
msg_format:
|
|
|
|
description:
|
|
|
|
- message format. html or text. Default is text.
|
|
|
|
required: false
|
|
|
|
default: text
|
|
|
|
choices: [ "text", "html" ]
|
|
|
|
notify:
|
|
|
|
description:
|
|
|
|
- notify or not (change the tab color, play a sound, etc)
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
version_added: 1.5.1
|
|
|
|
api:
|
|
|
|
description:
|
2015-07-02 13:17:56 +02:00
|
|
|
- API url if using a self-hosted hipchat server. For hipchat api version 2 use C(/v2) path in URI
|
2015-04-25 13:56:35 +02:00
|
|
|
required: false
|
2016-12-08 06:34:16 +01:00
|
|
|
default: 'https://api.hipchat.com/v1'
|
2015-04-25 13:56:35 +02:00
|
|
|
version_added: 1.6.0
|
|
|
|
|
|
|
|
|
2015-06-19 17:59:19 +02:00
|
|
|
requirements: [ ]
|
2015-05-15 22:54:53 +02:00
|
|
|
author: "WAKAYAMA Shirou (@shirou), BOURDEL Paul (@pb8226)"
|
2015-04-25 13:56:35 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2016-12-01 13:31:20 +01:00
|
|
|
- hipchat:
|
|
|
|
room: notif
|
|
|
|
msg: Ansible task finished
|
2015-07-02 13:17:56 +02:00
|
|
|
|
|
|
|
# Use Hipchat API version 2
|
2015-07-27 06:45:03 +02:00
|
|
|
- hipchat:
|
2016-12-10 15:28:15 +01:00
|
|
|
api: https://api.hipchat.com/v2/
|
2015-07-02 13:17:56 +02:00
|
|
|
token: OAUTH2_TOKEN
|
2015-07-27 06:45:03 +02:00
|
|
|
room: notify
|
2016-12-01 13:31:20 +01:00
|
|
|
msg: Ansible task finished
|
2015-04-25 13:56:35 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# HipChat module specific support methods.
|
|
|
|
#
|
|
|
|
|
2015-06-19 17:59:19 +02:00
|
|
|
import urllib
|
2016-08-24 17:40:31 +02:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
2015-04-25 13:56:35 +02:00
|
|
|
|
2016-08-24 17:40:31 +02:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.pycompat24 import get_exception
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
DEFAULT_URI = "https://api.hipchat.com/v1"
|
|
|
|
|
|
|
|
MSG_URI_V1 = "/rooms/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'''
|
2015-04-25 13:56:35 +02:00
|
|
|
|
|
|
|
params = {}
|
|
|
|
params['room_id'] = room
|
|
|
|
params['from'] = msg_from[:15] # max length is 15
|
|
|
|
params['message'] = msg
|
|
|
|
params['message_format'] = msg_format
|
|
|
|
params['color'] = color
|
|
|
|
params['api'] = api
|
2015-07-27 06:45:03 +02:00
|
|
|
params['notify'] = int(notify)
|
2015-04-25 13:56:35 +02:00
|
|
|
|
2015-07-27 06:45:03 +02:00
|
|
|
url = api + MSG_URI_V1 + "?auth_token=%s" % (token)
|
2015-04-25 13:56:35 +02:00
|
|
|
data = urllib.urlencode(params)
|
|
|
|
|
|
|
|
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)
|
|
|
|
if info['status'] == 200:
|
|
|
|
return response.read()
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))
|
|
|
|
|
|
|
|
|
2015-05-15 22:57:10 +02:00
|
|
|
def send_msg_v2(module, token, room, msg_from, msg, msg_format='text',
|
2016-08-24 17:40:31 +02:00
|
|
|
color='yellow', notify=False, api=NOTIFY_URI_V2):
|
2015-05-15 22:57:10 +02:00
|
|
|
'''sending message to hipchat v2 server'''
|
|
|
|
|
2016-08-24 17:40:31 +02:00
|
|
|
headers = {'Authorization': 'Bearer %s' % token, 'Content-Type': 'application/json'}
|
2015-05-15 22:57:10 +02:00
|
|
|
|
|
|
|
body = dict()
|
|
|
|
body['message'] = msg
|
|
|
|
body['color'] = color
|
|
|
|
body['message_format'] = msg_format
|
2015-07-27 06:45:03 +02:00
|
|
|
body['notify'] = notify
|
2015-05-15 22:57:10 +02:00
|
|
|
|
2015-07-02 13:17:56 +02:00
|
|
|
POST_URL = api + NOTIFY_URI_V2
|
2015-07-27 06:45:03 +02:00
|
|
|
|
2016-04-06 23:07:03 +02:00
|
|
|
url = POST_URL.replace('{id_or_name}', urllib.pathname2url(room))
|
2015-05-15 22:57:10 +02:00
|
|
|
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')
|
2016-05-04 07:27:17 +02:00
|
|
|
|
|
|
|
# https://www.hipchat.com/docs/apiv2/method/send_room_notification shows
|
|
|
|
# 204 to be the expected result code.
|
|
|
|
if info['status'] in [200, 204]:
|
2015-05-15 22:57:10 +02:00
|
|
|
return response.read()
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))
|
|
|
|
|
|
|
|
|
2015-04-25 13:56:35 +02:00
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2016-04-12 16:27:18 +02:00
|
|
|
token=dict(required=True, no_log=True),
|
2015-04-25 13:56:35 +02:00
|
|
|
room=dict(required=True),
|
|
|
|
msg=dict(required=True),
|
|
|
|
msg_from=dict(default="Ansible", aliases=['from']),
|
|
|
|
color=dict(default="yellow", choices=["yellow", "red", "green",
|
|
|
|
"purple", "gray", "random"]),
|
|
|
|
msg_format=dict(default="text", choices=["text", "html"]),
|
|
|
|
notify=dict(default=True, type='bool'),
|
|
|
|
validate_certs=dict(default='yes', type='bool'),
|
2016-12-08 06:34:16 +01:00
|
|
|
api=dict(default=DEFAULT_URI),
|
2015-04-25 13:56:35 +02:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
token = module.params["token"]
|
2015-12-16 03:11:41 +01:00
|
|
|
room = str(module.params["room"])
|
2015-04-25 13:56:35 +02:00
|
|
|
msg = module.params["msg"]
|
|
|
|
msg_from = module.params["msg_from"]
|
|
|
|
color = module.params["color"]
|
|
|
|
msg_format = module.params["msg_format"]
|
|
|
|
notify = module.params["notify"]
|
|
|
|
api = module.params["api"]
|
|
|
|
|
|
|
|
try:
|
2016-08-24 17:40:31 +02:00
|
|
|
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 = get_exception()
|
2015-04-25 13:56:35 +02:00
|
|
|
module.fail_json(msg="unable to send msg: %s" % e)
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
|
|
|
|
|
2016-08-24 17:40:31 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|