Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
boolean checks on difference checker (#210)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @rambleraptor
  • Loading branch information
modular-magician authored and rambleraptor committed Mar 13, 2019
1 parent 5e07039 commit 295a10a
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lib/ansible/module_utils/gcp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,11 @@ def _compare_dicts(self, dict1, dict2):
# Takes in two lists and compares them.
def _compare_lists(self, list1, list2):
difference = []
list1.sort()
list2.sort()
for index in range(len(list1)):
value1 = list1[index]
# If items are dicts or arrays, we're assuming the next value
# is the correct one.
if isinstance(value1, dict) or isinstance(value1, list):
if index < len(list2):
value2 = list2[index]
difference.append(self._compare_value(value1, value2))
else:
if value1 not in list2:
difference.append(value1)
if index < len(list2):
value2 = list2[index]
difference.append(self._compare_value(value1, value2))

difference2 = []
for value in difference:
Expand All @@ -307,6 +299,8 @@ def _compare_value(self, value1, value2):
diff = self._compare_lists(value1, value2)
elif isinstance(value2, dict):
diff = self._compare_dicts(value1, value2)
elif isinstance(value1, bool):
diff = self._compare_boolean(value1, value2)
# Always use to_text values to avoid unicode issues.
elif to_text(value1) != to_text(value2):
diff = value1
Expand All @@ -316,3 +310,25 @@ def _compare_value(self, value1, value2):
pass

return diff

def _compare_boolean(self, value1, value2):
try:
# Both True
if value1 and value2 is True:
return None
# Value1 True, value2 'true'
elif value1 and to_text(value2) == 'true':
return None
# Both False
elif not value1 and not value2:
return None
# Value1 False, value2 'false'
elif not value1 and to_text(value2) == 'false':
return None
else:
return value2

# to_text may throw UnicodeErrors.
# These errors shouldn't crash Ansible and should be hidden.
except UnicodeError:
return None

0 comments on commit 295a10a

Please sign in to comment.