Skip to content

Commit

Permalink
Handle errors correctly (#383)
Browse files Browse the repository at this point in the history
* Handle errors correctly

* Fix user creation/update/delete cycle in CI

---------

Co-authored-by: David Danielsson <[email protected]>
  • Loading branch information
Tompage1994 and djdanielsson authored Apr 17, 2024
1 parent cac92c2 commit d05e94e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
11 changes: 7 additions & 4 deletions plugins/module_utils/ah_api_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
12 changes: 8 additions & 4 deletions tests/playbooks/ah_configs/ah_user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -37,6 +39,8 @@ ah_users:
# Testing password change
- username: operator1
password: test123456

ah_users_delete:
- username: operator1
state: absent
...
12 changes: 12 additions & 0 deletions tests/playbooks/testing_playbook_user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d05e94e

Please sign in to comment.