Debian Codegraph @

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

raw symbols SCIP
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_bgp_address_family_context_policy
16 short_description: Manage BGP address family context policy (bgp:CtxAfPol)
17 description:
18 - Manage BGP address family context policies for the 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 address_family_context_policy:
26 description:
27 - The name of the BGP address family context policy.
28 type: str
29 aliases: [ address_family_context_name, name ]
30 host_route_leak:
31 description:
32 - The control state.
33 - The option to enable/disable host route leak.
34 - The APIC defaults to C(false) when unset during creation.
35 type: bool
36 ebgp_distance:
37 description:
38 - The administrative distance of eBGP routes.
39 - The APIC defaults to C(20) when unset during creation.
40 type: int
41 ibgp_distance:
42 description:
43 - The administrative distance of iBGP routes.
44 - The APIC defaults to C(200) when unset during creation.
45 type: int
46 local_distance:
47 description:
48 - The administrative distance of local routes.
49 - The APIC defaults to C(220) when unset during creation.
50 type: int
51 ebgp_max_ecmp:
52 description:
53 - The eBGP max-path.
54 - The APIC defaults to C(16) when unset during creation.
55 type: int
56 ibgp_max_ecmp:
57 description:
58 - The iBGP max-path.
59 - The APIC defaults to C(16) when unset during creation.
60 type: int
61 local_max_ecmp:
62 description:
63 - The maximum number of equal-cost local paths for redist.
64 - The APIC defaults to C(0) when unset during creation.
65 - Can not be configured for APIC version 4.2(7s) and prior.
66 type: int
67 bgp_add_path_capability:
68 description:
69 - The neighbor system capability.
70 - To delete this attribute, pass an empty string.
71 - Can not be configured for APIC version 6.0(2h) and prior.
72 type: str
73 choices: [ receive, send, "" ]
74 description:
75 description:
76 - Description for the BGP protocol profile.
77 type: str
78 aliases: [ descr ]
79 state:
80 description:
81 - Use C(present) or C(absent) for adding or removing.
82 - Use C(query) for listing an object or multiple objects.
83 type: str
84 choices: [ absent, present, query ]
85 default: present
86 name_alias:
87 description:
88 - The alias for the current object. This relates to the nameAlias field in ACI.
89 type: str
90 extends_documentation_fragment:
91 - cisco.aci.aci
92 - cisco.aci.annotation
93 - cisco.aci.owner
94
95 notes:
96 - The C(tenant) used must exist before using this module in your playbook.
97 The M(cisco.aci.aci_tenant) module can be used for this.
98 seealso:
99 - module: cisco.aci.aci_tenant
100 - name: APIC Management Information Model reference
101 description: More information about the internal APIC class B(bgp:CtxAfPol).
102 link: https://developer.cisco.com/docs/apic-mim-ref/
103 author:
104 - Gaspard Micol (@gmicol)
105 """
106
107 EXAMPLES = r"""
108 - name: Create a BGP address family context policy
109 cisco.aci.aci_bgp_address_family_context_policy:
110 host: apic
111 username: admin
112 password: SomeSecretPassword
113 bgp_address_family_context_policy: my_bgp_address_family_context_policy
114 host_route_leak: true
115 ebgp_distance: 40
116 ibgp_distance: 210
117 local_distance: 215
118 ebgp_max_ecmp: 32
119 ibgp_max_ecmp: 32
120 local_max_ecmp: 1
121 bgp_add_path_capability: receive
122 tenant: production
123 state: present
124 delegate_to: localhost
125
126 - name: Delete BGP address family context policy's child
127 cisco.aci.aci_bgp_address_family_context_policy:
128 host: apic
129 username: admin
130 password: SomeSecretPassword
131 bgp_address_family_context_policy: my_bgp_address_family_context_policy
132 bgp_add_path_capability: ""
133 tenant: production
134 state: absent
135 delegate_to: localhost
136
137 - name: Delete a BGP address family context policy
138 cisco.aci.aci_bgp_address_family_context_policy:
139 host: apic
140 username: admin
141 password: SomeSecretPassword
142 bgp_address_family_context_policy: my_bgp_address_family_context_policy
143 tenant: production
144 state: absent
145 delegate_to: localhost
146
147 - name: Query all BGP address family context policies
148 cisco.aci.aci_bgp_address_family_context_policy:
149 host: apic
150 username: admin
151 password: SomeSecretPassword
152 state: query
153 delegate_to: localhost
154 register: query_result
155
156 - name: Query a specific BGP address family context policy
157 cisco.aci.aci_bgp_address_family_context_policy:
158 host: apic
159 username: admin
160 password: SomeSecretPassword
161 bgp_address_family_context_policy: my_bgp_address_family_context_policy
162 tenant: production
163 state: query
164 delegate_to: localhost
165 register: query_result
166 """
167
168 RETURN = r"""
169 current:
170 description: The existing configuration from the APIC after the module has finished
171 returned: success
172 type: list
173 sample:
174 [
175 {
176 "fvTenant": {
177 "attributes": {
178 "descr": "Production environment",
179 "dn": "uni/tn-production",
180 "name": "production",
181 "nameAlias": "",
182 "ownerKey": "",
183 "ownerTag": ""
184 }
185 }
186 }
187 ]
188 error:
189 description: The error information as returned from the APIC
190 returned: failure
191 type: dict
192 sample:
193 {
194 "code": "122",
195 "text": "unknown managed object class foo"
196 }
197 raw:
198 description: The raw output returned by the APIC REST API (xml or json)
199 returned: parse error
200 type: str
201 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
202 sent:
203 description: The actual/minimal configuration pushed to the APIC
204 returned: info
205 type: list
206 sample:
207 {
208 "fvTenant": {
209 "attributes": {
210 "descr": "Production environment"
211 }
212 }
213 }
214 previous:
215 description: The original configuration from the APIC before the module has started
216 returned: info
217 type: list
218 sample:
219 [
220 {
221 "fvTenant": {
222 "attributes": {
223 "descr": "Production",
224 "dn": "uni/tn-production",
225 "name": "production",
226 "nameAlias": "",
227 "ownerKey": "",
228 "ownerTag": ""
229 }
230 }
231 }
232 ]
233 proposed:
234 description: The assembled configuration from the user-provided parameters
235 returned: info
236 type: dict
237 sample:
238 {
239 "fvTenant": {
240 "attributes": {
241 "descr": "Production environment",
242 "name": "production"
243 }
244 }
245 }
246 filter_string:
247 description: The filter string used for the request
248 returned: failure or debug
249 type: str
250 sample: ?rsp-prop-include=config-only
251 method:
252 description: The HTTP method used for the request to the APIC
253 returned: failure or debug
254 type: str
255 sample: POST
256 response:
257 description: The HTTP response from the APIC
258 returned: failure or debug
259 type: str
260 sample: OK (30 bytes)
261 status:
262 description: The HTTP status from the APIC
263 returned: failure or debug
264 type: int
265 sample: 200
266 url:
267 description: The HTTP url used for the request to the APIC
268 returned: failure or debug
269 type: str
270 sample: https://10.11.12.13/api/mo/uni/tn-production.json
271 """
272
273 from ansible.module_utils.basic import AnsibleModule
274 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
275
276
277 def main():
278 argument_spec = aci_argument_spec()
279 argument_spec.update(aci_annotation_spec())
280 argument_spec.update(aci_owner_spec())
281 argument_spec.update(
282 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
283 address_family_context_policy=dict(type="str", aliases=["address_family_context_name", "name"]), # Not required for querying all objects
284 host_route_leak=dict(type="bool"),
285 ebgp_distance=dict(type="int"),
286 ibgp_distance=dict(type="int"),
287 local_distance=dict(type="int"),
288 ebgp_max_ecmp=dict(type="int"),
289 ibgp_max_ecmp=dict(type="int"),
290 local_max_ecmp=dict(type="int"),
291 bgp_add_path_capability=dict(type="str", choices=["receive", "send", ""]),
292 description=dict(type="str", aliases=["descr"]),
293 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
294 name_alias=dict(type="str"),
295 )
296
297 module = AnsibleModule(
298 argument_spec=argument_spec,
299 supports_check_mode=True,
300 required_if=[
301 ["state", "absent", ["address_family_context_policy", "tenant"]],
302 ["state", "present", ["address_family_context_policy", "tenant"]],
303 ],
304 )
305
306 aci = ACIModule(module)
307
308 address_family_context_policy = module.params.get("address_family_context_policy")
309 host_route_leak = aci.boolean(module.params.get("host_route_leak"), "host-rt-leak", "")
310 ebgp_distance = module.params.get("ebgp_distance")
311 ibgp_distance = module.params.get("ibgp_distance")
312 local_distance = module.params.get("local_distance")
313 ebgp_max_ecmp = module.params.get("ebgp_max_ecmp")
314 ibgp_max_ecmp = module.params.get("ibgp_max_ecmp")
315 local_max_ecmp = module.params.get("local_max_ecmp")
316 bgp_add_path_capability = module.params.get("bgp_add_path_capability")
317 description = module.params.get("description")
318 state = module.params.get("state")
319 tenant = module.params.get("tenant")
320 name_alias = module.params.get("name_alias")
321
322 child_classes = []
323 if bgp_add_path_capability is not None:
324 child_classes.append("bgpCtxAddlPathPol")
325
326 aci.construct_url(
327 root_class=dict(
328 aci_class="fvTenant",
329 aci_rn="tn-{0}".format(tenant),
330 module_object=tenant,
331 target_filter={"name": tenant},
332 ),
333 subclass_1=dict(
334 aci_class="bgpCtxAfPol",
335 aci_rn="bgpCtxAfP-{0}".format(address_family_context_policy),
336 module_object=address_family_context_policy,
337 target_filter={"name": address_family_context_policy},
338 ),
339 child_classes=child_classes,
340 )
341
342 aci.get_existing()
343
344 if state == "present":
345 child_configs = []
346 if bgp_add_path_capability is not None:
347 if bgp_add_path_capability == "" and isinstance(aci.existing, list) and len(aci.existing) > 0:
348 for child in aci.existing[0].get("bgpCtxAfPol", {}).get("children", {}):
349 if child.get("bgpCtxAddlPathPol"):
350 child_configs.append(dict(bgpCtxAddlPathPol=dict(attributes=dict(status="deleted"))))
351 elif bgp_add_path_capability != "":
352 child_configs.append(dict(bgpCtxAddlPathPol=dict(attributes=dict(capability=bgp_add_path_capability))))
353
354 aci.payload(
355 aci_class="bgpCtxAfPol",
356 class_config=dict(
357 name=address_family_context_policy,
358 ctrl=host_route_leak,
359 eDist=ebgp_distance,
360 iDist=ibgp_distance,
361 localDist=local_distance,
362 maxEcmp=ebgp_max_ecmp,
363 maxEcmpIbgp=ibgp_max_ecmp,
364 maxLocalEcmp=local_max_ecmp,
365 descr=description,
366 nameAlias=name_alias,
367 ),
368 child_configs=child_configs,
369 )
370
371 aci.get_diff(aci_class="bgpCtxAfPol")
372
373 aci.post_config()
374
375 elif state == "absent":
376 aci.delete_config()
377
378 aci.exit_json()
379
380
381 if __name__ == "__main__":
382 main()