Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Abraham Mughal (@abmughal) [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_vrf_leak_internal_subnet
16 short_description: Manage VRF leaking of subnets (leak:InternalSubnet)
17 description:
18 - Manage the leaking of internal subnets under the VRF.
19 options:
20 tenant:
21 description:
22 - The name of the Tenant the VRF belongs to.
23 type: str
24 aliases: [ tenant_name ]
25 vrf:
26 description:
27 - The name of the VRF.
28 type: str
29 aliases: [ context, name, vrf_name ]
30 description:
31 description:
32 - The description for the VRF Leak Internal Subnet.
33 type: str
34 aliases: [ descr ]
35 name_alias:
36 description:
37 - The alias for the current object. This relates to the nameAlias field in ACI.
38 type: str
39 scope:
40 description:
41 - Scope of the object.
42 type: str
43 choices: [ public, private, shared ]
44 default: private
45 leak_to:
46 description:
47 - The VRFs to leak the subnet routes into.
48 type: list
49 elements: dict
50 suboptions:
51 tenant:
52 description:
53 - Name of the tenant.
54 type: str
55 aliases: [ tenant_name ]
56 vrf:
57 description:
58 - Name of the VRF.
59 type: str
60 aliases: [ vrf_name ]
61 ip:
62 description:
63 - The IP address / subnet used to match routes to be leaked.
64 type: str
65 state:
66 description:
67 - Use C(present) or C(absent) for adding or removing.
68 - Use C(query) for listing an object or multiple objects.
69 type: str
70 choices: [ absent, present, query ]
71 default: present
72 extends_documentation_fragment:
73 - cisco.aci.aci
74 - cisco.aci.annotation
75 - cisco.aci.owner
76
77 notes:
78 - The C(tenant) and C(vrf) used must exist before using this module in your playbook.
79 The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_vrf) modules can be used for this.
80 seealso:
81 - module: cisco.aci.aci_tenant
82 - module: cisco.aci.aci_vrf
83 - name: APIC Management Information Model reference
84 description: More information about the internal APIC class B(leak:InternalSubnet).
85 link: https://developer.cisco.com/docs/apic-mim-ref/
86 author:
87 - Abraham Mughal (@abmughal)
88 """
89
90 EXAMPLES = r"""
91 - name: Create leak internal subnet
92 cisco.aci.aci_vrf_leak_internal_subnet:
93 host: apic
94 username: admin
95 password: SomeSecretPassword
96 vrf: vrf_lab
97 tenant: lab_tenant
98 descr: Lab VRF
99 state: present
100 leak_to:
101 - vrf: "test"
102 tenant: "lab_tenant"
103 - vrf: "test2"
104 tenant: "lab_tenant"
105 description: Ansible Test
106 ip: 1.1.1.2
107 delegate_to: localhost
108
109 - name: Remove a subnet from leaking
110 cisco.aci.aci_vrf_leak_internal_subnet:
111 host: apic
112 username: admin
113 password: SomeSecretPassword
114 vrf: vrf_lab
115 tenant: lab_tenant
116 state: absent
117 leak_to:
118 - vrf: "test2"
119 tenant: "lab_tenant"
120 description: Ansible Test
121 ip: 1.1.1.2
122 delegate_to: localhost
123
124 - name: Delete leak internal subnet
125 cisco.aci.aci_vrf_leak_internal_subnet:
126 host: apic
127 username: admin
128 password: SomeSecretPassword
129 vrf: vrf_lab
130 tenant: lab_tenant
131 state: absent
132 description: Ansible Test
133 ip: 1.1.1.2
134 delegate_to: localhost
135
136 - name: Query all leak internal subnet
137 cisco.aci.aci_vrf_leak_internal_subnet:
138 host: apic
139 username: admin
140 password: SomeSecretPassword
141 state: query
142 ip: 1.1.1.2
143 delegate_to: localhost
144 register: query_result
145
146 - name: Query leak internal subnet
147 cisco.aci.aci_vrf_leak_internal_subnet:
148 host: apic
149 username: admin
150 password: SomeSecretPassword
151 vrf: vrf_lab
152 tenant: lab_tenant
153 state: query
154 ip: 1.1.1.2
155 delegate_to: localhost
156 register: query_result
157 """
158
159 RETURN = r"""
160 current:
161 description: The existing configuration from the APIC after the module has finished
162 returned: success
163 type: list
164 sample:
165 [
166 {
167 "fvTenant": {
168 "attributes": {
169 "descr": "Production environment",
170 "dn": "uni/tn-production",
171 "name": "production",
172 "nameAlias": "",
173 "ownerKey": "",
174 "ownerTag": ""
175 }
176 }
177 }
178 ]
179 error:
180 description: The error information as returned from the APIC
181 returned: failure
182 type: dict
183 sample:
184 {
185 "code": "122",
186 "text": "unknown managed object class foo"
187 }
188 raw:
189 description: The raw output returned by the APIC REST API (xml or json)
190 returned: parse error
191 type: str
192 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
193 sent:
194 description: The actual/minimal configuration pushed to the APIC
195 returned: info
196 type: list
197 sample:
198 {
199 "fvTenant": {
200 "attributes": {
201 "descr": "Production environment"
202 }
203 }
204 }
205 previous:
206 description: The original configuration from the APIC before the module has started
207 returned: info
208 type: list
209 sample:
210 [
211 {
212 "fvTenant": {
213 "attributes": {
214 "descr": "Production",
215 "dn": "uni/tn-production",
216 "name": "production",
217 "nameAlias": "",
218 "ownerKey": "",
219 "ownerTag": ""
220 }
221 }
222 }
223 ]
224 proposed:
225 description: The assembled configuration from the user-provided parameters
226 returned: info
227 type: dict
228 sample:
229 {
230 "fvTenant": {
231 "attributes": {
232 "descr": "Production environment",
233 "name": "production"
234 }
235 }
236 }
237 filter_string:
238 description: The filter string used for the request
239 returned: failure or debug
240 type: str
241 sample: ?rsp-prop-include=config-only
242 method:
243 description: The HTTP method used for the request to the APIC
244 returned: failure or debug
245 type: str
246 sample: POST
247 response:
248 description: The HTTP response from the APIC
249 returned: failure or debug
250 type: str
251 sample: OK (30 bytes)
252 status:
253 description: The HTTP status from the APIC
254 returned: failure or debug
255 type: int
256 sample: 200
257 url:
258 description: The HTTP url used for the request to the APIC
259 returned: failure or debug
260 type: str
261 sample: https://10.11.12.13/api/mo/uni/tn-production.json
262 """
263
264 from ansible.module_utils.basic import AnsibleModule
265 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
266
267
268 def main():
269 argument_spec = aci_argument_spec()
270 argument_spec.update(aci_annotation_spec())
271 argument_spec.update(aci_owner_spec())
272 argument_spec.update(
273 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
274 vrf=dict(type="str", aliases=["context", "name", "vrf_name"]), # Not required for querying all objects
275 leak_to=dict(
276 type="list",
277 elements="dict",
278 options=dict(
279 vrf=dict(type="str", aliases=["vrf_name"]),
280 tenant=dict(type="str", aliases=["tenant_name"]),
281 ),
282 ),
283 description=dict(type="str", aliases=["descr"]),
284 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
285 name_alias=dict(type="str"),
286 scope=dict(type="str", default="private", choices=["public", "private", "shared"]),
287 ip=dict(type="str"),
288 )
289
290 module = AnsibleModule(
291 argument_spec=argument_spec,
292 supports_check_mode=True,
293 required_if=[
294 ["state", "absent", ["tenant", "vrf"]],
295 ["state", "present", ["tenant", "vrf", "leak_to"]],
296 ],
297 )
298
299 description = module.params.get("description")
300 state = module.params.get("state")
301 tenant = module.params.get("tenant")
302 vrf = module.params.get("vrf")
303 leak_to = module.params.get("leak_to")
304 name_alias = module.params.get("name_alias")
305 scope = module.params.get("scope")
306 ip = module.params.get("ip")
307
308 aci = ACIModule(module)
309 aci.construct_url(
310 root_class=dict(
311 aci_class="fvTenant",
312 aci_rn="tn-{0}".format(tenant),
313 module_object=tenant,
314 target_filter={"name": tenant},
315 ),
316 subclass_1=dict(
317 aci_class="fvCtx",
318 aci_rn="ctx-{0}".format(vrf),
319 module_object=vrf,
320 target_filter={"name": vrf},
321 ),
322 subclass_2=dict(
323 aci_class="leakRoutes",
324 aci_rn="leakroutes",
325 module_object=True,
326 ),
327 subclass_3=dict(
328 aci_class="leakInternalSubnet",
329 aci_rn="leakintsubnet-[{0}]".format(ip),
330 module_object=ip,
331 target_filter={"ip": ip},
332 ),
333 child_classes=["leakTo"],
334 )
335
336 aci.get_existing()
337
338 if state == "present":
339 child_configs = []
340
341 subnet_rn_list = []
342 for subnet in leak_to:
343 subnet_rn_list.append("to-[{0}]-[{1}]".format(subnet.get("tenant"), subnet.get("vrf")))
344 child_configs.append(
345 dict(
346 leakTo=dict(
347 attributes=dict(
348 ctxName=subnet.get("vrf"),
349 tenantName=subnet.get("tenant"),
350 scope=scope,
351 )
352 )
353 )
354 )
355
356 if isinstance(aci.existing, list) and len(aci.existing) > 0:
357 for child in aci.existing[0].get("leakInternalSubnet", {}).get("children", {}):
358 child_attributes = child.get("leakTo", {}).get("attributes", {})
359 if child_attributes and "to-[{0}]-[{1}]".format(child_attributes.get("tenantName"), child_attributes.get("ctxName")) not in subnet_rn_list:
360 child_configs.append(
361 dict(
362 leakTo=dict(
363 attributes=dict(
364 ctxName=child_attributes.get("ctxName"),
365 tenantName=child_attributes.get("tenantName"),
366 status="deleted",
367 )
368 )
369 )
370 )
371
372 aci.payload(
373 aci_class="leakInternalSubnet",
374 class_config=dict(
375 descr=description,
376 ip=ip,
377 scope=scope,
378 nameAlias=name_alias,
379 ),
380 child_configs=child_configs,
381 )
382
383 aci.get_diff(aci_class="leakInternalSubnet")
384
385 if aci.existing:
386 aci.post_config()
387 else:
388 aci.post_config(parent_class="leakRoutes")
389
390 elif state == "absent":
391 aci.delete_config()
392
393 aci.exit_json()
394
395
396 if __name__ == "__main__":
397 main()