Skip to content

Commit

Permalink
dcnm_vrf: leverage get_vrf_lite_objects() everywhere
Browse files Browse the repository at this point in the history
1. Replace several bits that can be replaced with a call to get_vrf_lite_objects().

2. Fix a few pylint f-string complaints.  There are many more of these, which we'll address in the next commit.  One of these required a change to an associated unit test.
  • Loading branch information
allenrobel committed Dec 5, 2024
1 parent 6a6de04 commit 12f9e53
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
75 changes: 37 additions & 38 deletions plugins/modules/dcnm_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
from __future__ import absolute_import, division, print_function

__metaclass__ = type
__author__ = (
"Shrishail Kariyappanavar, Karthik Babu Harichandra Babu, Praveen Ramoorthy, Allen Robel"
)
__author__ = "Shrishail Kariyappanavar, Karthik Babu Harichandra Babu, Praveen Ramoorthy, Allen Robel"

DOCUMENTATION = """
---
Expand Down Expand Up @@ -1107,8 +1105,9 @@ def dict_values_differ(self, want_template, have_template, skip_keys=[]) -> bool
have_value = str(have_template[key]).lower()
if want_value != have_value:
msg = f"{self.class_name}.{method_name}: "
msg += f"DIFFERS: key {key} want_value {want_value} != have_value {have_value}. "
msg += f"returning True"
msg += f"DIFFERS: key {key} "
msg += f"want_value {want_value} != have_value {have_value}. "
msg += "returning True"
self.log.debug(msg)
return True
return False
Expand Down Expand Up @@ -1997,10 +1996,11 @@ def get_diff_merge(self, replace=False):
break

if not vrf_id:
self.module.fail_json(
msg="Unable to generate vrfId for vrf: {0} "
"under fabric: {1}".format(want_c["vrfName"], self.fabric)
)
# arobel: TODO: This is not covered in UT
msg = f"{self.class_name}.{method_name}: "
msg += f"Unable to generate vrfId for vrf: {want_c['vrfName']} "
msg += f"under fabric: {self.fabric}"
self.module.fail_json(msg=msg)

create_path = self.paths["GET_VRF"].format(self.fabric)

Expand Down Expand Up @@ -2347,12 +2347,16 @@ def get_diff_query(self):
attach_list = vrf_attach["lanAttachList"]

for attach in attach_list:
path = self.paths["GET_VRF_SWITCH"].format(
self.fabric,
attach["vrfName"],
attach["switchSerialNo"],
# copy attach and update it with the keys that
# get_vrf_lite_objects() expects.
attach_copy = copy.deepcopy(attach)
attach_copy.update({"fabric": self.fabric})
attach_copy.update(
{"serialNumber": attach["switchSerialNo"]}
)
lite_objects = self.get_vrf_lite_objects(
attach_copy
)
lite_objects = dcnm_send(self.module, method, path)
if not lite_objects.get("DATA"):
return
item["attach"].append(lite_objects.get("DATA")[0])
Expand Down Expand Up @@ -2394,11 +2398,13 @@ def get_diff_query(self):
attach_list = vrf_attach["lanAttachList"]

for attach in attach_list:
path = self.paths["GET_VRF_SWITCH"].format(
self.fabric, attach["vrfName"], attach["switchSerialNo"]
)
# copy attach and update it with the keys that
# get_vrf_lite_objects() expects.
attach_copy = copy.deepcopy(attach)
attach_copy.update({"fabric": self.fabric})
attach_copy.update({"serialNumber": attach["switchSerialNo"]})
lite_objects = self.get_vrf_lite_objects(attach_copy)

lite_objects = dcnm_send(self.module, method, path)
if not lite_objects.get("DATA"):
return
item["attach"].append(lite_objects.get("DATA")[0])
Expand Down Expand Up @@ -2596,19 +2602,12 @@ def push_to_remote(self, is_rollback=False):
role = self.inventory_data[ip].get("switchRole")
r = re.search(r"\bborder\b", role.lower())
if not r:
msg = "VRF LITE cannot be attached to switch {0} with role {1}".format(
ip, role
)
msg = f"{self.class_name}.{method_name}: "
msg += "VRF LITE cannot be attached to "
msg += f"switch {ip} with role {role}"
self.module.fail_json(msg=msg)

"""Get the IP/Interface that is connected to edge router can be get from below query"""
method = "GET"
path = self.paths["GET_VRF_SWITCH"].format(
self.fabric, v_a["vrfName"], v_a["serialNumber"]
)

lite_objects = dcnm_send(self.module, method, path)

lite_objects = self.get_vrf_lite_objects(v_a)
if not lite_objects.get("DATA"):
return

Expand Down Expand Up @@ -2703,10 +2702,11 @@ def push_to_remote(self, is_rollback=False):

if ext_values is None:
for ip, ser in self.ip_sn.items():
# arobel TODO: Not covered by UT
if ser == v_a["serialNumber"]:
msg = "There is no VRF LITE capable interface on this switch {0}".format(
ip
)
msg = f"{self.class_name}.{method_name}: "
msg += "No VRF LITE capable interfaces found "
msg += f"on this switch {ip}"
self.module.fail_json(msg=msg)
else:
extension_values["VRF_LITE_CONN"] = json.dumps(
Expand Down Expand Up @@ -2793,11 +2793,11 @@ def wait_for_vrf_del_ready(self):
vlan_id = atch.get("vlanId", "unknown")
msg = f"{self.class_name}.{method_name}: "
msg += f"Network attachments associated with vrf {vrf_name} "
msg += f"must be removed (e.g. using the dcnm_network module) "
msg += f"prior to deleting the vrf. "
msg += "must be removed (e.g. using the dcnm_network module) "
msg += "prior to deleting the vrf. "
msg += f"Details: fabric_name: {fabric_name}, "
msg += f"vrf_name: {vrf_name}. "
msg += f"Network attachments found on "
msg += "Network attachments found on "
msg += f"switch_ip: {switch_ip}, "
msg += f"switch_name: {switch_name}, "
msg += f"vlan_id: {vlan_id}"
Expand Down Expand Up @@ -2908,9 +2908,8 @@ def validate_input(self):
msg = "ip_address is mandatory under attach parameters"
else:
if state == "merged" or state == "replaced":
msg = "config: element is mandatory for this state {0}".format(
state
)
msg = f"{self.class_name}.{method_name}: "
msg += f"config element is mandatory for {state} state"

if msg:
self.module.fail_json(msg=msg)
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/modules/dcnm/test_dcnm_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,8 +1279,9 @@ def test_dcnm_vrf_validation(self):
def test_dcnm_vrf_validation_no_config(self):
set_module_args(dict(state="merged", fabric="test_fabric", config=[]))
result = self.execute_module(changed=False, failed=True)
msg = "DcnmVrf.validate_input: config element is mandatory for merged state"
self.assertEqual(
result["msg"], "config: element is mandatory for this state merged"
result["msg"], msg
)

def test_dcnm_vrf_12check_mode(self):
Expand Down

0 comments on commit 12f9e53

Please sign in to comment.