Skip to content

Commit

Permalink
[ignore] Modify Documentation, required attributes in ndo_port_channe…
Browse files Browse the repository at this point in the history
…l_interface module. enhance format_interface_descriptions function to format range of interface IDs.
  • Loading branch information
gmicol authored and lhercot committed Dec 17, 2024
1 parent 33f24c9 commit 9e6046b
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 58 deletions.
39 changes: 29 additions & 10 deletions plugins/module_utils/mso.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,38 @@ def write_file(module, url, dest, content, resp, tmpsrc=None):
os.remove(tmpsrc)


def format_interface_descriptions(interface_descriptions, node=None):
def format_interface_descriptions(mso, interface_descriptions, node=None):
if interface_descriptions:
formated_interface_descriptions = [
{
"nodeID": node if node is not None else interface_description.get("node"),
"interfaceID": interface_description.get("interface_id", interface_description.get("interfaceID")),
"description": interface_description.get("description"),
}

def format_range_interfaces(format_dict):
ids = format_dict.get("interfaceID")
if re.fullmatch(r"((\d+/)+\d+$)", ids):
yield format_dict
elif re.fullmatch(r"((\d+/)+\d+-\d+$)", ids):
slots = ids.rsplit("/", 1)[0]
range_start, range_stop = ids.rsplit("/", 1)[1].split("-")
if int(range_stop) > int(range_start):
for x in range(int(range_start), int(range_stop) + 1):
copy_format_dict = deepcopy(format_dict)
copy_format_dict.update(interfaceID="{0}/{1}".format(slots, x))
yield copy_format_dict
else:
mso.fail_json(msg="Range start is greater than or equal to range stop for range of IDs '{0}'".format(ids))
else:
mso.fail_json(msg="Incorrect interface ID or range of IDs. Got '{0}'".format(ids))

return [
item
for interface_description in interface_descriptions
for item in format_range_interfaces(
{
"nodeID": node if node is not None else interface_description.get("node"),
"interfaceID": interface_description.get("interface_id", interface_description.get("interfaceID")),
"description": interface_description.get("description"),
}
)
]
else:
formated_interface_descriptions = []
return formated_interface_descriptions
return []


class MSOModule(object):
Expand Down
42 changes: 26 additions & 16 deletions plugins/modules/ndo_port_channel_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@
description:
- The name of the Interface Policy Group.
type: str
required: true
template:
description:
- The name of the template in which the Interface Policy Group has been created.
type: str
required: true
aliases: [ policy, interface_policy, interface_setting ]
interface_descriptions:
description:
Expand All @@ -91,8 +93,10 @@
suboptions:
interface_id:
description:
- The interface ID.
- The interface ID or a range of IDs.
- Using a range of interface IDs will apply the same O(description) for every ID in range.
type: str
required: true
description:
description:
- The description of the interface.
Expand All @@ -106,6 +110,14 @@
choices: [ absent, query, present ]
default: query
extends_documentation_fragment: cisco.mso.modules
notes:
- The O(template) must exist before using this module in your playbook.
Use M(cisco.mso.ndo_template) to create the Fabric Resource template.
- The O(interface_policy_group) must exist before using this module in your playbook.
Use M(cisco.mso.ndo_interface_setting) to create the Interface Policy Group of type Port Channel.
seealso:
- module: cisco.mso.ndo_template
- module: cisco.mso.ndo_interface_setting
"""

EXAMPLES = r"""
Expand All @@ -126,11 +138,9 @@
template: ansible_fabric_policy_template
interface_descriptions:
- interface_id: 1/1
description: My first Ansible Interface
- interface_id: 1/10
description: My second Ansible Interface
- interface_id: 1/11
description: My third Ansible Interface
description: My single Ansible Interface
- interface_id: 1/10-11
description: My group of Ansible Interfaces
state: present
register: port_channel_interface_1
Expand All @@ -152,7 +162,7 @@
template: ansible_fabric_resource_template
name: ansible_port_channel_interface_changed
state: query
register: query_one
register: query_name
- name: Query a Port Channel Interface with UUID
cisco.mso.ndo_port_channel_interface:
Expand All @@ -162,7 +172,7 @@
template: ansible_fabric_resource_template
uuid: "{{ port_channel_interface_1.current.uuid }}"
state: query
register: query_one
register: query_uuid
- name: Query all Port Channel Interfaces in a Fabric Resource Template
cisco.mso.ndo_port_channel_interface:
Expand Down Expand Up @@ -220,8 +230,8 @@ def main():
interface_policy_group=dict(
type="dict",
options=dict(
name=dict(type="str"),
template=dict(type="str"),
name=dict(type="str", required=True),
template=dict(type="str", required=True),
),
aliases=["policy", "interface_policy", "interface_setting"],
),
Expand All @@ -230,7 +240,7 @@ def main():
type="list",
elements="dict",
options=dict(
interface_id=dict(type="str"),
interface_id=dict(type="str", required=True),
description=dict(type="str"),
),
),
Expand All @@ -254,7 +264,7 @@ def main():
description = module.params.get("description")
node = module.params.get("node")
interfaces = module.params.get("interfaces")
if interfaces:
if isinstance(interfaces, list):
interfaces = ",".join(interfaces)
interface_policy_group = module.params.get("interface_policy_group")
interface_policy_group_uuid = module.params.get("interface_policy_group_uuid")
Expand Down Expand Up @@ -319,9 +329,9 @@ def main():

if interface_descriptions or (node_changed and mso.existing.get("interfaceDescriptions")):
if node_changed and interface_descriptions is None:
interface_descriptions = format_interface_descriptions(mso.existing["interfaceDescriptions"], node)
interface_descriptions = format_interface_descriptions(mso, mso.existing["interfaceDescriptions"], node)
else:
interface_descriptions = format_interface_descriptions(interface_descriptions, proposed_payload["node"])
interface_descriptions = format_interface_descriptions(mso, interface_descriptions, proposed_payload["node"])
if interface_descriptions != mso.existing.get("interfaceDescriptions"):
ops.append(dict(op="replace", path="{0}/{1}/interfaceDescriptions".format(path, match.index), value=interface_descriptions))
proposed_payload["interfaceDescriptions"] = interface_descriptions
Expand All @@ -333,14 +343,14 @@ def main():

else:
if not node:
mso.fail_json(msg=("ERROR: Missing parameter 'node' for creating a Port Channel Interface"))
mso.fail_json(msg=("Missing parameter 'node' for creating a Port Channel Interface"))
payload = {
"name": name,
"node": node,
"memberInterfaces": interfaces,
"policy": interface_policy_group_uuid,
"description": description,
"interfaceDescriptions": format_interface_descriptions(interface_descriptions, node),
"interfaceDescriptions": format_interface_descriptions(mso, interface_descriptions, node),
}

mso.sanitize(payload)
Expand Down
Loading

0 comments on commit 9e6046b

Please sign in to comment.