Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: aci_l3out_extepg
15 short_description: Manage External Network Instance Profile (ExtEpg) objects (l3ext:InstP)
16 description:
17 - Manage External Network Instance Profile (ExtEpg) objects on Cisco ACI fabrics.
18 options:
19 tenant:
20 description:
21 - Name of an existing tenant.
22 type: str
23 aliases: [ tenant_name ]
24 l3out:
25 description:
26 - Name of an existing L3Out.
27 type: str
28 aliases: [ l3out_name ]
29 extepg:
30 description:
31 - Name of ExtEpg being created.
32 type: str
33 aliases: [ extepg_name, name ]
34 description:
35 description:
36 - Description for the ExtEpg.
37 type: str
38 aliases: [ descr ]
39 preferred_group:
40 description:
41 - Whether ot not the EPG is part of the Preferred Group and can communicate without contracts.
42 - This is very convenient for migration scenarios, or when ACI is used for network automation but not for policy.
43 - The APIC defaults to C(false) when unset during creation.
44 type: bool
45 dscp:
46 description:
47 - The target Differentiated Service (DSCP) value.
48 - The APIC defaults to C(unspecified) when unset during creation.
49 type: str
50 choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43, CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ]
51 aliases: [ target ]
52 contract_exception_tag:
53 description:
54 - The tag for contract exception.
55 type: str
56 qos_class:
57 description:
58 - The priority class identifier.
59 type: str
60 choices: [ level1, level2, level3, level4, level5, level6, unspecified ]
61 default: level3
62 intra_ext_epg_isolation:
63 description:
64 - The preferred policy control.
65 type: str
66 choices: [ enforced, unenforced ]
67 route_control_profiles:
68 description:
69 - The route control profiles.
70 type: dict
71 suboptions:
72 import_profile:
73 description:
74 - The route control profile whose direction is import.
75 - To remove a route import policy pass an empty string (see Examples).
76 type: str
77 aliases: [ import ]
78 export_profile:
79 description:
80 - The route control profile whose direction is export.
81 - To remove a route export policy pass an empty string (see Examples).
82 type: str
83 aliases: [ export ]
84 state:
85 description:
86 - Use C(present) or C(absent) for adding or removing.
87 - Use C(query) for listing an object or multiple objects.
88 type: str
89 choices: [ absent, present, query ]
90 default: present
91 name_alias:
92 description:
93 - The alias for the current object. This relates to the nameAlias field in ACI.
94 type: str
95 extends_documentation_fragment:
96 - cisco.aci.aci
97 - cisco.aci.annotation
98
99 notes:
100 - The C(tenant) and C(domain) and C(vrf) used must exist before using this module in your playbook.
101 The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_domain) and M(cisco.aci.aci_vrf) modules can be used for this.
102 seealso:
103 - module: cisco.aci.aci_tenant
104 - module: cisco.aci.aci_domain
105 - module: cisco.aci.aci_vrf
106 - name: APIC Management Information Model reference
107 description: More information about the internal APIC class B(l3extInstP:instP).
108 link: https://developer.cisco.com/docs/apic-mim-ref/
109 author:
110 - Rostyslav Davydenko (@rost-d)
111 """
112
113 EXAMPLES = r"""
114 - name: Add a new ExtEpg
115 cisco.aci.aci_l3out_extepg:
116 host: apic
117 username: admin
118 password: SomeSecretPassword
119 tenant: production
120 l3out: prod_l3out
121 name: prod_extepg
122 description: ExtEpg for Production L3Out
123 contract_exception_tag: test
124 qos_class: level6
125 route_control_profiles:
126 import_profile: test1
127 export_profile: test2
128 state: present
129 delegate_to: localhost
130
131 - name: Delete ExtEpg
132 cisco.aci.aci_l3out_extepg:
133 host: apic
134 username: admin
135 password: SomeSecretPassword
136 tenant: production
137 l3out: prod_l3out
138 name: prod_extepg
139 state: absent
140 delegate_to: localhost
141
142 - name: Delete the export route control profile in an ExtEpg
143 cisco.aci.aci_l3out_extepg:
144 host: apic
145 username: admin
146 password: SomeSecretPassword
147 tenant: production
148 l3out: prod_l3out
149 name: prod_extepg
150 route_control_profiles:
151 import_profile: test1
152 export_profile: ""
153 state: present
154 delegate_to: localhost
155
156 - name: Query ExtEpg information
157 cisco.aci.aci_l3out_extepg:
158 host: apic
159 username: admin
160 password: SomeSecretPassword
161 tenant: production
162 l3out: prod_l3out
163 name: prod_extepg
164 state: query
165 delegate_to: localhost
166 register: query_result
167 """
168
169 RETURN = r"""
170 current:
171 description: The existing configuration from the APIC after the module has finished
172 returned: success
173 type: list
174 sample:
175 [
176 {
177 "fvTenant": {
178 "attributes": {
179 "descr": "Production environment",
180 "dn": "uni/tn-production",
181 "name": "production",
182 "nameAlias": "",
183 "ownerKey": "",
184 "ownerTag": ""
185 }
186 }
187 }
188 ]
189 error:
190 description: The error information as returned from the APIC
191 returned: failure
192 type: dict
193 sample:
194 {
195 "code": "122",
196 "text": "unknown managed object class foo"
197 }
198 raw:
199 description: The raw output returned by the APIC REST API (xml or json)
200 returned: parse error
201 type: str
202 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
203 sent:
204 description: The actual/minimal configuration pushed to the APIC
205 returned: info
206 type: list
207 sample:
208 {
209 "fvTenant": {
210 "attributes": {
211 "descr": "Production environment"
212 }
213 }
214 }
215 previous:
216 description: The original configuration from the APIC before the module has started
217 returned: info
218 type: list
219 sample:
220 [
221 {
222 "fvTenant": {
223 "attributes": {
224 "descr": "Production",
225 "dn": "uni/tn-production",
226 "name": "production",
227 "nameAlias": "",
228 "ownerKey": "",
229 "ownerTag": ""
230 }
231 }
232 }
233 ]
234 proposed:
235 description: The assembled configuration from the user-provided parameters
236 returned: info
237 type: dict
238 sample:
239 {
240 "fvTenant": {
241 "attributes": {
242 "descr": "Production environment",
243 "name": "production"
244 }
245 }
246 }
247 filter_string:
248 description: The filter string used for the request
249 returned: failure or debug
250 type: str
251 sample: ?rsp-prop-include=config-only
252 method:
253 description: The HTTP method used for the request to the APIC
254 returned: failure or debug
255 type: str
256 sample: POST
257 response:
258 description: The HTTP response from the APIC
259 returned: failure or debug
260 type: str
261 sample: OK (30 bytes)
262 status:
263 description: The HTTP status from the APIC
264 returned: failure or debug
265 type: int
266 sample: 200
267 url:
268 description: The HTTP url used for the request to the APIC
269 returned: failure or debug
270 type: str
271 sample: https://10.11.12.13/api/mo/uni/tn-production.json
272 """
273
274 from ansible.module_utils.basic import AnsibleModule
275 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
276
277
278 def main():
279 argument_spec = aci_argument_spec()
280 argument_spec.update(aci_annotation_spec())
281 argument_spec.update(
282 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
283 l3out=dict(type="str", aliases=["l3out_name"]), # Not required for querying all objects
284 extepg=dict(type="str", aliases=["extepg_name", "name"]), # Not required for querying all objects
285 description=dict(type="str", aliases=["descr"]),
286 preferred_group=dict(type="bool"),
287 dscp=dict(
288 type="str",
289 choices=[
290 "AF11",
291 "AF12",
292 "AF13",
293 "AF21",
294 "AF22",
295 "AF23",
296 "AF31",
297 "AF32",
298 "AF33",
299 "AF41",
300 "AF42",
301 "AF43",
302 "CS0",
303 "CS1",
304 "CS2",
305 "CS3",
306 "CS4",
307 "CS5",
308 "CS6",
309 "CS7",
310 "EF",
311 "VA",
312 "unspecified",
313 ],
314 aliases=["target"],
315 ),
316 contract_exception_tag=dict(type="str"),
317 qos_class=dict(type="str", default="level3", choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"]),
318 intra_ext_epg_isolation=dict(type="str", choices=["enforced", "unenforced"]),
319 route_control_profiles=dict(
320 type="dict",
321 options=dict(import_profile=dict(type="str", aliases=["import"]), export_profile=dict(type="str", aliases=["export"])),
322 ),
323 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
324 name_alias=dict(type="str"),
325 )
326
327 module = AnsibleModule(
328 argument_spec=argument_spec,
329 supports_check_mode=True,
330 required_if=[
331 ["state", "present", ["extepg", "l3out", "tenant"]],
332 ["state", "absent", ["extepg", "l3out", "tenant"]],
333 ],
334 )
335
336 aci = ACIModule(module)
337
338 tenant = module.params.get("tenant")
339 l3out = module.params.get("l3out")
340 extepg = module.params.get("extepg")
341 description = module.params.get("description")
342 preferred_group = aci.boolean(module.params.get("preferred_group"), "include", "exclude")
343 dscp = module.params.get("dscp")
344 state = module.params.get("state")
345 contract_exception_tag = module.params.get("contract_exception_tag")
346 qos_class = module.params.get("qos_class")
347 intra_ext_epg_isolation = module.params.get("intra_ext_epg_isolation")
348 route_control_profiles = module.params.get("route_control_profiles")
349 name_alias = module.params.get("name_alias")
350
351 aci.construct_url(
352 root_class=dict(
353 aci_class="fvTenant",
354 aci_rn="tn-{0}".format(tenant),
355 module_object=tenant,
356 target_filter={"name": tenant},
357 ),
358 subclass_1=dict(
359 aci_class="l3extOut",
360 aci_rn="out-{0}".format(l3out),
361 module_object=l3out,
362 target_filter={"name": l3out},
363 ),
364 subclass_2=dict(
365 aci_class="l3extInstP",
366 aci_rn="instP-{0}".format(extepg),
367 module_object=extepg,
368 target_filter={"name": extepg},
369 ),
370 child_classes=["fvRsSecInherited", "l3extRsInstPToProfile"],
371 )
372
373 aci.get_existing()
374
375 if state == "present":
376 child_configs = []
377 if route_control_profiles:
378 for key, name in route_control_profiles.items():
379 if "_profile" in key:
380 direction = key.rstrip("_profile")
381 if name == "" and isinstance(aci.existing, list) and len(aci.existing) > 0:
382 for child in aci.existing[0].get("l3extInstP", {}).get("children", {}):
383 if child.get("l3extRsInstPToProfile") and child.get("l3extRsInstPToProfile").get("attributes").get("direction") == direction:
384 child_configs.append(
385 dict(
386 l3extRsInstPToProfile=dict(
387 attributes=dict(
388 status="deleted",
389 direction=direction,
390 tnRtctrlProfileName=child.get("l3extRsInstPToProfile").get("attributes").get("tnRtctrlProfileName"),
391 )
392 )
393 )
394 )
395 elif name:
396 child_configs.append(dict(l3extRsInstPToProfile=dict(attributes=dict(direction=direction, tnRtctrlProfileName=name))))
397
398 class_config = dict(
399 name=extepg,
400 descr=description,
401 prefGrMemb=preferred_group,
402 targetDscp=dscp,
403 nameAlias=name_alias,
404 prio=qos_class,
405 exceptionTag=contract_exception_tag,
406 )
407
408 if intra_ext_epg_isolation:
409 class_config.update(pcEnfPref=intra_ext_epg_isolation)
410
411 aci.payload(aci_class="l3extInstP", class_config=class_config, child_configs=child_configs)
412
413 aci.get_diff(aci_class="l3extInstP")
414
415 aci.post_config()
416
417 elif state == "absent":
418 aci.delete_config()
419
420 aci.exit_json()
421
422
423 if __name__ == "__main__":
424 main()