Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Tim Cragg (@timcragg)
5 # Copyright: (c) 2025, Shreyas Srish (@shrsr)
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": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_l4l7_device_selection_policy
17 version_added: "2.12.0"
18 short_description: Manage L4-L7 Device Selection Policies (vns:LDevCtx)
19 description:
20 - Manage L4-L7 Device Selection Policies
21 options:
22 tenant:
23 description:
24 - The name of an existing tenant.
25 type: str
26 aliases: [ tenant_name ]
27 contract:
28 description:
29 - The name of an existing contract.
30 - The APIC defaults to C(any) when unset during creation.
31 type: str
32 aliases: [ contract_name ]
33 graph:
34 description:
35 - The name of an existing service graph.
36 - The APIC defaults to C(any) when unset during creation.
37 type: str
38 aliases: [ service_graph, service_graph_name ]
39 node:
40 description:
41 - The name of an existing L4-L7 node.
42 - The APIC defaults to C(any) when unset during creation.
43 type: str
44 aliases: [ node_name ]
45 device:
46 description:
47 - The name of the L4-L7 Device to bind to the policy.
48 type: str
49 context:
50 description:
51 - The context name.
52 type: str
53 description:
54 description:
55 - A brief description for the Device Selection Policy.
56 type: str
57 state:
58 description:
59 - Use C(present) or C(absent) for adding or removing.
60 - Use C(query) for listing an object or multiple objects.
61 type: str
62 choices: [ absent, present, query ]
63 default: present
64 extends_documentation_fragment:
65 - cisco.aci.aci
66 - cisco.aci.annotation
67 notes:
68 - The I(tenant), I(contract), I(graph), I(device) and I(node) must exist before using this module in your playbook.
69 The M(cisco.aci.aci_tenant), M(cisco.aci.aci_contract), M(cisco.aci.aci_l4l7_service_graph), M(cisco.aci.aci_l4l7_device) and
70 M(cisco.aci.aci_l4l7_service_graph_template_node) modules can be used for this.
71 seealso:
72 - module: cisco.aci.aci_tenant
73 - module: cisco.aci.aci_contract
74 - module: cisco.aci.aci_l4l7_service_graph
75 - module: cisco.aci.aci_l4l7_device
76 - module: cisco.aci.aci_l4l7_service_graph_template_node
77 - name: APIC Management Information Model reference
78 description: More information about the internal APIC class B(vns:LDevCtx)
79 link: https://developer.cisco.com/docs/apic-mim-ref/
80 author:
81 - Tim Cragg (@timcragg)
82 - Shreyas Srish (@shrsr)
83 """
84
85 EXAMPLES = r"""
86 - name: Add a new device selection policy
87 cisco.aci.aci_l4l7_device_selection_policy:
88 host: apic
89 username: admin
90 password: SomeSecretPassword
91 tenant: my_tenant
92 contract: my_contract
93 graph: my_graph
94 node: my_node
95 state: present
96 delegate_to: localhost
97
98 - name: Query a device selection policy
99 cisco.aci.aci_l4l7_device_selection_policy:
100 host: apic
101 username: admin
102 password: SomeSecretPassword
103 tenant: my_tenant
104 contract: my_contract
105 graph: my_graph
106 node: my_node
107 state: query
108 delegate_to: localhost
109 register: query_result
110
111 - name: Query all device selection policies
112 cisco.aci.aci_l4l7_device_selection_policy:
113 host: apic
114 username: admin
115 password: SomeSecretPassword
116 state: query
117 delegate_to: localhost
118 register: query_result
119
120 - name: Delete a device selection policy
121 cisco.aci.aci_l4l7_device_selection_policy:
122 host: apic
123 username: admin
124 password: SomeSecretPassword
125 tenant: my_tenant
126 contract: my_contract
127 graph: my_graph
128 node: my_node
129 state: absent
130 delegate_to: localhost
131 """
132
133 RETURN = r"""
134 current:
135 description: The existing configuration from the APIC after the module has finished
136 returned: success
137 type: list
138 sample:
139 [
140 {
141 "fvTenant": {
142 "attributes": {
143 "descr": "Production environment",
144 "dn": "uni/tn-production",
145 "name": "production",
146 "nameAlias": "",
147 "ownerKey": "",
148 "ownerTag": ""
149 }
150 }
151 }
152 ]
153 error:
154 description: The error information as returned from the APIC
155 returned: failure
156 type: dict
157 sample:
158 {
159 "code": "122",
160 "text": "unknown managed object class foo"
161 }
162 raw:
163 description: The raw output returned by the APIC REST API (xml or json)
164 returned: parse error
165 type: str
166 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
167 sent:
168 description: The actual/minimal configuration pushed to the APIC
169 returned: info
170 type: list
171 sample:
172 {
173 "fvTenant": {
174 "attributes": {
175 "descr": "Production environment"
176 }
177 }
178 }
179 previous:
180 description: The original configuration from the APIC before the module has started
181 returned: info
182 type: list
183 sample:
184 [
185 {
186 "fvTenant": {
187 "attributes": {
188 "descr": "Production",
189 "dn": "uni/tn-production",
190 "name": "production",
191 "nameAlias": "",
192 "ownerKey": "",
193 "ownerTag": ""
194 }
195 }
196 }
197 ]
198 proposed:
199 description: The assembled configuration from the user-provided parameters
200 returned: info
201 type: dict
202 sample:
203 {
204 "fvTenant": {
205 "attributes": {
206 "descr": "Production environment",
207 "name": "production"
208 }
209 }
210 }
211 filter_string:
212 description: The filter string used for the request
213 returned: failure or debug
214 type: str
215 sample: ?rsp-prop-include=config-only
216 method:
217 description: The HTTP method used for the request to the APIC
218 returned: failure or debug
219 type: str
220 sample: POST
221 response:
222 description: The HTTP response from the APIC
223 returned: failure or debug
224 type: str
225 sample: OK (30 bytes)
226 status:
227 description: The HTTP status from the APIC
228 returned: failure or debug
229 type: int
230 sample: 200
231 url:
232 description: The HTTP url used for the request to the APIC
233 returned: failure or debug
234 type: str
235 sample: https://10.11.12.13/api/mo/uni/tn-production.json
236 """
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
241
242
243 def main():
244 argument_spec = aci_argument_spec()
245 argument_spec.update(aci_annotation_spec())
246 argument_spec.update(
247 tenant=dict(type="str", aliases=["tenant_name"]),
248 contract=dict(type="str", aliases=["contract_name"]),
249 graph=dict(type="str", aliases=["service_graph", "service_graph_name"]),
250 node=dict(type="str", aliases=["node_name"]),
251 device=dict(type="str"),
252 context=dict(type="str"),
253 description=dict(type="str"),
254 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
255 )
256
257 module = AnsibleModule(
258 argument_spec=argument_spec,
259 supports_check_mode=True,
260 required_if=[
261 ["state", "absent", ["tenant", "contract", "graph", "node"]],
262 ["state", "present", ["tenant", "contract", "graph", "node"]],
263 ],
264 )
265
266 tenant = module.params.get("tenant")
267 state = module.params.get("state")
268 contract = module.params.get("contract")
269 graph = module.params.get("graph")
270 node = module.params.get("node")
271 device = module.params.get("device")
272 context = module.params.get("context")
273 description = module.params.get("description")
274
275 ldev_ctx_rn = "ldevCtx-c-{0}-g-{1}-n-{2}".format(contract, graph, node) if (contract, graph, node) != (None, None, None) else None
276
277 aci = ACIModule(module)
278
279 aci.construct_url(
280 root_class=dict(
281 aci_class="fvTenant",
282 aci_rn="tn-{0}".format(tenant),
283 module_object=tenant,
284 target_filter={"name": tenant},
285 ),
286 subclass_1=dict(
287 aci_class="vnsLDevCtx",
288 aci_rn=ldev_ctx_rn,
289 module_object=ldev_ctx_rn,
290 target_filter={"dn": ldev_ctx_rn},
291 ),
292 child_classes=["vnsRsLDevCtxToLDev"],
293 )
294
295 aci.get_existing()
296
297 if state == "present":
298 child_configs = []
299 if device:
300 device_tdn = "uni/tn-{0}/lDevVip-{1}".format(tenant, device)
301 child_configs.append({"vnsRsLDevCtxToLDev": {"attributes": {"tDn": device_tdn}}})
302 else:
303 device_tdn = None
304 # Validate if existing and remove child objects when do not match provided configuration
305 if isinstance(aci.existing, list) and len(aci.existing) > 0:
306 for child in aci.existing[0].get("vnsLDevCtx", {}).get("children", {}):
307 if child.get("vnsRsLDevCtxToLDev") and child.get("vnsRsLDevCtxToLDev").get("attributes").get("tDn") != device_tdn:
308 child_configs.append(
309 {
310 "vnsRsLDevCtxToLDev": {
311 "attributes": {
312 "dn": child.get("vnsRsLDevCtxToLDev").get("attributes").get("dn"),
313 "status": "deleted",
314 }
315 }
316 }
317 )
318 aci.payload(
319 aci_class="vnsLDevCtx",
320 class_config=dict(
321 ctrctNameOrLbl=contract,
322 graphNameOrLbl=graph,
323 nodeNameOrLbl=node,
324 context=context,
325 descr=description,
326 ),
327 child_configs=child_configs,
328 )
329 aci.get_diff(aci_class="vnsLDevCtx")
330
331 aci.post_config()
332
333 elif state == "absent":
334 aci.delete_config()
335
336 aci.exit_json()
337
338
339 if __name__ == "__main__":
340 main()