Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_encap_pool_range.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_encap_pool_range
15 short_description: Manage encap ranges assigned to pools (fvns:EncapBlk and fvns:VsanEncapBlk)
16 description:
17 - Manage vlan, vxlan, and vsan ranges that are assigned to pools on Cisco ACI fabrics.
18 options:
19 allocation_mode:
20 description:
21 - The method used for allocating encaps to resources.
22 - Only vlan and vsan support allocation modes.
23 type: str
24 choices: [ dynamic, inherit, static]
25 aliases: [ mode ]
26 description:
27 description:
28 - Description for the pool range.
29 type: str
30 aliases: [ descr ]
31 pool:
32 description:
33 - The name of the pool that the range should be assigned to.
34 type: str
35 aliases: [ pool_name ]
36 pool_allocation_mode:
37 description:
38 - The method used for allocating encaps to resources.
39 - Only vlan and vsan support allocation modes.
40 type: str
41 choices: [ dynamic, static]
42 aliases: [ pool_mode ]
43 pool_type:
44 description:
45 - The encap type of C(pool).
46 type: str
47 required: true
48 aliases: [ type ]
49 choices: [ vlan, vxlan, vsan]
50 range_end:
51 description:
52 - The end of encap range.
53 type: int
54 aliases: [ end ]
55 range_name:
56 description:
57 - The name to give to the encap range.
58 type: str
59 aliases: [ name, range ]
60 range_start:
61 description:
62 - The start of the encap range.
63 type: int
64 aliases: [ start ]
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 name_alias:
73 description:
74 - The alias for the current object. This relates to the nameAlias field in ACI.
75 type: str
76 extends_documentation_fragment:
77 - cisco.aci.aci
78 - cisco.aci.annotation
79
80 notes:
81 - The C(pool) must exist in order to add or delete a range.
82 seealso:
83 - module: cisco.aci.aci_encap_pool
84 - module: cisco.aci.aci_vlan_pool_encap_block
85 - name: APIC Management Information Model reference
86 description: More information about the internal APIC classes B(fvns:EncapBlk) and B(fvns:VsanEncapBlk).
87 link: https://developer.cisco.com/docs/apic-mim-ref/
88 author:
89 - Jacob McGill (@jmcgill298)
90 """
91
92 EXAMPLES = r"""
93 - name: Add a new VLAN pool range
94 cisco.aci.aci_encap_pool_range:
95 host: apic
96 username: admin
97 password: SomeSecretPassword
98 pool: production
99 pool_type: vlan
100 pool_allocation_mode: static
101 range_name: anstest
102 range_start: 20
103 range_end: 40
104 allocation_mode: inherit
105 state: present
106 delegate_to: localhost
107
108 - name: Remove a VLAN pool range
109 cisco.aci.aci_encap_pool_range:
110 host: apic
111 username: admin
112 password: SomeSecretPassword
113 pool: production
114 pool_type: vlan
115 pool_allocation_mode: static
116 range_name: anstest
117 range_start: 20
118 range_end: 40
119 state: absent
120 delegate_to: localhost
121
122 - name: Query a VLAN range
123 cisco.aci.aci_encap_pool_range:
124 host: apic
125 username: admin
126 password: SomeSecretPassword
127 pool: production
128 pool_type: vlan
129 pool_allocation_mode: static
130 range_name: anstest
131 range_start: 20
132 range_end: 50
133 state: query
134 delegate_to: localhost
135 register: query_result
136
137 - name: Query a VLAN pool for ranges by range_name
138 cisco.aci.aci_encap_pool_range:
139 host: apic
140 username: admin
141 password: SomeSecretPassword
142 pool_type: vlan
143 range_name: anstest
144 state: query
145 delegate_to: localhost
146 register: query_result
147
148 - name: Query a VLAN pool for ranges by range_start
149 cisco.aci.aci_encap_pool_range:
150 host: apic
151 username: admin
152 password: SomeSecretPassword
153 pool_type: vlan
154 range_start: 20
155 state: query
156 delegate_to: localhost
157 register: query_result
158
159 - name: Query a VLAN pool for ranges by range_start and range_end
160 cisco.aci.aci_encap_pool_range:
161 host: apic
162 username: admin
163 password: SomeSecretPassword
164 pool_type: vlan
165 range_start: 20
166 range_end: 40
167 state: query
168 delegate_to: localhost
169 register: query_result
170
171 - name: Query all VLAN pool ranges
172 cisco.aci.aci_encap_pool_range:
173 host: apic
174 username: admin
175 password: SomeSecretPassword
176 pool_type: vlan
177 state: query
178 delegate_to: localhost
179 register: query_result
180 """
181
182 RETURN = r"""
183 current:
184 description: The existing configuration from the APIC after the module has finished
185 returned: success
186 type: list
187 sample:
188 [
189 {
190 "fvTenant": {
191 "attributes": {
192 "descr": "Production environment",
193 "dn": "uni/tn-production",
194 "name": "production",
195 "nameAlias": "",
196 "ownerKey": "",
197 "ownerTag": ""
198 }
199 }
200 }
201 ]
202 error:
203 description: The error information as returned from the APIC
204 returned: failure
205 type: dict
206 sample:
207 {
208 "code": "122",
209 "text": "unknown managed object class foo"
210 }
211 raw:
212 description: The raw output returned by the APIC REST API (xml or json)
213 returned: parse error
214 type: str
215 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
216 sent:
217 description: The actual/minimal configuration pushed to the APIC
218 returned: info
219 type: list
220 sample:
221 {
222 "fvTenant": {
223 "attributes": {
224 "descr": "Production environment"
225 }
226 }
227 }
228 previous:
229 description: The original configuration from the APIC before the module has started
230 returned: info
231 type: list
232 sample:
233 [
234 {
235 "fvTenant": {
236 "attributes": {
237 "descr": "Production",
238 "dn": "uni/tn-production",
239 "name": "production",
240 "nameAlias": "",
241 "ownerKey": "",
242 "ownerTag": ""
243 }
244 }
245 }
246 ]
247 proposed:
248 description: The assembled configuration from the user-provided parameters
249 returned: info
250 type: dict
251 sample:
252 {
253 "fvTenant": {
254 "attributes": {
255 "descr": "Production environment",
256 "name": "production"
257 }
258 }
259 }
260 filter_string:
261 description: The filter string used for the request
262 returned: failure or debug
263 type: str
264 sample: ?rsp-prop-include=config-only
265 method:
266 description: The HTTP method used for the request to the APIC
267 returned: failure or debug
268 type: str
269 sample: POST
270 response:
271 description: The HTTP response from the APIC
272 returned: failure or debug
273 type: str
274 sample: OK (30 bytes)
275 status:
276 description: The HTTP status from the APIC
277 returned: failure or debug
278 type: int
279 sample: 200
280 url:
281 description: The HTTP url used for the request to the APIC
282 returned: failure or debug
283 type: str
284 sample: https://10.11.12.13/api/mo/uni/tn-production.json
285 """
286
287 from ansible.module_utils.basic import AnsibleModule
288 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
289
290 ACI_POOL_MAPPING = dict(
291 vlan=dict(
292 aci_class="fvnsVlanInstP",
293 aci_mo="infra/vlanns-",
294 ),
295 vxlan=dict(
296 aci_class="fvnsVxlanInstP",
297 aci_mo="infra/vxlanns-",
298 ),
299 vsan=dict(
300 aci_class="fvnsVsanInstP",
301 aci_mo="infra/vsanns-",
302 ),
303 )
304
305
306 def main():
307 argument_spec = aci_argument_spec()
308 argument_spec.update(aci_annotation_spec())
309 argument_spec.update(
310 pool_type=dict(type="str", required=True, aliases=["type"], choices=["vlan", "vxlan", "vsan"]),
311 allocation_mode=dict(type="str", aliases=["mode"], choices=["dynamic", "inherit", "static"]),
312 description=dict(type="str", aliases=["descr"]),
313 pool=dict(type="str", aliases=["pool_name"]), # Not required for querying all objects
314 pool_allocation_mode=dict(type="str", aliases=["pool_mode"], choices=["dynamic", "static"]),
315 range_end=dict(type="int", aliases=["end"]), # Not required for querying all objects
316 range_name=dict(type="str", aliases=["name", "range"]), # Not required for querying all objects
317 range_start=dict(type="int", aliases=["start"]), # Not required for querying all objects
318 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
319 name_alias=dict(type="str"),
320 )
321
322 module = AnsibleModule(
323 argument_spec=argument_spec,
324 supports_check_mode=True,
325 required_if=[
326 ["state", "absent", ["pool", "range_end", "range_start"]],
327 ["state", "present", ["pool", "range_end", "range_start"]],
328 ],
329 )
330
331 allocation_mode = module.params.get("allocation_mode")
332 description = module.params.get("description")
333 pool = module.params.get("pool")
334 pool_allocation_mode = module.params.get("pool_allocation_mode")
335 pool_type = module.params.get("pool_type")
336 range_end = module.params.get("range_end")
337 range_name = module.params.get("range_name")
338 range_start = module.params.get("range_start")
339 state = module.params.get("state")
340 name_alias = module.params.get("name_alias")
341
342 if range_end is not None:
343 encap_end = "{0}-{1}".format(pool_type, range_end)
344 else:
345 encap_end = None
346
347 if range_start is not None:
348 encap_start = "{0}-{1}".format(pool_type, range_start)
349 else:
350 encap_start = None
351
352 ACI_RANGE_MAPPING = dict(
353 vlan=dict(
354 aci_class="fvnsEncapBlk",
355 aci_mo="from-[{0}]-to-[{1}]".format(encap_start, encap_end),
356 ),
357 vxlan=dict(
358 aci_class="fvnsEncapBlk",
359 aci_mo="from-[{0}]-to-[{1}]".format(encap_start, encap_end),
360 ),
361 vsan=dict(
362 aci_class="fvnsVsanEncapBlk",
363 aci_mo="vsanfrom-[{0}]-to-[{1}]".format(encap_start, encap_end),
364 ),
365 )
366
367 # Collect proper class and mo information based on pool_type
368 aci_range_class = ACI_RANGE_MAPPING[pool_type]["aci_class"]
369 aci_range_mo = ACI_RANGE_MAPPING[pool_type]["aci_mo"]
370 aci_pool_class = ACI_POOL_MAPPING[pool_type]["aci_class"]
371 aci_pool_mo = ACI_POOL_MAPPING[pool_type]["aci_mo"]
372 pool_name = pool
373
374 # Validate range_end and range_start are valid for its respective encap type
375 for encap_id in range_end, range_start:
376 if encap_id is not None:
377 if pool_type == "vlan":
378 if not 1 <= encap_id <= 4094:
379 module.fail_json(msg='vlan pools must have "range_start" and "range_end" values between 1 and 4094')
380 elif pool_type == "vxlan":
381 if not 5000 <= encap_id <= 16777215:
382 module.fail_json(msg='vxlan pools must have "range_start" and "range_end" values between 5000 and 16777215')
383 else:
384 if not 1 <= encap_id <= 4093:
385 module.fail_json(msg='vsan pools must have "range_start" and "range_end" values between 1 and 4093')
386
387 if range_end is not None and range_start is not None:
388 # Validate range_start is less than range_end
389 if range_start > range_end:
390 module.fail_json(msg='The "range_start" must be less than or equal to the "range_end"')
391
392 elif range_end is None and range_start is None:
393 if range_name is None:
394 # Reset range managed object to None for aci util to properly handle query
395 aci_range_mo = None
396
397 # Vxlan does not support setting the allocation mode
398 if pool_type == "vxlan" and allocation_mode is not None:
399 module.fail_json(msg='vxlan pools do not support setting the "allocation_mode"; please omit this parameter for vxlan pools')
400
401 # ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
402 if pool_type != "vxlan" and pool is not None:
403 if pool_allocation_mode is not None:
404 pool_name = "[{0}]-{1}".format(pool, pool_allocation_mode)
405 else:
406 module.fail_json(msg='ACI requires the "pool_allocation_mode" for "pool_type" of "vlan" and "vsan" when the "pool" is provided')
407
408 aci = ACIModule(module)
409 aci.construct_url(
410 root_class=dict(
411 aci_class=aci_pool_class,
412 aci_rn="{0}{1}".format(aci_pool_mo, pool_name),
413 module_object=pool,
414 target_filter={"name": pool},
415 ),
416 subclass_1=dict(
417 aci_class=aci_range_class,
418 aci_rn="{0}".format(aci_range_mo),
419 module_object=aci_range_mo,
420 target_filter={"from": encap_start, "to": encap_end, "name": range_name},
421 ),
422 )
423
424 aci.get_existing()
425
426 if state == "present":
427 aci.payload(
428 aci_class=aci_range_class,
429 class_config={
430 "allocMode": allocation_mode,
431 "descr": description,
432 "from": encap_start,
433 "name": range_name,
434 "to": encap_end,
435 "nameAlias": name_alias,
436 },
437 )
438
439 aci.get_diff(aci_class=aci_range_class)
440
441 aci.post_config()
442
443 elif state == "absent":
444 aci.delete_config()
445
446 aci.exit_json()
447
448
449 if __name__ == "__main__":
450 main()