mirror of
https://github.com/ansible/awx.git
synced 2026-01-11 10:00:01 -03:30
Merge pull request #8072 from john-westcott-iv/get_one_fix
Modify get_one method to allow IDs in addition to names Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
commit
a3e08a3d09
@ -25,6 +25,11 @@ class TowerAPIModule(TowerModule):
|
||||
}
|
||||
session = None
|
||||
cookie_jar = CookieJar()
|
||||
IDENTITY_FIELDS = {
|
||||
'users': 'username',
|
||||
'workflow_job_template_nodes': 'identifier',
|
||||
'instances': 'hostname'
|
||||
}
|
||||
|
||||
def __init__(self, argument_spec, direct_params=None, error_callback=None, warn_callback=None, **kwargs):
|
||||
kwargs['supports_check_mode'] = True
|
||||
@ -42,6 +47,30 @@ class TowerAPIModule(TowerModule):
|
||||
}
|
||||
return exceptions.get(name, '{0}s'.format(name))
|
||||
|
||||
@staticmethod
|
||||
def get_name_field_from_endpoint(endpoint):
|
||||
return TowerAPIModule.IDENTITY_FIELDS.get(endpoint, 'name')
|
||||
|
||||
def get_item_name(self, item, allow_unknown=False):
|
||||
if item:
|
||||
if 'name' in item:
|
||||
return item['name']
|
||||
|
||||
for field_name in TowerAPIModule.IDENTITY_FIELDS.values():
|
||||
if field_name in item:
|
||||
return item[field_name]
|
||||
|
||||
if item.get('type', None) in ('o_auth2_access_token', 'credential_input_source'):
|
||||
return item['id']
|
||||
|
||||
if allow_unknown:
|
||||
return 'unknown'
|
||||
|
||||
if item:
|
||||
self.exit_json(msg='Cannot determine identity field for {0} object.'.format(item.get('type', 'unknown')))
|
||||
else:
|
||||
self.exit_json(msg='Cannot determine identity field for Undefined object.')
|
||||
|
||||
def head_endpoint(self, endpoint, *args, **kwargs):
|
||||
return self.make_request('HEAD', endpoint, **kwargs)
|
||||
|
||||
@ -88,7 +117,21 @@ class TowerAPIModule(TowerModule):
|
||||
response['json']['next'] = next_page
|
||||
return response
|
||||
|
||||
def get_one(self, endpoint, *args, **kwargs):
|
||||
def get_one(self, endpoint, name_or_id=None, *args, **kwargs):
|
||||
if name_or_id:
|
||||
name_field = self.get_name_field_from_endpoint(endpoint)
|
||||
new_args = kwargs.get('data', {}).copy()
|
||||
if name_field in new_args:
|
||||
self.fail_json(msg="You can't specify the field {0} in your search data if using the name_or_id field".format(name_field))
|
||||
|
||||
new_args['or__{0}'.format(name_field)] = name_or_id
|
||||
try:
|
||||
new_args['or__id'] = int(name_or_id)
|
||||
except ValueError:
|
||||
# If we get a value error, then we didn't have an integer so we can just pass and fall down to the fail
|
||||
pass
|
||||
kwargs['data'] = new_args
|
||||
|
||||
response = self.get_endpoint(endpoint, *args, **kwargs)
|
||||
if response['status_code'] != 200:
|
||||
fail_msg = "Got a {0} response when trying to get one from {1}".format(response['status_code'], endpoint)
|
||||
@ -102,16 +145,19 @@ class TowerAPIModule(TowerModule):
|
||||
if response['json']['count'] == 0:
|
||||
return None
|
||||
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
|
||||
# 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]
|
||||
|
||||
def get_one_by_name_or_id(self, endpoint, name_or_id):
|
||||
name_field = 'name'
|
||||
if endpoint == 'users':
|
||||
name_field = 'username'
|
||||
elif endpoint == 'instances':
|
||||
name_field = 'hostname'
|
||||
name_field = self.get_name_field_from_endpoint(endpoint)
|
||||
|
||||
query_params = {'or__{0}'.format(name_field): name_or_id}
|
||||
try:
|
||||
@ -319,24 +365,10 @@ class TowerAPIModule(TowerModule):
|
||||
item_url = existing_item['url']
|
||||
item_type = existing_item['type']
|
||||
item_id = existing_item['id']
|
||||
item_name = self.get_item_name(existing_item, allow_unknown=True)
|
||||
except KeyError as ke:
|
||||
self.fail_json(msg="Unable to process delete of item due to missing data {0}".format(ke))
|
||||
|
||||
if 'name' in existing_item:
|
||||
item_name = existing_item['name']
|
||||
elif 'username' in existing_item:
|
||||
item_name = existing_item['username']
|
||||
elif 'identifier' in existing_item:
|
||||
item_name = existing_item['identifier']
|
||||
elif item_type == 'o_auth2_access_token':
|
||||
# An oauth2 token has no name, instead we will use its id for any of the messages
|
||||
item_name = existing_item['id']
|
||||
elif item_type == 'credential_input_source':
|
||||
# An credential_input_source has no name, instead we will use its id for any of the messages
|
||||
item_name = existing_item['id']
|
||||
else:
|
||||
self.fail_json(msg="Unable to process delete of {0} due to missing name".format(item_type))
|
||||
|
||||
response = self.delete_endpoint(item_url)
|
||||
|
||||
if response['status_code'] in [202, 204]:
|
||||
@ -409,12 +441,7 @@ class TowerAPIModule(TowerModule):
|
||||
|
||||
# We have to rely on item_type being passed in since we don't have an existing item that declares its type
|
||||
# We will pull the item_name out from the new_item, if it exists
|
||||
for key in ('name', 'username', 'identifier', 'hostname'):
|
||||
if key in new_item:
|
||||
item_name = new_item[key]
|
||||
break
|
||||
else:
|
||||
item_name = 'unknown'
|
||||
item_name = self.get_item_name(new_item, allow_unknown=True)
|
||||
|
||||
response = self.post_endpoint(endpoint, **{'data': new_item})
|
||||
if response['status_code'] == 201:
|
||||
|
||||
@ -365,13 +365,12 @@ def main():
|
||||
|
||||
# Attempt to look up the object based on the provided name, credential type and optional organization
|
||||
lookup_data = {
|
||||
'name': name,
|
||||
'credential_type': cred_type_id,
|
||||
}
|
||||
if organization:
|
||||
lookup_data['organization'] = org_id
|
||||
|
||||
credential = module.get_one('credentials', **{'data': lookup_data})
|
||||
credential = 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
|
||||
@ -397,7 +396,7 @@ def main():
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
credential_fields = {
|
||||
'name': new_name if new_name else name,
|
||||
'name': new_name if new_name else (module.get_item_name(credential) if credential else name),
|
||||
'credential_type': cred_type_id,
|
||||
}
|
||||
if has_inputs:
|
||||
|
||||
@ -115,7 +115,6 @@ def main():
|
||||
|
||||
# These will be passed into the create/updates
|
||||
credential_type_params = {
|
||||
'name': new_name if new_name else name,
|
||||
'managed_by_tower': False,
|
||||
}
|
||||
if kind:
|
||||
@ -128,16 +127,14 @@ 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 = 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
|
||||
module.delete_if_needed(credential_type)
|
||||
|
||||
credential_type_params['name'] = new_name if new_name else (module.get_item_name(credential_type) if credential_type else name)
|
||||
|
||||
# If the state was present and we can let the module build or update the existing credential type, this will return on its own
|
||||
module.create_or_update_if_needed(credential_type, credential_type_params, endpoint='credential_types', item_type='credential type')
|
||||
|
||||
|
||||
@ -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 = module.get_one('groups', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'inventory': inventory_id
|
||||
}
|
||||
})
|
||||
@ -121,7 +120,7 @@ def main():
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
group_fields = {
|
||||
'name': new_name if new_name else name,
|
||||
'name': new_name if new_name else (module.get_item_name(group) if group else name),
|
||||
'inventory': inventory_id,
|
||||
}
|
||||
if description is not None:
|
||||
@ -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 = 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))
|
||||
|
||||
@ -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 = module.get_one('hosts', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'inventory': inventory_id
|
||||
}
|
||||
})
|
||||
@ -117,7 +116,7 @@ def main():
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
host_fields = {
|
||||
'name': new_name if new_name else name,
|
||||
'name': new_name if new_name else (module.get_item_name(host) if host else name),
|
||||
'inventory': inventory_id,
|
||||
'enabled': enabled,
|
||||
}
|
||||
|
||||
@ -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 = 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
|
||||
@ -131,7 +127,7 @@ def main():
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
new_fields = {}
|
||||
new_fields['name'] = new_name if new_name else name
|
||||
new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name)
|
||||
if credential is not None:
|
||||
new_fields['credential'] = credential_id
|
||||
if policy_instance_percentage is not None:
|
||||
|
||||
@ -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 = module.get_one('inventories', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'organization': org_id
|
||||
}
|
||||
})
|
||||
@ -122,7 +121,7 @@ def main():
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
inventory_fields = {
|
||||
'name': name,
|
||||
'name': module.get_item_name(inventory) if inventory else name,
|
||||
'organization': org_id,
|
||||
'kind': kind,
|
||||
'host_filter': host_filter,
|
||||
|
||||
@ -202,17 +202,16 @@ def main():
|
||||
source_project = module.params.get('source_project')
|
||||
state = module.params.get('state')
|
||||
|
||||
lookup_data = {'name': inventory}
|
||||
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'],
|
||||
}
|
||||
})
|
||||
|
||||
@ -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 = 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))
|
||||
|
||||
@ -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,14 +419,14 @@ 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 = 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
|
||||
module.delete_if_needed(existing_item)
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
new_fields['name'] = new_name if new_name else name
|
||||
new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name)
|
||||
for field_name in (
|
||||
'description', 'job_type', 'playbook', 'scm_branch', 'forks', 'limit', 'verbosity',
|
||||
'job_tags', 'force_handlers', 'skip_tags', 'start_at_task', 'timeout', 'use_fact_cache',
|
||||
@ -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 = module.get_one('projects', name_or_id=project, **{
|
||||
'data': {
|
||||
'name': project,
|
||||
'organization': organization_id,
|
||||
}
|
||||
})
|
||||
|
||||
@ -80,16 +80,15 @@ 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 = module.get_one('labels', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'organization': organization_id,
|
||||
}
|
||||
})
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
new_fields = {}
|
||||
new_fields['name'] = new_name if new_name else name
|
||||
new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name)
|
||||
if organization:
|
||||
new_fields['organization'] = organization_id
|
||||
|
||||
|
||||
@ -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 = module.get_one('notification_templates', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'organization': organization_id,
|
||||
}
|
||||
})
|
||||
@ -404,7 +403,7 @@ def main():
|
||||
new_fields = {}
|
||||
if final_notification_configuration:
|
||||
new_fields['notification_configuration'] = final_notification_configuration
|
||||
new_fields['name'] = new_name if new_name else name
|
||||
new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name)
|
||||
if description is not None:
|
||||
new_fields['description'] = description
|
||||
if organization is not None:
|
||||
|
||||
@ -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 = 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
|
||||
@ -154,7 +150,7 @@ def main():
|
||||
association_fields['notification_templates_approvals'].append(module.resolve_name_to_id('notification_templates', item))
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
org_fields = {'name': name}
|
||||
org_fields = {'name': module.get_item_name(organization) if organization else name}
|
||||
if description is not None:
|
||||
org_fields['description'] = description
|
||||
if custom_virtualenv is not None:
|
||||
|
||||
@ -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 = module.get_one('projects', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'organization': org_id
|
||||
}
|
||||
})
|
||||
@ -273,7 +272,7 @@ def main():
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
project_fields = {
|
||||
'name': name,
|
||||
'name': module.get_item_name(project) if project else name,
|
||||
'scm_type': scm_type,
|
||||
'scm_url': scm_url,
|
||||
'scm_branch': scm_branch,
|
||||
|
||||
@ -103,18 +103,12 @@ def main():
|
||||
timeout = module.params.get('timeout')
|
||||
|
||||
# Attempt to look up project based on the provided name or id
|
||||
if name.isdigit():
|
||||
results = module.get_endpoint('projects', **{'data': {'id': name}})
|
||||
if results['json']['count'] == 0:
|
||||
module.fail_json(msg='Could not find Project with ID: {0}'.format(name))
|
||||
project = results['json']['results'][0]
|
||||
else:
|
||||
lookup_data = {'name': name}
|
||||
if organization:
|
||||
lookup_data['organization'] = module.resolve_name_to_id('organizations', organization)
|
||||
project = module.get_one('projects', data=lookup_data)
|
||||
if project is None:
|
||||
module.fail_json(msg="Unable to find project")
|
||||
lookup_data = {}
|
||||
if organization:
|
||||
lookup_data['organization'] = module.resolve_name_to_id('organizations', organization)
|
||||
project = module.get_one('projects', name_or_id=name, data=lookup_data)
|
||||
if project is None:
|
||||
module.fail_json(msg="Unable to find project")
|
||||
|
||||
# Update the project
|
||||
result = module.post_endpoint(project['related']['update'])
|
||||
@ -138,7 +132,7 @@ def main():
|
||||
# Invoke wait function
|
||||
module.wait_on_url(
|
||||
url=result['json']['url'],
|
||||
object_name=name,
|
||||
object_name=module.get_item_name(project),
|
||||
object_type='Project Update',
|
||||
timeout=timeout, interval=interval
|
||||
)
|
||||
|
||||
@ -190,17 +190,13 @@ 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 = module.get_one('schedules', name_or_id=name)
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
new_fields = {}
|
||||
if rrule is not None:
|
||||
new_fields['rrule'] = rrule
|
||||
new_fields['name'] = new_name if new_name else name
|
||||
new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name)
|
||||
if description is not None:
|
||||
new_fields['description'] = description
|
||||
if extra_data is not None:
|
||||
|
||||
@ -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 = module.get_one('teams', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'organization': org_id
|
||||
}
|
||||
})
|
||||
@ -100,7 +99,7 @@ def main():
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
team_fields = {
|
||||
'name': new_name if new_name else name,
|
||||
'name': new_name if new_name else (module.get_item_name(team) if team else name),
|
||||
'organization': org_id
|
||||
}
|
||||
if description is not None:
|
||||
|
||||
@ -134,11 +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', **{
|
||||
'data': {
|
||||
'username': username,
|
||||
}
|
||||
})
|
||||
existing_item = 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
|
||||
@ -147,7 +143,7 @@ def main():
|
||||
# Create the data that gets sent for create and update
|
||||
new_fields = {}
|
||||
if username:
|
||||
new_fields['username'] = username
|
||||
new_fields['username'] = module.get_item_name(existing_item) if existing_item else username
|
||||
if first_name:
|
||||
new_fields['first_name'] = first_name
|
||||
if last_name:
|
||||
|
||||
@ -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 = 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
|
||||
@ -208,7 +208,7 @@ def main():
|
||||
new_fields['webhook_credential'] = module.resolve_name_to_id('webhook_credential', webhook_credential)
|
||||
|
||||
# Create the data that gets sent for create and update
|
||||
new_fields['name'] = new_name if new_name else name
|
||||
new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name)
|
||||
for field_name in (
|
||||
'description', 'survey_enabled', 'allow_simultaneous',
|
||||
'limit', 'scm_branch', 'extra_vars',
|
||||
|
||||
@ -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 = 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
|
||||
|
||||
@ -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 = 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")
|
||||
|
||||
@ -82,9 +82,9 @@
|
||||
that:
|
||||
- "result is changed"
|
||||
|
||||
- name: Re-create the Org-specific credential (new school)
|
||||
- name: Re-create the Org-specific credential (new school) with an ID
|
||||
tower_credential:
|
||||
name: "{{ ssh_cred_name1 }}"
|
||||
name: "{{ result.id }}"
|
||||
organization: Default
|
||||
credential_type: 'Machine'
|
||||
state: present
|
||||
|
||||
@ -1,105 +1,113 @@
|
||||
---
|
||||
- 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 names
|
||||
set_fact:
|
||||
src_cred_name: src_cred
|
||||
target_cred_name: target_cred
|
||||
src_cred_name: "AWX-Collection-tests-tower_credential_input_source-src_cred-{{ test_id }}"
|
||||
target_cred_name: "AWX-Collection-tests-tower_credential_input_source-target_cred-{{ test_id }}"
|
||||
|
||||
- name: Add Tower credential Lookup
|
||||
tower_credential:
|
||||
description: Credential for Testing Source
|
||||
name: "{{ src_cred_name }}"
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
inputs:
|
||||
url: "https://cyberark.example.com"
|
||||
app_id: "My-App-ID"
|
||||
organization: Default
|
||||
register: result
|
||||
- block:
|
||||
- name: Add Tower credential Lookup
|
||||
tower_credential:
|
||||
description: Credential for Testing Source
|
||||
name: "{{ src_cred_name }}"
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
inputs:
|
||||
url: "https://cyberark.example.com"
|
||||
app_id: "My-App-ID"
|
||||
organization: Default
|
||||
register: src_cred_result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
- assert:
|
||||
that:
|
||||
- "src_cred_result is changed"
|
||||
|
||||
- name: Add Tower credential Target
|
||||
tower_credential:
|
||||
description: Credential for Testing Target
|
||||
name: "{{ target_cred_name }}"
|
||||
credential_type: Machine
|
||||
inputs:
|
||||
username: user
|
||||
organization: Default
|
||||
register: result
|
||||
- name: Add Tower credential Target
|
||||
tower_credential:
|
||||
description: Credential for Testing Target
|
||||
name: "{{ target_cred_name }}"
|
||||
credential_type: Machine
|
||||
inputs:
|
||||
username: user
|
||||
organization: Default
|
||||
register: target_cred_result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
- assert:
|
||||
that:
|
||||
- "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 }}"
|
||||
metadata:
|
||||
object_query: "Safe=MY_SAFE;Object=AWX-user"
|
||||
object_query_format: "Exact"
|
||||
state: present
|
||||
- name: Add credential Input Source
|
||||
tower_credential_input_source:
|
||||
input_field_name: password
|
||||
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"
|
||||
state: present
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
|
||||
- name: Add Second Tower credential Lookup
|
||||
tower_credential:
|
||||
description: Credential for Testing Source Change
|
||||
name: "{{ src_cred_name }}-2"
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
inputs:
|
||||
url: "https://cyberark-prod.example.com"
|
||||
app_id: "My-App-ID"
|
||||
organization: Default
|
||||
register: result
|
||||
- name: Add Second Tower credential Lookup
|
||||
tower_credential:
|
||||
description: Credential for Testing Source Change
|
||||
name: "{{ src_cred_name }}-2"
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
inputs:
|
||||
url: "https://cyberark-prod.example.com"
|
||||
app_id: "My-App-ID"
|
||||
organization: Default
|
||||
register: result
|
||||
|
||||
- name: Change credential Input Source
|
||||
tower_credential_input_source:
|
||||
input_field_name: password
|
||||
target_credential: "{{ target_cred_name }}"
|
||||
source_credential: "{{ src_cred_name }}-2"
|
||||
state: present
|
||||
- name: Change credential Input Source
|
||||
tower_credential_input_source:
|
||||
input_field_name: password
|
||||
target_credential: "{{ target_cred_name }}"
|
||||
source_credential: "{{ src_cred_name }}-2"
|
||||
state: present
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
|
||||
- name: Remove a Tower credential source
|
||||
tower_credential_input_source:
|
||||
input_field_name: password
|
||||
target_credential: "{{ target_cred_name }}"
|
||||
state: absent
|
||||
register: result
|
||||
always:
|
||||
- name: Remove a Tower credential source
|
||||
tower_credential_input_source:
|
||||
input_field_name: password
|
||||
target_credential: "{{ target_cred_name }}"
|
||||
state: absent
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
- assert:
|
||||
that:
|
||||
- "result is changed"
|
||||
|
||||
- name: Remove Tower credential Lookup
|
||||
tower_credential:
|
||||
name: "{{ src_cred_name }}"
|
||||
organization: Default
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
state: absent
|
||||
register: result
|
||||
- name: Remove Tower credential Lookup
|
||||
tower_credential:
|
||||
name: "{{ src_cred_name }}"
|
||||
organization: Default
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
state: absent
|
||||
register: result
|
||||
|
||||
- name: Remove Alt Tower credential Lookup
|
||||
tower_credential:
|
||||
name: "{{ src_cred_name }}-2"
|
||||
organization: Default
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
state: absent
|
||||
register: result
|
||||
- name: Remove Alt Tower credential Lookup
|
||||
tower_credential:
|
||||
name: "{{ src_cred_name }}-2"
|
||||
organization: Default
|
||||
credential_type: CyberArk AIM Central Credential Provider Lookup
|
||||
state: absent
|
||||
register: result
|
||||
|
||||
- name: Remove Tower credential
|
||||
tower_credential:
|
||||
name: "{{ target_cred_name }}"
|
||||
organization: Default
|
||||
credential_type: Machine
|
||||
state: absent
|
||||
register: result
|
||||
- name: Remove Tower credential
|
||||
tower_credential:
|
||||
name: "{{ target_cred_name }}"
|
||||
organization: Default
|
||||
credential_type: Machine
|
||||
state: absent
|
||||
register: result
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
|
||||
- name: Remove a Tower credential type
|
||||
tower_credential_type:
|
||||
name: "{{ cred_type_name }}"
|
||||
name: "{{ result.id }}"
|
||||
state: absent
|
||||
register: result
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -29,7 +29,7 @@
|
||||
tower_inventory:
|
||||
name: "{{ inv_name1 }}"
|
||||
organization: Default
|
||||
insights_credential: "{{ cred_name1 }}"
|
||||
insights_credential: "{{ result.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
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -15,9 +15,9 @@
|
||||
that:
|
||||
- "result is changed"
|
||||
|
||||
- name: Change a User
|
||||
- name: Change a User by ID
|
||||
tower_user:
|
||||
username: "{{ username }}"
|
||||
username: "{{ result.id }}"
|
||||
last_name: User
|
||||
email: joe@example.org
|
||||
state: present
|
||||
|
||||
@ -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 = 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 %}
|
||||
@ -195,7 +194,7 @@ def main():
|
||||
new_fields = {}
|
||||
{% for option in item['json']['actions']['POST'] %}
|
||||
{% if option == name_option %}
|
||||
new_fields['{{ name_option }}'] = new_{{ name_option }} if new_{{ name_option }} else {{ name_option }}
|
||||
new_fields['{{ name_option }}'] = new_{{ name_option }} if new_{{ name_option }} else (module.get_item_name(existing_item) if existing_item else {{ name_option }}t)
|
||||
{% else %}
|
||||
if {{ option }} is not None:
|
||||
{% if item['json']['actions']['POST'][option]['type'] == 'id' %}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user