From 61d5e59cb8b3f17cc5f92bd2240db28aa6d3fb3a Mon Sep 17 00:00:00 2001 From: Johanna Dorothea Reichmann Date: Mon, 30 Jan 2023 13:07:34 +0100 Subject: [PATCH] fix: check-mode in modules behaves properly --- plugins/modules/matrix_member.py | 9 +++++---- plugins/modules/matrix_notification.py | 5 ++++- plugins/modules/matrix_room.py | 5 +++++ plugins/modules/matrix_state.py | 7 +++++-- plugins/modules/matrix_token_login.py | 12 +++++++++++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/plugins/modules/matrix_member.py b/plugins/modules/matrix_member.py index be89d0a..0fb8d3c 100644 --- a/plugins/modules/matrix_member.py +++ b/plugins/modules/matrix_member.py @@ -199,10 +199,6 @@ async def run_module(): await module.fail_json(msg=missing_required_lib("matrix-nio")) await module.matrix_login() - - if module.check_mode: - return result - action = module.params["state"] room_id = module.params["room_id"] user_ids = module.params["user_ids"] @@ -211,6 +207,11 @@ async def run_module(): if module.params["exclusive"] and action != "member": await module.fail_json(msg="exclusive=True can only be used with state=member") + # Handle ansible check mode + if module.check_mode: + result["members"] = user_ids + await module.exit_json(**result) + # Create client object client = module.client diff --git a/plugins/modules/matrix_notification.py b/plugins/modules/matrix_notification.py index 283be5d..6fda2b0 100644 --- a/plugins/modules/matrix_notification.py +++ b/plugins/modules/matrix_notification.py @@ -116,7 +116,8 @@ async def run_module(): await module.fail_json(msg=missing_required_lib("matrix-nio")) if module.check_mode: - return result + result["changed"] = True + await module.exit_json(**result) await module.matrix_login() client = module.client @@ -134,6 +135,8 @@ async def run_module(): ) if isinstance(response, RoomSendError): await module.fail_json(**result) + else: + result["changed"] = True await module.exit_json(**result) diff --git a/plugins/modules/matrix_room.py b/plugins/modules/matrix_room.py index 371d3db..1f03a0f 100644 --- a/plugins/modules/matrix_room.py +++ b/plugins/modules/matrix_room.py @@ -111,6 +111,11 @@ async def run_module(): await module.matrix_login() client = module.client + if module.check_mode: + result["changed"] = True + result["room_id"] = "!fakeRoomId:localhost" + await module.exit_json(**result) + # Try to look up room_id room_id_resp = await client.room_resolve_alias(module.params["alias"]) diff --git a/plugins/modules/matrix_state.py b/plugins/modules/matrix_state.py index 4b9cb8c..b25a04d 100644 --- a/plugins/modules/matrix_state.py +++ b/plugins/modules/matrix_state.py @@ -131,8 +131,11 @@ async def run_module(): client = module.client if module.check_mode: - # TODO: Do proper check - return result + # TODO: Attempt to check if the state event is already present + # Implementation note: this might not be possible as a previous check-moded + # login token might be invalid / not provided + result["changed"] = True + await module.exit_json(**result) failed = False diff --git a/plugins/modules/matrix_token_login.py b/plugins/modules/matrix_token_login.py index 3b880cb..e0c3715 100644 --- a/plugins/modules/matrix_token_login.py +++ b/plugins/modules/matrix_token_login.py @@ -124,7 +124,10 @@ async def run_module(): await module.fail_json(msg=missing_required_lib("matrix-nio")) if module.check_mode: - return result + result["changed"] = True + result["device_id"] = "FAKEDEVICE" + result["token"] = "syt_fake_token" + await module.exit_json(**result) failed = False @@ -137,6 +140,13 @@ async def run_module(): if key is None: await module.fail_json(msg="A key has to be provided") + # Move check-mode handling after check for missing key + if module.check_mode: + result["changed"] = True + result["device_id"] = "FAKEDEVICE" + result["token"] = "syt_fake_token" + await module.exit_json(**result) + admin = module.params["admin"] method, path, data = Api.login(