Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Akini Ross <[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 DOCUMENTATION = r"""
14 ---
15 module: aci_fabric_span_src_group_src
16 short_description: Manage Fabric SPAN sources (span:Src)
17 description:
18 - Manage Fabric SPAN sources on Cisco ACI fabrics.
19 options:
20 description:
21 description:
22 - The description for Fabric SPAN source.
23 type: str
24 aliases: [ descr ]
25 source_group:
26 description:
27 - The name of the Fabric SPAN source group.
28 type: str
29 aliases: [ src_group ]
30 source:
31 description:
32 - The name of the Fabric SPAN source.
33 type: str
34 aliases: [ name, src ]
35 direction:
36 description:
37 - The direction of the SPAN source.
38 - The APIC defaults to C(both) when unset during creation.
39 type: str
40 choices: [ incoming, outgoing, both ]
41 drop_packets:
42 description:
43 - Enable SPAN for only dropped packets.
44 - The APIC defaults to C(false) when unset during creation.
45 type: bool
46 vrf:
47 description:
48 - The SPAN source VRF details.
49 - The I(vrf) and I(bd) cannot be configured simultaneously.
50 type: dict
51 suboptions:
52 tenant:
53 description:
54 - The name of the SPAN source Tenant.
55 type: str
56 required: true
57 aliases: [ tenant_name ]
58 vrf:
59 description:
60 - The name of the SPAN source VRF.
61 type: str
62 required: true
63 aliases: [ vrf_name ]
64 bd:
65 description:
66 - The SPAN source BD details.
67 - The I(vrf) and I(bd) cannot be configured simultaneously.
68 type: dict
69 suboptions:
70 tenant:
71 description:
72 - The name of the SPAN source Tenant.
73 type: str
74 required: true
75 aliases: [ tenant_name ]
76 bd:
77 description:
78 - The name of the SPAN source BD.
79 type: str
80 required: true
81 aliases: [ bd_name, bridge_domain ]
82 state:
83 description:
84 - Use C(present) or C(absent) for adding or removing.
85 - Use C(query) for listing an object or multiple objects.
86 type: str
87 choices: [ absent, present, query ]
88 default: present
89 name_alias:
90 description:
91 - The alias for the current object. This relates to the nameAlias field in ACI.
92 type: str
93 extends_documentation_fragment:
94 - cisco.aci.aci
95 - cisco.aci.annotation
96 - cisco.aci.owner
97
98 notes:
99 - The I(source_group) must exist before using this module in your playbook.
100 The M(cisco.aci.aci_fabric_span_src_group) module can be used for this.
101 seealso:
102 - module: cisco.aci.aci_fabric_span_src_group
103 - module: cisco.aci.aci_tenant
104 - module: cisco.aci.aci_vrf
105 - module: cisco.aci.aci_bd
106 - name: APIC Management Information Model reference
107 description: More information about the internal APIC class B(span:Src).
108 link: https://developer.cisco.com/docs/apic-mim-ref/
109 author:
110 - Akini Ross (@akinross)
111 """
112
113 EXAMPLES = r"""
114 - name: Create a Fabric SPAN source
115 cisco.aci.aci_fabric_span_src_group_src:
116 host: apic
117 username: admin
118 password: SomeSecretPassword
119 source_group: my_span_source_group
120 source: my_source
121 state: present
122 delegate_to: localhost
123
124 - name: Create a Fabric SPAN source with bd
125 cisco.aci.aci_fabric_span_src_group_src:
126 host: apic
127 username: admin
128 password: SomeSecretPassword
129 source_group: my_span_source_group
130 source: my_source
131 bd:
132 tenant: my_tenant
133 bd: my_bd
134 state: present
135 delegate_to: localhost
136
137 - name: Create a Fabric SPAN source with vrf
138 cisco.aci.aci_fabric_span_src_group_src:
139 host: apic
140 username: admin
141 password: SomeSecretPassword
142 source_group: my_span_source_group
143 source: my_source
144 vrf:
145 tenant: my_tenant
146 vrf: my_vrf
147 state: present
148 delegate_to: localhost
149
150 - name: Delete a Fabric SPAN source
151 cisco.aci.aci_fabric_span_src_group_src:
152 host: apic
153 username: admin
154 password: SomeSecretPassword
155 source_group: my_span_source_group
156 source: my_source
157 state: absent
158 delegate_to: localhost
159
160 - name: Query all Fabric SPAN sources
161 cisco.aci.aci_fabric_span_src_group_src:
162 host: apic
163 username: admin
164 password: SomeSecretPassword
165 state: query
166 delegate_to: localhost
167 register: query_result
168
169 - name: Query a specific Fabric SPAN source
170 cisco.aci.aci_fabric_span_src_group_src:
171 host: apic
172 username: admin
173 password: SomeSecretPassword
174 source_group: my_span_source_group
175 source: my_source
176 state: query
177 delegate_to: localhost
178 register: query_result
179 """
180
181 RETURN = r"""
182 current:
183 description: The existing configuration from the APIC after the module has finished
184 returned: success
185 type: list
186 sample:
187 [
188 {
189 "fvTenant": {
190 "attributes": {
191 "descr": "Production environment",
192 "dn": "uni/tn-production",
193 "name": "production",
194 "nameAlias": "",
195 "ownerKey": "",
196 "ownerTag": ""
197 }
198 }
199 }
200 ]
201 error:
202 description: The error information as returned from the APIC
203 returned: failure
204 type: dict
205 sample:
206 {
207 "code": "122",
208 "text": "unknown managed object class foo"
209 }
210 raw:
211 description: The raw output returned by the APIC REST API (xml or json)
212 returned: parse error
213 type: str
214 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
215 sent:
216 description: The actual/minimal configuration pushed to the APIC
217 returned: info
218 type: list
219 sample:
220 {
221 "fvTenant": {
222 "attributes": {
223 "descr": "Production environment"
224 }
225 }
226 }
227 previous:
228 description: The original configuration from the APIC before the module has started
229 returned: info
230 type: list
231 sample:
232 [
233 {
234 "fvTenant": {
235 "attributes": {
236 "descr": "Production",
237 "dn": "uni/tn-production",
238 "name": "production",
239 "nameAlias": "",
240 "ownerKey": "",
241 "ownerTag": ""
242 }
243 }
244 }
245 ]
246 proposed:
247 description: The assembled configuration from the user-provided parameters
248 returned: info
249 type: dict
250 sample:
251 {
252 "fvTenant": {
253 "attributes": {
254 "descr": "Production environment",
255 "name": "production"
256 }
257 }
258 }
259 filter_string:
260 description: The filter string used for the request
261 returned: failure or debug
262 type: str
263 sample: ?rsp-prop-include=config-only
264 method:
265 description: The HTTP method used for the request to the APIC
266 returned: failure or debug
267 type: str
268 sample: POST
269 response:
270 description: The HTTP response from the APIC
271 returned: failure or debug
272 type: str
273 sample: OK (30 bytes)
274 status:
275 description: The HTTP status from the APIC
276 returned: failure or debug
277 type: int
278 sample: 200
279 url:
280 description: The HTTP url used for the request to the APIC
281 returned: failure or debug
282 type: str
283 sample: https://10.11.12.13/api/mo/uni/tn-production.json
284 """
285
286 from ansible.module_utils.basic import AnsibleModule
287 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
288 from ansible_collections.cisco.aci.plugins.module_utils.constants import SPAN_DIRECTION_MAP
289
290
291 def main():
292 argument_spec = aci_argument_spec()
293 argument_spec.update(aci_annotation_spec())
294 argument_spec.update(aci_owner_spec())
295 argument_spec.update(
296 source_group=dict(type="str", aliases=["src_group"]), # Not required for querying all objects
297 description=dict(type="str", aliases=["descr"]),
298 source=dict(type="str", aliases=["name", "src"]), # Not required for querying all objects
299 direction=dict(type="str", choices=list(SPAN_DIRECTION_MAP.keys())),
300 drop_packets=dict(type="bool"),
301 vrf=dict(
302 type="dict",
303 options=dict(
304 vrf=dict(type="str", required=True, aliases=["vrf_name"]),
305 tenant=dict(type="str", required=True, aliases=["tenant_name"]),
306 ),
307 ),
308 bd=dict(
309 type="dict",
310 options=dict(
311 bd=dict(type="str", required=True, aliases=["bd_name", "bridge_domain"]),
312 tenant=dict(type="str", required=True, aliases=["tenant_name"]),
313 ),
314 ),
315 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
316 name_alias=dict(type="str"),
317 )
318
319 module = AnsibleModule(
320 argument_spec=argument_spec,
321 supports_check_mode=True,
322 required_if=[
323 ["state", "absent", ["source_group", "source"]],
324 ["state", "present", ["source_group", "source"]],
325 ],
326 mutually_exclusive=[
327 ("vrf", "bd"),
328 ],
329 )
330
331 aci = ACIModule(module)
332
333 description = module.params.get("description")
334 source_group = module.params.get("source_group")
335 source = module.params.get("source")
336 direction = module.params.get("direction")
337 drop_packets = module.params.get("drop_packets")
338 vrf = module.params.get("vrf")
339 bd = module.params.get("bd")
340 state = module.params.get("state")
341 name_alias = module.params.get("name_alias")
342
343 if vrf and drop_packets:
344 module.fail_json(msg="It is not allowed to configure 'drop_packets: true' when 'vrf' is configured on the source.")
345 elif bd and drop_packets:
346 module.fail_json(msg="It is not allowed to configure 'drop_packets: true' when 'bd' is configured on the source.")
347
348 aci.construct_url(
349 root_class=dict(
350 aci_class="fabric",
351 aci_rn="fabric",
352 ),
353 subclass_1=dict(
354 aci_class="spanSrcGrp",
355 aci_rn="srcgrp-{0}".format(source_group),
356 module_object=source_group,
357 target_filter={"name": source_group},
358 ),
359 subclass_2=dict(
360 aci_class="spanSrc",
361 aci_rn="src-{0}".format(source),
362 module_object=source,
363 target_filter={"name": source},
364 ),
365 child_classes=["spanRsSrcToCtx", "spanRsSrcToBD"],
366 )
367
368 aci.get_existing()
369
370 if state == "present":
371 # Create new child configs payload
372 child_configs = []
373 vrf_dn = bd_dn = None
374
375 if vrf:
376 vrf_dn = "uni/tn-{0}/ctx-{1}".format(vrf.get("tenant"), vrf.get("vrf"))
377 child_configs.append({"spanRsSrcToCtx": {"attributes": {"tDn": vrf_dn}}})
378 elif bd:
379 bd_dn = "uni/tn-{0}/BD-{1}".format(bd.get("tenant"), bd.get("bd"))
380 child_configs.append({"spanRsSrcToBD": {"attributes": {"tDn": bd_dn}}})
381
382 # Validate if existing and remove child objects when do not match provided configuration
383 if isinstance(aci.existing, list) and len(aci.existing) > 0:
384 source_path = "/api/mo/uni/fabric/srcgrp-{0}/src-{1}".format(source_group, source)
385 for child in aci.existing[0].get("spanSrc", {}).get("children", {}):
386 if child.get("spanRsSrcToCtx") and child.get("spanRsSrcToCtx").get("attributes").get("tDn") != vrf_dn:
387 # Appending to child_config list not possible because of APIC Error 103: child (Rn) of class spanRsSrcToCtx is already attached.
388 aci.api_call("DELETE", "{0}/rssrcToCtx.json".format(source_path))
389 elif child.get("spanRsSrcToBD") and child.get("spanRsSrcToBD").get("attributes").get("tDn") != bd_dn:
390 # Appending to child_config list not possible because of APIC Error 103: child (Rn) of class spanRsSrcToBD is already attached.
391 aci.api_call("DELETE", "{0}/rssrcToBD.json".format(source_path))
392
393 aci.payload(
394 aci_class="spanSrc",
395 class_config=dict(
396 descr=description,
397 name=source,
398 dir=SPAN_DIRECTION_MAP.get(direction),
399 spanOnDrop=aci.boolean(drop_packets),
400 nameAlias=name_alias,
401 ),
402 child_configs=child_configs,
403 )
404
405 aci.get_diff(aci_class="spanSrc")
406
407 aci.post_config()
408
409 elif state == "absent":
410 aci.delete_config()
411
412 aci.exit_json()
413
414
415 if __name__ == "__main__":
416 main()