ansible/notification/typetalk.py

138 lines
3.8 KiB
Python
Raw Normal View History

2014-01-25 07:11:13 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# 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/>.
2014-01-25 07:11:13 +01:00
DOCUMENTATION = '''
---
module: typetalk
version_added: "1.6"
2014-01-25 07:11:13 +01:00
short_description: Send a message to typetalk
description:
- Send a message to typetalk using typetalk API ( http://developers.typetalk.in/ )
options:
client_id:
description:
- OAuth2 client ID
required: true
client_secret:
description:
- OAuth2 client secret
required: true
topic:
description:
- topic id to post message
required: true
msg:
description:
- message body
required: true
requirements: [ json ]
author: "Takashi Someda (@tksmd)"
2014-01-25 07:11:13 +01:00
'''
EXAMPLES = '''
- typetalk: client_id=12345 client_secret=12345 topic=1 msg="install completed"
'''
import urllib
2014-01-25 07:11:13 +01:00
try:
import json
except ImportError:
2015-07-21 08:47:56 +02:00
try:
import simplejson as json
except ImportError:
json = None
2014-01-25 07:11:13 +01: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, ConnectionError
2014-01-25 07:11:13 +01:00
2015-07-21 08:47:56 +02:00
def do_request(module, url, params, headers=None):
2014-01-25 07:11:13 +01:00
data = urllib.urlencode(params)
2015-07-21 08:47:56 +02:00
if headers is None:
headers = dict()
2014-01-25 07:11:13 +01:00
headers = dict(headers, **{
'User-Agent': 'Ansible/typetalk module',
})
2015-07-21 08:47:56 +02:00
r, info = fetch_url(module, url, data=data, headers=headers)
if info['status'] != 200:
exc = ConnectionError(info['msg'])
exc.code = info['status']
raise exc
return r
2014-01-25 07:11:13 +01:00
def get_access_token(module, client_id, client_secret):
2014-01-25 07:11:13 +01:00
params = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'client_credentials',
'scope': 'topic.post'
}
res = do_request(module, 'https://typetalk.in/oauth2/access_token', params)
2014-01-25 07:11:13 +01:00
return json.load(res)['access_token']
2015-07-21 08:47:56 +02:00
def send_message(module, client_id, client_secret, topic, msg):
2014-01-25 07:11:13 +01:00
"""
send message to typetalk
"""
try:
access_token = get_access_token(module, client_id, client_secret)
2014-01-25 07:11:13 +01:00
url = 'https://typetalk.in/api/v1/topics/%d' % topic
headers = {
'Authorization': 'Bearer %s' % access_token,
}
2015-07-21 08:47:56 +02:00
do_request(module, url, {'message': msg}, headers)
2014-01-25 07:11:13 +01:00
return True, {'access_token': access_token}
except ConnectionError:
e = get_exception()
2014-01-25 07:11:13 +01:00
return False, e
def main():
module = AnsibleModule(
argument_spec=dict(
client_id=dict(required=True),
client_secret=dict(required=True, no_log=True),
2014-01-25 07:11:13 +01:00
topic=dict(required=True, type='int'),
msg=dict(required=True),
),
supports_check_mode=False
)
if not json:
module.fail_json(msg="json module is required")
2014-01-25 07:11:13 +01:00
client_id = module.params["client_id"]
client_secret = module.params["client_secret"]
topic = module.params["topic"]
msg = module.params["msg"]
2015-07-21 08:47:56 +02:00
res, error = send_message(module, client_id, client_secret, topic, msg)
2014-01-25 07:11:13 +01:00
if not res:
module.fail_json(msg='fail to send message with response code %s' % error.code)
module.exit_json(changed=True, topic=topic, msg=msg)
2015-07-21 08:47:56 +02:00
if __name__ == '__main__':
main()