Removing recursive check, allowwing old pattern to commence

This commit is contained in:
John Westcott IV
2020-04-03 12:47:28 -04:00
committed by beeankha
parent f5cf7c204f
commit c1bb62cc36
3 changed files with 51 additions and 27 deletions

View File

@@ -574,30 +574,6 @@ class TowerModule(AnsibleModule):
else:
self.exit_json(**self.json_output)
# We need to be able to recursevly step through fields in the case of inputs for credentials.
# They are dicts and we can't just compare them at the top level because the dict from the tower api may have more fields that we have.
# For example, say someone did:
# - tower_credential:
# name: 'a cred'
# username: 'John'
# Our new dict would be like { 'username': 'new_name' }
# But the existing cred from tower might come back as: { 'username': 'new_name', 'password': '$encrypted$', 'field2': 'something else' }
@staticmethod
def compare_fields(new_item, existing_item):
needs_update = False
for field in new_item:
existing_field = existing_item.get(field, None)
new_field = new_item.get(field, None)
if type(existing_field) == dict and type(new_field) == dict:
needs_update = needs_update or TowerModule.compare_fields(new_field, existing_field)
# If the two items don't match and we are not comparing '' to None
# In the case of extra_vars in a job template, we have to pass in {} for nothing ('' is not valid)
# But when its returned from the API, instead of {} we get back ''.
elif existing_field != new_field and not (existing_field in (None, '') and new_field in ('', {})):
# Something doesn't match so let's update it
needs_update = True
return needs_update
def update_if_needed(self, existing_item, new_item, on_update=None, associations=None):
# This will exit from the module on its own
# If the method successfully updates an item and on_update param is defined,
@@ -625,7 +601,15 @@ class TowerModule(AnsibleModule):
self.fail_json(msg="Unable to process update of item due to missing data {0}".format(ke))
# Check to see if anything within the item requires the item to be updated
needs_update = TowerModule.compare_fields(new_item, existing_item)
needs_update = False
for field in new_item:
existing_field = existing_item.get(field, None)
new_field = new_item.get(field, None)
# If the two items don't match and we are not comparing '' to None
if existing_field != new_field and not (existing_field in (None, '') and new_field == ''):
# Something doesn't match so let's update it
needs_update = True
break
# If we decided the item needs to be updated, update it
self.json_output['id'] = item_id