update to auto_exit, add tests, add mutual exclusive parameters

This commit is contained in:
sean-m-sullivan 2020-10-13 18:17:22 -05:00
parent e16a910062
commit 51eb4e6d6b
3 changed files with 38 additions and 28 deletions

View File

@ -337,7 +337,7 @@ class TowerAPIModule(TowerModule):
# If we have neither of these, then we can try un-authenticated access
self.authenticated = True
def delete_if_needed(self, existing_item, on_delete=None, on_continue=None):
def delete_if_needed(self, existing_item, on_delete=None, auto_exit=True):
# This will exit from the module on its own.
# If the method successfully deletes an item and on_delete param is defined,
# the on_delete parameter will be called as a method pasing in this object and the json from the response
@ -363,10 +363,10 @@ class TowerAPIModule(TowerModule):
self.json_output['changed'] = True
self.json_output['id'] = item_id
self.exit_json(**self.json_output)
if on_continue is not None:
return self.json_output
else:
if auto_exit:
self.exit_json(**self.json_output)
else:
return self.json_output #########
else:
if 'json' in response and '__all__' in response['json']:
self.fail_json(msg="Unable to delete {0} {1}: {2}".format(item_type, item_name, response['json']['__all__'][0]))
@ -379,10 +379,10 @@ class TowerAPIModule(TowerModule):
else:
self.fail_json(msg="Unable to delete {0} {1}: {2}".format(item_type, item_name, response['status_code']))
else:
if on_continue is not None:
return None
else:
if auto_exit:
self.exit_json(**self.json_output)
else:
return None
def modify_associations(self, association_endpoint, new_association_list):
# if we got None instead of [] we are not modifying the association_list
@ -410,7 +410,7 @@ class TowerAPIModule(TowerModule):
else:
self.fail_json(msg="Failed to associate item {0}".format(response['json']['detail']))
def create_if_needed(self, existing_item, new_item, endpoint, on_create=None, on_continue=None, item_type='unknown', associations=None):
def create_if_needed(self, existing_item, new_item, endpoint, on_create=None, auto_exit=True, item_type='unknown', associations=None):
# This will exit from the module on its own
# If the method successfully creates an item and on_create param is defined,
@ -472,11 +472,11 @@ class TowerAPIModule(TowerModule):
# If we have an on_create method and we actually changed something we can call on_create
if on_create is not None and self.json_output['changed']:
on_create(self, response['json'])
elif on_continue is not None:
elif auto_exit:
self.exit_json(**self.json_output)
else:
last_data = response['json']
return last_data
else:
self.exit_json(**self.json_output)
def _encrypted_changed_warning(self, field, old, warning=False):
if not warning:
@ -538,7 +538,7 @@ class TowerAPIModule(TowerModule):
return True
return False
def update_if_needed(self, existing_item, new_item, on_update=None, on_continue=None, associations=None):
def update_if_needed(self, existing_item, new_item, on_update=None, auto_exit=True, associations=None):
# This will exit from the module on its own
# If the method successfully updates an item and on_update param is defined,
# the on_update parameter will be called as a method pasing in this object and the json from the response
@ -598,20 +598,20 @@ class TowerAPIModule(TowerModule):
else:
last_data = response['json']
on_update(self, last_data)
elif on_continue is not None:
elif auto_exit:
self.exit_json(**self.json_output)
else:
if response is None:
last_data = existing_item
else:
last_data = response['json']
return last_data
else:
self.exit_json(**self.json_output)
def create_or_update_if_needed(self, existing_item, new_item, endpoint=None, item_type='unknown', on_create=None, on_update=None, on_continue=None, associations=None):
def create_or_update_if_needed(self, existing_item, new_item, endpoint=None, item_type='unknown', on_create=None, on_update=None, auto_exit=True, associations=None):
if existing_item:
return self.update_if_needed(existing_item, new_item, on_update=on_update, on_continue=on_continue, associations=associations)
return self.update_if_needed(existing_item, new_item, on_update=on_update, auto_exit=auto_exit, associations=associations)
else:
return self.create_if_needed(existing_item, new_item, endpoint, on_create=on_create, item_type=item_type, on_continue=on_continue, associations=associations)
return self.create_if_needed(existing_item, new_item, endpoint, on_create=on_create, item_type=item_type, auto_exit=auto_exit, associations=associations)
def logout(self):
if self.authenticated and self.oauth_token_id:

View File

@ -91,11 +91,13 @@ options:
description:
- Name of unified job template to run in the workflow.
- Can be a job template, project, inventory source, etc.
- Omit if creating an approval node (not yet implemented).
- Omit if creating an approval node.
- This parameter is mutually exclusive with C(approval_node).
type: str
approval_node:
description:
- A dictionary of Name, description, and timeout values for the approval node.
- This parameter is mutually exclusive with C(unified_job_template).
type: dict
suboptions:
name:
@ -202,9 +204,15 @@ def main():
credentials=dict(type='list', elements='str'),
state=dict(choices=['present', 'absent'], default='present'),
)
mutually_exclusive = [("unified_job_template", "approval_node")]
required_one_of = [["unified_job_template", "approval_node", "success_nodes", "always_nodes", "failure_nodes"]]
# Create a module for ourselves
module = TowerAPIModule(argument_spec=argument_spec)
module = TowerAPIModule(
argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
required_one_of=required_one_of,
)
# Extract our parameters
identifier = module.params.get('identifier')
@ -236,13 +244,7 @@ def main():
existing_item = module.get_one('workflow_job_template_nodes', **{'data': search_fields})
if state == 'absent':
# Look up existing approval node for deletion
if existing_item['related'].get('unified_job_template') is not None:
existing_approval_node = module.get_endpoint(existing_item['related']['unified_job_template'])['json']
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
# Delete the Approval Node
module.delete_if_needed(existing_approval_node, on_continue=True,)
# Delete Workflow Node
module.delete_if_needed(existing_item)
unified_job_template = module.params.get('unified_job_template')
@ -289,7 +291,7 @@ def main():
# If the state was present and we can let the module build or update the existing item, this will return on its own
module.create_or_update_if_needed(
existing_item, new_fields,
endpoint='workflow_job_template_nodes', item_type='workflow_job_template_node', on_continue=approval_node,
endpoint='workflow_job_template_nodes', item_type='workflow_job_template_node', auto_exit=not approval_node,
associations=association_fields
)
@ -319,7 +321,7 @@ def main():
existing_item = module.get_endpoint(workflow_job_template_node['related']['unified_job_template'])['json']
module.create_or_update_if_needed(
existing_item, new_fields,
endpoint='workflow_job_template_nodes/' + str(workflow_job_template_node_id) + '/create_approval_template/', item_type='workflow_job_template_approval_node', on_continue=approval_node,
endpoint='workflow_job_template_nodes/' + str(workflow_job_template_node_id) + '/create_approval_template/', item_type='workflow_job_template_approval_node',
associations=association_fields
)
module.exit_json(**module.json_output)

View File

@ -176,6 +176,14 @@
always_nodes:
- leaf
- name: Delete approval node
awx.awx.tower_workflow_job_template_node:
identifier: approval_test
approval_node:
name: "{{ approval_node_name }}"
state: absent
workflow: "{{ wfjt_name }}"
- name: Add started notifications to workflow job template
tower_workflow_job_template:
name: "{{ wfjt_name }}"