From f4454a6c931a9783e7a5054a610e76c8d9b81cb4 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Thu, 11 Jun 2020 13:34:40 -0400 Subject: [PATCH] Make tower_api a generic GET'er --- awx_collection/plugins/lookup/tower_api.py | 30 ++++++------------- .../plugins/module_utils/tower_api.py | 5 +++- .../tower_lookup_api_plugin/tasks/main.yml | 20 +++++++++++++ 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/awx_collection/plugins/lookup/tower_api.py b/awx_collection/plugins/lookup/tower_api.py index f849d78f77..e07295a724 100644 --- a/awx_collection/plugins/lookup/tower_api.py +++ b/awx_collection/plugins/lookup/tower_api.py @@ -23,7 +23,7 @@ options: description: - The query parameters to search for in the form of key/value pairs. type: dict - required: True + required: False get_all: description: - If the resulting query is paginated, return all pages. @@ -36,6 +36,10 @@ notes: """ EXAMPLES = """ +- name: Load the UI settings + debug: + msg: "{{ query('awx.awx.tower_api', 'settings/ui') }}" + - name: Lookup any users who are admins debug: msg: "{{ query('awx.awx.tower_api', 'users', query_params={ 'is_superuser': true }) }}" @@ -44,22 +48,9 @@ EXAMPLES = """ RETURN = """ _raw: description: - - Response of objects from API + - Response from the API type: dict - contains: - count: - description: The number of objects your filter returned in total (not necessarally on this page) - type: str - next: - description: The URL path for the next page - type: str - previous: - description: The URL path for the previous page - type: str - results: - description: An array of results that were returned - type: list - returned: on successful create + returned: on successful request """ from ansible.plugins.lookup import LookupBase @@ -98,12 +89,9 @@ class LookupModule(LookupBase): self.set_options(direct=kwargs) if self.get_option('get_all'): - return_data = module.get_all_endpoint(terms[0], data=self.get_option('query_params')) + return_data = module.get_all_endpoint(terms[0], data=self.get_option('query_params', {} ), none_is_not_fatal=True) else: - return_data = module.get_endpoint(terms[0], data=self.get_option('query_params')) - with open('/tmp/john', 'w') as f: - import json - f.write(json.dumps(return_data, indent=4)) + return_data = module.get_endpoint(terms[0], data=self.get_option('query_params', {} )) if return_data['status_code'] != 200: error = return_data diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index d0120ec003..1a911d96b1 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -274,7 +274,10 @@ class TowerModule(AnsibleModule): def get_all_endpoint(self, endpoint, *args, **kwargs): response = self.get_endpoint(endpoint, *args, **kwargs) if 'next' not in response['json']: - raise RuntimeError('Expected list from API at {0}, got: {1}'.format(endpoint, response)) + if not 'none_is_not_fatal' in kwargs or not kwargs['none_is_not_fatal']: + raise RuntimeError('Expected list from API at {0}, got: {1}'.format(endpoint, response)) + else: + return response next_page = response['json']['next'] if response['json']['count'] > 10000: diff --git a/awx_collection/tests/integration/targets/tower_lookup_api_plugin/tasks/main.yml b/awx_collection/tests/integration/targets/tower_lookup_api_plugin/tasks/main.yml index 60e679ee35..584f730514 100644 --- a/awx_collection/tests/integration/targets/tower_lookup_api_plugin/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_lookup_api_plugin/tasks/main.yml @@ -67,6 +67,26 @@ that: - users['results'] | length() >= 3 + - name: Get the settings page + set_fact: + settings: "{{ query('awx.awx.tower_api', 'settings/ui' ) }}" + register: results + + - assert: + that: + - results is succeeded + - "'CUSTOM_LOGO' in settings" + + - name: Get the ping page + set_fact: + ping_data: "{{ query('awx.awx.tower_api', 'ping' ) }}" + register: results + + - assert: + that: + - results is succeeded + - "'active_node' in ping_data" + always: - name: Cleanup users tower_user: