Debian Codegraph @

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