1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-30 22:21:13 +03:00

Make tower_api a generic GET'er

This commit is contained in:
John Westcott IV 2020-06-11 13:34:40 -04:00
parent 44a3057d16
commit f4454a6c93
3 changed files with 33 additions and 22 deletions

View File

@ -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

View File

@ -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:

View File

@ -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: