awx/awx_collection/plugins/lookup/controller_api.py
Sandra McCann 8c4be1c529
Cherrypick recent docs prs to docs branch (#15477)
* Update  docs replacements to AWX (#15349)

Update replacements to AWX

Signed-off-by: Sandra McCann <samccann@redhat.com>
(cherry picked from commit 9979fc659efbf4d54a39f9f36912d5ed7b0fa6cd)

* Remove remnants of controller terms from quickstart docs (#15350)

Remove remnants of controller terms from quickstart

Signed-off-by: Sandra McCann <samccann@redhat.com>
(cherry picked from commit 864a30e3d451e6daf39421a598f725419f105101)

* Remove references to translated versions of the docs (#15354)

remove references to translated versions of the docs

Signed-off-by: Sandra McCann <samccann@redhat.com>
Co-authored-by: TVo <thavo@redhat.com>
(cherry picked from commit 5f42db67e6fbcffa8e5b26553d41273b7730344c)

* update terminology (#15357)

* update terminology

Replace some instances of Tower with AWX and remove some references to
enterprise left over from the migration of RST content from the
Automation Controller docs.

* Update docs/docsite/rst/userguide/overview.rst

Co-authored-by: TVo <thavo@redhat.com>

---------

Co-authored-by: TVo <thavo@redhat.com>
(cherry picked from commit f1448fced1411b9d7cce19a0ad91df992e447014)

* Replaced all references of downstream docs to upstream docs (#15388)

* Replaced all references of downstream docs to upstream docs.

* Update README.md

Co-authored-by: Don Naro <dnaro@redhat.com>

* Update README.md.j2

Co-authored-by: Don Naro <dnaro@redhat.com>

* Update README.md.j2

Co-authored-by: Don Naro <dnaro@redhat.com>

* Incorpor'd review feedback from @oraNod and @samccann

* Updated with agreed link (for now) until further change is needed.

---------

Co-authored-by: Don Naro <dnaro@redhat.com>
(cherry picked from commit 018f235a645163ee820ff33b47a70194fabeea66)

* Re-do PR #14685 for alt-text inventories. (#15394)

(cherry picked from commit 6d0c47fdd0f0ca00c06afb5db5bd49c8da995a50)

* Docs: add Communication guide (#15469)

* Docs: add Communication guide

* Update docs/docsite/rst/contributor/communication.rst

Co-authored-by: Don Naro <dnaro@redhat.com>

* Update docs/docsite/rst/contributor/communication.rst

---------

Co-authored-by: Don Naro <dnaro@redhat.com>
(cherry picked from commit 79c1921ea480ae26b0d7faf6e1a8e89b61f76c30)

---------

Co-authored-by: Don Naro <dnaro@redhat.com>
Co-authored-by: TVo <thavo@redhat.com>
Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
2024-08-29 12:26:30 +01:00

193 lines
7.2 KiB
Python

# (c) 2020 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
name: controller_api
author: John Westcott IV (@john-westcott-iv)
short_description: Search the API for objects
requirements:
- None
description:
- Returns GET requests from the Automation Platform Controller API. See
U(https://docs.ansible.com/automation-controller/latest/html/towerapi/) for API usage.
- For use that is cross-compatible between the awx.awx and ansible.controller collection
see the controller_meta module
options:
_terms:
description:
- The endpoint to query, i.e. teams, users, tokens, job_templates, etc.
required: True
query_params:
description:
- The query parameters to search for in the form of key/value pairs.
type: dict
required: False
aliases: [query, data, filter, params]
expect_objects:
description:
- Error if the response does not contain either a detail view or a list view.
type: boolean
default: False
aliases: [expect_object]
expect_one:
description:
- Error if the response contains more than one object.
type: boolean
default: False
return_objects:
description:
- If a list view is returned, promote the list of results to the top-level of list returned.
- Allows using this lookup plugin to loop over objects without additional work.
type: boolean
default: True
return_all:
description:
- If the response is paginated, return all pages.
type: boolean
default: False
return_ids:
description:
- If response contains objects, promote the id key to the top-level entries in the list.
- Allows looking up a related object and passing it as a parameter to another module.
- This will convert the return to a string or list of strings depending on the number of selected items.
type: boolean
aliases: [return_id]
default: False
max_objects:
description:
- if C(return_all) is true, this is the maximum of number of objects to return from the list.
- If a list view returns more an max_objects an exception will be raised
type: integer
default: 1000
extends_documentation_fragment: awx.awx.auth_plugin
notes:
- If the query is not filtered properly this can cause a performance impact.
"""
EXAMPLES = """
- name: Load the UI settings
set_fact:
controller_settings: "{{ lookup('awx.awx.controller_api', 'settings/ui') }}"
- name: Load the UI settings specifying the connection info
set_fact:
controller_settings: "{{ lookup('awx.awx.controller_api', 'settings/ui', host='controller.example.com',
username='admin', password=my_pass_var, verify_ssl=False) }}"
- name: Report the usernames of all users with admin privs
debug:
msg: "Admin users: {{ query('awx.awx.controller_api', 'users', query_params={ 'is_superuser': true }) | map(attribute='username') | join(', ') }}"
- name: debug all organizations in a loop # use query to return a list
debug:
msg: "Organization description={{ item['description'] }} id={{ item['id'] }}"
loop: "{{ query('awx.awx.controller_api', 'organizations') }}"
loop_control:
label: "{{ item['name'] }}"
- name: Make sure user 'john' is an org admin of the default org if the user exists
role:
organization: Default
role: admin
user: john
when: "lookup('awx.awx.controller_api', 'users', query_params={ 'username': 'john' }) | length == 1"
- name: Create an inventory group with all 'foo' hosts
group:
name: "Foo Group"
inventory: "Demo Inventory"
hosts: >-
{{ query(
'awx.awx.controller_api',
'hosts',
query_params={ 'name__startswith' : 'foo', },
) | map(attribute='name') | list }}
register: group_creation
"""
RETURN = """
_raw:
description:
- Response from the API
type: dict
returned: on successful request
"""
from ansible.plugins.lookup import LookupBase
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_native
from ansible.utils.display import Display
from ..module_utils.controller_api import ControllerAPIModule
class LookupModule(LookupBase):
display = Display()
def handle_error(self, **kwargs):
raise AnsibleError(to_native(kwargs.get('msg')))
def warn_callback(self, warning):
self.display.warning(warning)
def run(self, terms, variables=None, **kwargs):
if len(terms) != 1:
raise AnsibleError('You must pass exactly one endpoint to query')
self.set_options(direct=kwargs)
# Defer processing of params to logic shared with the modules
module_params = {}
for plugin_param, module_param in ControllerAPIModule.short_params.items():
opt_val = self.get_option(plugin_param)
if opt_val is not None:
module_params[module_param] = opt_val
# Create our module
module = ControllerAPIModule(argument_spec={}, direct_params=module_params, error_callback=self.handle_error, warn_callback=self.warn_callback)
response = module.get_endpoint(terms[0], data=self.get_option('query_params', {}))
if 'status_code' not in response:
raise AnsibleError("Unclear response from API: {0}".format(response))
if response['status_code'] != 200:
raise AnsibleError("Failed to query the API: {0}".format(response['json'].get('detail', response['json'])))
return_data = response['json']
if self.get_option('expect_objects') or self.get_option('expect_one'):
if ('id' not in return_data) and ('results' not in return_data):
raise AnsibleError('Did not obtain a list or detail view at {0}, and ' 'expect_objects or expect_one is set to True'.format(terms[0]))
if self.get_option('expect_one'):
if 'results' in return_data and len(return_data['results']) != 1:
raise AnsibleError('Expected one object from endpoint {0}, ' 'but obtained {1} from API'.format(terms[0], len(return_data['results'])))
if self.get_option('return_all') and 'results' in return_data:
if return_data['count'] > self.get_option('max_objects'):
raise AnsibleError(
'List view at {0} returned {1} objects, which is more than the maximum allowed '
'by max_objects, {2}'.format(terms[0], return_data['count'], self.get_option('max_objects'))
)
next_page = return_data['next']
while next_page is not None:
next_response = module.get_endpoint(next_page)
return_data['results'] += next_response['json']['results']
next_page = next_response['json']['next']
return_data['next'] = None
if self.get_option('return_ids'):
if 'results' in return_data:
return_data['results'] = [str(item['id']) for item in return_data['results']]
elif 'id' in return_data:
return_data = str(return_data['id'])
if self.get_option('return_objects') and 'results' in return_data:
return return_data['results']
else:
return [return_data]