Skip to content

Commit

Permalink
Fix for interface module, Issues 276 and 278 (#281)
Browse files Browse the repository at this point in the history
* Fix for interface module, Issues 276 and 278

* Fix for issues reported by ansible sanity test

* Added code to handle SPEED for SVI and FEX interfaces
  • Loading branch information
mmudigon authored Apr 8, 2024
1 parent 3cdca42 commit 00e2c93
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 53 deletions.
120 changes: 89 additions & 31 deletions plugins/modules/dcnm_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@
description:
- Speed of the interface.
type: str
choices: ['Auto', '100Mb', '1Gb', '10Gb', '25Gb', '40Gb', '100Gb']
default: Auto
int_vrf:
description:
Expand Down Expand Up @@ -1852,6 +1851,21 @@ def dcnm_intf_dump_have_all(self):
)
self.log_msg(f"HAVE ALL = {lhave_all}")

def dcnm_intf_xlate_speed(self, speed):

# Controllers accept speed value in a particular format i.e. 1Gb, 100Gb etc. To make the playbook input
# case insensitive for speed, this routine translates the incoming speed to appropriate format.

if speed == "":
return ""

if speed.lower() == "auto":
return "auto".capitalize()
else:
comp = re.compile("([0-9]+)([a-zA-Z]+)")
match = comp.match(speed)
return str(match.group(1)) + match.group(2).capitalize()

# New Interfaces
def dcnm_intf_get_if_name(self, name, if_type):

Expand Down Expand Up @@ -2005,6 +2019,7 @@ def dcnm_intf_validate_port_channel_input(self, config):
bpdu_guard=dict(type="str", default="true"),
port_type_fast=dict(type="bool", default=True),
mtu=dict(type="str", default="jumbo"),
speed=dict(type="str", default="Auto"),
allowed_vlans=dict(type="str", default="none"),
cmds=dict(type="list", elements="str"),
description=dict(type="str", default=""),
Expand All @@ -2018,6 +2033,7 @@ def dcnm_intf_validate_port_channel_input(self, config):
bpdu_guard=dict(type="str", default="true"),
port_type_fast=dict(type="bool", default=True),
mtu=dict(type="str", default="jumbo"),
speed=dict(type="str", default="Auto"),
access_vlan=dict(type="str", default=""),
cmds=dict(type="list", elements="str"),
description=dict(type="str", default=""),
Expand All @@ -2033,6 +2049,7 @@ def dcnm_intf_validate_port_channel_input(self, config):
ipv4_mask_len=dict(type="int", default=8),
route_tag=dict(type="str", default=""),
mtu=dict(type="int", default=9216, range_min=576, range_max=9216),
speed=dict(type="str", default="Auto"),
cmds=dict(type="list", elements="str"),
description=dict(type="str", default=""),
admin_state=dict(type="bool", default=True),
Expand Down Expand Up @@ -2077,6 +2094,7 @@ def dcnm_intf_validate_virtual_port_channel_input(self, cfg):
bpdu_guard=dict(type="str", default="true"),
port_type_fast=dict(type="bool", default=True),
mtu=dict(type="str", default="jumbo"),
speed=dict(type="str", default="Auto"),
peer1_allowed_vlans=dict(type="str", default="none"),
peer2_allowed_vlans=dict(type="str", default="none"),
peer1_cmds=dict(type="list"),
Expand All @@ -2100,6 +2118,7 @@ def dcnm_intf_validate_virtual_port_channel_input(self, cfg):
bpdu_guard=dict(type="str", default="true"),
port_type_fast=dict(type="bool", default=True),
mtu=dict(type="str", default="jumbo"),
speed=dict(type="str", default="Auto"),
peer1_access_vlan=dict(type="str", default=""),
peer2_access_vlan=dict(type="str", default=""),
peer1_cmds=dict(type="list"),
Expand Down Expand Up @@ -2141,6 +2160,7 @@ def dcnm_intf_validate_sub_interface_input(self, cfg):
type="int", range_min=64, range_max=127, default=64
),
mtu=dict(type="int", range_min=576, range_max=9216, default=9216),
speed=dict(type="str", default="Auto"),
cmds=dict(type="list", elements="str"),
description=dict(type="str", default=""),
admin_state=dict(type="bool", default=True),
Expand All @@ -2165,6 +2185,7 @@ def dcnm_intf_validate_loopback_interface_input(self, cfg):
int_vrf=dict(type="str", default="default"),
ipv6_addr=dict(type="ipv6", default=""),
route_tag=dict(type="str", default=""),
speed=dict(type="str", default="Auto"),
cmds=dict(type="list", elements="str"),
description=dict(type="str", default=""),
admin_state=dict(type="bool", default=True),
Expand Down Expand Up @@ -2572,6 +2593,11 @@ def dcnm_intf_get_pc_payload(self, delem, intf, profile):
intf["interfaces"][0]["nvPairs"]["ADMIN_STATE"] = str(
delem[profile]["admin_state"]
).lower()
intf["interfaces"][0]["nvPairs"][
"SPEED"
] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

def dcnm_intf_get_vpc_payload(self, delem, intf, profile):

Expand Down Expand Up @@ -2710,6 +2736,9 @@ def dcnm_intf_get_vpc_payload(self, delem, intf, profile):
delem[profile]["admin_state"]
).lower()
intf["interfaces"][0]["nvPairs"]["INTF_NAME"] = ifname
intf["interfaces"][0]["nvPairs"]["SPEED"] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

def dcnm_intf_get_sub_intf_payload(self, delem, intf, profile):

Expand Down Expand Up @@ -2754,6 +2783,9 @@ def dcnm_intf_get_sub_intf_payload(self, delem, intf, profile):
intf["interfaces"][0]["nvPairs"]["ADMIN_STATE"] = str(
delem[profile]["admin_state"]
).lower()
intf["interfaces"][0]["nvPairs"]["SPEED"] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

def dcnm_intf_get_loopback_payload(self, delem, intf, profile):

Expand All @@ -2780,6 +2812,10 @@ def dcnm_intf_get_loopback_payload(self, delem, intf, profile):
delem[profile]["admin_state"]
).lower()

intf["interfaces"][0]["nvPairs"]["SPEED"] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

# Properties for mode 'lo' Loopback Interfaces
if delem[profile]["mode"] == "lo":

Expand Down Expand Up @@ -2813,8 +2849,12 @@ def dcnm_intf_get_loopback_payload(self, delem, intf, profile):
# properties that can be modified. They will be updated from the
# self.have dictionary to reflect the actual values later in the
# code workflow that walks the want values and compares to have values.
intf["interfaces"][0]["nvPairs"]["DCI_ROUTING_PROTO"] = "PLACE_HOLDER"
intf["interfaces"][0]["nvPairs"]["DCI_ROUTING_TAG"] = "PLACE_HOLDER"
intf["interfaces"][0]["nvPairs"][
"DCI_ROUTING_PROTO"
] = "PLACE_HOLDER"
intf["interfaces"][0]["nvPairs"][
"DCI_ROUTING_TAG"
] = "PLACE_HOLDER"

def dcnm_intf_get_eth_payload(self, delem, intf, profile):

Expand All @@ -2835,9 +2875,6 @@ def dcnm_intf_get_eth_payload(self, delem, intf, profile):
intf["interfaces"][0]["nvPairs"]["MTU"] = str(
delem[profile]["mtu"]
)
intf["interfaces"][0]["nvPairs"]["SPEED"] = str(
delem[profile]["speed"]
)
intf["interfaces"][0]["nvPairs"]["ALLOWED_VLANS"] = delem[profile][
"allowed_vlans"
]
Expand All @@ -2852,9 +2889,6 @@ def dcnm_intf_get_eth_payload(self, delem, intf, profile):
intf["interfaces"][0]["nvPairs"]["MTU"] = str(
delem[profile]["mtu"]
)
intf["interfaces"][0]["nvPairs"]["SPEED"] = str(
delem[profile]["speed"]
)
intf["interfaces"][0]["nvPairs"]["ACCESS_VLAN"] = delem[profile][
"access_vlan"
]
Expand All @@ -2878,9 +2912,6 @@ def dcnm_intf_get_eth_payload(self, delem, intf, profile):
intf["interfaces"][0]["nvPairs"]["MTU"] = str(
delem[profile]["mtu"]
)
intf["interfaces"][0]["nvPairs"]["SPEED"] = str(
delem[profile]["speed"]
)
intf["interfaces"][0]["nvPairs"]["INTF_NAME"] = ifname
if delem[profile]["mode"] == "monitor":
intf["interfaces"][0]["nvPairs"]["INTF_NAME"] = ifname
Expand All @@ -2903,9 +2934,6 @@ def dcnm_intf_get_eth_payload(self, delem, intf, profile):
intf["interfaces"][0]["nvPairs"]["MTU"] = str(
delem[profile]["mtu"]
)
intf["interfaces"][0]["nvPairs"]["SPEED"] = str(
delem[profile]["speed"]
)
intf["interfaces"][0]["nvPairs"]["INTF_NAME"] = ifname

if delem[profile]["mode"] != "monitor":
Expand All @@ -2921,6 +2949,11 @@ def dcnm_intf_get_eth_payload(self, delem, intf, profile):
intf["interfaces"][0]["nvPairs"]["ADMIN_STATE"] = str(
delem[profile]["admin_state"]
).lower()
intf["interfaces"][0]["nvPairs"][
"SPEED"
] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

def dcnm_intf_get_st_fex_payload(self, delem, intf, profile):

Expand Down Expand Up @@ -2966,6 +2999,10 @@ def dcnm_intf_get_st_fex_payload(self, delem, intf, profile):
delem[profile]["netflow_monitor"]
)

intf["interfaces"][0]["nvPairs"]["SPEED"] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

def dcnm_intf_get_aa_fex_payload(self, delem, intf, profile):

# Extract port id from the given name, which is of the form 'vPC300'
Expand Down Expand Up @@ -3032,6 +3069,10 @@ def dcnm_intf_get_aa_fex_payload(self, delem, intf, profile):

intf["interfaces"][0]["nvPairs"]["INTF_NAME"] = ifname

intf["interfaces"][0]["nvPairs"]["SPEED"] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

def dcnm_intf_get_svi_payload(self, delem, intf, profile):

# Extract port id from the given name, which is of the form 'vlan300'
Expand Down Expand Up @@ -3135,8 +3176,11 @@ def dcnm_intf_get_svi_payload(self, delem, intf, profile):

intf["interfaces"][0]["nvPairs"]["INTF_NAME"] = ifname

# we don't need SPEED for SVI interfaces
intf["interfaces"][0]["nvPairs"].pop("SPEED")
intf["interfaces"][0]["nvPairs"][
"SPEED"
] = self.dcnm_intf_xlate_speed(
str(delem[profile].get("speed", ""))
)

# New Interfaces
def dcnm_get_intf_payload(self, delem, sw):
Expand Down Expand Up @@ -3432,7 +3476,7 @@ def dcnm_intf_compare_elements(
if (state == "replaced") or (state == "overridden"):
# Special handling is required for mode 'mpls' loopback interfaces.
# They will contain either of the following two read_only properties.
if k in ['DCI_ROUTING_PROTO', 'DCI_ROUTING_TAG']:
if k in ["DCI_ROUTING_PROTO", "DCI_ROUTING_TAG"]:
return "copy_and_add"

return "add"
Expand Down Expand Up @@ -3560,7 +3604,12 @@ def dcnm_intf_compare_want_and_have(self, state):
if ik == "nvPairs":
nv_keys = list(want[k][0][ik].keys())
if "SPEED" in nv_keys:
nv_keys.remove("SPEED")
# Remove 'SPEED' only if it is not included in 'have'.
if (
d[k][index][ik].get("SPEED", None)
is None
):
nv_keys.remove("SPEED")
for nk in nv_keys:
# HAVE may have an entry with a list # of interfaces. Check all the
# interface entries for a match. Even if one entry matches do not
Expand Down Expand Up @@ -4061,12 +4110,15 @@ def dcnm_intf_get_diff_overridden(self, cfg):
del_list.append(have)

if str(deploy).lower() == "true":
self.diff_delete_deploy[
self.int_index[have["ifType"]]
].append(delem)
self.changed_dict[0]["delete_deploy"].append(
copy.deepcopy(delem)
)
if (have["complianceStatus"] == "In-Sync") or (
have["complianceStatus"] == "Pending"
):
self.diff_delete_deploy[
self.int_index[have["ifType"]]
].append(delem)
self.changed_dict[0]["delete_deploy"].append(
copy.deepcopy(delem)
)

for intf in defer_list:
# Check if the 'source' for the ethernet interface is one of the interfaces that is already deleted.
Expand Down Expand Up @@ -4224,12 +4276,18 @@ def dcnm_intf_get_diff_deleted(self):
self.changed_dict[0][
"replaced"
].append(copy.deepcopy(uelem))
delem["serialNumber"] = intf[
"serialNumber"
]
delem["ifName"] = if_name
delem["fabricName"] = self.fabric
self.diff_deploy.append(delem)
if (
str(
cfg.get("deploy", "true")
).lower()
== "true"
):
delem["serialNumber"] = intf[
"serialNumber"
]
delem["ifName"] = if_name
delem["fabricName"] = self.fabric
self.diff_deploy.append(delem)
else:
intf_payload = self.dcnm_intf_get_intf_info_from_dcnm(
intf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"peerOneId": "{{ ansible_cxt_fabric_sno1 }}",
"peerTwoId": "{{ ansible_cxt_fabric_sno2 }}",
"useVirtualPeerlink": false,
"templateName": "vpc_pair",
"nvPairs": {
"DOMAIN_ID": "{{ ansible_cxt_vpc_domain_id }}",
"PEER1_KEEP_ALIVE_LOCAL_IP": "{{ ansible_cxt_fabric_sw1 }}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
"cmds": [
"no shutdown"
],
"speed": "auto",
"speed": "Auto",
"description": "eth interface acting as routed"
},
"name": "eth1/32",
Expand All @@ -236,7 +236,7 @@
"cmds": [
"no shutdown"
],
"speed": "auto",
"speed": "Auto",
"description": "eth interface acting as routed"
},
"name": "eth1/22",
Expand All @@ -261,7 +261,7 @@
"admin_state": "False",
"ifname": "Ethernet1/13",
"route_tag": "",
"speed": "auto",
"speed": "Auto",
"cmds": [
"no shutdown"
],
Expand Down Expand Up @@ -289,7 +289,7 @@
"admin_state": "False",
"ifname": "Ethernet1/14",
"route_tag": "",
"speed": "auto",
"speed": "Auto",
"cmds": [
"no shutdown"
],
Expand Down
Loading

0 comments on commit 00e2c93

Please sign in to comment.