From 47c967fcfea7ccf2434149e964723fec7c6b3df6 Mon Sep 17 00:00:00 2001 From: "tompage1994@hotmail.co.uk" Date: Wed, 17 Apr 2024 15:53:48 +0100 Subject: [PATCH 1/2] Handle errors correctly --- plugins/module_utils/ah_api_module.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/module_utils/ah_api_module.py b/plugins/module_utils/ah_api_module.py index 4ce63739..0eda08b2 100644 --- a/plugins/module_utils/ah_api_module.py +++ b/plugins/module_utils/ah_api_module.py @@ -294,11 +294,14 @@ def make_request(self, method, url, wait_for_task=True, **kwargs): try: response_body = response.read() except Exception as e: - if response["json"]["non_field_errors"]: + if "non_field_errors" in response["json"]: raise AHAPIModuleError("Errors occurred with request (HTTP 400). Errors: {errors}".format(errors=response["json"]["non_field_errors"])) - elif response["json"]["errors"]: - raise AHAPIModuleError("Errors occurred with request (HTTP 400). Errors: {errors}".format(errors=response["json"]["errors"])) - elif response["text"]: + elif "errors" in response["json"]: + def get_details(err): + return err["detail"] + raise AHAPIModuleError("Errors occurred with request (HTTP 400). Details: {errors}".format( + errors=", ".join(map(get_details, response["json"]["errors"])))) + elif "text" in response: raise AHAPIModuleError("Errors occurred with request (HTTP 400). Errors: {errors}".format(errors=response["text"])) raise AHAPIModuleError("Failed to read response body: {error}".format(error=e)) From bc81f5c9d8e0281c355d2859ee84dd05eae367d7 Mon Sep 17 00:00:00 2001 From: "tompage1994@hotmail.co.uk" Date: Wed, 17 Apr 2024 16:06:37 +0100 Subject: [PATCH 2/2] Fix user creation/update/delete cycle in CI --- tests/playbooks/ah_configs/ah_user.yml | 12 ++++++++---- tests/playbooks/testing_playbook_user.yml | 12 ++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/playbooks/ah_configs/ah_user.yml b/tests/playbooks/ah_configs/ah_user.yml index d0a696c2..79b272fb 100644 --- a/tests/playbooks/ah_configs/ah_user.yml +++ b/tests/playbooks/ah_configs/ah_user.yml @@ -12,14 +12,16 @@ ah_users: groups: - operators - administrators - # Testing user update\ - - username: admin1 - first_name: Róża - is_superuser: true # Testing group membership - username: operator1 groups: operators state: present + +ah_users_update: + # Testing user update + - username: admin1 + first_name: Róża + is_superuser: true # Ensure operator1 is member of group operators and managers - username: operator1 groups: @@ -37,6 +39,8 @@ ah_users: # Testing password change - username: operator1 password: test123456 + +ah_users_delete: - username: operator1 state: absent ... diff --git a/tests/playbooks/testing_playbook_user.yml b/tests/playbooks/testing_playbook_user.yml index 447ed566..e85d2941 100644 --- a/tests/playbooks/testing_playbook_user.yml +++ b/tests/playbooks/testing_playbook_user.yml @@ -53,6 +53,18 @@ ansible.builtin.include_role: name: user + - name: Test User update + ansible.builtin.include_role: + name: user + vars: + ah_users: "{{ ah_users_update }}" + + - name: Test User deletion + ansible.builtin.include_role: + name: user + vars: + ah_users: "{{ ah_users_delete }}" + # Testing deletion - name: Ensure the administrators group is deleted ah_group: