Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: aci_l2out_logical_interface_path
15 short_description: Manage Layer 2 Outside (L2Out) logical interface path (l2ext:RsPathL2OutAtt)
16 description:
17 - Manage interface path entry of L2 outside node (BD extension) on Cisco ACI fabrics.
18 options:
19 tenant:
20 description:
21 - Name of an existing tenant.
22 type: str
23 aliases: [ tenant_name ]
24 l2out:
25 description:
26 - Name of an existing L2Out.
27 type: str
28 aliases: [ l2out_name ]
29 node_profile:
30 description:
31 - Name of the node profile.
32 type: str
33 aliases: [ node_profile_name, logical_node ]
34 interface_profile:
35 description:
36 - Name of the interface profile.
37 type: str
38 aliases: [ name, interface_profile_name, logical_interface ]
39 interface_type:
40 description:
41 - The type of interface for the static EPG deployment.
42 type: str
43 choices: [ switch_port, port_channel, vpc ]
44 default: switch_port
45 pod_id:
46 description:
47 - The pod number part of the tDn.
48 - C(pod_id) is usually an integer below C(10).
49 type: int
50 aliases: [ pod, pod_number ]
51 leaves:
52 description:
53 - The switch ID(s) that the C(interface) belongs to.
54 - When C(interface_type) is C(switch_port) or C(port_channel), then C(leaves) is a string of the leaf ID.
55 - When C(interface_type) is C(vpc), then C(leaves) is a list with both leaf IDs.
56 - The C(leaves) value is usually something like '101' or '101-102' depending on C(connection_type).
57 type: list
58 elements: str
59 aliases: [ leafs, nodes, paths, switches ]
60 interface:
61 description:
62 - The C(interface) string value part of the tDn.
63 - Usually a policy group like C(test-IntPolGrp) or an interface of the following format C(1/7) depending on C(interface_type).
64 type: str
65 state:
66 description:
67 - Use C(present) or C(absent) for adding or removing.
68 - Use C(query) for listing an object or multiple objects.
69 type: str
70 choices: [ absent, present, query ]
71 default: present
72 extends_documentation_fragment:
73 - cisco.aci.aci
74 - cisco.aci.annotation
75
76 seealso:
77 - module: cisco.aci.aci_l2out
78 - module: cisco.aci.aci_l2out_logical_node_profile
79 - module: cisco.aci.aci_l2out_logical_interface_profile
80 - module: cisco.aci.aci_l2out_extepg
81 - name: APIC Management Information Model reference
82 description: More information about the internal APIC class B(l2ext:RsPathL2OutAtt).
83 link: https://developer.cisco.com/docs/apic-mim-ref/
84 author:
85 - Oleksandr Kreshchenko (@alexkross)
86 """
87
88 EXAMPLES = r"""
89 - name: Add new path to interface profile
90 cisco.aci.aci_l2out_logical_interface_path:
91 host: apic
92 username: admin
93 password: SomeSecretPassword
94 tenant: my_tenant
95 l2out: my_l2out
96 node_profile: my_node_profile
97 interface_profile: my_interface_profile
98 pod_id: 1
99 leaves: 101-102
100 interface: L2o1
101 state: present
102 delegate_to: localhost
103
104 - name: Delete path to interface profile
105 cisco.aci.aci_l2out_logical_interface_path:
106 host: apic
107 username: admin
108 password: SomeSecretPassword
109 tenant: my_tenant
110 l2out: my_l2out
111 node_profile: my_node_profile
112 interface_profile: my_interface_profile
113 pod_id: 1
114 leaves: 101-102
115 interface: L2o1
116 state: absent
117 delegate_to: localhost
118
119 - name: Query a path to interface profile
120 cisco.aci.aci_l2out_logical_interface_path:
121 host: apic
122 username: admin
123 password: SomeSecretPassword
124 tenant: my_tenant
125 l2out: my_l2out
126 node_profile: my_node_profile
127 interface_profile: my_interface_profile
128 pod_id: 1
129 leaves: 101-102
130 interface: L2o1
131 state: query
132 delegate_to: localhost
133 register: query_result
134
135 - name: Query all paths to interface profiles
136 cisco.aci.aci_l2out_logical_interface_path:
137 host: apic
138 username: admin
139 password: SomeSecretPassword
140 state: query
141 delegate_to: localhost
142 register: query_result
143 """
144
145 RETURN = r"""
146 current:
147 description: The existing configuration from the APIC after the module has finished
148 returned: success
149 type: list
150 sample:
151 [
152 {
153 "fvTenant": {
154 "attributes": {
155 "descr": "Production environment",
156 "dn": "uni/tn-production",
157 "name": "production",
158 "nameAlias": "",
159 "ownerKey": "",
160 "ownerTag": ""
161 }
162 }
163 }
164 ]
165 error:
166 description: The error information as returned from the APIC
167 returned: failure
168 type: dict
169 sample:
170 {
171 "code": "122",
172 "text": "unknown managed object class foo"
173 }
174 raw:
175 description: The raw output returned by the APIC REST API (xml or json)
176 returned: parse error
177 type: str
178 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
179 sent:
180 description: The actual/minimal configuration pushed to the APIC
181 returned: info
182 type: list
183 sample:
184 {
185 "fvTenant": {
186 "attributes": {
187 "descr": "Production environment"
188 }
189 }
190 }
191 previous:
192 description: The original configuration from the APIC before the module has started
193 returned: info
194 type: list
195 sample:
196 [
197 {
198 "fvTenant": {
199 "attributes": {
200 "descr": "Production",
201 "dn": "uni/tn-production",
202 "name": "production",
203 "nameAlias": "",
204 "ownerKey": "",
205 "ownerTag": ""
206 }
207 }
208 }
209 ]
210 proposed:
211 description: The assembled configuration from the user-provided parameters
212 returned: info
213 type: dict
214 sample:
215 {
216 "fvTenant": {
217 "attributes": {
218 "descr": "Production environment",
219 "name": "production"
220 }
221 }
222 }
223 filter_string:
224 description: The filter string used for the request
225 returned: failure or debug
226 type: str
227 sample: ?rsp-prop-include=config-only
228 method:
229 description: The HTTP method used for the request to the APIC
230 returned: failure or debug
231 type: str
232 sample: POST
233 response:
234 description: The HTTP response from the APIC
235 returned: failure or debug
236 type: str
237 sample: OK (30 bytes)
238 status:
239 description: The HTTP status from the APIC
240 returned: failure or debug
241 type: int
242 sample: 200
243 url:
244 description: The HTTP url used for the request to the APIC
245 returned: failure or debug
246 type: str
247 sample: https://10.11.12.13/api/mo/uni/tn-production.json
248 """
249
250
251 from ansible.module_utils.basic import AnsibleModule
252 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
253
254 INTERFACE_TYPE_MAPPING = dict(
255 switch_port="topology/pod-{pod_id}/paths-{leaves}/pathep-[eth{interface}]",
256 port_channel="topology/pod-{pod_id}/paths-{leaves}/pathep-[{interface}]",
257 vpc="topology/pod-{pod_id}/protpaths-{leaves}/pathep-[{interface}]",
258 )
259
260
261 def main():
262 argument_spec = aci_argument_spec()
263 argument_spec.update(aci_annotation_spec())
264 argument_spec.update( # See comments in aci_static_binding_to_epg module.
265 tenant=dict(type="str", aliases=["tenant_name"]),
266 l2out=dict(type="str", aliases=["l2out_name"]),
267 node_profile=dict(type="str", aliases=["node_profile_name", "logical_node"]),
268 interface_profile=dict(type="str", aliases=["name", "interface_profile_name", "logical_interface"]),
269 interface_type=dict(type="str", default="switch_port", choices=["switch_port", "port_channel", "vpc"]),
270 pod_id=dict(type="int", aliases=["pod", "pod_number"]),
271 leaves=dict(type="list", elements="str", aliases=["leafs", "nodes", "paths", "switches"]),
272 interface=dict(type="str"),
273 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
274 )
275
276 module = AnsibleModule(
277 argument_spec=argument_spec,
278 supports_check_mode=True,
279 required_if=[
280 ["state", "absent", ["tenant", "l2out", "node_profile", "interface_profile", "pod_id", "leaves", "interface"]],
281 ["state", "present", ["tenant", "l2out", "node_profile", "interface_profile", "pod_id", "leaves", "interface"]],
282 ],
283 )
284
285 tenant = module.params.get("tenant")
286 l2out = module.params.get("l2out")
287 node_profile = module.params.get("node_profile")
288 interface_profile = module.params.get("interface_profile")
289 interface_type = module.params.get("interface_type")
290 pod_id = module.params.get("pod_id")
291 leaves = module.params.get("leaves")
292 if leaves is not None: # Process leaves, and support dash-delimited leaves
293 leaves = []
294 for leaf in module.params.get("leaves"): # Users are likely to use integers for leaf IDs, which would raise an exception when using the join method
295 leaves.extend(str(leaf).split("-"))
296 if len(leaves) == 1:
297 if interface_type == "vpc":
298 module.fail_json(msg='A interface_type of "vpc" requires 2 leaves')
299 leaves = leaves[0]
300 elif len(leaves) == 2:
301 if interface_type != "vpc":
302 module.fail_json(msg='The interface_types "switch_port" and "port_channel" do not support using multiple leaves for a single binding')
303 leaves = "-".join(leaves)
304 else:
305 module.fail_json(msg='The "leaves" parameter must not have more than 2 entries')
306 interface = module.params.get("interface")
307 state = module.params.get("state")
308
309 path = INTERFACE_TYPE_MAPPING[interface_type].format(pod_id=pod_id, leaves=leaves, interface=interface)
310 if not pod_id or not leaves or not interface:
311 path = None
312
313 path_target_filter = {}
314 if any((pod_id, leaves, interface)):
315 path_target_filter = {"tDn": path}
316
317 aci = ACIModule(module)
318
319 aci.construct_url(
320 root_class=dict(
321 aci_class="fvTenant",
322 aci_rn="tn-{0}".format(tenant),
323 module_object=tenant,
324 target_filter={"name": tenant},
325 ),
326 subclass_1=dict(
327 aci_class="l2extOut",
328 aci_rn="l2out-{0}".format(l2out),
329 module_object=l2out,
330 target_filter={"name": l2out},
331 ),
332 subclass_2=dict(
333 aci_class="l2extLNodeP",
334 aci_rn="lnodep-{0}".format(node_profile),
335 module_object=node_profile,
336 target_filter={"name": node_profile},
337 ),
338 subclass_3=dict(
339 aci_class="l2extLIfP",
340 aci_rn="lifp-{0}".format(interface_profile),
341 module_object=interface_profile,
342 target_filter={"name": interface_profile},
343 ),
344 subclass_4=dict(
345 aci_class="l2extRsPathL2OutAtt",
346 aci_rn="rspathL2OutAtt-[{0}]".format(path),
347 # rspathL2OutAtt-[topology/pod-1/protpaths-101-102/pathep-[L2o2_n7]]
348 module_object=path,
349 target_filter=path_target_filter,
350 ),
351 )
352
353 aci.get_existing()
354
355 if state == "present":
356 aci.payload(
357 aci_class="l2extRsPathL2OutAtt",
358 class_config=dict(tDn=path),
359 )
360
361 aci.get_diff(aci_class="l2extRsPathL2OutAtt")
362
363 aci.post_config()
364
365 elif state == "absent":
366 aci.delete_config()
367
368 aci.exit_json()
369
370
371 if __name__ == "__main__":
372 main()