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: