Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_bgp_peer_prefix_policy.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_bgp_peer_prefix_policy
16 short_description: Manage BGP peer prefix policy (bgp:PeerPfxPol)
17 description:
18 - Manage BGP peer prefix 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 peer_prefix_policy:
26 description:
27 - The name of the BGP peer prefix policy.
28 type: str
29 aliases: [ peer_prefix_policy_name, name ]
30 action:
31 description:
32 - The action to be performed when the maximum prefix limit is reached.
33 - The APIC defaults to C(reject) when unset during creation.
34 type: str
35 choices: [ log, reject, restart, shut ]
36 maximum_number_prefix:
37 description:
38 - The maximum number of prefixes allowed from the peer.
39 - The APIC defaults to C(20000) when unset during creation.
40 type: int
41 aliases: [ max_prefix, max_num_prefix ]
42 restart_time:
43 description:
44 - The period of time in minutes before restarting the peer when the prefix limit is reached.
45 - Used only if C(action) is set to C(restart).
46 - The APIC defaults to C(infinite) when unset during creation.
47 type: str
48 threshold:
49 description:
50 - The threshold percentage of the maximum number of prefixes before a warning is issued.
51 - For example, if the maximum number of prefixes is 10 and the threshold is 70%, a warning is issued when the number of prefixes exceeds 7 (70%).
52 - The APIC defaults to C(75) when unset during creation.
53 type: int
54 aliases: [ thresh ]
55 description:
56 description:
57 - Description for the BGP peer prefix policy.
58 type: str
59 aliases: [ descr ]
60 state:
61 description:
62 - Use C(present) or C(absent) for adding or removing.
63 - Use C(query) for listing an object or multiple objects.
64 type: str
65 choices: [ absent, present, query ]
66 default: present
67 name_alias:
68 description:
69 - The alias for the current object. This relates to the nameAlias field in ACI.
70 type: str
71 extends_documentation_fragment:
72 - cisco.aci.aci
73 - cisco.aci.annotation
74 - cisco.aci.owner
75
76 notes:
77 - The C(tenant) used must exist before using this module in your playbook.
78 The M(cisco.aci.aci_tenant) module can be used for this.
79 seealso:
80 - module: cisco.aci.aci_tenant
81 - name: APIC Management Information Model reference
82 description: More information about the internal APIC class B(bgp:PeerPfxPol).
83 link: https://developer.cisco.com/docs/apic-mim-ref/
84 author:
85 - Gaspard Micol (@gmicol)
86 """
87
88 EXAMPLES = r"""
89 - name: Create a BGP peer prefix policy
90 cisco.aci.aci_bgp_peer_prefix_policy:
91 host: apic
92 username: admin
93 password: SomeSecretPassword
94 peer_prefix_policy: my_bgp_peer_prefix_policy
95 action: restart
96 restart_time: 10
97 max_prefix: 10000
98 threshold: 80
99 tenant: production
100 state: present
101 delegate_to: localhost
102
103 - name: Delete a BGP peer prefix policy
104 cisco.aci.aci_bgp_peer_prefix_policy:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 peer_prefix_policy: my_bgp_peer_prefix_policy
109 tenant: production
110 state: absent
111 delegate_to: localhost
112
113 - name: Query all BGP peer prefix policies
114 cisco.aci.aci_bgp_peer_prefix_policy:
115 host: apic
116 username: admin
117 password: SomeSecretPassword
118 state: query
119 delegate_to: localhost
120 register: query_result
121
122 - name: Query a specific BGP peer prefix policy
123 cisco.aci.aci_bgp_peer_prefix_policy:
124 host: apic
125 username: admin
126 password: SomeSecretPassword
127 peer_prefix_policy: my_bgp_peer_prefix_policy
128 tenant: production
129 state: query
130 delegate_to: localhost
131 register: query_result
132 """
133
134 RETURN = r"""
135 current:
136 description: The existing configuration from the APIC after the module has finished
137 returned: success
138 type: list
139 sample:
140 [
141 {
142 "fvTenant": {
143 "attributes": {
144 "descr": "Production environment",
145 "dn": "uni/tn-production",
146 "name": "production",
147 "nameAlias": "",
148 "ownerKey": "",
149 "ownerTag": ""
150 }
151 }
152 }
153 ]
154 error:
155 description: The error information as returned from the APIC
156 returned: failure
157 type: dict
158 sample:
159 {
160 "code": "122",
161 "text": "unknown managed object class foo"
162 }
163 raw:
164 description: The raw output returned by the APIC REST API (xml or json)
165 returned: parse error
166 type: str
167 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
168 sent:
169 description: The actual/minimal configuration pushed to the APIC
170 returned: info
171 type: list
172 sample:
173 {
174 "fvTenant": {
175 "attributes": {
176 "descr": "Production environment"
177 }
178 }
179 }
180 previous:
181 description: The original configuration from the APIC before the module has started
182 returned: info
183 type: list
184 sample:
185 [
186 {
187 "fvTenant": {
188 "attributes": {
189 "descr": "Production",
190 "dn": "uni/tn-production",
191 "name": "production",
192 "nameAlias": "",
193 "ownerKey": "",
194 "ownerTag": ""
195 }
196 }
197 }
198 ]
199 proposed:
200 description: The assembled configuration from the user-provided parameters
201 returned: info
202 type: dict
203 sample:
204 {
205 "fvTenant": {
206 "attributes": {
207 "descr": "Production environment",
208 "name": "production"
209 }
210 }
211 }
212 filter_string:
213 description: The filter string used for the request
214 returned: failure or debug
215 type: str
216 sample: ?rsp-prop-include=config-only
217 method:
218 description: The HTTP method used for the request to the APIC
219 returned: failure or debug
220 type: str
221 sample: POST
222 response:
223 description: The HTTP response from the APIC
224 returned: failure or debug
225 type: str
226 sample: OK (30 bytes)
227 status:
228 description: The HTTP status from the APIC
229 returned: failure or debug
230 type: int
231 sample: 200
232 url:
233 description: The HTTP url used for the request to the APIC
234 returned: failure or debug
235 type: str
236 sample: https://10.11.12.13/api/mo/uni/tn-production.json
237 """
238
239 from ansible.module_utils.basic import AnsibleModule
240 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
241
242
243 def main():
244 argument_spec = aci_argument_spec()
245 argument_spec.update(aci_annotation_spec())
246 argument_spec.update(aci_owner_spec())
247 argument_spec.update(
248 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
249 peer_prefix_policy=dict(type="str", aliases=["peer_prefix_policy_name", "name"]), # Not required for querying all objects
250 action=dict(type="str", choices=["log", "reject", "restart", "shut"]),
251 maximum_number_prefix=dict(type="int", aliases=["max_prefix", "max_num_prefix"]),
252 restart_time=dict(type="str"),
253 threshold=dict(type="int", aliases=["thresh"]),
254 description=dict(type="str", aliases=["descr"]),
255 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
256 name_alias=dict(type="str"),
257 )
258
259 module = AnsibleModule(
260 argument_spec=argument_spec,
261 supports_check_mode=True,
262 required_if=[
263 ["state", "absent", ["peer_prefix_policy", "tenant"]],
264 ["state", "present", ["peer_prefix_policy", "tenant"]],
265 ],
266 )
267
268 peer_prefix_policy = module.params.get("peer_prefix_policy")
269 action = module.params.get("action")
270 maximum_number_prefix = module.params.get("maximum_number_prefix")
271 restart_time = module.params.get("restart_time")
272 threshold = module.params.get("threshold")
273 description = module.params.get("description")
274 state = module.params.get("state")
275 tenant = module.params.get("tenant")
276 name_alias = module.params.get("name_alias")
277
278 aci = ACIModule(module)
279
280 aci.construct_url(
281 root_class=dict(
282 aci_class="fvTenant",
283 aci_rn="tn-{0}".format(tenant),
284 module_object=tenant,
285 target_filter={"name": tenant},
286 ),
287 subclass_1=dict(
288 aci_class="bgpPeerPfxPol",
289 aci_rn="bgpPfxP-{0}".format(peer_prefix_policy),
290 module_object=peer_prefix_policy,
291 target_filter={"name": peer_prefix_policy},
292 ),
293 )
294
295 aci.get_existing()
296
297 if state == "present":
298 aci.payload(
299 aci_class="bgpPeerPfxPol",
300 class_config=dict(
301 name=peer_prefix_policy,
302 action=action,
303 maxPfx=maximum_number_prefix,
304 restartTime=restart_time,
305 thresh=threshold,
306 descr=description,
307 nameAlias=name_alias,
308 ),
309 )
310
311 aci.get_diff(aci_class="bgpPeerPfxPol")
312
313 aci.post_config()
314
315 elif state == "absent":
316 aci.delete_config()
317
318 aci.exit_json()
319
320
321 if __name__ == "__main__":
322 main()