Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Gaspard Micol (@gmicol) <[email protected]>
5 # Copyright: (c) 2023, Tim Cragg (@timcragg) <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_route_control_context
17 short_description: Manage Route Control Context (rtctrl:CtxP)
18 description:
19 - Manage Route Control Context Policies for the Route Control Profiles on Cisco ACI fabrics.
20 options:
21 tenant:
22 description:
23 - The name of an existing tenant.
24 type: str
25 aliases: [ tenant_name ]
26 l3out:
27 description:
28 - The name of an existing L3Out.
29 - To use only if the route control profile is linked to an existing L3Out.
30 type: str
31 aliases: [ l3out_name ]
32 route_control_profile:
33 description:
34 - The name of an existing route control profile.
35 type: str
36 aliases: [ route_control_profile_name ]
37 route_control_context:
38 description:
39 - The name of the route control context being created.
40 type: str
41 aliases: [ name, route_control_context_name, context ]
42 action:
43 description:
44 - The action required when the condition is met.
45 type: str
46 choices: [ deny, permit ]
47 action_rule:
48 description:
49 - The name of the action rule profile to be associated with this route control context.
50 - Set the rules for a Route Map.
51 type: str
52 aliases: [ action_rule_name ]
53 match_rule:
54 description:
55 - The name of the match rule profile to be associated with this route control context.
56 - Set the associated Matched rules.
57 type: str
58 aliases: [ match_rule_name ]
59 order:
60 description:
61 - The order of the route control context.
62 - The value range from 0 to 9.
63 type: int
64 description:
65 description:
66 - The description for the route control context.
67 type: str
68 aliases: [ descr ]
69 state:
70 description:
71 - Use C(present) or C(absent) for adding or removing.
72 - Use C(query) for listing an object or multiple objects.
73 type: str
74 choices: [ absent, present, query ]
75 default: present
76 name_alias:
77 description:
78 - The alias for the current object. This relates to the nameAlias field in ACI.
79 type: str
80 extends_documentation_fragment:
81 - cisco.aci.aci
82 - cisco.aci.annotation
83 - cisco.aci.owner
84
85 notes:
86 - The C(tenant) and the C(route_control_profile) used must exist before using this module in your playbook.
87 The M(cisco.aci.aci_tenant) and the M(cisco.aci.aci_route_control_profile) modules can be used for this.
88 - If C(l3out) is used, the C(l3out) used must exist before using this module in your playbook.
89 The M(cisco.aci.aci_l3out) module can be used for this.
90 - if C(action_rule) is used, the C(action_rule) used must exist before using this module in your plabook.
91 The module M(cisco.aci.aci_tenant_action_rule_profile) can be used for this.
92 - if C(match_rule) is used, the C(match_rule) used must exist before using this module in your plabook.
93 The module M(cisco.aci.aci_match_rule) can be used for this.
94 seealso:
95 - module: cisco.aci.aci_tenant
96 - module: cisco.aci.aci_route_control_profile
97 - module: cisco.aci.aci_l3out
98 - module: cisco.aci.aci_tenant_action_rule_profile
99 - module: cisco.aci.aci_match_rule
100 - name: APIC Management Information Model reference
101 description: More information about the internal APIC class B(rtctrl:CtxP).
102 link: https://developer.cisco.com/docs/apic-mim-ref/
103 author:
104 - Gaspard Micol (@gmicol)
105 - Tim Cragg (@timcragg)
106 """
107
108 EXAMPLES = r"""
109 - name: Create a route context policy
110 cisco.aci.aci_route_control_context:
111 host: apic
112 username: admin
113 password: SomeSecretPassword
114 route_control_context: prod_route_control_context
115 route_control_profile: prod_route_control_profile
116 tenant: production
117 l3out: prod_l3out
118 action: permit
119 order: 0
120 action_rule: prod_action_rule_profile
121 match_rule: prod_match_rule
122 state: present
123 delegate_to: localhost
124
125 - name: Delete a route context policy
126 cisco.aci.aci_route_control_context:
127 host: apic
128 username: admin
129 password: SomeSecretPassword
130 route_control_context: prod_route_control_context
131 route_control_profile: prod_route_control_profile
132 tenant: production
133 l3out: prod_l3out
134 state: absent
135 delegate_to: localhost
136
137 - name: Query all route context policy
138 cisco.aci.aci_route_control_context:
139 host: apic
140 username: admin
141 password: SomeSecretPassword
142 state: query
143 delegate_to: localhost
144 register: query_result
145
146 - name: Query a specific route context policy
147 cisco.aci.aci_route_control_profile:
148 host: apic
149 username: admin
150 password: SomeSecretPassword
151 route_control_context: prod_route_control_context
152 route_control_profile: prod_route_control_profile
153 tenant: production
154 l3out: prod_l3out
155 state: query
156 delegate_to: localhost
157 register: query_result
158 """
159
160 RETURN = r"""
161 current:
162 description: The existing configuration from the APIC after the module has finished
163 returned: success
164 type: list
165 sample:
166 [
167 {
168 "fvTenant": {
169 "attributes": {
170 "descr": "Production environment",
171 "dn": "uni/tn-production",
172 "name": "production",
173 "nameAlias": "",
174 "ownerKey": "",
175 "ownerauto_continue": ""
176 }
177 }
178 }
179 ]
180 error:
181 description: The error information as returned from the APIC
182 returned: failure
183 type: dict
184 sample:
185 {
186 "code": "122",
187 "text": "unknown managed object class foo"
188 }
189 raw:
190 description: The raw output returned by the APIC REST API (xml or json)
191 returned: parse error
192 type: str
193 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
194 sent:
195 description: The actual/minimal configuration pushed to the APIC
196 returned: info
197 type: list
198 sample:
199 {
200 "fvTenant": {
201 "attributes": {
202 "descr": "Production environment"
203 }
204 }
205 }
206 previous:
207 description: The original configuration from the APIC before the module has started
208 returned: info
209 type: list
210 sample:
211 [
212 {
213 "fvTenant": {
214 "attributes": {
215 "descr": "Production",
216 "dn": "uni/tn-production",
217 "name": "production",
218 "nameAlias": "",
219 "ownerKey": "",
220 "ownerauto_continue": ""
221 }
222 }
223 }
224 ]
225 proposed:
226 description: The assembled configuration from the user-provided parameters
227 returned: info
228 type: dict
229 sample:
230 {
231 "fvTenant": {
232 "attributes": {
233 "descr": "Production environment",
234 "name": "production"
235 }
236 }
237 }
238 filter_string:
239 description: The filter string used for the request
240 returned: failure or debug
241 type: str
242 sample: ?rsp-prop-include=config-only
243 method:
244 description: The HTTP method used for the request to the APIC
245 returned: failure or debug
246 type: str
247 sample: POST
248 response:
249 description: The HTTP response from the APIC
250 returned: failure or debug
251 type: str
252 sample: OK (30 bytes)
253 status:
254 description: The HTTP status from the APIC
255 returned: failure or debug
256 type: int
257 sample: 200
258 url:
259 description: The HTTP url used for the request to the APIC
260 returned: failure or debug
261 type: str
262 sample: https://10.11.12.13/api/mo/uni/tn-production.json
263 """
264
265 from ansible.module_utils.basic import AnsibleModule
266 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
267
268
269 def main():
270 argument_spec = aci_argument_spec()
271 argument_spec.update(aci_annotation_spec())
272 argument_spec.update(aci_owner_spec())
273 argument_spec.update(
274 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
275 l3out=dict(type="str", aliases=["l3out_name"]), # Not required for querying all objects
276 route_control_profile=dict(type="str", aliases=["route_control_profile_name"]), # Not required for querying all objects
277 route_control_context=dict(type="str", aliases=["name", "route_control_context_name", "context"]), # Not required for querying all objects
278 match_rule=dict(type="str", aliases=["match_rule_name"]),
279 action_rule=dict(type="str", aliases=["action_rule_name"]),
280 action=dict(type="str", choices=["deny", "permit"]),
281 order=dict(type="int"),
282 description=dict(type="str", aliases=["descr"]),
283 name_alias=dict(type="str"),
284 state=dict(type="str", default="present", choices=["present", "absent", "query"]),
285 )
286
287 module = AnsibleModule(
288 argument_spec=argument_spec,
289 supports_check_mode=True,
290 required_if=[
291 ["state", "absent", ["route_control_context", "tenant"]],
292 ["state", "present", ["route_control_context", "tenant"]],
293 ],
294 )
295
296 route_control_context = module.params.get("route_control_context")
297 description = module.params.get("description")
298 action = module.params.get("action")
299 order = module.params.get("order")
300 state = module.params.get("state")
301 tenant = module.params.get("tenant")
302 l3out = module.params.get("l3out")
303 route_control_profile = module.params.get("route_control_profile")
304 match_rule = module.params.get("match_rule")
305 action_rule = module.params.get("action_rule")
306 name_alias = module.params.get("name_alias")
307
308 aci = ACIModule(module)
309
310 child_classes = ["rtctrlRsCtxPToSubjP", "rtctrlScope"]
311
312 tenant_url_config = dict(
313 aci_class="fvTenant",
314 aci_rn="tn-{0}".format(tenant),
315 module_object=tenant,
316 target_filter={"name": tenant},
317 )
318
319 route_control_profile_url_config = dict(
320 aci_class="rtctrlProfile",
321 aci_rn="prof-{0}".format(route_control_profile),
322 module_object=route_control_profile,
323 target_filter={"name": route_control_profile},
324 )
325
326 route_control_context_url_config = dict(
327 aci_class="rtctrlCtxP",
328 aci_rn="ctx-{0}".format(route_control_context),
329 module_object=route_control_context,
330 target_filter={"name": route_control_context},
331 )
332
333 if l3out is not None:
334 aci.construct_url(
335 root_class=tenant_url_config,
336 subclass_1=dict(
337 aci_class="l3extOut",
338 aci_rn="out-{0}".format(l3out),
339 module_object=l3out,
340 target_filter={"name": l3out},
341 ),
342 subclass_2=route_control_profile_url_config,
343 subclass_3=route_control_context_url_config,
344 child_classes=child_classes,
345 )
346 else:
347 aci.construct_url(
348 root_class=tenant_url_config,
349 subclass_1=route_control_profile_url_config,
350 subclass_2=route_control_context_url_config,
351 child_classes=child_classes,
352 )
353
354 aci.get_existing()
355
356 if state == "present":
357 child_configs = []
358 if match_rule is not None:
359 child_configs.append({"rtctrlRsCtxPToSubjP": {"attributes": {"tnRtctrlSubjPName": match_rule}}})
360 if action_rule is not None:
361 child_configs.append(
362 {
363 "rtctrlScope": {
364 "attributes": {"descr": ""},
365 "children": [{"rtctrlRsScopeToAttrP": {"attributes": {"tnRtctrlAttrPName": action_rule}}}],
366 }
367 }
368 )
369
370 aci.payload(
371 aci_class="rtctrlCtxP",
372 class_config=dict(
373 name=route_control_context,
374 descr=description,
375 action=action,
376 order=order,
377 nameAlias=name_alias,
378 ),
379 child_configs=child_configs,
380 )
381
382 aci.get_diff(aci_class="rtctrlCtxP")
383
384 aci.post_config()
385
386 elif state == "absent":
387 aci.delete_config()
388
389 aci.exit_json()
390
391
392 if __name__ == "__main__":
393 main()