mirror of
https://github.com/famedly/ansible-collection-matrix
synced 2024-11-10 05:34:16 +00:00
fix(matrix_token_login): fix regression in required module args
52785ab768
refactored required and
mutually exclusive module parameters and meanwhile dropped some
arguments required for our matrix_token_login module. The change
made sense for most of our modules, but the matrix_token_login as the
only one doesn't need token or password, but definitely needs a key.
This commit is contained in:
parent
a8f6381cda
commit
45b4aa85b4
2 changed files with 63 additions and 47 deletions
|
@ -32,34 +32,51 @@ except ImportError:
|
||||||
|
|
||||||
|
|
||||||
class AnsibleNioModule:
|
class AnsibleNioModule:
|
||||||
def __init__(self,
|
def __init__(
|
||||||
custom_spec=None,
|
self,
|
||||||
bypass_checks=False,
|
custom_spec=None,
|
||||||
no_log=False,
|
bypass_checks=False,
|
||||||
mutually_exclusive=None,
|
no_log=False,
|
||||||
required_together=None,
|
mutually_exclusive=None,
|
||||||
required_one_of=None,
|
required_together=None,
|
||||||
required_by=None,
|
required_one_of=None,
|
||||||
add_file_common_args=False,
|
required_by=None,
|
||||||
supports_check_mode=True,
|
add_file_common_args=False,
|
||||||
required_if=None,
|
supports_check_mode=True,
|
||||||
user_logout=None):
|
required_if=None,
|
||||||
|
user_logout=None,
|
||||||
|
add_default_arguments=True,
|
||||||
|
):
|
||||||
|
if add_default_arguments:
|
||||||
|
if required_by is None:
|
||||||
|
required_by = {"password": "user_id"}
|
||||||
|
|
||||||
if required_by is None:
|
if required_one_of is None:
|
||||||
required_by = {'password': 'user_id'}
|
required_one_of = [["password", "token"]]
|
||||||
|
|
||||||
if required_one_of is None:
|
if mutually_exclusive is None:
|
||||||
required_one_of = [['password', 'token']]
|
mutually_exclusive = [["password", "token"]]
|
||||||
|
|
||||||
if mutually_exclusive is None:
|
if custom_spec is None:
|
||||||
mutually_exclusive = [['password', 'token']]
|
custom_spec = {}
|
||||||
|
|
||||||
if custom_spec is None:
|
custom_spec = AnsibleNioModule.__common_argument_spec(custom_spec)
|
||||||
custom_spec = {}
|
else:
|
||||||
|
if required_by is None:
|
||||||
|
required_by = {}
|
||||||
|
|
||||||
|
if required_one_of is None:
|
||||||
|
required_one_of = []
|
||||||
|
|
||||||
|
if mutually_exclusive is None:
|
||||||
|
mutually_exclusive = []
|
||||||
|
|
||||||
|
if custom_spec is None:
|
||||||
|
custom_spec = {}
|
||||||
|
|
||||||
# Create the Ansible module
|
# Create the Ansible module
|
||||||
self.module = AnsibleModule(
|
self.module = AnsibleModule(
|
||||||
argument_spec=AnsibleNioModule.__common_argument_spec(custom_spec),
|
argument_spec=custom_spec,
|
||||||
bypass_checks=bypass_checks,
|
bypass_checks=bypass_checks,
|
||||||
no_log=no_log,
|
no_log=no_log,
|
||||||
mutually_exclusive=mutually_exclusive,
|
mutually_exclusive=mutually_exclusive,
|
||||||
|
@ -73,7 +90,7 @@ class AnsibleNioModule:
|
||||||
|
|
||||||
if user_logout is None:
|
if user_logout is None:
|
||||||
# If a user/password login is provided, should we logout when exiting?
|
# If a user/password login is provided, should we logout when exiting?
|
||||||
self.user_logout = self.module.params['token'] is not None
|
self.user_logout = self.module.params.get('token') is not None
|
||||||
else:
|
else:
|
||||||
self.user_logout = user_logout
|
self.user_logout = user_logout
|
||||||
|
|
||||||
|
@ -88,12 +105,12 @@ class AnsibleNioModule:
|
||||||
|
|
||||||
async def matrix_login(self):
|
async def matrix_login(self):
|
||||||
# Login with token or supplied user account
|
# Login with token or supplied user account
|
||||||
if self.module.params['token'] is None:
|
if self.module.params.get('token') is None:
|
||||||
self.client = AsyncClient(self.module.params['hs_url'], self.module.params['user_id'])
|
self.client = AsyncClient(self.module.params.get('hs_url'), self.module.params.get('user_id'))
|
||||||
login_response = await self.client.login(password=self.module.params['password'])
|
login_response = await self.client.login(password=self.module.params.get('password'))
|
||||||
else:
|
else:
|
||||||
self.client = AsyncClient(self.module.params['hs_url'])
|
self.client = AsyncClient(self.module.params.get('hs_url'))
|
||||||
login_response = await self.client.login(token=self.module.params['token'])
|
login_response = await self.client.login(token=self.module.params.get('token'))
|
||||||
if isinstance(login_response, LoginResponse):
|
if isinstance(login_response, LoginResponse):
|
||||||
self.access_token = login_response.access_token
|
self.access_token = login_response.access_token
|
||||||
self.device_id = login_response.device_id
|
self.device_id = login_response.device_id
|
||||||
|
@ -112,13 +129,13 @@ class AnsibleNioModule:
|
||||||
self.module.fail_json(**result)
|
self.module.fail_json(**result)
|
||||||
|
|
||||||
async def exit_json(self, **result):
|
async def exit_json(self, **result):
|
||||||
if self.module.params['token'] is None and self.user_logout is True:
|
if self.module.params.get('token') is None and self.user_logout is True:
|
||||||
await self.matrix_logout()
|
await self.matrix_logout()
|
||||||
await self.client.close()
|
await self.client.close()
|
||||||
self.module.exit_json(**result)
|
self.module.exit_json(**result)
|
||||||
|
|
||||||
async def fail_json(self, **result):
|
async def fail_json(self, **result):
|
||||||
if self.module.params['token'] is None and self.user_logout is True:
|
if self.module.params.get('token') is None and self.user_logout is True:
|
||||||
await self.matrix_logout()
|
await self.matrix_logout()
|
||||||
await self.client.close()
|
await self.client.close()
|
||||||
self.module.fail_json(**result)
|
self.module.fail_json(**result)
|
||||||
|
|
|
@ -32,26 +32,16 @@ options:
|
||||||
user_id:
|
user_id:
|
||||||
description:
|
description:
|
||||||
- The user id of the user
|
- The user id of the user
|
||||||
required: false
|
required: true
|
||||||
type: str
|
type: str
|
||||||
password:
|
|
||||||
description:
|
|
||||||
- The password to log in with
|
|
||||||
required: false
|
|
||||||
type: str
|
|
||||||
token:
|
|
||||||
description:
|
|
||||||
- Authentication token for the API call
|
|
||||||
required: false
|
|
||||||
type: str
|
|
||||||
admin:
|
|
||||||
description:
|
|
||||||
- Whether to set the user as admin during login
|
|
||||||
type: bool
|
|
||||||
key:
|
key:
|
||||||
description: Login key to use
|
description: Login key to use
|
||||||
type: str
|
type: str
|
||||||
required: true
|
required: true
|
||||||
|
admin:
|
||||||
|
description:
|
||||||
|
- Whether to set the user as admin during login
|
||||||
|
type: bool
|
||||||
requirements:
|
requirements:
|
||||||
- matrix-nio (Python library)
|
- matrix-nio (Python library)
|
||||||
- jwcrypto (Python library)
|
- jwcrypto (Python library)
|
||||||
|
@ -107,8 +97,10 @@ except ImportError:
|
||||||
|
|
||||||
async def run_module():
|
async def run_module():
|
||||||
module_args = dict(
|
module_args = dict(
|
||||||
key=dict(type='str', required=True, no_log=True),
|
hs_url=dict(type="str", required=True),
|
||||||
admin=dict(type='bool', required=False),
|
user_id=dict(type="str", required=True),
|
||||||
|
key=dict(type="str", required=True, no_log=True),
|
||||||
|
admin=dict(type="bool", required=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
result = dict(
|
result = dict(
|
||||||
|
@ -116,7 +108,14 @@ async def run_module():
|
||||||
msg="",
|
msg="",
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleNioModule(module_args, user_logout=False)
|
args = {
|
||||||
|
"add_default_arguments": False,
|
||||||
|
"required_by": {},
|
||||||
|
"required_one_of": [],
|
||||||
|
"mutually_exclusive": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
module = AnsibleNioModule(module_args, user_logout=False, **args)
|
||||||
if not HAS_JWCRYPTO:
|
if not HAS_JWCRYPTO:
|
||||||
await module.fail_json(msg=missing_required_lib("jwcrypto"))
|
await module.fail_json(msg=missing_required_lib("jwcrypto"))
|
||||||
if not HAS_NIO:
|
if not HAS_NIO:
|
||||||
|
|
Loading…
Reference in a new issue