1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-30 05:25:29 +03:00

get_one now also returns the name field, and modifying modules for get_one and added in some IDs in a handful of unit tests

This commit is contained in:
John Westcott IV 2020-09-04 15:41:36 -04:00
parent 4c4d6dad49
commit 106157c600
34 changed files with 83 additions and 99 deletions

View File

@ -141,18 +141,18 @@ class TowerAPIModule(TowerModule):
self.fail_json(msg="The endpoint did not provide count and results")
if response['json']['count'] == 0:
return None
return None, name_or_id
elif response['json']['count'] > 1:
if name_or_id:
# Since we did a name or ID search and got > 1 return something if the id matches
for asset in response['json']['results']:
if asset['id'] == name_or_id:
return asset
return asset, asset['id'][name_field]
# We got > 1 and either didn't find something by ID (which means multiple names)
# Or we weren't running with a or search and just got back too many to begin with.
self.fail_json(msg="An unexpected number of items was returned from the API ({0})".format(response['json']['count']))
return response['json']['results'][0]
return response['json']['results'][0], response['json']['results'][0][name_field]
def get_one_by_name_or_id(self, endpoint, name_or_id):
name_field = self.get_name_field_from_endpoint(endpoint)

View File

@ -370,9 +370,7 @@ def main():
if organization:
lookup_data['organization'] = org_id
credential = module.get_one('credentials', name_or_id=name, **{'data': lookup_data})
# If we got an item back make sure the name field reflects the actual name (incase we were passed an ID)
name = credential['name'] if (credential) else name
credential, name = module.get_one('credentials', name_or_id=name, **{'data': lookup_data})
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this

View File

@ -102,7 +102,7 @@ def main():
'target_credential': target_credential_id,
'input_field_name': input_field_name,
}
credential_input_source = module.get_one('credential_input_sources', **{'data': lookup_data})
credential_input_source, junk = module.get_one('credential_input_sources', **{'data': lookup_data})
if state == 'absent':
module.delete_if_needed(credential_input_source)

View File

@ -128,11 +128,7 @@ def main():
credential_type_params['injectors'] = module.params.get('injectors')
# Attempt to look up credential_type based on the provided name
credential_type = module.get_one('credential_types', **{
'data': {
'name': name,
}
})
credential_type, name = module.get_one('credential_types', name_or_id=name)
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this

View File

@ -108,9 +108,8 @@ def main():
inventory_id = module.resolve_name_to_id('inventories', inventory)
# Attempt to look up the object based on the provided name and inventory ID
group = module.get_one('groups', **{
group, name = module.get_one('groups', name_or_id=name, **{
'data': {
'name': name,
'inventory': inventory_id
}
})
@ -136,8 +135,8 @@ def main():
continue
id_list = []
for sub_name in name_list:
sub_obj = module.get_one(resource, **{
'data': {'inventory': inventory_id, 'name': sub_name}
sub_obj, sub_name = module.get_one(resource, name_or_id=sub_name, **{
'data': {'inventory': inventory_id},
})
if sub_obj is None:
module.fail_json(msg='Could not find {0} with name {1}'.format(resource, sub_name))

View File

@ -104,9 +104,8 @@ def main():
inventory_id = module.resolve_name_to_id('inventories', inventory)
# Attempt to look up host based on the provided name and inventory ID
host = module.get_one('hosts', **{
host, name = module.get_one('hosts', name_or_id=name, **{
'data': {
'name': name,
'inventory': inventory_id
}
})

View File

@ -109,11 +109,7 @@ def main():
state = module.params.get('state')
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('instance_groups', **{
'data': {
'name': name,
}
})
existing_item, name = module.get_one('instance_groups', name_or_id=name)
if state is 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this

View File

@ -109,9 +109,8 @@ def main():
org_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up inventory based on the provided name and org ID
inventory = module.get_one('inventories', **{
inventory, name = module.get_one('inventories', name_or_id=name, **{
'data': {
'name': name,
'organization': org_id
}
})

View File

@ -202,17 +202,17 @@ def main():
source_project = module.params.get('source_project')
state = module.params.get('state')
lookup_data = {'name': inventory}
<<<<<<< HEAD
lookup_data = {}
if organization:
lookup_data['organization'] = module.resolve_name_to_id('organizations', organization)
inventory_object = module.get_one('inventories', data=lookup_data)
inventory_object = module.get_one('inventories', name_or_id=inventory, data=lookup_data)
if not inventory_object:
module.fail_json(msg='The specified inventory, {0}, was not found.'.format(lookup_data))
inventory_source_object = module.get_one('inventory_sources', **{
inventory_source_object = module.get_one('inventory_sources', name_or_id=name, **{
'data': {
'name': name,
'inventory': inventory_object['id'],
}
})

View File

@ -68,7 +68,7 @@ def main():
fail_if_not_running = module.params.get('fail_if_not_running')
# Attempt to look up the job based on the provided name
job = module.get_one('jobs', **{
job, job_name = module.get_one('jobs', **{
'data': {
'id': job_id,
}

View File

@ -201,11 +201,7 @@ def main():
post_data['credentials'].append(module.resolve_name_to_id('credentials', credential))
# Attempt to look up job_template based on the provided name
job_template = module.get_one('job_templates', **{
'data': {
'name': name,
}
})
job_template, name = module.get_one('job_templates', name_or_id=name)
if job_template is None:
module.fail_json(msg="Unable to find job template by name {0}".format(name))

View File

@ -409,7 +409,7 @@ def main():
credentials.append(credential)
new_fields = {}
search_fields = {'name': name}
search_fields = {}
# Attempt to look up the related items the user specified (these will fail the module if not found)
organization_id = None
@ -419,7 +419,7 @@ def main():
search_fields['organization'] = new_fields['organization'] = organization_id
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('job_templates', **{'data': search_fields})
existing_item, name = module.get_one('job_templates', name_or_id=name, **{'data': search_fields})
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
@ -453,9 +453,8 @@ def main():
new_fields['inventory'] = module.resolve_name_to_id('inventories', inventory)
if project is not None:
if organization_id is not None:
project_data = module.get_one('projects', **{
project_data, project = module.get_one('projects', name_or_id=project, **{
'data': {
'name': project,
'organization': organization_id,
}
})

View File

@ -130,7 +130,7 @@ def main():
)
# Attempt to look up job based on the provided id
job = module.get_one('jobs', **{
job, junk = module.get_one('jobs', **{
'data': {
'id': job_id,
}

View File

@ -80,9 +80,8 @@ def main():
organization_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('labels', **{
existing_item, name = module.get_one('labels', name_or_id=name, **{
'data': {
'name': name,
'organization': organization_id,
}
})

View File

@ -380,9 +380,8 @@ def main():
organization_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('notification_templates', **{
existing_item, name = module.get_one('notification_templates', name_or_id=name, **{
'data': {
'name': name,
'organization': organization_id,
}
})

View File

@ -117,11 +117,7 @@ def main():
state = module.params.get('state')
# Attempt to look up organization based on the provided name
organization = module.get_one('organizations', **{
'data': {
'name': name,
}
})
organization, name = module.get_one('organizations', name_or_id=name)
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this

View File

@ -239,9 +239,8 @@ def main():
credential = module.resolve_name_to_id('credentials', credential)
# Attempt to look up project based on the provided name and org ID
project = module.get_one('projects', **{
project, name = module.get_one('projects', name_or_id=name, **{
'data': {
'name': name,
'organization': org_id
}
})

View File

@ -109,10 +109,10 @@ def main():
module.fail_json(msg='Could not find Project with ID: {0}'.format(name))
project = results['json']['results'][0]
else:
lookup_data = {'name': name}
lookup_data = {}
if organization:
lookup_data['organization'] = module.resolve_name_to_id('organizations', organization)
project = module.get_one('projects', data=lookup_data)
project, name = module.get_one('projects', name_or_id=name, data=lookup_data)
if project is None:
module.fail_json(msg="Unable to find project")

View File

@ -190,11 +190,7 @@ def main():
unified_job_template_id = module.resolve_name_to_id('unified_job_templates', unified_job_template)
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('schedules', **{
'data': {
'name': name,
}
})
existing_item, name = module.get_one('schedules', name_or_id=name)
# Create the data that gets sent for create and update
new_fields = {}

View File

@ -87,9 +87,8 @@ def main():
org_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up team based on the provided name and org ID
team = module.get_one('teams', **{
team, name = module.get_one('teams', name_or_id=name, **{
'data': {
'name': name,
'organization': org_id
}
})

View File

@ -164,7 +164,7 @@ def main():
if state == 'absent':
if not existing_token:
existing_token = module.get_one('tokens', **{
existing_token, token_name = module.get_one('tokens', **{
'data': {
'id': existing_token_id,
}

View File

@ -134,9 +134,7 @@ def main():
# Attempt to look up the related items the user specified (these will fail the module if not found)
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('users', name_or_id=username)
# If we got an item back make sure the name field reflects the actual name (incase we were passed an ID)
username = existing_item['username'] if (existing_item) else username
existing_item, username = module.get_one('users', name_or_id=username)
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this

View File

@ -184,7 +184,7 @@ def main():
state = module.params.get('state')
new_fields = {}
search_fields = {'name': name}
search_fields = {}
# Attempt to look up the related items the user specified (these will fail the module if not found)
organization = module.params.get('organization')
@ -193,7 +193,7 @@ def main():
search_fields['organization'] = new_fields['organization'] = organization_id
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('workflow_job_templates', **{'data': search_fields})
existing_item, name = module.get_one('workflow_job_templates', name_or_id=name, **{'data': search_fields})
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this

View File

@ -198,12 +198,14 @@ def main():
workflow_job_template = module.params.get('workflow_job_template')
workflow_job_template_id = None
if workflow_job_template:
wfjt_search_fields = {'name': workflow_job_template}
wfjt_search_fields = {}
organization = module.params.get('organization')
if organization:
organization_id = module.resolve_name_to_id('organizations', organization)
wfjt_search_fields['organization'] = organization_id
wfjt_data = module.get_one('workflow_job_templates', **{'data': wfjt_search_fields})
wfjt_data, workflow_job_template = module.get_one('workflow_job_templates', name_or_id=workflow_job_template, **{
'data': wfjt_search_fields
})
if wfjt_data is None:
module.fail_json(msg="The workflow {0} in organization {1} was not found on the Tower server".format(
workflow_job_template, organization
@ -212,7 +214,7 @@ def main():
search_fields['workflow_job_template'] = new_fields['workflow_job_template'] = workflow_job_template_id
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('workflow_job_template_nodes', **{'data': search_fields})
existing_item, junk = module.get_one('workflow_job_template_nodes', **{'data': search_fields})
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
@ -249,7 +251,7 @@ def main():
lookup_data = {'identifier': sub_name}
if workflow_job_template_id:
lookup_data['workflow_job_template'] = workflow_job_template_id
sub_obj = module.get_one(endpoint, **{'data': lookup_data})
sub_obj, junk = module.get_one(endpoint, **{'data': lookup_data})
if sub_obj is None:
module.fail_json(msg='Could not find {0} entry with name {1}'.format(association, sub_name))
id_list.append(sub_obj['id'])

View File

@ -138,10 +138,10 @@ def main():
post_data['inventory'] = module.resolve_name_to_id('inventories', inventory)
# Attempt to look up job_template based on the provided name
lookup_data = {'name': name}
lookup_data = {}
if organization:
lookup_data['organization'] = module.resolve_name_to_id('organizations', organization)
workflow_job_template = module.get_one('workflow_job_templates', data=lookup_data)
workflow_job_template, name = module.get_one('workflow_job_templates', name_or_id=name, data=lookup_data)
if workflow_job_template is None:
module.fail_json(msg="Unable to find workflow job template")

View File

@ -13,11 +13,11 @@
url: "https://cyberark.example.com"
app_id: "My-App-ID"
organization: Default
register: result
register: src_cred_result
- assert:
that:
- "result is changed"
- "src_cred_result is changed"
- name: Add Tower credential Target
tower_credential:
@ -27,17 +27,17 @@
inputs:
username: user
organization: Default
register: result
register: target_cred_result
- assert:
that:
- "result is changed"
- "target_cred_result is changed"
- name: Add credential Input Source
tower_credential_input_source:
input_field_name: password
target_credential: "{{ target_cred_name }}"
source_credential: "{{ src_cred_name }}"
target_credential: "{{ target_cred_result.id }}"
source_credential: "{{ src_cred_result.id }}"
metadata:
object_query: "Safe=MY_SAFE;Object=AWX-user"
object_query_format: "Exact"

View File

@ -18,7 +18,7 @@
- name: Remove a Tower credential type
tower_credential_type:
name: "{{ cred_type_name }}"
name: "{{ result.id }}"
state: absent
register: result

View File

@ -14,11 +14,12 @@
name: "{{ inv_name }}"
organization: Default
state: present
register: result
- name: Create a Group
tower_group:
name: "{{ group_name1 }}"
inventory: "{{ inv_name }}"
inventory: "{{ result.id }}"
state: present
variables:
foo: bar
@ -30,7 +31,7 @@
- name: Delete a Group
tower_group:
name: "{{ group_name1 }}"
name: "{{ result.id }}"
inventory: "{{ inv_name }}"
state: absent
register: result

View File

@ -9,11 +9,12 @@
name: "{{ inv_name }}"
organization: Default
state: present
register: result
- name: Create a Host
tower_host:
name: "{{ host_name }}"
inventory: "{{ inv_name }}"
inventory: "{{ result.id }}"
state: present
variables:
foo: bar
@ -25,7 +26,7 @@
- name: Delete a Host
tower_host:
name: "{{ host_name }}"
name: "{{ result.id }}"
inventory: "{{ inv_name }}"
state: absent
register: result

View File

@ -19,11 +19,11 @@
host: "https://openshift.org"
bearer_token: "asdf1234"
verify_ssl: false
register: result
register: cred_result
- assert:
that:
- "result is changed"
- "cred_result is changed"
- name: Create an Instance Group
tower_instance_group:
@ -37,10 +37,22 @@
that:
- "result is changed"
- name: Update an Instance Group
tower_instance_group:
name: "{{ result.id }}"
policy_instance_percentage: 34
policy_instance_minimum: 24
state: present
register: result
- assert:
that:
- "result is changed"
- name: Create a container group
tower_instance_group:
name: "{{ group_name2 }}"
credential: "{{ cred_name1 }}"
credential: "{{ cred_result.id }}"
register: result
- assert:

View File

@ -29,7 +29,7 @@
tower_inventory:
name: "{{ inv_name1 }}"
organization: Default
insights_credential: "{{ cred_name1 }}"
insights_credential: "{{ results.id }}"
state: present
register: result
@ -39,7 +39,7 @@
- name: Test Inventory module idempotency
tower_inventory:
name: "{{ inv_name1 }}"
name: "{{ result.id }}"
organization: Default
insights_credential: "{{ cred_name1 }}"
state: present

View File

@ -16,6 +16,7 @@
host: https://example.org:5000
password: passw0rd
domain: test
register: credential_result
- name: Add a Tower inventory
tower_inventory:
@ -28,7 +29,7 @@
name: "{{ openstack_inv_source }}"
description: Source for Test inventory
inventory: "{{ openstack_inv }}"
credential: "{{ openstack_cred }}"
credential: "{{ credential_result.id }}"
overwrite: true
update_on_launch: true
source_vars:
@ -42,7 +43,7 @@
- name: Delete the inventory source with an invalid cred, source_project, sourece_script specified
tower_inventory_source:
name: "{{ openstack_inv_source }}"
name: "{{ result.id }}"
inventory: "{{ openstack_inv }}"
credential: "Does Not Exit"
source_project: "Does Not Exist"

View File

@ -22,14 +22,14 @@
state: present
scm_type: git
scm_url: https://github.com/ansible/ansible-tower-samples.git
register: result
register: proj_result
- name: Create Credential1
tower_credential:
name: "{{ cred1 }}"
organization: Default
kind: tower
register: cred1_result
- name: Create Credential2
tower_credential:
@ -84,19 +84,19 @@
credentials: ["{{ cred1 }}", "{{ cred2 }}"]
job_type: run
state: present
register: result
register: jt1_result
- assert:
that:
- "result is changed"
- "jt1_result is changed"
- name: Add a credential to this JT
tower_job_template:
name: "{{ jt1 }}"
project: "{{ proj1 }}"
project: "{{ proj_result.id }}"
playbook: hello_world.yml
credentials:
- "{{ cred1 }}"
- "{{ cred1_result.id }}"
register: result
- assert:
@ -105,7 +105,7 @@
- name: Try to add the same credential to this JT
tower_job_template:
name: "{{ jt1 }}"
name: "{{ jt1_result.id }}"
project: "{{ proj1 }}"
playbook: hello_world.yml
credentials:

View File

@ -175,9 +175,8 @@ def main():
{% endfor %}
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('{{ item_type }}', **{
existing_item, name = module.get_one('{{ item_type }}', name_or_id={{ name_option }}, **{
'data': {
'{{ name_option }}': {{ name_option }},
{% if 'organization' in item['json']['actions']['POST'] and item['json']['actions']['POST']['organization']['type'] == 'id' %}
'organization': org_id,
{% endif %}