mirror of
https://gitlab.com/famedly/ansible/modules/matrix.git
synced 2024-12-22 07:11:22 +00:00
migrate modules matrix-nio and add tests
This commit is contained in:
parent
36aecd6c3e
commit
aea8668d0a
6 changed files with 106 additions and 41 deletions
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz>
|
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz>
|
||||||
# (c) 2020, Famedly GmbH
|
# (c) 2020, Famedly GmbH
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU Affero General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/agpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
@ -35,7 +35,7 @@ options:
|
||||||
- The password to log in with
|
- The password to log in with
|
||||||
required: true
|
required: true
|
||||||
requirements:
|
requirements:
|
||||||
- matrix-client (Python library)
|
- matrix-nio (Python library)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -51,22 +51,26 @@ token:
|
||||||
description: The access token aquired by logging in
|
description: The access token aquired by logging in
|
||||||
returned: When login was successful
|
returned: When login was successful
|
||||||
type: str
|
type: str
|
||||||
|
device_id:
|
||||||
|
description: The device ID assigned by the server
|
||||||
|
returned: When login was successful
|
||||||
|
type: str
|
||||||
'''
|
'''
|
||||||
import traceback
|
import traceback
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||||
|
|
||||||
MATRIX_IMP_ERR = None
|
MATRIX_IMP_ERR = None
|
||||||
try:
|
try:
|
||||||
from matrix_client.client import MatrixClient
|
from nio import AsyncClient
|
||||||
except ImportError:
|
except ImportError:
|
||||||
MATRIX_IMP_ERR = traceback.format_exc()
|
MATRIX_IMP_ERR = traceback.format_exc()
|
||||||
matrix_found = False
|
matrix_found = False
|
||||||
else:
|
else:
|
||||||
matrix_found = True
|
matrix_found = True
|
||||||
|
|
||||||
|
async def run_module():
|
||||||
def run_module():
|
|
||||||
module_args = dict(
|
module_args = dict(
|
||||||
hs_url=dict(type='str', required=True),
|
hs_url=dict(type='str', required=True),
|
||||||
user_id=dict(type='str', required=True),
|
user_id=dict(type='str', required=True),
|
||||||
|
@ -83,22 +87,28 @@ def run_module():
|
||||||
)
|
)
|
||||||
|
|
||||||
if not matrix_found:
|
if not matrix_found:
|
||||||
module.fail_json(msg=missing_required_lib('matrix-client'), exception=MATRIX_IMP_ERR)
|
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
|
||||||
|
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# create a client object
|
# Create client object
|
||||||
client = MatrixClient(module.params['hs_url'])
|
client = AsyncClient(module.params['hs_url'], module.params['user_id'])
|
||||||
token = client.login(module.params['user_id'], module.params['password'], sync=False)
|
# Log in
|
||||||
|
login_response = await client.login(module.params['password'])
|
||||||
|
|
||||||
result['token'] = token
|
# Store results
|
||||||
|
result['token'] = login_response.access_token
|
||||||
|
result['device_id'] = login_response.device_id
|
||||||
|
|
||||||
|
# Close client sessions
|
||||||
|
await client.close()
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
run_module()
|
asyncio.run(run_module())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz>
|
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz>
|
||||||
# (c) 2020, Famedly GmbH
|
# (c) 2020, Famedly GmbH
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU Affero General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/agpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
@ -31,7 +31,7 @@ options:
|
||||||
- Authentication token for the API call
|
- Authentication token for the API call
|
||||||
required: true
|
required: true
|
||||||
requirements:
|
requirements:
|
||||||
- matrix-client (Python library)
|
- matrix-nio (Python library)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -44,20 +44,20 @@ EXAMPLES = '''
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
'''
|
'''
|
||||||
import traceback
|
import traceback
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||||
|
|
||||||
MATRIX_IMP_ERR = None
|
MATRIX_IMP_ERR = None
|
||||||
try:
|
try:
|
||||||
from matrix_client.client import MatrixClient
|
from nio import AsyncClient
|
||||||
except ImportError:
|
except ImportError:
|
||||||
MATRIX_IMP_ERR = traceback.format_exc()
|
MATRIX_IMP_ERR = traceback.format_exc()
|
||||||
matrix_found = False
|
matrix_found = False
|
||||||
else:
|
else:
|
||||||
matrix_found = True
|
matrix_found = True
|
||||||
|
|
||||||
|
async def run_module():
|
||||||
def run_module():
|
|
||||||
module_args = dict(
|
module_args = dict(
|
||||||
hs_url=dict(type='str', required=True),
|
hs_url=dict(type='str', required=True),
|
||||||
token=dict(type='str', required=True, no_log=True),
|
token=dict(type='str', required=True, no_log=True),
|
||||||
|
@ -73,23 +73,24 @@ def run_module():
|
||||||
)
|
)
|
||||||
|
|
||||||
if not matrix_found:
|
if not matrix_found:
|
||||||
module.fail_json(msg=missing_required_lib('matrix-client'), exception=MATRIX_IMP_ERR)
|
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
|
||||||
|
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# create a client object
|
# create a client object
|
||||||
client = MatrixClient(module.params['hs_url'])
|
client = AsyncClient(module.params['hs_url'])
|
||||||
client.api.token = module.params['token']
|
client.access_token = module.params['access_token']
|
||||||
|
# log out
|
||||||
client.logout()
|
await client.logout()
|
||||||
|
# close client sessions
|
||||||
|
await client.close()
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
run_module()
|
asyncio.run(run_module())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz>
|
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz>
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# (c) 2020, Famedly GmbH
|
||||||
|
# GNU Affero General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/agpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
@ -16,7 +17,7 @@ ANSIBLE_METADATA = {
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
author: "Jan Christian Grünhage (@jcgruenhage)"
|
author: "Jan Christian Grünhage (@jcgruenhage)"
|
||||||
module: matrix
|
module: matrix-notification
|
||||||
short_description: Send notifications to matrix
|
short_description: Send notifications to matrix
|
||||||
description:
|
description:
|
||||||
- This module sends html formatted notifications to matrix rooms.
|
- This module sends html formatted notifications to matrix rooms.
|
||||||
|
@ -48,12 +49,12 @@ options:
|
||||||
description:
|
description:
|
||||||
- The password to log in with
|
- The password to log in with
|
||||||
requirements:
|
requirements:
|
||||||
- matrix-client (Python library)
|
- matrix-nio (Python library)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
- name: Send matrix notification with token
|
- name: Send matrix notification with token
|
||||||
matrix:
|
matrix-notification:
|
||||||
msg_plain: "**hello world**"
|
msg_plain: "**hello world**"
|
||||||
msg_html: "<b>hello world</b>"
|
msg_html: "<b>hello world</b>"
|
||||||
room_id: "!12345678:server.tld"
|
room_id: "!12345678:server.tld"
|
||||||
|
@ -61,7 +62,7 @@ EXAMPLES = '''
|
||||||
token: "{{ matrix_auth_token }}"
|
token: "{{ matrix_auth_token }}"
|
||||||
|
|
||||||
- name: Send matrix notification with user_id and password
|
- name: Send matrix notification with user_id and password
|
||||||
matrix:
|
matrix-notification:
|
||||||
msg_plain: "**hello world**"
|
msg_plain: "**hello world**"
|
||||||
msg_html: "<b>hello world</b>"
|
msg_html: "<b>hello world</b>"
|
||||||
room_id: "!12345678:server.tld"
|
room_id: "!12345678:server.tld"
|
||||||
|
@ -73,20 +74,30 @@ EXAMPLES = '''
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
'''
|
'''
|
||||||
import traceback
|
import traceback
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||||
|
|
||||||
MATRIX_IMP_ERR = None
|
MATRIX_IMP_ERR = None
|
||||||
try:
|
try:
|
||||||
from matrix_client.client import MatrixClient
|
from nio import AsyncClient
|
||||||
except ImportError:
|
except ImportError:
|
||||||
MATRIX_IMP_ERR = traceback.format_exc()
|
MATRIX_IMP_ERR = traceback.format_exc()
|
||||||
matrix_found = False
|
matrix_found = False
|
||||||
else:
|
else:
|
||||||
matrix_found = True
|
matrix_found = True
|
||||||
|
|
||||||
|
async def get_client_with_token(hs_url, token):
|
||||||
|
client = AsyncClient(hs_url)
|
||||||
|
client.access_token = token
|
||||||
|
return client
|
||||||
|
|
||||||
def run_module():
|
async def get_client_with_password(hs_url, user, password):
|
||||||
|
client = AsyncClient(hs_url, user)
|
||||||
|
await client.login(password)
|
||||||
|
return client
|
||||||
|
|
||||||
|
async def run_module():
|
||||||
module_args = dict(
|
module_args = dict(
|
||||||
msg_plain=dict(type='str', required=True),
|
msg_plain=dict(type='str', required=True),
|
||||||
msg_html=dict(type='str', required=True),
|
msg_html=dict(type='str', required=True),
|
||||||
|
@ -98,7 +109,7 @@ def run_module():
|
||||||
)
|
)
|
||||||
|
|
||||||
result = dict(
|
result = dict(
|
||||||
changed=False,
|
changed=True,
|
||||||
message=''
|
message=''
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -111,28 +122,46 @@ def run_module():
|
||||||
)
|
)
|
||||||
|
|
||||||
if not matrix_found:
|
if not matrix_found:
|
||||||
module.fail_json(msg=missing_required_lib('matrix-client'), exception=MATRIX_IMP_ERR)
|
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
|
||||||
|
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# create a client object
|
# create a client object
|
||||||
client = MatrixClient(module.params['hs_url'])
|
|
||||||
if module.params['token'] is not None:
|
if module.params['token'] is not None:
|
||||||
client.api.token = module.params['token']
|
client = await get_client_with_token(
|
||||||
|
module.params['hs_url'],
|
||||||
|
module.params['token']
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
client.login(module.params['user_id'], module.params['password'], sync=False)
|
client = await get_client_with_password(
|
||||||
|
module.params['hs_url'],
|
||||||
|
module.params['user_id'],
|
||||||
|
module.params['password']
|
||||||
|
)
|
||||||
|
|
||||||
# make sure we are in a given room and return a room object for it
|
# send message
|
||||||
room = client.join_room(module.params['room_id'])
|
await client.room_send(
|
||||||
# send an html formatted messages
|
room_id=module.params['room_id'],
|
||||||
room.send_html(module.params['msg_html'], module.params['msg_plain'])
|
message_type="m.room.message",
|
||||||
|
content={
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"body": module.params['msg_plain'],
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": module.params['msg_html'],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# when we did the login ourselves, invalidate the access token
|
||||||
|
if module.params['token'] is None:
|
||||||
|
await client.logout()
|
||||||
|
|
||||||
|
await client.close()
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
run_module()
|
asyncio.run(run_module())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
11
test-login-logout.sh
Executable file
11
test-login-logout.sh
Executable file
|
@ -0,0 +1,11 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
source test-settings.sh
|
||||||
|
|
||||||
|
login_resp=` echo "{\"ANSIBLE_MODULE_ARGS\": {\"hs_url\": \"${HS_URL}\",\"user_id\": \"${USER_ID}\",\"password\": \"${PASSWORD}\"}}" | python matrix-login.py`
|
||||||
|
|
||||||
|
echo $login_resp
|
||||||
|
|
||||||
|
local_token=`echo $login_resp | jq --raw-output '.token'`
|
||||||
|
|
||||||
|
echo "{\"ANSIBLE_MODULE_ARGS\": {\"hs_url\": \"${HS_URL}\",\"token\": \"${local_token}\"}}" | python matrix-logout.py
|
7
test-notification.sh
Executable file
7
test-notification.sh
Executable file
|
@ -0,0 +1,7 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
source test-settings.sh
|
||||||
|
|
||||||
|
notification_resp=` echo "{\"ANSIBLE_MODULE_ARGS\": {\"hs_url\": \"${HS_URL}\", \"token\": \"${TOKEN}\", \"room_id\": \"${ROOM_ID}\", \"msg_plain\": \"**Hello, World!**\", \"msg_html\": \"<b>Hello, World!</b>\"}}" | python matrix-notification.py`
|
||||||
|
|
||||||
|
echo $notification_resp
|
7
test-settings.sample.sh
Normal file
7
test-settings.sample.sh
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
HS_URL=
|
||||||
|
USER_ID=
|
||||||
|
PASSWORD=
|
||||||
|
TOKEN=
|
||||||
|
|
||||||
|
ROOM_ID=
|
||||||
|
ROOM_ALIAS=
|
Loading…
Reference in a new issue