2015-01-12 22:13:08 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2015, Billy Kimble <basslines@gmail.com>
2017-07-29 15:05:38 +00:00
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import , division , print_function
__metaclass__ = type
2015-01-12 22:13:08 +00:00
2016-12-06 10:35:25 +00:00
2017-08-16 03:16:38 +00:00
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
2017-03-14 16:07:22 +00:00
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' }
2016-12-06 10:35:25 +00:00
2015-01-12 22:13:08 +00:00
DOCUMENTATION = """
module : hall
short_description : Send notification to Hall
description :
2017-02-13 14:02:34 +00:00
- " The C(hall) module connects to the U(https://hall.com) messaging API and allows you to deliver notication messages to rooms. "
2016-12-08 05:34:16 +00:00
version_added : " 2.0 "
author : Billy Kimble ( @bkimble ) < basslines @gmail.com >
2015-01-12 22:13:08 +00:00
options :
room_token :
description :
2016-12-08 05:34:16 +00:00
- " Room token provided to you by setting up the Ansible room integation on U(https://hall.com) "
2015-01-12 22:13:08 +00:00
required : true
msg :
description :
2017-06-12 06:55:19 +00:00
- The message you wish to deliver as a notification
2015-01-12 22:13:08 +00:00
required : true
title :
description :
- The title of the message
required : true
picture :
description :
2017-03-23 01:50:28 +00:00
- >
The full URL to the image you wish to use for the Icon of the message . Defaults to
U ( http : / / cdn2 . hubspot . net / hub / 330046 / file - 769078210 - png / Official_Logos / ansible_logo_black_square_small . png ? t = 1421076128627 )
2015-01-12 22:13:08 +00:00
required : false
2016-12-08 14:38:05 +00:00
"""
2015-01-12 22:13:08 +00:00
EXAMPLES = """
2016-12-08 14:38:05 +00:00
- name : Send Hall notifiation
2016-12-11 22:16:42 +00:00
hall :
2015-01-12 22:13:08 +00:00
room_token : < hall room integration token >
title : Nginx
2016-12-11 22:16:42 +00:00
msg : ' Created virtual host file on {{ inventory_hostname }} '
delegate_to : loclahost
2015-01-12 22:13:08 +00:00
- name : Send Hall notification if EC2 servers were created .
2016-12-11 22:16:42 +00:00
hall :
2016-12-08 14:38:05 +00:00
room_token : < hall room integration token >
2015-01-12 22:13:08 +00:00
title : Server Creation
2017-03-23 01:50:28 +00:00
msg : ' Created instance {{ item.id }} of type {{ item.instance_type }}. \\ nInstance can be reached at {{ item.public_ip }} in the {{ item.region }} region. '
2016-12-11 22:16:42 +00:00
delegate_to : loclahost
when : ec2 . instances | length > 0
with_items : ' {{ ec2.instances }} '
2015-01-12 22:13:08 +00:00
"""
2017-07-29 15:05:38 +00:00
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . urls import fetch_url
2015-01-12 22:13:08 +00:00
2017-12-07 16:27:06 +00:00
HALL_API_ENDPOINT = ' https://hall.com/api/1/services/generic/ %s '
2015-01-12 22:13:08 +00:00
2017-07-29 15:05:38 +00:00
2015-01-12 22:13:08 +00:00
def send_request_to_hall ( module , room_token , payload ) :
2016-12-08 14:38:05 +00:00
headers = { ' Content-Type ' : ' application/json ' }
2017-12-07 16:27:06 +00:00
payload = module . jsonify ( payload )
2015-01-12 22:13:08 +00:00
api_endpoint = HALL_API_ENDPOINT % ( room_token )
response , info = fetch_url ( module , api_endpoint , data = payload , headers = headers )
if info [ ' status ' ] != 200 :
secure_url = HALL_API_ENDPOINT % ( ' [redacted] ' )
module . fail_json ( msg = " failed to send %s to %s : %s " % ( payload , secure_url , info [ ' msg ' ] ) )
2017-07-29 15:05:38 +00:00
2015-01-12 22:13:08 +00:00
def main ( ) :
module = AnsibleModule (
2017-03-23 01:50:28 +00:00
argument_spec = dict (
room_token = dict ( type = ' str ' , required = True ) ,
msg = dict ( type = ' str ' , required = True ) ,
title = dict ( type = ' str ' , required = True ) ,
picture = dict ( type = ' str ' ,
default = ' http://cdn2.hubspot.net/hub/330046/file-769078210-png/Official_Logos/ansible_logo_black_square_small.png?t=1421076128627 ' ) ,
2015-01-12 22:13:08 +00:00
)
)
2016-12-08 14:38:05 +00:00
2015-01-12 22:13:08 +00:00
room_token = module . params [ ' room_token ' ]
message = module . params [ ' msg ' ]
title = module . params [ ' title ' ]
picture = module . params [ ' picture ' ]
payload = { ' title ' : title , ' message ' : message , ' picture ' : picture }
send_request_to_hall ( module , room_token , payload )
module . exit_json ( msg = " OK " )
2016-12-05 16:21:26 +00:00
if __name__ == ' __main__ ' :
main ( )