summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsean-m-sullivan <ssulliva@redhat.com>2020-10-14 01:17:22 +0200
committersean-m-sullivan <ssulliva@redhat.com>2020-10-14 01:17:22 +0200
commit51eb4e6d6bff0a6e50357e518bed59ddb0c88001 (patch)
tree034a32ea586b12cff9829c3d2c0812bfac3bc1d6
parentupdated workflow task name (diff)
downloadawx-51eb4e6d6bff0a6e50357e518bed59ddb0c88001.tar.xz
awx-51eb4e6d6bff0a6e50357e518bed59ddb0c88001.zip
update to auto_exit, add tests, add mutual exclusive parameters
-rw-r--r--awx_collection/plugins/module_utils/tower_api.py36
-rw-r--r--awx_collection/plugins/modules/tower_workflow_job_template_node.py22
-rw-r--r--awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml8
3 files changed, 38 insertions, 28 deletions
diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py
index 4c180cead7..30879a332b 100644
--- a/awx_collection/plugins/module_utils/tower_api.py
+++ b/awx_collection/plugins/module_utils/tower_api.py
@@ -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:
diff --git a/awx_collection/plugins/modules/tower_workflow_job_template_node.py b/awx_collection/plugins/modules/tower_workflow_job_template_node.py
index 173f3b1173..dc3ac1cf8a 100644
--- a/awx_collection/plugins/modules/tower_workflow_job_template_node.py
+++ b/awx_collection/plugins/modules/tower_workflow_job_template_node.py
@@ -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)
diff --git a/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml b/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml
index 7df132c139..5c6b4911be 100644
--- a/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml
+++ b/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml
@@ -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 }}"