Skip to content

Commit

Permalink
dcnm_vrf: Address mwiebe review comments
Browse files Browse the repository at this point in the history
1. compare_properties: refactor comparison in diff_for_attach_deploy() using this new method.

2. diff_for_attach_deploy(): Leverate to_bool() to add dictionary access protection and remove try/except block.
  • Loading branch information
allenrobel committed Dec 6, 2024
1 parent 978b0d2 commit 75a24d5
Showing 1 changed file with 32 additions and 50 deletions.
82 changes: 32 additions & 50 deletions plugins/modules/dcnm_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,19 @@ def to_bool(self, key, dict_with_key):
msg += "is not convertable to boolean"
self.module.fail_json(msg=msg)

@staticmethod
def compare_properties(dict1, dict2, property_list):
"""
Given two dictionaries and a list of keys:
- Return True if all property values match.
- Return False otherwise
"""
for prop in property_list:
if dict1.get(prop) != dict2.get(prop):
return False
return True

def diff_for_attach_deploy(self, want_a, have_a, replace=False):
method_name = inspect.stack()[0][3]
msg = f"{self.class_name}.{method_name}: ENTERED"
Expand Down Expand Up @@ -803,6 +816,14 @@ def diff_for_attach_deploy(self, want_a, have_a, replace=False):
# this switch
break

vrf_lite_properties = [
"DOT1Q_ID",
"IP_MASK",
"IPV6_MASK",
"IPV6_NEIGHBOR",
"NEIGHBOR_IP",
"PEER_VRF_NAME",
]
for wlite in want_e["VRF_LITE_CONN"]:
for hlite in have_e["VRF_LITE_CONN"]:
found = False
Expand All @@ -811,41 +832,11 @@ def diff_for_attach_deploy(self, want_a, have_a, replace=False):
continue
found = True
interface_match = True
if wlite["DOT1Q_ID"]:
if wlite["DOT1Q_ID"] != hlite["DOT1Q_ID"]:
found = False
break

if wlite["IP_MASK"]:
if wlite["IP_MASK"] != hlite["IP_MASK"]:
found = False
break

if wlite["NEIGHBOR_IP"]:
if wlite["NEIGHBOR_IP"] != hlite["NEIGHBOR_IP"]:
found = False
break

if wlite["IPV6_MASK"]:
if wlite["IPV6_MASK"] != hlite["IPV6_MASK"]:
found = False
break

if wlite["IPV6_NEIGHBOR"]:
if (
wlite["IPV6_NEIGHBOR"]
!= hlite["IPV6_NEIGHBOR"]
):
found = False
break

if wlite["PEER_VRF_NAME"]:
if (
wlite["PEER_VRF_NAME"]
!= hlite["PEER_VRF_NAME"]
):
found = False
break
if not self.compare_properties(
wlite, hlite, vrf_lite_properties
):
found = False
break

if found:
break
Expand Down Expand Up @@ -907,21 +898,12 @@ def diff_for_attach_deploy(self, want_a, have_a, replace=False):
break

if not found:
try:
if want["isAttached"]:
del want["isAttached"]
want["deployment"] = True
attach_list.append(want)
if want["is_deploy"]:
deploy_vrf = True
except KeyError as error:
msg = f"{self.class_name}.{method_name}: "
msg += "Unexpected values for "
msg += f"isAttached ({want.get('isAttached')}) "
msg += f"and/or is_deploy ({want.get('is_deploy')}) "
msg += f"Details: {error}"
self.log.debug(msg)
self.module.fail_json(msg=msg)
if self.to_bool("isAttached", want):
del want["isAttached"]
want["deployment"] = True
attach_list.append(want)
if self.to_bool("is_deploy", want):
deploy_vrf = True

msg = f"{self.class_name}.{method_name}: "
msg += f"deploy_vrf: {deploy_vrf}, "
Expand Down

0 comments on commit 75a24d5

Please sign in to comment.