update logic

This commit is contained in:
Sean
2021-03-02 23:55:03 -06:00
parent e05db68bde
commit a0090c7c52
7 changed files with 80 additions and 150 deletions

View File

@@ -398,32 +398,21 @@ def main():
lookup_data = {
'credential_type': cred_type_id,
}
# Create a copy of lookup data for copying without org.
copy_lookup_data = lookup_data
if organization:
lookup_data['organization'] = org_id
credential = module.get_one('credentials', name_or_id=name, **{'data': lookup_data})
# Attempt to look up credential to copy based on the provided name
if copy_from:
# Check if credential exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
credential = module.get_one('credentials', name_or_id=name, **{'data': lookup_data})
if credential is not None:
module.fail_json(msg="A credential with the name {0} already exists.".format(name))
else:
# Lookup existing credential.
copy_lookup_data = {
'credential_type': cred_type_id,
}
copy_from_lookup = module.get_one('credentials', name_or_id=copy_from, **{'data': copy_lookup_data})
if copy_from_lookup is None:
module.fail_json(msg="A credential with the name {0} was not able to be found.".format(copy_from))
else:
# Because the initial copy will keep its organization, this can be different then the specified one.
lookup_data['organization'] = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='credential'
)
credential = module.get_one('credentials', name_or_id=name, **{'data': lookup_data})
# a new existing item is formed when copying and is returned.
credential = module.copy_item(
credential, copy_from, name,
endpoint='credentials', item_type='credential',
copy_lookup_data=copy_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

@@ -126,29 +126,6 @@ def main():
# Attempt to look up the related items the user specified (these will fail the module if not found)
org_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up inventory to copy based on the provided name
if copy_from:
# Check if inventory exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
inventory = module.get_one('inventories', name_or_id=name, **{
'data': {
'organization': org_id
}
})
if inventory is not None:
module.fail_json(msg="A inventory with the name {0} already exists.".format(name))
else:
# Lookup existing inventory.
copy_from_lookup = module.get_one('inventories', name_or_id=copy_from)
if copy_from_lookup is None:
module.fail_json(msg="An inventory with the name {0} was not able to be found.".format(copy_from))
else:
# Because the initial copy will keep its organization, this can be different then the specified one.
org_id = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='inventory'
)
# Attempt to look up inventory based on the provided name and org ID
inventory = module.get_one('inventories', name_or_id=name, **{
'data': {
@@ -156,13 +133,18 @@ def main():
}
})
# Attempt to look up credential to copy based on the provided name
if copy_from:
# a new existing item is formed when copying and is returned.
inventory = module.copy_item(
inventory, copy_from, name,
endpoint='inventories', item_type='inventory',
)
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(inventory)
# Reset Org id to push in case copy_from was used.
org_id = module.resolve_name_to_id('organizations', organization)
# Create the data that gets sent for create and update
inventory_fields = {
'name': module.get_item_name(inventory) if inventory else name,

View File

@@ -437,28 +437,17 @@ def main():
organization_id = module.resolve_name_to_id('organizations', organization)
search_fields['organization'] = new_fields['organization'] = organization_id
# Attempt to look up job template to copy based on the provided name
if copy_from:
# Check if job template exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
job_template = module.get_one('job_templates', name_or_id=name, **{'data': search_fields})
if job_template is not None:
module.fail_json(msg="A job template with the name {0} already exists.".format(name))
else:
# Lookup existing job template.
copy_from_lookup = module.get_one('job_templates', name_or_id=copy_from)
if copy_from_lookup is None:
module.fail_json(msg="A job template with the name {0} was not able to be found.".format(copy_from))
else:
# Because the initial copy will keep its organization, this can be different then the specified one.
search_fields['organization'] = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='job_template'
)
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('job_templates', name_or_id=name, **{'data': search_fields})
# Attempt to look up credential to copy based on the provided name
if copy_from:
# a new existing item is formed when copying and is returned.
existing_item = module.copy_item(
existing_item, copy_from, name,
endpoint='job_templates', item_type='job_template',
)
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)

View File

@@ -395,29 +395,6 @@ def main():
if organization:
organization_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up notification template to copy based on the provided name
if copy_from:
# Check if notification template exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
notification_template = module.get_one('notification_templates', name_or_id=name, **{
'data': {
'organization': organization_id
}
})
if notification_template is not None:
module.fail_json(msg="A notification template with the name {0} already exists.".format(name))
else:
# Lookup existing notification template.
copy_from_lookup = module.get_one('notification_templates', name_or_id=copy_from)
if copy_from_lookup is None:
module.fail_json(msg="An notification template with the name {0} was not able to be found.".format(copy_from))
else:
# Because the initial copy will keep its organization, this can be different then the specified one.
organization_id = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='notification_template'
)
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('notification_templates', name_or_id=name, **{
'data': {
@@ -425,9 +402,13 @@ def main():
}
})
# Reset Org id to push in case copy_from was used.
if organization:
organization_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up credential to copy based on the provided name
if copy_from:
# a new existing item is formed when copying and is returned.
existing_item = module.copy_item(
existing_item, copy_from, name,
endpoint='notification_templates', item_type='notification_template',
)
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

@@ -292,28 +292,17 @@ def main():
org_id = module.resolve_name_to_id('organizations', organization)
lookup_data['organization'] = org_id
# Attempt to look up project to copy based on the provided name
if copy_from:
# Check if project exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
project = module.get_one('projects', name_or_id=name, data=lookup_data)
if project is not None:
module.fail_json(msg="A project with the name {0} already exists.".format(name))
else:
# Lookup existing project.
copy_from_lookup = module.get_one('projects', name_or_id=copy_from)
if copy_from_lookup is None:
module.fail_json(msg="A project with the name {0} was not able to be found.".format(copy_from))
else:
# Because the initial copy will keep its organization, this can be different then the specified one.
lookup_data['organization'] = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='project'
)
# Attempt to look up project based on the provided name and org ID
project = module.get_one('projects', name_or_id=name, data=lookup_data)
# Attempt to look up credential to copy based on the provided name
if copy_from:
# a new existing item is formed when copying and is returned.
project = module.copy_item(
project, copy_from, name,
endpoint='projects', item_type='project',
)
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(project)

View File

@@ -224,37 +224,17 @@ def main():
organization_id = module.resolve_name_to_id('organizations', organization)
search_fields['organization'] = new_fields['organization'] = organization_id
# Attempt to look up workflow job template to copy based on the provided name
if copy_from:
# Check if workflow job template exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
workflow_job_template = module.get_one('workflow_job_templates', name_or_id=name, data=search_fields)
if workflow_job_template is not None:
module.fail_json(msg="A workflow job template with the name {0} already exists.".format(name))
else:
# Lookup existing workflow job template.
copy_from_lookup = module.get_one('workflow_job_templates', name_or_id=copy_from)
if copy_from_lookup is None:
module.fail_json(msg="A workflow job template with the name {0} was not able to be found.".format(copy_from))
else:
workflow_copy_get_check = module.get_endpoint(copy_from_lookup['related']['copy'])
if workflow_copy_get_check['status_code'] in [200]:
if (workflow_copy_get_check['json']['can_copy'] and workflow_copy_get_check['json']['can_copy_without_user_input'] and
not workflow_copy_get_check['json']['templates_unable_to_copy'] and not workflow_copy_get_check['json']['credentials_unable_to_copy']
and not workflow_copy_get_check['json']['inventories_unable_to_copy']):
# Because the initial copy will keep its organization, this can be different then the specified one.
search_fields['organization'] = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='workflow_job_template'
)
else:
module.fail_json(msg="Unable to copy workflow {0} error: {1}".format(copy_from, workflow_copy_get_check))
else:
module.fail_json(msg="Error accessing workflow {0} error: {1} ".format(copy_from, workflow_copy_get_check))
# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('workflow_job_templates', name_or_id=name, **{'data': search_fields})
# Attempt to look up credential to copy based on the provided name
if copy_from:
# a new existing item is formed when copying and is returned.
existing_item = module.copy_item(
existing_item, copy_from, name,
endpoint='workflow_job_templates', item_type='workflow_job_template',
)
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)