mirror of
https://github.com/ansible/awx.git
synced 2026-02-01 17:48:10 -03:30
@@ -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,
|
||||
|
||||
@@ -128,6 +128,10 @@ options:
|
||||
- list of notifications to send on error
|
||||
type: list
|
||||
elements: str
|
||||
organization:
|
||||
description:
|
||||
- Name of the inventory source's inventory's organization.
|
||||
type: str
|
||||
extends_documentation_fragment: awx.awx.auth
|
||||
'''
|
||||
|
||||
@@ -140,6 +144,7 @@ EXAMPLES = '''
|
||||
credential: previously-created-credential
|
||||
overwrite: True
|
||||
update_on_launch: True
|
||||
organization: Default
|
||||
source_vars:
|
||||
private: false
|
||||
'''
|
||||
@@ -168,6 +173,7 @@ def main():
|
||||
enabled_value=dict(),
|
||||
host_filter=dict(),
|
||||
credential=dict(),
|
||||
organization=dict(),
|
||||
overwrite=dict(type='bool'),
|
||||
overwrite_vars=dict(type='bool'),
|
||||
custom_virtualenv=dict(),
|
||||
@@ -190,23 +196,29 @@ def main():
|
||||
name = module.params.get('name')
|
||||
new_name = module.params.get('new_name')
|
||||
inventory = module.params.get('inventory')
|
||||
organization = module.params.get('organization')
|
||||
source_script = module.params.get('source_script')
|
||||
credential = module.params.get('credential')
|
||||
source_project = module.params.get('source_project')
|
||||
state = module.params.get('state')
|
||||
|
||||
# Attempt to look up inventory source based on the provided name and inventory ID
|
||||
inventory_id = module.resolve_name_to_id('inventories', inventory)
|
||||
inventory_source = module.get_one('inventory_sources', **{
|
||||
lookup_data = {}
|
||||
if organization:
|
||||
lookup_data['organization'] = module.resolve_name_to_id('organizations', organization)
|
||||
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', name_or_id=name, **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'inventory': inventory_id,
|
||||
'inventory': inventory_object['id'],
|
||||
}
|
||||
})
|
||||
|
||||
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_source)
|
||||
module.delete_if_needed(inventory_source_object)
|
||||
|
||||
# Attempt to look up associated field items the user specified.
|
||||
association_fields = {}
|
||||
@@ -232,7 +244,7 @@ def main():
|
||||
# Create the data that gets sent for create and update
|
||||
inventory_source_fields = {
|
||||
'name': new_name if new_name else name,
|
||||
'inventory': inventory_id,
|
||||
'inventory': inventory_object['id'],
|
||||
}
|
||||
|
||||
# Attempt to look up the related items the user specified (these will fail the module if not found)
|
||||
@@ -261,12 +273,12 @@ def main():
|
||||
inventory_source_fields['source_vars'] = dumps(inventory_source_fields['source_vars'])
|
||||
|
||||
# Sanity check on arguments
|
||||
if state == 'present' and not inventory_source and not inventory_source_fields['source']:
|
||||
if state == 'present' and not inventory_source_object and not inventory_source_fields['source']:
|
||||
module.fail_json(msg="If creating a new inventory source, the source param must be present")
|
||||
|
||||
# If the state was present we can let the module build or update the existing inventory_source, this will return on its own
|
||||
# If the state was present we can let the module build or update the existing inventory_source_object, this will return on its own
|
||||
module.create_or_update_if_needed(
|
||||
inventory_source, inventory_source_fields,
|
||||
inventory_source_object, inventory_source_fields,
|
||||
endpoint='inventory_sources', item_type='inventory source',
|
||||
associations=association_fields
|
||||
)
|
||||
|
||||
153
awx_collection/plugins/modules/tower_inventory_source_update.py
Normal file
153
awx_collection/plugins/modules/tower_inventory_source_update.py
Normal file
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/python
|
||||
# coding: utf-8 -*-
|
||||
|
||||
# (c) 2020, Bianca Henderson <bianca@redhat.com>
|
||||
# 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
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: tower_inventory_source_update
|
||||
author: "Bianca Henderson (@beeankha)"
|
||||
short_description: Update inventory source(s).
|
||||
description:
|
||||
- Update Ansible Tower inventory source(s). See
|
||||
U(https://www.ansible.com/tower) for an overview.
|
||||
options:
|
||||
inventory:
|
||||
description:
|
||||
- Name of the inventory that contains the inventory source(s) to update.
|
||||
required: True
|
||||
type: str
|
||||
inventory_source:
|
||||
description:
|
||||
- The name of the inventory source to update.
|
||||
required: True
|
||||
type: str
|
||||
organization:
|
||||
description:
|
||||
- Name of the inventory source's inventory's organization.
|
||||
type: str
|
||||
wait:
|
||||
description:
|
||||
- Wait for the job to complete.
|
||||
default: False
|
||||
type: bool
|
||||
interval:
|
||||
description:
|
||||
- The interval to request an update from Tower.
|
||||
required: False
|
||||
default: 1
|
||||
type: float
|
||||
timeout:
|
||||
description:
|
||||
- If waiting for the job to complete this will abort after this
|
||||
amount of seconds
|
||||
type: int
|
||||
extends_documentation_fragment: awx.awx.auth
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Update a single inventory source
|
||||
tower_inventory_source_update:
|
||||
inventory: "My Inventory"
|
||||
inventory_source: "Example Inventory Source"
|
||||
organization: Default
|
||||
|
||||
- name: Update all inventory sources
|
||||
tower_inventory_source_update:
|
||||
inventory: "My Other Inventory"
|
||||
inventory_source: "{{ item }}"
|
||||
loop: "{{ query('awx.awx.tower_api', 'inventory_sources', query_params={ 'inventory': 30 }, return_ids=True ) }}"
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
id:
|
||||
description: id of the inventory update
|
||||
returned: success
|
||||
type: int
|
||||
sample: 86
|
||||
status:
|
||||
description: status of the inventory update
|
||||
returned: success
|
||||
type: str
|
||||
sample: pending
|
||||
'''
|
||||
|
||||
from ..module_utils.tower_api import TowerAPIModule
|
||||
|
||||
|
||||
def main():
|
||||
# Any additional arguments that are not fields of the item can be added here
|
||||
argument_spec = dict(
|
||||
inventory=dict(required=True),
|
||||
inventory_source=dict(required=True),
|
||||
organization=dict(),
|
||||
wait=dict(default=False, type='bool'),
|
||||
interval=dict(default=1.0, type='float'),
|
||||
timeout=dict(default=None, type='int'),
|
||||
)
|
||||
|
||||
# Create a module for ourselves
|
||||
module = TowerAPIModule(argument_spec=argument_spec)
|
||||
|
||||
# Extract our parameters
|
||||
inventory = module.params.get('inventory')
|
||||
inventory_source = module.params.get('inventory_source')
|
||||
organization = module.params.get('organization')
|
||||
wait = module.params.get('wait')
|
||||
interval = module.params.get('interval')
|
||||
timeout = module.params.get('timeout')
|
||||
|
||||
lookup_data = {'name': inventory}
|
||||
if organization:
|
||||
lookup_data['organization'] = module.resolve_name_to_id('organizations', organization)
|
||||
inventory_object = module.get_one('inventories', 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', **{
|
||||
'data': {
|
||||
'name': inventory_source,
|
||||
'inventory': inventory_object['id'],
|
||||
}
|
||||
})
|
||||
|
||||
if not inventory_source_object:
|
||||
module.fail_json(msg='The specified inventory source was not found.')
|
||||
|
||||
# Sync the inventory source(s)
|
||||
inventory_source_update_results = module.post_endpoint(inventory_source_object['related']['update'], **{'data': {}})
|
||||
|
||||
if inventory_source_update_results['status_code'] != 202:
|
||||
module.fail_json(msg="Failed to update inventory source, see response for details", **{'response': inventory_source_update_results})
|
||||
|
||||
module.json_output['changed'] = True
|
||||
module.json_output['id'] = inventory_source_update_results['json']['id']
|
||||
module.json_output['status'] = inventory_source_update_results['json']['status']
|
||||
|
||||
if not wait:
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
# Invoke wait function
|
||||
module.wait_on_url(
|
||||
url=inventory_source_update_results['json']['url'],
|
||||
object_name=inventory_object,
|
||||
object_type='inventory_update',
|
||||
timeout=timeout, interval=interval
|
||||
)
|
||||
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -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,
|
||||
|
||||
144
awx_collection/plugins/modules/tower_project_update.py
Normal file
144
awx_collection/plugins/modules/tower_project_update.py
Normal file
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/python
|
||||
# coding: utf-8 -*-
|
||||
|
||||
# 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
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: tower_project_update
|
||||
author: "Sean Sullivan (@sean-m-sullivan)"
|
||||
short_description: Update a Project in Ansible Tower
|
||||
description:
|
||||
- Update a Ansible Tower Project. See
|
||||
U(https://www.ansible.com/tower) for an overview.
|
||||
options:
|
||||
name:
|
||||
description:
|
||||
- The name or id of the project to update.
|
||||
required: True
|
||||
type: str
|
||||
aliases:
|
||||
- project
|
||||
organization:
|
||||
description:
|
||||
- Organization the project exists in.
|
||||
- Used to help lookup the object, cannot be modified using this module.
|
||||
- If not provided, will lookup by name only, which does not work with duplicates.
|
||||
type: str
|
||||
wait:
|
||||
description:
|
||||
- Wait for the project to update.
|
||||
default: True
|
||||
type: bool
|
||||
interval:
|
||||
description:
|
||||
- The interval to request an update from Tower.
|
||||
required: False
|
||||
default: 1
|
||||
type: float
|
||||
timeout:
|
||||
description:
|
||||
- If waiting for the project to update this will abort after this
|
||||
amount of seconds
|
||||
type: int
|
||||
extends_documentation_fragment: awx.awx.auth
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
id:
|
||||
description: project id of the updated project
|
||||
returned: success
|
||||
type: int
|
||||
sample: 86
|
||||
status:
|
||||
description: status of the updated project
|
||||
returned: success
|
||||
type: str
|
||||
sample: pending
|
||||
'''
|
||||
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Launch a project with a timeout of 10 seconds
|
||||
tower_project_update:
|
||||
project: "Networking Project"
|
||||
timeout: 10
|
||||
|
||||
- name: Launch a Project with extra_vars without waiting
|
||||
tower_project_update:
|
||||
project: "Networking Project"
|
||||
wait: False
|
||||
'''
|
||||
|
||||
from ..module_utils.tower_api import TowerAPIModule
|
||||
import json
|
||||
import time
|
||||
|
||||
|
||||
def main():
|
||||
# Any additional arguments that are not fields of the item can be added here
|
||||
argument_spec = dict(
|
||||
name=dict(required=True, aliases=['project']),
|
||||
organization=dict(),
|
||||
wait=dict(default=True, type='bool'),
|
||||
interval=dict(default=1.0, type='float'),
|
||||
timeout=dict(default=None, type='int'),
|
||||
)
|
||||
|
||||
# Create a module for ourselves
|
||||
module = TowerAPIModule(argument_spec=argument_spec)
|
||||
|
||||
# Extract our parameters
|
||||
name = module.params.get('name')
|
||||
organization = module.params.get('organization')
|
||||
wait = module.params.get('wait')
|
||||
interval = module.params.get('interval')
|
||||
timeout = module.params.get('timeout')
|
||||
|
||||
# Attempt to look up project based on the provided name or id
|
||||
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'])
|
||||
|
||||
if result['status_code'] != 202:
|
||||
module.fail_json(msg="Failed to update project, see response for details", response=result)
|
||||
|
||||
module.json_output['changed'] = True
|
||||
module.json_output['id'] = result['json']['id']
|
||||
module.json_output['status'] = result['json']['status']
|
||||
|
||||
if not wait:
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
# Grab our start time to compare against for the timeout
|
||||
start = time.time()
|
||||
|
||||
if not wait:
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
# Invoke wait function
|
||||
module.wait_on_url(
|
||||
url=result['json']['url'],
|
||||
object_name=module.get_item_name(project),
|
||||
object_type='Project Update',
|
||||
timeout=timeout, interval=interval
|
||||
)
|
||||
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -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:
|
||||
|
||||
@@ -190,7 +190,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')
|
||||
@@ -199,7 +199,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
|
||||
@@ -214,7 +214,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")
|
||||
|
||||
Reference in New Issue
Block a user