Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_interface_config.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022, Sabari Jaganathan <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
12
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_interface_config
17 short_description: Manage Interface Configuration of Access (infra:PortConfig) and Fabric (fabric:PortConfig) Ports is only supported for ACI 5.2(7)+
18 description:
19 - Manage Interface Configuration of Access (infra:PortConfig) and Fabric (fabric:PortConfig) Ports is only supported for ACI 5.2(7)+
20 options:
21 policy_group:
22 description:
23 - The name of the Policy Group being associated with the Port.
24 - The I(policy_group) and I(breakout) cannot be configured simultaneously.
25 type: str
26 aliases: [ policy_group_name ]
27 breakout:
28 description:
29 - The Breakout Map of the interface.
30 - The I(policy_group) and I(breakout) cannot be configured simultaneously.
31 type: str
32 choices: [ 100g-2x, 100g-4x, 10g-4x, 25g-4x, 50g-8x ]
33 description:
34 description:
35 - The description of the Interface Configuration object.
36 type: str
37 aliases: [ descr ]
38 node:
39 description:
40 - The ID of the Node.
41 - The value must be between 101 to 4000.
42 type: int
43 aliases: [ node_id ]
44 pc_member:
45 description:
46 - The name of the Port Channel Member Policy (lacp:IfPol).
47 - A Port Channel Member Policy is used to override LACP port priority and transmit rate of LACP packets.
48 type: str
49 aliases: [ port_channel_member ]
50 port_type:
51 description:
52 - The type of the interface can be either access or fabric.
53 type: str
54 default: access
55 choices: [ access, fabric ]
56 role:
57 description:
58 - The role of the switch (node) can be either a leaf or a spine.
59 - The APIC defaults to leaf when unset during creation.
60 type: str
61 aliases: [ node_type ]
62 choices: [ leaf, spine ]
63 admin_state:
64 description:
65 - The Admin State of the Interface.
66 - The APIC defaults to up when unset during creation.
67 type: str
68 choices: [ up, down ]
69 interface_type:
70 description:
71 - The type of the interface.
72 type: str
73 default: switch_port
74 choices: [ switch_port, pc_or_vpc, fc, fc_port_channel, leaf_fabric, spine_access, spine_fabric ]
75 interface:
76 description:
77 - The address of the interface.
78 - The format of the interface value should be 1/1/1 (card/port_id/sub_port) or 1/1 (card/port_id).
79 - The Card ID must be in the range of 1 to 255.
80 - The Port ID must be in the range of 1 to 128.
81 - The Sub Port ID must be in the range of 0 to 64.
82 type: str
83 state:
84 description:
85 - Use C(present) or C(absent) for adding or removing.
86 - Use C(query) for listing an object or multiple objects.
87 type: str
88 choices: [ absent, present, query ]
89 default: present
90 extends_documentation_fragment:
91 - cisco.aci.aci
92 - cisco.aci.annotation
93 - cisco.aci.owner
94
95 seealso:
96 - name: APIC Management Information Model reference
97 description: More information about the internal APIC class B(infra:PortConfig).
98 link: https://developer.cisco.com/docs/apic-mim-ref/
99 author:
100 - Sabari Jaganathan (@sajagana)
101 """
102 EXAMPLES = r"""
103 - name: Add an interface with port channel(PC) policy group
104 cisco.aci.aci_interface_config:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 role: leaf
109 port_type: access
110 interface_type: pc_or_vpc
111 policy_group: ans_test_port_channel
112 node: 502
113 interface: "2/2/2"
114 state: present
115 delegate_to: localhost
116
117 - name: Add an interface with port channel(PC) policy group with a PC member policy override
118 cisco.aci.aci_interface_config:
119 host: apic
120 username: admin
121 password: SomeSecretPassword
122 role: leaf
123 port_type: access
124 interface_type: pc_or_vpc
125 policy_group: ans_test_port_channel
126 pc_member: ans_test_pc_member_policy
127 node: 502
128 interface: "2/2"
129 state: present
130 delegate_to: localhost
131
132 - name: Breakout the existing interface with "100g-4x"
133 cisco.aci.aci_interface_config:
134 host: apic
135 username: admin
136 password: SomeSecretPassword
137 role: leaf
138 port_type: access
139 node: 502
140 interface: "2/2/2"
141 breakout: "100g-4x"
142 state: present
143 delegate_to: localhost
144
145 - name: Query an access interface with node id
146 cisco.aci.aci_interface_config:
147 host: apic
148 username: admin
149 password: SomeSecretPassword
150 port_type: access
151 node: 201
152 state: query
153 delegate_to: localhost
154
155 - name: Query a fabric interface with node id
156 cisco.aci.aci_interface_config:
157 host: apic
158 username: admin
159 password: SomeSecretPassword
160 port_type: fabric
161 node: 202
162 state: query
163 delegate_to: localhost
164
165 - name: Query all access interfaces
166 cisco.aci.aci_interface_config:
167 host: apic
168 username: admin
169 password: SomeSecretPassword
170 port_type: access
171 state: query
172 delegate_to: localhost
173
174 - name: Query all fabric interfaces
175 cisco.aci.aci_interface_config:
176 host: apic
177 username: admin
178 password: SomeSecretPassword
179 port_type: fabric
180 state: query
181 delegate_to: localhost
182
183 - name: Remove a interface
184 cisco.aci.aci_interface_config:
185 host: apic
186 username: admin
187 password: SomeSecretPassword
188 port_type: access
189 node: 201
190 interface: "1/1/1"
191 state: absent
192 delegate_to: localhost
193 """
194
195 RETURN = r"""
196 current:
197 description: The existing configuration from the APIC after the module has finished
198 returned: success
199 type: list
200 sample:
201 [
202 {
203 "fvTenant": {
204 "attributes": {
205 "descr": "Production environment",
206 "dn": "uni/tn-production",
207 "name": "production",
208 "nameAlias": "",
209 "ownerKey": "",
210 "ownerTag": ""
211 }
212 }
213 }
214 ]
215 error:
216 description: The error information as returned from the APIC
217 returned: failure
218 type: dict
219 sample:
220 {
221 "code": "122",
222 "text": "unknown managed object class foo"
223 }
224 raw:
225 description: The raw output returned by the APIC REST API (xml or json)
226 returned: parse error
227 type: str
228 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
229 sent:
230 description: The actual/minimal configuration pushed to the APIC
231 returned: info
232 type: list
233 sample:
234 {
235 "fvTenant": {
236 "attributes": {
237 "descr": "Production environment"
238 }
239 }
240 }
241 previous:
242 description: The original configuration from the APIC before the module has started
243 returned: info
244 type: list
245 sample:
246 [
247 {
248 "fvTenant": {
249 "attributes": {
250 "descr": "Production",
251 "dn": "uni/tn-production",
252 "name": "production",
253 "nameAlias": "",
254 "ownerKey": "",
255 "ownerTag": ""
256 }
257 }
258 }
259 ]
260 proposed:
261 description: The assembled configuration from the user-provided parameters
262 returned: info
263 type: dict
264 sample:
265 {
266 "fvTenant": {
267 "attributes": {
268 "descr": "Production environment",
269 "name": "production"
270 }
271 }
272 }
273 filter_string:
274 description: The filter string used for the request
275 returned: failure or debug
276 type: str
277 sample: ?rsp-prop-include=config-only
278 method:
279 description: The HTTP method used for the request to the APIC
280 returned: failure or debug
281 type: str
282 sample: POST
283 response:
284 description: The HTTP response from the APIC
285 returned: failure or debug
286 type: str
287 sample: OK (30 bytes)
288 status:
289 description: The HTTP status from the APIC
290 returned: failure or debug
291 type: int
292 sample: 200
293 url:
294 description: The HTTP url used for the request to the APIC
295 returned: failure or debug
296 type: str
297 sample: https://10.11.12.13/api/mo/uni/tn-production.json
298 """
299
300
301 from ansible.module_utils.basic import AnsibleModule
302 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
303
304 PORT_TYPE_MAPPING = dict(
305 access=dict(
306 root_class="infraInfra",
307 root_class_rn="infra",
308 interface_class="infraPortConfig",
309 ),
310 fabric=dict(
311 root_class="fabricInst",
312 root_class_rn="fabric",
313 interface_class="fabricPortConfig",
314 ),
315 )
316
317 ADMIN_STATE_MAPPING = {"up": "no", "down": "yes"}
318
319
320 POLICY_GROUP_MAPPING = dict(
321 switch_port="uni/infra/funcprof/accportgrp-{0}",
322 pc_or_vpc="uni/infra/funcprof/accbundle-{0}",
323 fc="uni/infra/funcprof/fcaccportgrp-{0}",
324 fc_port_channel="uni/infra/funcprof/fcaccbundle-{0}",
325 leaf_fabric="uni/fabric/funcprof/leportgrp-{0}",
326 spine_access="uni/infra/funcprof/spaccportgrp-{0}",
327 spine_fabric="uni/fabric/funcprof/spportgrp-{0}",
328 )
329
330
331 def main():
332 argument_spec = aci_argument_spec()
333 argument_spec.update(aci_annotation_spec())
334 argument_spec.update(aci_owner_spec())
335 argument_spec.update(
336 policy_group=dict(type="str", aliases=["policy_group_name"]),
337 breakout=dict(type="str", choices=["100g-2x", "100g-4x", "10g-4x", "25g-4x", "50g-8x"]),
338 description=dict(type="str", aliases=["descr"]),
339 node=dict(type="int", aliases=["node_id"]),
340 pc_member=dict(type="str", aliases=["port_channel_member"]),
341 port_type=dict(type="str", default="access", choices=["access", "fabric"]),
342 role=dict(type="str", choices=["leaf", "spine"], aliases=["node_type"]),
343 admin_state=dict(type="str", choices=["up", "down"]),
344 interface_type=dict(
345 type="str",
346 default="switch_port",
347 choices=["switch_port", "pc_or_vpc", "fc", "fc_port_channel", "leaf_fabric", "spine_access", "spine_fabric"],
348 ),
349 interface=dict(type="str"),
350 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
351 )
352
353 module = AnsibleModule(
354 argument_spec=argument_spec,
355 supports_check_mode=True,
356 required_if=[
357 ["state", "absent", ["node", "interface"]],
358 ["state", "present", ["node", "interface"]],
359 ],
360 mutually_exclusive=[("policy_group", "breakout")],
361 )
362
363 policy_group = module.params.get("policy_group")
364 breakout = module.params.get("breakout")
365 description = module.params.get("description")
366 node = module.params.get("node")
367 pc_member = module.params.get("pc_member")
368 port_type = module.params.get("port_type")
369 role = module.params.get("role")
370 admin_state = module.params.get("admin_state")
371 interface = module.params.get("interface")
372 interface_type = module.params.get("interface_type")
373 state = module.params.get("state")
374
375 aci = ACIModule(module)
376
377 try:
378 if node is not None and int(node) not in range(101, 4001):
379 aci.fail_json(msg="Node ID: {0} is invalid; it must be in the range of 101 to 4000.".format(node))
380
381 card, port_id, sub_port = (None, None, None)
382 if interface is not None:
383 interface_parts = interface.split("/")
384 if len(interface_parts) == 3:
385 card, port_id, sub_port = interface_parts
386 elif len(interface_parts) == 2:
387 card, port_id = interface_parts
388 sub_port = 0
389 else:
390 aci.fail_json(msg="Interface: {0} is invalid; The format must be either card/port/sub_port(1/1/1) or card/port(1/1)".format(interface))
391
392 if int(card) not in range(1, 256):
393 aci.fail_json(msg="Card ID: {0} is invalid; it must be in the range of 1 to 255.".format(card))
394
395 if int(port_id) not in range(1, 129):
396 aci.fail_json(msg="Port ID: {0} is invalid; it must be in the range of 1 to 128.".format(port_id))
397
398 # Sub Port ID - 0 is default value
399 if int(sub_port) not in range(0, 65):
400 aci.fail_json(msg="Sub Port ID: {0} is invalid; it must be in the range of 0 to 64.".format(sub_port))
401 except ValueError as error:
402 aci.fail_json(msg="Interface configuration failed due to: {0}".format(error))
403
404 root_class = PORT_TYPE_MAPPING.get(port_type)["root_class"]
405 root_class_rn = PORT_TYPE_MAPPING.get(port_type)["root_class_rn"]
406 interface_class = PORT_TYPE_MAPPING.get(port_type)["interface_class"]
407
408 aci.construct_url(
409 root_class=dict(
410 aci_class=root_class,
411 aci_rn=root_class_rn,
412 ),
413 subclass_1=dict(
414 aci_class=interface_class,
415 aci_rn="portconfnode-{0}-card-{1}-port-{2}-sub-{3}".format(node, card, port_id, sub_port),
416 target_filter=dict(node=node),
417 ),
418 )
419
420 aci.get_existing()
421
422 if breakout is None and policy_group:
423 policy_group_dn = POLICY_GROUP_MAPPING.get(interface_type).format(policy_group)
424 else:
425 # To handle the existing object property
426 policy_group_dn = ""
427
428 if state == "present":
429 aci.payload(
430 aci_class=interface_class,
431 class_config=dict(
432 assocGrp=policy_group_dn,
433 brkoutMap=breakout,
434 card=card,
435 description=description,
436 node=node,
437 pcMember=pc_member,
438 port=port_id,
439 role=role,
440 shutdown=ADMIN_STATE_MAPPING.get(admin_state),
441 subPort=sub_port,
442 ),
443 )
444
445 aci.get_diff(aci_class=interface_class)
446
447 aci.post_config()
448
449 elif state == "absent":
450 aci.delete_config()
451
452 aci.exit_json()
453
454
455 if __name__ == "__main__":
456 main()