1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Get hipchat, sns, and typetalk notification modules compiling on py3 (#2782)

This commit is contained in:
Toshio Kuratomi 2016-08-24 08:40:31 -07:00 committed by Matt Clay
parent 4999bb28cd
commit 7a8b080506
4 changed files with 54 additions and 29 deletions

View file

@ -102,13 +102,10 @@ env:
network/nmcli.py network/nmcli.py
network/openvswitch_bridge.py network/openvswitch_bridge.py
network/openvswitch_port.py network/openvswitch_port.py
notification/hipchat.py
notification/irc.py notification/irc.py
notification/jabber.py notification/jabber.py
notification/mail.py notification/mail.py
notification/mqtt.py notification/mqtt.py"
notification/sns.py
notification/typetalk.py"
before_install: before_install:
- git config user.name "ansible" - git config user.name "ansible"
- git config user.email "ansible@ansible.com" - git config user.email "ansible@ansible.com"

View file

@ -98,10 +98,26 @@ EXAMPLES = '''
MSG_URI = "https://api.hipchat.com/v1/rooms/message" MSG_URI = "https://api.hipchat.com/v1/rooms/message"
import urllib import urllib
try:
import json
except ImportError:
import simplejson as json
def send_msg(module, token, room, msg_from, msg, msg_format='text', # import module snippets
color='yellow', notify=False, api=MSG_URI): from ansible.module_utils.basic import AnsibleModule
'''sending message to hipchat''' 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'''
params = {} params = {}
params['room_id'] = room params['room_id'] = room
@ -129,9 +145,8 @@ def send_msg(module, token, room, msg_from, msg, msg_format='text',
def send_msg_v2(module, token, room, msg_from, msg, msg_format='text', def send_msg_v2(module, token, room, msg_from, msg, msg_format='text',
color='yellow', notify=False, api=NOTIFY_URI_V2): color='yellow', notify=False, api=NOTIFY_URI_V2):
'''sending message to hipchat v2 server''' '''sending message to hipchat v2 server'''
print "Sending message to v2 server"
headers = {'Authorization':'Bearer %s' % token, 'Content-Type':'application/json'} headers = {'Authorization': 'Bearer %s' % token, 'Content-Type': 'application/json'}
body = dict() body = dict()
body['message'] = msg body['message'] = msg
@ -190,15 +205,16 @@ def main():
api = module.params["api"] api = module.params["api"]
try: try:
send_msg(module, token, room, msg_from, msg, msg_format, color, notify, api) if api.find('/v2') != -1:
except Exception, e: 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()
module.fail_json(msg="unable to send msg: %s" % e) module.fail_json(msg="unable to send msg: %s" % e)
changed = True changed = True
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg) module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
# import module snippets if __name__ == '__main__':
from ansible.module_utils.basic import * main()
from ansible.module_utils.urls import *
main()

View file

@ -77,7 +77,8 @@ options:
required: false required: false
aliases: ['aws_region', 'ec2_region'] aliases: ['aws_region', 'ec2_region']
requirements: [ "boto" ] requirements:
- "boto"
""" """
EXAMPLES = """ EXAMPLES = """
@ -97,10 +98,14 @@ EXAMPLES = """
topic: "deploy" topic: "deploy"
""" """
import sys try:
import json
except ImportError:
import simplejson as json
from ansible.module_utils.basic import * from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import ec2_argument_spec, connect_to_aws, get_aws_connection_info
from ansible.module_utils.pycompat24 import get_exception
try: try:
import boto import boto
@ -156,7 +161,8 @@ def main():
module.fail_json(msg="region must be specified") module.fail_json(msg="region must be specified")
try: try:
connection = connect_to_aws(boto.sns, region, **aws_connect_params) connection = connect_to_aws(boto.sns, region, **aws_connect_params)
except boto.exception.NoAuthHandlerFound, e: except boto.exception.NoAuthHandlerFound:
e = get_exception()
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
# .publish() takes full ARN topic id, but I'm lazy and type shortnames # .publish() takes full ARN topic id, but I'm lazy and type shortnames
@ -185,9 +191,11 @@ def main():
try: try:
connection.publish(topic=arn_topic, subject=subject, connection.publish(topic=arn_topic, subject=subject,
message_structure='json', message=json_msg) message_structure='json', message=json_msg)
except boto.exception.BotoServerError, e: except boto.exception.BotoServerError:
e = get_exception()
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
module.exit_json(msg="OK") module.exit_json(msg="OK")
main() if __name__ == '__main__':
main()

View file

@ -57,6 +57,11 @@ except ImportError:
except ImportError: except ImportError:
json = None json = None
# 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, ConnectionError
def do_request(module, url, params, headers=None): def do_request(module, url, params, headers=None):
data = urllib.urlencode(params) data = urllib.urlencode(params)
@ -72,6 +77,7 @@ def do_request(module, url, params, headers=None):
raise exc raise exc
return r return r
def get_access_token(module, client_id, client_secret): def get_access_token(module, client_id, client_secret):
params = { params = {
'client_id': client_id, 'client_id': client_id,
@ -95,7 +101,8 @@ def send_message(module, client_id, client_secret, topic, msg):
} }
do_request(module, url, {'message': msg}, headers) do_request(module, url, {'message': msg}, headers)
return True, {'access_token': access_token} return True, {'access_token': access_token}
except ConnectionError, e: except ConnectionError:
e = get_exception()
return False, e return False, e
@ -126,8 +133,5 @@ def main():
module.exit_json(changed=True, topic=topic, msg=msg) module.exit_json(changed=True, topic=topic, msg=msg)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()