Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_port_channel_interface.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Gaspard Micol (@gmicol) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: ndo_port_channel_interface
21 short_description: Manage Port Channel Interfaces on Cisco Nexus Dashboard Orchestrator (NDO).
22 description:
23 - Manage Port Channel Interfaces on Cisco Nexus Dashboard Orchestrator (NDO).
24 - This module is only supported on ND v3.2 (NDO v4.4) and later.
25 author:
26 - Gaspard Micol (@gmicol)
27 options:
28 template:
29 description:
30 - The name of the template.
31 - The template must be a Fabric Resource template.
32 type: str
33 required: true
34 name:
35 description:
36 - The name of the Port Channel Interface.
37 type: str
38 aliases: [ port_channel_interface, port_channel ]
39 uuid:
40 description:
41 - The UUID of the Port Channel Interface.
42 - This parameter can be used instead of O(port_channel_interface)
43 when an existing Port Channel Interface is updated.
44 - This parameter is required when parameter O(port_channel_interface) is updated.
45 type: str
46 aliases: [ port_channel_interface_uuid, port_channel_uuid ]
47 description:
48 description:
49 - The description of the Port Channel Interface.
50 type: str
51 node:
52 description:
53 - The node ID.
54 - This is only required when creating a new Port Channel Interface.
55 type: str
56 interfaces:
57 description:
58 - The list of used Interface IDs.
59 - Ranges of Interface IDs can be used.
60 - This is only required when creating a new Port Channel Interface.
61 type: list
62 elements: str
63 aliases: [ members ]
64 interface_policy_group_uuid:
65 description:
66 - The UUID of the Port Channel Interface Policy Group.
67 - This is only required when creating a new Port Channel Interface.
68 type: str
69 aliases: [ policy_uuid, interface_policy_uuid, interface_setting_uuid ]
70 interface_policy_group:
71 description:
72 - The Port Channel Interface Policy Group.
73 - This parameter can be used instead of O(interface_policy_group_uuid).
74 - If both parameter are used, O(interface_policy_group) will be ignored.
75 type: dict
76 suboptions:
77 name:
78 description:
79 - The name of the Interface Policy Group.
80 type: str
81 required: true
82 template:
83 description:
84 - The name of the template in which the Interface Policy Group has been created.
85 type: str
86 required: true
87 aliases: [ policy, interface_policy, interface_setting ]
88 interface_descriptions:
89 description:
90 - The list of interface descriptions of the Port Channel Interface.
91 - Providing a new list of O(interface_descriptions) will completely
92 replace an existing one from the Port Channel Interface.
93 - Providing an empty list will remove the O(interface_descriptions=[])
94 from the Port Channel Interface.
95 type: list
96 elements: dict
97 suboptions:
98 interface_id:
99 description:
100 - The interface ID or a range of interface IDs.
101 - Using a range of interface IDs will
102 apply the same O(interface_descriptions.description) for every ID in range.
103 type: str
104 required: true
105 description:
106 description:
107 - The description of the interface or group of interfaces.
108 type: str
109 state:
110 description:
111 - Use C(absent) for removing.
112 - Use C(query) for listing an object or multiple objects.
113 - Use C(present) for creating or updating.
114 type: str
115 choices: [ absent, query, present ]
116 default: query
117 extends_documentation_fragment: cisco.mso.modules
118 notes:
119 - The O(template) must exist before using this module in your playbook.
120 Use M(cisco.mso.ndo_template) to create the Fabric Resource template.
121 - The O(interface_policy_group) must exist before using this module in your playbook.
122 Use M(cisco.mso.ndo_interface_setting) to create the Interface Policy Group of type Port Channel.
123 seealso:
124 - module: cisco.mso.ndo_template
125 - module: cisco.mso.ndo_interface_setting
126 """
127
128 EXAMPLES = r"""
129 - name: Create a new Port Channel Interface
130 cisco.mso.ndo_port_channel_interface:
131 host: mso_host
132 username: admin
133 password: SomeSecretPassword
134 template: ansible_fabric_resource_template
135 description: My Ansible Port Channel
136 name: ansible_port_channel_interface
137 node: 101
138 interfaces:
139 - 1/1
140 - 1/10-11
141 interface_policy_group:
142 name: ansible_policy_group
143 template: ansible_fabric_policy_template
144 interface_descriptions:
145 - interface_id: 1/1
146 description: My single Ansible Interface
147 - interface_id: 1/10-11
148 description: My group of Ansible Interfaces
149 state: present
150 register: port_channel_interface_1
151
152 - name: Update a Port Channel Interface's name with UUID
153 cisco.mso.ndo_port_channel_interface:
154 host: mso_host
155 username: admin
156 password: SomeSecretPassword
157 template: ansible_fabric_resource_template
158 name: ansible_port_channel_interface_changed
159 uuid: "{{ port_channel_interface_1.current.uuid }}"
160 state: present
161
162 - name: Update a Port Channel Interface's interfaces and their descriptions
163 cisco.mso.ndo_port_channel_interface:
164 host: mso_host
165 username: admin
166 password: SomeSecretPassword
167 template: ansible_fabric_resource_template
168 name: ansible_port_channel_interface_changed
169 interfaces:
170 - 1/1
171 - 1/3
172 - 1/5-7
173 interface_descriptions:
174 - interface_id: 1/1
175 description: My single unchanged Ansible Interface
176 - interface_id: 1/3
177 description: My new single Ansible Interface
178 - interface_id: 1/5-7
179 description: My new group of Ansible Interfaces
180 state: present
181
182 - name: Query a Port Channel Interface with name
183 cisco.mso.ndo_port_channel_interface:
184 host: mso_host
185 username: admin
186 password: SomeSecretPassword
187 template: ansible_fabric_resource_template
188 name: ansible_port_channel_interface_changed
189 state: query
190 register: query_name
191
192 - name: Query a Port Channel Interface with UUID
193 cisco.mso.ndo_port_channel_interface:
194 host: mso_host
195 username: admin
196 password: SomeSecretPassword
197 template: ansible_fabric_resource_template
198 uuid: "{{ port_channel_interface_1.current.uuid }}"
199 state: query
200 register: query_uuid
201
202 - name: Query all Port Channel Interfaces in a Fabric Resource Template
203 cisco.mso.ndo_port_channel_interface:
204 host: mso_host
205 username: admin
206 password: SomeSecretPassword
207 template: ansible_fabric_resource_template
208 state: query
209 register: query_all
210
211 - name: Delete a Port Channel Interface with name
212 cisco.mso.ndo_port_channel_interface:
213 host: mso_host
214 username: admin
215 password: SomeSecretPassword
216 template: ansible_fabric_resource_template
217 name: ansible_port_channel_interface_changed
218 state: absent
219
220 - name: Delete a Port Channel Interface with UUID
221 cisco.mso.ndo_port_channel_interface:
222 host: mso_host
223 username: admin
224 password: SomeSecretPassword
225 template: ansible_fabric_resource_template
226 uuid: "{{ port_channel_interface_1.current.uuid }}"
227 state: absent
228 """
229
230 RETURN = r"""
231 """
232
233 import copy
234 from ansible.module_utils.basic import AnsibleModule
235 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
236 MSOModule,
237 mso_argument_spec,
238 format_interface_descriptions,
239 )
240 from ansible_collections.cisco.mso.plugins.module_utils.template import (
241 MSOTemplate,
242 KVPair,
243 )
244 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
245
246
247 def main():
248 argument_spec = mso_argument_spec()
249 argument_spec.update(
250 template=dict(type="str", required=True),
251 name=dict(type="str", aliases=["port_channel_interface", "port_channel"]),
252 uuid=dict(type="str", aliases=["port_channel_interface_uuid", "port_channel_uuid"]),
253 description=dict(type="str"),
254 node=dict(type="str"),
255 interfaces=dict(type="list", elements="str", aliases=["members"]),
256 interface_policy_group=dict(
257 type="dict",
258 options=dict(
259 name=dict(type="str", required=True),
260 template=dict(type="str", required=True),
261 ),
262 aliases=["policy", "interface_policy", "interface_setting"],
263 ),
264 interface_policy_group_uuid=dict(type="str", aliases=["policy_uuid", "interface_policy_uuid", "interface_setting_uuid"]),
265 interface_descriptions=dict(
266 type="list",
267 elements="dict",
268 options=dict(
269 interface_id=dict(type="str", required=True),
270 description=dict(type="str"),
271 ),
272 ),
273 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
274 )
275
276 module = AnsibleModule(
277 argument_spec=argument_spec,
278 supports_check_mode=True,
279 required_if=[
280 ["state", "absent", ["name", "uuid"], True],
281 ["state", "present", ["name", "uuid"], True],
282 ],
283 )
284
285 mso = MSOModule(module)
286
287 template = module.params.get("template")
288 name = module.params.get("name")
289 uuid = module.params.get("uuid")
290 description = module.params.get("description")
291 node = module.params.get("node")
292 interfaces = module.params.get("interfaces")
293 if isinstance(interfaces, list):
294 interfaces = ",".join(interfaces)
295 interface_policy_group = module.params.get("interface_policy_group")
296 interface_policy_group_uuid = module.params.get("interface_policy_group_uuid")
297 interface_descriptions = module.params.get("interface_descriptions")
298 state = module.params.get("state")
299
300 mso_template = MSOTemplate(mso, "fabric_resource", template)
301 mso_template.validate_template("fabricResource")
302
303 existing_port_channel_interfaces = mso_template.template.get("fabricResourceTemplate", {}).get("template", {}).get("portChannels", [])
304 object_description = "Port Channel Interface"
305 port_channel_attrs_path = None
306 match = None
307
308 policy_match = dict()
309
310 if state in ["query", "absent"] and not existing_port_channel_interfaces:
311 mso.exit_json()
312 elif state == "query" and not (name or uuid):
313 for port_channel in existing_port_channel_interfaces:
314 policy_match = mso_template.get_fabric_template_object_by_key_value(
315 "pcPolicyGroup", "Interface Settings", [KVPair("uuid", port_channel.get("policy"))]
316 )
317 port_channel["policyName"] = policy_match.get("name")
318 mso.existing = existing_port_channel_interfaces
319 elif existing_port_channel_interfaces and (name or uuid):
320 match = mso_template.get_object_by_key_value_pairs(
321 object_description,
322 existing_port_channel_interfaces,
323 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
324 )
325 if match:
326 port_channel_attrs_path = "/fabricResourceTemplate/template/portChannels/{0}".format(match.index)
327
328 policy_match = mso_template.get_fabric_template_object_by_key_value(
329 "pcPolicyGroup", "Interface Settings", [KVPair("uuid", match.details.get("policy"))]
330 )
331 match.details["policyName"] = policy_match.get("name")
332
333 mso.existing = mso.previous = copy.deepcopy(match.details)
334
335 ops = []
336
337 if state == "present":
338 if uuid and not mso.existing:
339 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
340
341 if interface_policy_group_uuid:
342 policy_match = mso_template.get_fabric_template_object_by_key_value(
343 "pcPolicyGroup", "Interface Settings", [KVPair("uuid", interface_policy_group_uuid)]
344 )
345 elif interface_policy_group and interface_policy_group.get("name"):
346 policy_match = mso_template.get_fabric_template_object_by_key_value(
347 "pcPolicyGroup",
348 "Interface Settings",
349 [KVPair("name", interface_policy_group.get("name")), KVPair("templateName", interface_policy_group.get("template"))],
350 True,
351 )
352 interface_policy_group_uuid = policy_match.get("uuid")
353
354 mso_values = dict(
355 name=name,
356 node=node,
357 memberInterfaces=interfaces,
358 policy=interface_policy_group_uuid,
359 description=description,
360 )
361
362 if mso.existing and match:
363 if node and interface_descriptions:
364 interface_descriptions = format_interface_descriptions(mso, interface_descriptions, node)
365 elif node and interface_descriptions is None and mso.existing.get("interfaceDescriptions"):
366 interface_descriptions = format_interface_descriptions(mso, mso.existing["interfaceDescriptions"], node)
367 elif node is None and interface_descriptions:
368 interface_descriptions = format_interface_descriptions(mso, interface_descriptions, mso.existing["node"])
369 mso_values["interfaceDescriptions"] = interface_descriptions
370 append_update_ops_data(ops, match.details, port_channel_attrs_path, copy.deepcopy(mso_values))
371 mso_values["policyName"] = policy_match.get("name")
372 mso.sanitize(match.details, collate=True)
373 else:
374 if not node:
375 mso.fail_json(msg=("Missing parameter 'node' for creating a Port Channel Interface"))
376 mso_values["interfaceDescriptions"] = format_interface_descriptions(mso, interface_descriptions, node)
377 ops.append(dict(op="add", path="/fabricResourceTemplate/template/portChannels/-", value=copy.deepcopy(mso_values)))
378 mso_values["policyName"] = policy_match.get("name")
379 mso.sanitize(mso_values)
380
381 elif state == "absent":
382 if mso.existing and match:
383 ops.append(dict(op="remove", path=port_channel_attrs_path))
384
385 if not module.check_mode and ops:
386 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
387 port_channel_interfaces = response.get("fabricResourceTemplate", {}).get("template", {}).get("portChannels", []) or []
388 match = mso_template.get_object_by_key_value_pairs(
389 object_description,
390 port_channel_interfaces,
391 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
392 )
393 if match:
394 policy_match = mso_template.get_fabric_template_object_by_key_value(
395 "pcPolicyGroup", "Interface Settings", [KVPair("uuid", match.details.get("policy"))]
396 )
397 match.details["policyName"] = policy_match.get("name")
398 mso.existing = match.details
399 else:
400 mso.existing = {}
401 elif module.check_mode and state != "query":
402 mso.existing = mso.proposed if state == "present" else {}
403
404 mso.exit_json()
405
406
407 if __name__ == "__main__":
408 main()