Skip to content

Commit

Permalink
dcnm_vrf: to_bool() fix to return correct value, or call fail_json()
Browse files Browse the repository at this point in the history
The initial implementation would return True for e.g. "false" since bool(non-null-string) is always True.

1. Modify to explicitly compare against known boolean-like strings i.e. "false", "False", "true", and "True".

2. Add the caller to the error message for better debugging ability in the future.
  • Loading branch information
allenrobel committed Dec 3, 2024
1 parent 40d93d6 commit 0b17715
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions plugins/modules/dcnm_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,16 +697,35 @@ def __init__(self, module):
self.failed_to_rollback = False
self.WAIT_TIME_FOR_DELETE_LOOP = 5 # in seconds

@staticmethod
def to_bool(key, dict_with_key):
def to_bool(self, key, dict_with_key):
"""
# Summary
Given a dictionary and key, access dictionary[key] and
try to convert the value therein to a boolean.
- If the value is a boolean, return a like boolean.
- If the value is a boolean-like string (e.g. "false"
"True", etc), return the value converted to boolean.
## Raises
- Call fail_json() if the value is not convertable to boolean.
"""
value = dict_with_key.get(key)
try:
# TODO: Any string value e.g. "false" will return True here.
# We need to test for common bool-like strings e.g.:
#if value in ["false", False] return False
return bool(value)
except:
return value
if value in ["false", "False", False]:
return False
if value in ["true", "True", True]:
return True

method_name = inspect.stack()[0][3]
caller = inspect.stack()[1][3]

msg = f"{self.class_name}.{method_name}: "
msg += f"caller: {caller}: "
msg = f"{str(value)} with type {type(value)} "
msg += "is not convertable to boolean"
self.module.fail_json(msg=msg)

def diff_for_attach_deploy(self, want_a, have_a, replace=False):

Expand Down

0 comments on commit 0b17715

Please sign in to comment.