mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 09:27:36 -02:30
Delete the tower_get_id plugin and tests
This commit is contained in:
committed by
John Westcott IV
parent
30ff112c87
commit
8239232d4d
@@ -1,82 +0,0 @@
|
|||||||
# (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 = """
|
|
||||||
lookup: tower_get_id
|
|
||||||
author: John Westcott IV (@john-westcott-iv)
|
|
||||||
short_description: Search for a specific ID of an option
|
|
||||||
requirements:
|
|
||||||
- None
|
|
||||||
description:
|
|
||||||
- Returns an ID of an object found in tower by the fiter criteria. See
|
|
||||||
U(https://docs.ansible.com/ansible-tower/latest/html/towerapi/index.html) for API usage.
|
|
||||||
Raises an exception if not exactly one object is found.
|
|
||||||
extends_documentation_fragment:
|
|
||||||
- awx.awx.auth_plugin
|
|
||||||
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: True
|
|
||||||
"""
|
|
||||||
|
|
||||||
EXAMPLES = """
|
|
||||||
- name: Lookup a users ID
|
|
||||||
debug:
|
|
||||||
msg: "{{ query('awx.awx.tower_get_id', 'users', query_params={ 'username': 'admin' }) }}"
|
|
||||||
"""
|
|
||||||
|
|
||||||
RETURN = """
|
|
||||||
_raw:
|
|
||||||
description:
|
|
||||||
- The ID found for the filter criteria returned as a string (i.e. "42" instead of 42).
|
|
||||||
type: str
|
|
||||||
"""
|
|
||||||
|
|
||||||
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.tower_api import TowerModule
|
|
||||||
|
|
||||||
|
|
||||||
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')
|
|
||||||
|
|
||||||
# Defer processing of params to logic shared with the modules
|
|
||||||
module_params = {}
|
|
||||||
for plugin_param, module_param in TowerModule.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 = TowerModule(
|
|
||||||
argument_spec={}, direct_params=module_params,
|
|
||||||
error_callback=self.handle_error, warn_callback=self.warn_callback
|
|
||||||
)
|
|
||||||
|
|
||||||
self.set_options(direct=kwargs)
|
|
||||||
|
|
||||||
found_object = module.get_one(terms[0], data=self.get_option('query_params'))
|
|
||||||
if found_object is None:
|
|
||||||
self.handle_error(msg='No objects matched that criteria')
|
|
||||||
else:
|
|
||||||
return found_object['id']
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
---
|
|
||||||
- name: Generate a random string for test
|
|
||||||
set_fact:
|
|
||||||
test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
|
||||||
when: test_id is not defined
|
|
||||||
|
|
||||||
- name: Generate usernames
|
|
||||||
set_fact:
|
|
||||||
usernames:
|
|
||||||
- "AWX-Collection-tests-tower_get_id-user1-{{ test_id }}"
|
|
||||||
- "AWX-Collection-tests-tower_get_id-user2-{{ test_id }}"
|
|
||||||
- "AWX-Collection-tests-tower_get_id-user3-{{ test_id }}"
|
|
||||||
|
|
||||||
- name: Create all of our users
|
|
||||||
tower_user:
|
|
||||||
username: "{{ item }}"
|
|
||||||
is_superuser: true
|
|
||||||
password: "{{ test_id }}"
|
|
||||||
loop: "{{ usernames }}"
|
|
||||||
register: user_creation_results
|
|
||||||
|
|
||||||
- block:
|
|
||||||
- name: Test too many params (failure from validation of terms)
|
|
||||||
debug:
|
|
||||||
msg: "{{ query('awx.awx.tower_get_id', 'users', 'teams', query_params={}, ) }}"
|
|
||||||
ignore_errors: true
|
|
||||||
register: result
|
|
||||||
|
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- result is failed
|
|
||||||
- "'You must pass exactly one endpoint to query' in result.msg"
|
|
||||||
|
|
||||||
- name: Try to load invalid endpoint
|
|
||||||
debug:
|
|
||||||
msg: "{{ query('awx.awx.tower_get_id', 'john', query_params={}, ) }}"
|
|
||||||
ignore_errors: true
|
|
||||||
register: result
|
|
||||||
|
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- result is failed
|
|
||||||
- "'The requested object could not be found at' in result.msg"
|
|
||||||
|
|
||||||
- name: Get the ID of the first user created
|
|
||||||
set_fact:
|
|
||||||
user_id: "{{ query('awx.awx.tower_get_id', 'users', query_params={ 'username' : user_creation_results['results'][0]['item'] }) }}"
|
|
||||||
|
|
||||||
- assert:
|
|
||||||
that: "{{ user_id }} == {{ user_creation_results['results'][0]['id'] }}"
|
|
||||||
|
|
||||||
- name: Try to get an ID of someone who does not exist
|
|
||||||
set_fact:
|
|
||||||
failed_user_id: "{{ query('awx.awx.tower_get_id', 'users', query_params={ 'username': 'john jacob jingleheimer schmidt' }) }}"
|
|
||||||
register: results
|
|
||||||
ignore_errors: true
|
|
||||||
|
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- results is failed
|
|
||||||
- "'No objects matched that criteria' in results['msg']"
|
|
||||||
|
|
||||||
- name: Lookup too many users
|
|
||||||
set_fact:
|
|
||||||
too_many_user_ids: " {{ query('awx.awx.tower_get_id', 'users', query_params={ 'username__endswith': test_id }) }}"
|
|
||||||
register: results
|
|
||||||
ignore_errors: true
|
|
||||||
|
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- results is failed
|
|
||||||
- "'An unexpected number of items was returned from the API (3)' in results['msg']"
|
|
||||||
always:
|
|
||||||
- name: Cleanup users
|
|
||||||
tower_user:
|
|
||||||
username: "{{ item }}"
|
|
||||||
state: absent
|
|
||||||
loop: "{{ usernames }}"
|
|
||||||
Reference in New Issue
Block a user