Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors correctly #383

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading