diff options
author | Paul Bourdel <paul.bourdel@slalom.com> | 2015-05-15 15:54:07 -0500 |
---|---|---|
committer | Paul Bourdel <paul.bourdel@slalom.com> | 2015-05-15 15:54:07 -0500 |
commit | 98e3ee36b7c85763a00c5ea5645672fc6ad9dd03 (patch) | |
tree | ce3debc50c17400c38ba9e4464f3db9846e1122f /notification | |
parent | 0a1784736ccb7676141b921a11543ed736a02068 (diff) | |
download | ansible-modules-extras-98e3ee36b7c85763a00c5ea5645672fc6ad9dd03.tar.gz |
adding new line to end of file
Diffstat (limited to 'notification')
-rw-r--r-- | notification/hipchat.py | 197 |
1 files changed, 196 insertions, 1 deletions
diff --git a/notification/hipchat.py b/notification/hipchat.py index 57db6be0..a7bf1fdc 100644 --- a/notification/hipchat.py +++ b/notification/hipchat.py @@ -1 +1,196 @@ -#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: hipchat
version_added: "1.2"
short_description: Send a message to hipchat
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:
- API url if using a self-hosted hipchat server
required: false
default: 'https://api.hipchat.com/v1'
version_added: 1.6.0
# informational: requirements for nodes
requirements: [ urllib, urllib2 ]
author: "WAKAYAMA Shirou (@shirou), BOURDEL Paul"
'''
EXAMPLES = '''
- hipchat: token=AAAAAA room=notify msg="Ansible task finished"
'''
# ===========================================
# HipChat module specific support methods.
#
DEFAULT_URI = "https://api.hipchat.com/v1"
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
params['from'] = msg_from[:15] # max length is 15
params['message'] = msg
params['message_format'] = msg_format
params['color'] = color
params['api'] = api
if notify:
params['notify'] = 1
else:
params['notify'] = 0
url = api + MSG_URI_V1 + "?auth_token=%s" % (token)
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']))
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.
#
def main():
module = AnsibleModule(
argument_spec=dict(
token=dict(required=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'),
validate_certs=dict(default='yes', type='bool'),
api=dict(default=DEFAULT_URI),
),
supports_check_mode=True
)
token = module.params["token"]
room = 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, e:
module.fail_json(msg="unable to sent msg: %s" % e)
changed = True
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
main()
\ No newline at end of file +#!/usr/bin/python +# -*- coding: utf-8 -*- + +DOCUMENTATION = ''' +--- +module: hipchat +version_added: "1.2" +short_description: Send a message to hipchat +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: + - API url if using a self-hosted hipchat server + required: false + default: 'https://api.hipchat.com/v1' + version_added: 1.6.0 + + +# informational: requirements for nodes +requirements: [ urllib, urllib2 ] +author: "WAKAYAMA Shirou (@shirou), BOURDEL Paul" +''' + +EXAMPLES = ''' +- hipchat: token=AAAAAA room=notify msg="Ansible task finished" +''' + +# =========================================== +# HipChat module specific support methods. +# + +DEFAULT_URI = "https://api.hipchat.com/v1" + +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 + params['from'] = msg_from[:15] # max length is 15 + params['message'] = msg + params['message_format'] = msg_format + params['color'] = color + params['api'] = api + + if notify: + params['notify'] = 1 + else: + params['notify'] = 0 + + url = api + MSG_URI_V1 + "?auth_token=%s" % (token) + 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'])) + + + +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. +# + +def main(): + + module = AnsibleModule( + argument_spec=dict( + token=dict(required=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'), + validate_certs=dict(default='yes', type='bool'), + api=dict(default=DEFAULT_URI), + ), + supports_check_mode=True + ) + + token = module.params["token"] + room = 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, e: + module.fail_json(msg="unable to sent msg: %s" % e) + + changed = True + module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg) + +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.urls import * + +main() + |