Skip to content

Commit

Permalink
[ignore] Optimize for loops for ndo_port_channel_interface and Move l…
Browse files Browse the repository at this point in the history
…ookup_valid_interfaces function to mso.py from module_utils.
  • Loading branch information
gmicol committed Oct 23, 2024
1 parent 37f4bcb commit 108465f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
20 changes: 20 additions & 0 deletions plugins/module_utils/mso.py
Original file line number Diff line number Diff line change
Expand Up @@ -1700,3 +1700,23 @@ def listener_rules_spec():
),
target_ip_type=dict(type="str", choices=["unspecified", "primary", "secondary"]),
)


def lookup_valid_interfaces(interfaces):
interface_ids = []
errors_interfaces = []
modified_interfaces = interfaces.replace(" ", "").split(",")

Check warning on line 1708 in plugins/module_utils/mso.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/mso.py#L1706-L1708

Added lines #L1706 - L1708 were not covered by tests
for interface in modified_interfaces:
if re.fullmatch(r"((\d+/)+\d+-\d+$)", interface):
slots = interface.rsplit("/", 1)[0]
range_start, range_stop = interface.rsplit("/", 1)[1].split("-")

Check warning on line 1712 in plugins/module_utils/mso.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/mso.py#L1711-L1712

Added lines #L1711 - L1712 were not covered by tests
if int(range_stop) > int(range_start):
for x in range(int(range_start), int(range_stop) + 1):
interface_ids.append("{0}/{1}".format(slots, x))

Check warning on line 1715 in plugins/module_utils/mso.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/mso.py#L1715

Added line #L1715 was not covered by tests
else:
errors_interfaces.append(interface)

Check warning on line 1717 in plugins/module_utils/mso.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/mso.py#L1717

Added line #L1717 was not covered by tests
elif re.fullmatch(r"((\d+/)+\d+$)", interface):
interface_ids.append(interface)

Check warning on line 1719 in plugins/module_utils/mso.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/mso.py#L1719

Added line #L1719 was not covered by tests
else:
errors_interfaces.append(interface)
return set(interface_ids), errors_interfaces

Check warning on line 1722 in plugins/module_utils/mso.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/mso.py#L1721-L1722

Added lines #L1721 - L1722 were not covered by tests
72 changes: 25 additions & 47 deletions plugins/modules/ndo_port_channel_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
description: My third Ansible Interface
state: present
- name: Query an Port Channel Interface with template_name
- name: Query a Port Channel Interface with template_name
cisco.mso.ndo_port_channel_interface:
host: mso_host
username: admin
Expand All @@ -139,7 +139,7 @@
state: query
register: query_all
- name: Delete an Port Channel Interface
- name: Delete a Port Channel Interface
cisco.mso.ndo_port_channel_interface:
host: mso_host
username: admin
Expand All @@ -153,38 +153,18 @@
"""

import copy
import re
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.cisco.mso.plugins.module_utils.mso import (
MSOModule,
mso_argument_spec,
lookup_valid_interfaces,
)
from ansible_collections.cisco.mso.plugins.module_utils.template import (
MSOTemplate,
KVPair,
)


def lookup_valid_interfaces(interfaces):
interface_ids = []
errors_interfaces = []
modified_interfaces = interfaces.replace(" ", "").split(",")
for interface in modified_interfaces:
if re.fullmatch(r"((\d+/)+\d+-\d+$)", interface):
slots = interface.rsplit("/", 1)[0]
range_start, range_stop = interface.rsplit("/", 1)[1].split("-")
if int(range_stop) > int(range_start):
for x in range(int(range_start), int(range_stop) + 1):
interface_ids.append("{0}/{1}".format(slots, x))
else:
errors_interfaces.append(interface)
elif re.fullmatch(r"((\d+/)+\d+$)", interface):
interface_ids.append(interface)
else:
errors_interfaces.append(interface)
return set(interface_ids), errors_interfaces


def main():
argument_spec = mso_argument_spec()
argument_spec.update(
Expand Down Expand Up @@ -296,7 +276,7 @@ def main():

if interface_descriptions or (node_changed and match.details.get("interfaceDescriptions")):
if node_changed and interface_descriptions is None:
interface_descriptions = [
formated_interface_descriptions = [
{
"nodeID": node,
"interfaceID": interface.get("interfaceID"),
Expand All @@ -305,18 +285,18 @@ def main():
for interface in match.details["interfaceDescriptions"]
]
else:
interface_descriptions = [
{
"nodeID": match.details["node"],
"interfaceID": interface.get("interface_id"),
"description": interface.get("description"),
}
for interface in interface_descriptions
]
error_descriptions = []
formated_interface_descriptions, error_descriptions = [], []
for interface_description in interface_descriptions:
if interface_description["interfaceID"] not in interface_ids:
if interface_description["interface_id"] not in interface_ids:
error_descriptions.append(interface_description["interfaceID"])
else:
formated_interface_descriptions.append(
{
"nodeID": match.details["node"],
"interfaceID": interface_description.get("interface_id"),
"description": interface_description.get("description"),
}
)
if error_descriptions:
mso.fail_json(
msg=(
Expand All @@ -326,9 +306,9 @@ def main():
)
)
)
if interface_descriptions != match.details.get("interfaceDescriptions"):
ops.append(dict(op="replace", path="{0}/{1}/interfaceDescriptions".format(path, match.index), value=interface_descriptions))
match.details["interfaceDescriptions"] = interface_descriptions
if formated_interface_descriptions != match.details.get("interfaceDescriptions"):
ops.append(dict(op="replace", path="{0}/{1}/interfaceDescriptions".format(path, match.index), value=formated_interface_descriptions))
match.details["interfaceDescriptions"] = formated_interface_descriptions
elif interface_descriptions == [] and match.details["interfaceDescriptions"]:
ops.append(dict(op="remove", path="{0}/{1}/interfaceDescriptions".format(path, match.index)))

Expand All @@ -350,8 +330,15 @@ def main():
if description:
payload["description"] = description
if interface_descriptions:
error_descriptions = []
payload["interfaceDescriptions"], error_descriptions = [], []
for interface_description in interface_descriptions:
payload["interfaceDescriptions"].append(
{
"nodeID": node,
"interfaceID": interface_description.get("interface_id"),
"description": interface_description.get("description"),
}
)
if interface_description["interface_id"] not in interface_ids:
error_descriptions.append(interface_description["interface_id"])
if error_descriptions:
Expand All @@ -363,15 +350,6 @@ def main():
)
)
)
payload["interfaceDescriptions"] = [
{
"nodeID": node,
"interfaceID": interface.get("interface_id"),
"description": interface.get("description"),
}
for interface in interface_descriptions
]

ops.append(dict(op="add", path="{0}/-".format(path), value=payload))

mso.sanitize(payload)
Expand Down

0 comments on commit 108465f

Please sign in to comment.