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 21, 2024
1 parent 37f4bcb commit 027e458
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
19 changes: 19 additions & 0 deletions plugins/module_utils/mso.py
Original file line number Diff line number Diff line change
Expand Up @@ -1700,3 +1700,22 @@ 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(",")
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
43 changes: 11 additions & 32 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 @@ -158,33 +158,14 @@
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 @@ -350,8 +331,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 +351,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 027e458

Please sign in to comment.