2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-08-05 13:17:19 +02:00
|
|
|
# Copyright Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: hipchat
|
|
|
|
short_description: Send a message to Hipchat.
|
|
|
|
description:
|
|
|
|
- Send a message to a Hipchat room, with options to control the formatting.
|
|
|
|
options:
|
|
|
|
token:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- API token.
|
|
|
|
required: true
|
|
|
|
room:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- ID or name of the room.
|
|
|
|
required: true
|
2020-11-05 06:50:47 +01:00
|
|
|
msg_from:
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Name the message will appear to be sent from. Max length is 15
|
|
|
|
characters - above this it will be truncated.
|
|
|
|
default: Ansible
|
2020-11-05 06:50:47 +01:00
|
|
|
aliases: [from]
|
2020-03-09 10:11:07 +01:00
|
|
|
msg:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The message body.
|
|
|
|
required: true
|
|
|
|
color:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Background color for the message.
|
|
|
|
default: yellow
|
|
|
|
choices: [ "yellow", "red", "green", "purple", "gray", "random" ]
|
|
|
|
msg_format:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Message format.
|
|
|
|
default: text
|
|
|
|
choices: [ "text", "html" ]
|
|
|
|
notify:
|
|
|
|
description:
|
|
|
|
- If true, a notification will be triggered for users in the room.
|
|
|
|
type: bool
|
|
|
|
default: 'yes'
|
|
|
|
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.
|
|
|
|
type: bool
|
|
|
|
default: 'yes'
|
|
|
|
api:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- API url if using a self-hosted hipchat server. For Hipchat API version
|
|
|
|
2 use the default URI with C(/v2) instead of C(/v1).
|
|
|
|
default: 'https://api.hipchat.com/v1'
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Shirou Wakayama (@shirou)
|
|
|
|
- Paul Bourdel (@pb8226)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Send a message to a Hipchat room
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.hipchat:
|
2020-03-09 10:11:07 +01:00
|
|
|
room: notif
|
|
|
|
msg: Ansible task finished
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Send a message to a Hipchat room using Hipchat API version 2
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.hipchat:
|
2020-03-09 10:11:07 +01:00
|
|
|
api: https://api.hipchat.com/v2/
|
|
|
|
token: OAUTH2_TOKEN
|
|
|
|
room: notify
|
|
|
|
msg: Ansible task finished
|
|
|
|
'''
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# HipChat module specific support methods.
|
|
|
|
#
|
|
|
|
|
|
|
|
import json
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
|
|
|
from ansible.module_utils.six.moves.urllib.request import pathname2url
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
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'''
|
|
|
|
|
|
|
|
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
|
|
|
|
params['notify'] = int(notify)
|
|
|
|
|
|
|
|
url = api + MSG_URI_V1 + "?auth_token=%s" % (token)
|
|
|
|
data = 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']))
|
|
|
|
|
|
|
|
|
|
|
|
def send_msg_v2(module, token, room, msg_from, msg, msg_format='text',
|
|
|
|
color='yellow', notify=False, api=NOTIFY_URI_V2):
|
|
|
|
'''sending message to hipchat v2 server'''
|
|
|
|
|
|
|
|
headers = {'Authorization': 'Bearer %s' % token, 'Content-Type': 'application/json'}
|
|
|
|
|
|
|
|
body = dict()
|
|
|
|
body['message'] = msg
|
|
|
|
body['color'] = color
|
|
|
|
body['message_format'] = msg_format
|
|
|
|
body['notify'] = notify
|
|
|
|
|
|
|
|
POST_URL = api + NOTIFY_URI_V2
|
|
|
|
|
|
|
|
url = POST_URL.replace('{id_or_name}', pathname2url(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')
|
|
|
|
|
|
|
|
# https://www.hipchat.com/docs/apiv2/method/send_room_notification shows
|
|
|
|
# 204 to be the expected result code.
|
|
|
|
if info['status'] in [200, 204]:
|
|
|
|
return response.read()
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
token=dict(required=True, no_log=True),
|
|
|
|
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'),
|
2020-06-22 15:56:35 +02:00
|
|
|
validate_certs=dict(default=True, type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
api=dict(default=DEFAULT_URI),
|
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
token = module.params["token"]
|
|
|
|
room = str(module.params["room"])
|
|
|
|
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:
|
|
|
|
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 as e:
|
|
|
|
module.fail_json(msg="unable to send msg: %s" % to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|