Skip to content

Commit

Permalink
Auto remove api empty (#259)
Browse files Browse the repository at this point in the history
* update references to ansible.tower to controller, update playbook to be user friendly

* remove file

* update pre commit

* update errors and set default to warn

* Lint fix and add changelog

Co-authored-by: [email protected] <[email protected]>
  • Loading branch information
sean-m-sullivan and Tompage1994 authored Jan 18, 2022
1 parent b18c9b9 commit eef0991
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelogs/fragments/diff_empty_fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
bugfixes:
- warn on default if the api list fed to controller_object_diff lookup is empty
...
23 changes: 23 additions & 0 deletions examples/configure_controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@
set_fact:
controller_organization_id: "{{ lookup('awx.awx.controller_api', 'organizations', query_params={ 'name': 'Default' } ,host=controller_hostname, username=controller_username, password=controller_password, verify_ssl=false) }}"

- name: "Set empty lists for testing"
set_fact:
controller_api_results: []
differential_test_items: []

- name: "Error out on empty list"
set_fact:
error_empty_diff: "{{ lookup('controller_object_diff', api_list=controller_api_results, compare_list=differential_test_items, warn_on_empty_api=false ) }}"
ignore_errors: true
register: error_results

- name: "Warn out on empty list"
set_fact:
warn_empty_diff: "{{ lookup('controller_object_diff', api_list=controller_api_results, compare_list=differential_test_items ) }}"
register: warn_results

- name: "Assert that the empty list error correctly"
assert:
that:
- error_empty_diff is not defined
- warn_empty_diff | length == 0
- "'Unable to find items in api_list' in error_results.msg"

- name: Differential Testing
include_tasks: "./tasks/differential.yml"
loop: "{{ differential_items }}"
Expand Down
16 changes: 15 additions & 1 deletion plugins/lookup/controller_object_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
- Include items in the original compare list in the output, and set state: present
type: boolean
default: True
warn_on_empty_api:
description:
- If the API list is empty, issue an ansible warning and return the empty list.
- This allows the module to be idempotent.
- Setting to false will make the lookup error and fail when there is an empty list.
type: boolean
default: True
"""

EXAMPLES = """
Expand Down Expand Up @@ -75,7 +82,7 @@
"""

from ansible.plugins.lookup import LookupBase
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleLookupError
from ansible.module_utils._text import to_native
from ansible.utils.display import Display

Expand All @@ -95,6 +102,13 @@ def run(self, terms, variables=None, **kwargs):
# Set Variables for user input
api_list = self.get_option("api_list")
compare_list = self.get_option("compare_list")
warn_on_empty_api = self.get_option("warn_on_empty_api")
if not api_list:
if warn_on_empty_api:
self._display.warning("Skipping, did not find items in api_list")
else:
raise AnsibleLookupError("Unable to find items in api_list")
return [api_list]

# Set Keys to keep for each list.
keys_to_keep = ["name", "organization"]
Expand Down

0 comments on commit eef0991

Please sign in to comment.