Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Gaspard Micol (@gmicol) <[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_interface_policy_pim
16 short_description: Manage Protocol-Independent Multicast (PIM) interface policies (pim:IfPol)
17 description:
18 - Manage Protocol Independent Multicast interface policies for Tenants on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - The name of an existing Tenant.
23 type: str
24 aliases: [ tenant_name ]
25 pim:
26 description:
27 - The PIM interface policy name.
28 - The name cannot be changed after the object has been created.
29 type: str
30 aliases: [ pim_interface_policy, name ]
31 authentication_key:
32 description:
33 - The authentication key.
34 type: str
35 aliases: [ auth_key ]
36 authentication_type:
37 description:
38 - The authentication type.
39 type: str
40 choices: [ none, md5_hmac ]
41 aliases: [ auth_type ]
42 control_state:
43 description:
44 - The PIM interface policy control state.
45 - 'This is a list of one or more of the following controls:'
46 - C(multicast_domain_boundary) -- Boundary of Multicast domain.
47 - C(strict_rfc_compliant) -- Only listen to PIM protocol packets.
48 - C(passive) -- Do not send/receive PIM protocol packets.
49 type: list
50 elements: str
51 choices: [ multicast_domain_boundary, strict_rfc_compliant, passive ]
52 designated_router_delay:
53 description:
54 - The PIM designated router delay.
55 - Accepted values range between C(1) and C(65535).
56 - The APIC defaults to C(3) when unset during creation.
57 type: int
58 aliases: [ delay ]
59 designated_router_priority:
60 description:
61 - The PIM designated router priority.
62 - Accepted values range between C(1) and C(4294967295).
63 - The APIC defaults to C(1) when unset during creation.
64 type: int
65 aliases: [ prio ]
66 hello_interval:
67 description:
68 - The time interval in seconds between hello packets that PIM sends on the interface.
69 - The smaller the hello interval, the faster topological changes will be detected, but more routing traffic will ensue.
70 - Accepted values range between C(1) and C(18724286).
71 - The APIC defaults to C(30000) when unset during creation.
72 type: int
73 join_prune_interval:
74 description:
75 - The join prune interval in seconds.
76 - Accepted values range between C(60) and C(65520).
77 - The APIC defaults to C(60) when unset during creation.
78 type: int
79 aliases: [ jp_interval ]
80 inbound_join_prune_filter_policy:
81 description:
82 - The interface-level inbound join/prune filter policy.
83 - The M(cisco.aci.aci_pim_route_map_policy) can be used for this.
84 - To delete it, pass an empty string.
85 type: str
86 aliases: [ inbound_filter ]
87 outbound_join_prune_filter_policy:
88 description:
89 - The interface-level outbound join/prune filter policy.
90 - The M(cisco.aci.aci_pim_route_map_policy) can be used for this.
91 - To delete it, pass an empty string.
92 type: str
93 aliases: [ outbound_filter ]
94 neighbor_filter_policy:
95 description:
96 - The Interface-level neighbor filter policy.
97 - The M(cisco.aci.aci_pim_route_map_policy) can be used for this.
98 - To delete it, pass an empty string.
99 type: str
100 aliases: [ neighbor_filter ]
101 description:
102 description:
103 - The description of the PIM interface policy.
104 type: str
105 aliases: [ descr ]
106 state:
107 description:
108 - Use C(present) or C(absent) for adding or removing.
109 - Use C(query) for listing an object or multiple objects.
110 type: str
111 choices: [ absent, present, query ]
112 default: present
113 name_alias:
114 description:
115 - The alias for the current object. This relates to the nameAlias field in ACI.
116 type: str
117 extends_documentation_fragment:
118 - cisco.aci.aci
119 - cisco.aci.annotation
120 - cisco.aci.owner
121
122 notes:
123 - The C(tenant) used must exist before using this module in your playbook.
124 The M(cisco.aci.aci_tenant) module can be used for this.
125 seealso:
126 - module: cisco.aci.aci_tenant
127 - module: cisco.aci.aci_pim_route_map_policy
128 - name: APIC Management Information Model reference
129 description: More information about the internal APIC class B(pim:IfPol).
130 link: https://developer.cisco.com/docs/apic-mim-ref/
131 author:
132 - Gaspard Micol (@gmicol)
133 """
134
135 EXAMPLES = r"""
136 - name: Create a PIM interface policy
137 cisco.aci.aci_interface_policy_pim:
138 host: apic
139 username: admin
140 password: SomeSecretPassword
141 tenant: production
142 pim: my_pim_policy
143 control_state: [multicast_domain_boundary, strict_rfc_compliant]
144 designated_router_delay: 10
145 designated_router_priority: tens_of_micro
146 hello_interval: 5
147 join_prune_interval: 15
148 inbound_join_prune_filter_policy: my_pim_route_map_policy_1
149 outbound_join_prune_filter_policy: my_pim_route_map_policy_2
150 neighbor_filter_policy: my_pim_route_map_policy_3
151 state: present
152 delegate_to: localhost
153
154 - name: Query a PIM interface policy
155 cisco.aci.aci_interface_policy_pim:
156 host: apic
157 username: admin
158 password: SomeSecretPassword
159 tenant: production
160 pim: my_pim_policy
161 state: query
162 delegate_to: localhost
163 register: query_result
164
165 - name: Query all PIM interface policies in tenant production
166 cisco.aci.aci_interface_policy_pim:
167 host: apic
168 username: admin
169 password: SomeSecretPassword
170 tenant: production
171 state: query
172 delegate_to: localhost
173 register: query_result
174
175 - name: Delete a PIM interface policy
176 cisco.aci.aci_interface_policy_pim:
177 host: apic
178 username: admin
179 password: SomeSecretPassword
180 tenant: production
181 pim: my_pim_policy
182 state: present
183 delegate_to: localhost
184 """
185
186 RETURN = r"""
187 current:
188 description: The existing configuration from the APIC after the module has finished
189 returned: success
190 type: list
191 sample:
192 [
193 {
194 "fvTenant": {
195 "attributes": {
196 "descr": "Production environment",
197 "dn": "uni/tn-production",
198 "name": "production",
199 "nameAlias": "",
200 "ownerKey": "",
201 "ownerTag": ""
202 }
203 }
204 }
205 ]
206 error:
207 description: The error information as returned from the APIC
208 returned: failure
209 type: dict
210 sample:
211 {
212 "code": "122",
213 "text": "unknown managed object class foo"
214 }
215 raw:
216 description: The raw output returned by the APIC REST API (xml or json)
217 returned: parse error
218 type: str
219 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
220 sent:
221 description: The actual/minimal configuration pushed to the APIC
222 returned: info
223 type: list
224 sample:
225 {
226 "fvTenant": {
227 "attributes": {
228 "descr": "Production environment"
229 }
230 }
231 }
232 previous:
233 description: The original configuration from the APIC before the module has started
234 returned: info
235 type: list
236 sample:
237 [
238 {
239 "fvTenant": {
240 "attributes": {
241 "descr": "Production",
242 "dn": "uni/tn-production",
243 "name": "production",
244 "nameAlias": "",
245 "ownerKey": "",
246 "ownerTag": ""
247 }
248 }
249 }
250 ]
251 proposed:
252 description: The assembled configuration from the user-provided parameters
253 returned: info
254 type: dict
255 sample:
256 {
257 "fvTenant": {
258 "attributes": {
259 "descr": "Production environment",
260 "name": "production"
261 }
262 }
263 }
264 filter_string:
265 description: The filter string used for the request
266 returned: failure or debug
267 type: str
268 sample: ?rsp-prop-include=config-only
269 method:
270 description: The HTTP method used for the request to the APIC
271 returned: failure or debug
272 type: str
273 sample: POST
274 response:
275 description: The HTTP response from the APIC
276 returned: failure or debug
277 type: str
278 sample: OK (30 bytes)
279 status:
280 description: The HTTP status from the APIC
281 returned: failure or debug
282 type: int
283 sample: 200
284 url:
285 description: The HTTP url used for the request to the APIC
286 returned: failure or debug
287 type: str
288 sample: https://10.11.12.13/api/mo/uni/tn-production.json
289 """
290
291 from ansible.module_utils.basic import AnsibleModule
292 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
293 from ansible_collections.cisco.aci.plugins.module_utils.constants import (
294 MATCH_PIM_INTERFACE_POLICY_CONTROL_STATE_MAPPING,
295 MATCH_PIM_INTERFACE_POLICY_AUTHENTICATION_TYPE_MAPPING,
296 )
297
298
299 def main():
300 argument_spec = aci_argument_spec()
301 argument_spec.update(aci_annotation_spec())
302 argument_spec.update(aci_owner_spec())
303 argument_spec.update(
304 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
305 pim=dict(type="str", aliases=["pim_interface_policy", "name"]), # Not required for querying all objects
306 description=dict(type="str", aliases=["descr"]),
307 authentication_key=dict(type="str", aliases=["auth_key"], no_log=True),
308 authentication_type=dict(type="str", choices=["none", "md5_hmac"], aliases=["auth_type"]),
309 control_state=dict(type="list", elements="str", choices=["multicast_domain_boundary", "strict_rfc_compliant", "passive"]),
310 designated_router_delay=dict(type="int", aliases=["delay"]),
311 designated_router_priority=dict(type="int", aliases=["prio"]),
312 hello_interval=dict(type="int"),
313 join_prune_interval=dict(type="int", aliases=["jp_interval"]),
314 inbound_join_prune_filter_policy=dict(type="str", aliases=["inbound_filter"]),
315 outbound_join_prune_filter_policy=dict(type="str", aliases=["outbound_filter"]),
316 neighbor_filter_policy=dict(type="str", aliases=["neighbor_filter"]),
317 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
318 name_alias=dict(type="str"),
319 )
320
321 module = AnsibleModule(
322 argument_spec=argument_spec,
323 supports_check_mode=True,
324 required_if=[
325 ["state", "absent", ["pim", "tenant"]],
326 ["state", "present", ["pim", "tenant"]],
327 ],
328 )
329
330 aci = ACIModule(module)
331
332 tenant = module.params.get("tenant")
333 pim = module.params.get("pim")
334 authentication_key = module.params.get("authentication_key")
335 authentication_type = MATCH_PIM_INTERFACE_POLICY_AUTHENTICATION_TYPE_MAPPING.get(module.params.get("authentication_type"))
336 description = module.params.get("description")
337 name_alias = module.params.get("name_alias")
338 state = module.params.get("state")
339
340 designated_router_delay = module.params.get("designated_router_delay")
341 if designated_router_delay is not None and designated_router_delay not in range(1, 65536):
342 module.fail_json(msg="Parameter 'designated_router_delay' is only valid in range between 1 and 65535.")
343
344 designated_router_priority = module.params.get("designated_router_priority")
345 if designated_router_priority is not None and designated_router_priority not in range(1, 4294967296):
346 module.fail_json(msg="Parameter 'designated_router_priority' is only valid in range between 1 and 4294967295.")
347
348 hello_interval = module.params.get("hello_interval")
349 if hello_interval is not None and hello_interval not in range(1, 18724287):
350 module.fail_json(msg="Parameter 'hello_interval' is only valid in range between 1 and 18724286.")
351
352 join_prune_interval = module.params.get("join_prune_interval")
353 if join_prune_interval is not None and join_prune_interval not in range(60, 65521):
354 module.fail_json(msg="Parameter 'join_prune_interval' is only valid in range between 60 and 65520.")
355
356 if module.params.get("control_state"):
357 control_state = ",".join([MATCH_PIM_INTERFACE_POLICY_CONTROL_STATE_MAPPING.get(v) for v in module.params.get("control_state")])
358 else:
359 control_state = None
360
361 child_classes = dict(
362 pimJPInbFilterPol=module.params.get("inbound_join_prune_filter_policy"),
363 pimJPOutbFilterPol=module.params.get("outbound_join_prune_filter_policy"),
364 pimNbrFilterPol=module.params.get("neighbor_filter_policy"),
365 )
366
367 aci.construct_url(
368 root_class=dict(
369 aci_class="fvTenant",
370 aci_rn="tn-{0}".format(tenant),
371 module_object=tenant,
372 target_filter={"name": tenant},
373 ),
374 subclass_1=dict(
375 aci_class="pimIfPol",
376 aci_rn="pimifpol-{0}".format(pim),
377 module_object=pim,
378 target_filter={"name": pim},
379 ),
380 child_classes=list(child_classes.keys()),
381 )
382
383 aci.get_existing()
384
385 if state == "present":
386 child_configs = []
387 for class_name, class_input in child_classes.items():
388 if class_input is not None:
389 if class_input == "" and isinstance(aci.existing, list) and len(aci.existing) > 0:
390 for child in aci.existing[0].get("pimIfPol", {}).get("children", {}):
391 if child.get(class_name):
392 child_configs.append(dict([(class_name, dict(attributes=dict(status="deleted")))]))
393 elif class_input != "":
394 child_configs.append(
395 dict(
396 [
397 (
398 class_name,
399 dict(
400 attributes=dict(),
401 children=[
402 dict(
403 rtdmcRsFilterToRtMapPol=dict(
404 attributes=dict(
405 tDn="uni/tn-{0}/rtmap-{1}".format(tenant, class_input),
406 ),
407 )
408 )
409 ],
410 ),
411 )
412 ]
413 )
414 )
415
416 aci.payload(
417 aci_class="pimIfPol",
418 class_config=dict(
419 name=pim,
420 descr=description,
421 authKey=authentication_key,
422 authT=authentication_type,
423 ctrl=control_state,
424 drDelay=designated_router_delay,
425 drPrio=designated_router_priority,
426 helloItvl=hello_interval,
427 jpInterval=join_prune_interval,
428 nameAlias=name_alias,
429 ),
430 child_configs=child_configs,
431 )
432
433 aci.get_diff(aci_class="pimIfPol")
434
435 aci.post_config()
436
437 elif state == "absent":
438 aci.delete_config()
439
440 aci.exit_json()
441
442
443 if __name__ == "__main__":
444 main()