Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2023, Tim Cragg (@timcragg) <[email protected]>
4 # Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]>
5
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_fabric_pod_selector
17 short_description: Manage Fabric Pod Selectors (fabric:PodS)
18 description:
19 - Manage Fabric Pod Selectors on Cisco ACI fabrics.
20 options:
21 pod_profile:
22 description:
23 - The name of the Pod Profile that contains the Selector.
24 type: str
25 name:
26 description:
27 - The name of the Pod Selector.
28 type: str
29 aliases: [ selector, pod_selector ]
30 description:
31 description:
32 - The description for the Fabric Pod Selector.
33 type: str
34 aliases: [ descr ]
35 type:
36 description:
37 - The type of the Pod Selector.
38 type: str
39 choices: [ all, range ]
40 blocks:
41 description:
42 - The pod id(s) associated with the Pod Selector.
43 - Existing blocks will be removed when they are not matching provided blocks.
44 - A comma-separated string of pod ids or ranges of pod ids. (ex. 1,3-4)
45 type: str
46 aliases: [ pod_id, pod_id_range ]
47 policy_group:
48 description:
49 - The Fabric Policy Group to bind to this Pod Selector.
50 - Provide an empty string C("") to remove the Fabric Policy Group binding.
51 type: str
52 state:
53 description:
54 - Use C(present) or C(absent) for adding or removing.
55 - Use C(query) for listing an object or multiple objects.
56 type: str
57 choices: [ absent, present, query ]
58 default: present
59 name_alias:
60 description:
61 - The alias for the current object. This relates to the nameAlias field in ACI.
62 type: str
63 extends_documentation_fragment:
64 - cisco.aci.aci
65 - cisco.aci.annotation
66 - cisco.aci.owner
67
68 notes:
69 - The C(pod_profile) must exist before using this module in your playbook.
70 - The M(cisco.aci.aci_fabric_pod_profile) module can be used to create the C(pod_profile).
71 seealso:
72 - name: APIC Management Information Model reference
73 description: More information about the internal APIC class B(fabric:PodS).
74 link: https://developer.cisco.com/docs/apic-mim-ref/
75 author:
76 - Tim Cragg (@timcragg)
77 - Akini Ross (@akinross)
78 """
79
80 EXAMPLES = r"""
81 - name: Add a new pod selector with type all
82 cisco.aci.aci_fabric_pod_selector:
83 host: apic
84 username: admin
85 password: SomeSecretPassword
86 pod_profile: default
87 name: ans_pod_selector
88 type: all
89 policy_group: ansible_policy_group
90 state: present
91 delegate_to: localhost
92
93 - name: Add a new pod selector with type range and blocks
94 cisco.aci.aci_fabric_pod_selector:
95 host: apic
96 username: admin
97 password: SomeSecretPassword
98 pod_profile: default
99 name: ans_pod_selector
100 type: range
101 blocks: 1,3-4
102 policy_group: ansible_policy_group
103 state: present
104 delegate_to: localhost
105
106 - name: Remove a policy_group from an existing pod selector
107 cisco.aci.aci_fabric_pod_selector:
108 host: apic
109 username: admin
110 password: SomeSecretPassword
111 pod_profile: default
112 type: all
113 name: ans_pod_selector
114 policy_group: ""
115 state: present
116 delegate_to: localhost
117
118 - name: Remove a pod selector type all
119 cisco.aci.aci_fabric_pod_selector:
120 host: apic
121 username: admin
122 password: SomeSecretPassword
123 pod_profile: default
124 type: all
125 name: ans_pod_selector
126 state: absent
127 delegate_to: localhost
128
129 - name: Remove a pod selector type range
130 cisco.aci.aci_fabric_pod_selector:
131 host: apic
132 username: admin
133 password: SomeSecretPassword
134 pod_profile: default
135 type: range
136 name: ans_pod_selector
137 state: absent
138 delegate_to: localhost
139
140 - name: Query a pod selector
141 cisco.aci.aci_fabric_pod_selector:
142 host: apic
143 username: admin
144 password: SomeSecretPassword
145 pod_profile: default
146 name: ans_pod_selector
147 type: all
148 state: query
149 delegate_to: localhost
150 register: query_result
151
152 - name: Query all pod selectors
153 cisco.aci.aci_fabric_pod_selector:
154 host: apic
155 username: admin
156 password: SomeSecretPassword
157 state: query
158 delegate_to: localhost
159 register: query_result
160 """
161
162 RETURN = r"""
163 current:
164 description: The existing configuration from the APIC after the module has finished
165 returned: success
166 type: list
167 sample:
168 [
169 {
170 "fvTenant": {
171 "attributes": {
172 "descr": "Production environment",
173 "dn": "uni/tn-production",
174 "name": "production",
175 "nameAlias": "",
176 "ownerKey": "",
177 "ownerTag": ""
178 }
179 }
180 }
181 ]
182 error:
183 description: The error information as returned from the APIC
184 returned: failure
185 type: dict
186 sample:
187 {
188 "code": "122",
189 "text": "unknown managed object class foo"
190 }
191 raw:
192 description: The raw output returned by the APIC REST API (xml or json)
193 returned: parse error
194 type: str
195 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
196 sent:
197 description: The actual/minimal configuration pushed to the APIC
198 returned: info
199 type: list
200 sample:
201 {
202 "fvTenant": {
203 "attributes": {
204 "descr": "Production environment"
205 }
206 }
207 }
208 previous:
209 description: The original configuration from the APIC before the module has started
210 returned: info
211 type: list
212 sample:
213 [
214 {
215 "fvTenant": {
216 "attributes": {
217 "descr": "Production",
218 "dn": "uni/tn-production",
219 "name": "production",
220 "nameAlias": "",
221 "ownerKey": "",
222 "ownerTag": ""
223 }
224 }
225 }
226 ]
227 proposed:
228 description: The assembled configuration from the user-provided parameters
229 returned: info
230 type: dict
231 sample:
232 {
233 "fvTenant": {
234 "attributes": {
235 "descr": "Production environment",
236 "name": "production"
237 }
238 }
239 }
240 filter_string:
241 description: The filter string used for the request
242 returned: failure or debug
243 type: str
244 sample: ?rsp-prop-include=config-only
245 method:
246 description: The HTTP method used for the request to the APIC
247 returned: failure or debug
248 type: str
249 sample: POST
250 response:
251 description: The HTTP response from the APIC
252 returned: failure or debug
253 type: str
254 sample: OK (30 bytes)
255 status:
256 description: The HTTP status from the APIC
257 returned: failure or debug
258 type: int
259 sample: 200
260 url:
261 description: The HTTP url used for the request to the APIC
262 returned: failure or debug
263 type: str
264 sample: https://10.11.12.13/api/mo/uni/tn-production.json
265 """
266
267 import binascii
268 import os
269
270 from ansible.module_utils.basic import AnsibleModule
271 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
272 from ansible_collections.cisco.aci.plugins.module_utils.constants import FABRIC_POD_SELECTOR_TYPE_MAPPING
273
274
275 def main():
276 argument_spec = aci_argument_spec()
277 argument_spec.update(aci_annotation_spec())
278 argument_spec.update(aci_owner_spec())
279 argument_spec.update(
280 description=dict(type="str", aliases=["descr"]),
281 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
282 name_alias=dict(type="str"),
283 pod_profile=dict(type="str"),
284 name=dict(type="str", aliases=["selector", "pod_selector"]),
285 type=dict(type="str", choices=["all", "range"]),
286 blocks=dict(type="str", aliases=["pod_id", "pod_id_range"]),
287 policy_group=dict(type="str"),
288 )
289
290 module = AnsibleModule(
291 argument_spec=argument_spec,
292 supports_check_mode=True,
293 required_if=[
294 ["state", "absent", ["pod_profile", "name", "type"]],
295 ["state", "present", ["pod_profile", "name", "type"]],
296 ],
297 )
298
299 aci = ACIModule(module)
300
301 name_alias = module.params.get("name_alias")
302 pod_profile = module.params.get("pod_profile")
303 name = module.params.get("name")
304 policy_group = module.params.get("policy_group")
305 description = module.params.get("description")
306 selector_type = FABRIC_POD_SELECTOR_TYPE_MAPPING.get(module.params.get("type"))
307 blocks = [i.strip().split("-") for i in module.params.get("blocks").split(",")] if module.params.get("blocks") else []
308 state = module.params.get("state")
309
310 if state == "present" and selector_type == "range" and not blocks:
311 module.fail_json(msg="The 'blocks' parameter is required when the 'type' parameter is set to 'range' and 'state' parameter is set to 'present'.")
312
313 child_classes = ["fabricRsPodPGrp", "fabricPodBlk"]
314
315 aci.construct_url(
316 root_class=dict(
317 aci_class="fabricPodP",
318 aci_rn="fabric/podprof-{0}".format(pod_profile),
319 module_object=pod_profile,
320 target_filter={"name": pod_profile},
321 ),
322 subclass_1=dict(
323 aci_class="fabricPodS",
324 aci_rn="pods-{0}-typ-{1}".format(name, selector_type),
325 module_object=name,
326 target_filter={"name": name, "type": selector_type},
327 ),
328 child_classes=child_classes,
329 )
330
331 aci.get_existing()
332
333 if state == "present":
334 child_configs = []
335
336 if policy_group is not None:
337 child_configs.append(
338 {
339 "fabricRsPodPGrp": {
340 "attributes": {"status": "deleted"} if policy_group == "" else {"tDn": "uni/fabric/funcprof/podpgrp-{0}".format(policy_group)}
341 }
342 }
343 )
344
345 if blocks:
346 if isinstance(aci.existing, list) and len(aci.existing) > 0:
347 for child in aci.existing[0].get("fabricPodS", {}).get("children", {}):
348 if child.get("fabricPodBlk"):
349 from_ = child.get("fabricPodBlk").get("attributes").get("from_")
350 to_ = child.get("fabricPodBlk").get("attributes").get("to_")
351 if [from_, to_] in blocks:
352 blocks.remove([from_, to_])
353 elif (from_ == to_) and [from_] in blocks:
354 blocks.remove([from_])
355 else:
356 child_configs.append(
357 {
358 "fabricPodBlk": {
359 "attributes": {
360 "dn": "uni/fabric/podprof-{0}/pods-{1}-typ-{2}/podblk-{3}".format(
361 pod_profile, name, selector_type, child.get("fabricPodBlk").get("attributes").get("name")
362 ),
363 "status": "deleted",
364 }
365 }
366 }
367 )
368
369 for block in blocks:
370 child_configs.append(
371 {
372 "fabricPodBlk": {
373 "attributes": {
374 "name": binascii.b2a_hex(os.urandom(8)).decode("utf-8"),
375 "from_": block[0],
376 "to_": block[1] if len(block) > 1 else block[0],
377 }
378 }
379 }
380 )
381
382 aci.payload(
383 aci_class="fabricPodS",
384 class_config=dict(
385 name=name,
386 descr=description,
387 nameAlias=name_alias,
388 type=selector_type,
389 ),
390 child_configs=child_configs,
391 )
392
393 aci.get_diff(aci_class="fabricPodS")
394
395 aci.post_config()
396
397 elif state == "absent":
398 aci.delete_config()
399
400 aci.exit_json()
401
402
403 if __name__ == "__main__":
404 main()