Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Jacob McGill (jmcgill298)
5 # Copyright: (c) 2018, Dag Wieers (dagwieers) <[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_vlan_pool_encap_block
17 short_description: Manage encap blocks assigned to VLAN pools (fvns:EncapBlk)
18 description:
19 - Manage VLAN encap blocks that are assigned to VLAN pools on Cisco ACI fabrics.
20 options:
21 allocation_mode:
22 description:
23 - The method used for allocating encaps to resources.
24 type: str
25 choices: [ dynamic, inherit, static]
26 aliases: [ mode ]
27 description:
28 description:
29 - Description for the pool encap block.
30 type: str
31 aliases: [ descr ]
32 pool:
33 description:
34 - The name of the pool that the encap block should be assigned to.
35 type: str
36 aliases: [ pool_name ]
37 pool_allocation_mode:
38 description:
39 - The method used for allocating encaps to resources.
40 type: str
41 choices: [ dynamic, static]
42 aliases: [ pool_mode ]
43 block_end:
44 description:
45 - The end of encap block.
46 type: int
47 aliases: [ end ]
48 block_name:
49 description:
50 - The name to give to the encap block.
51 type: str
52 aliases: [ name ]
53 block_start:
54 description:
55 - The start of the encap block.
56 type: int
57 aliases: [ start ]
58 state:
59 description:
60 - Use C(present) or C(absent) for adding or removing.
61 - Use C(query) for listing an object or multiple objects.
62 type: str
63 choices: [ absent, present, query ]
64 default: present
65 name_alias:
66 description:
67 - The alias for the current object. This relates to the nameAlias field in ACI.
68 type: str
69 extends_documentation_fragment:
70 - cisco.aci.aci
71 - cisco.aci.annotation
72
73 notes:
74 - The C(pool) must exist in order to add or delete a encap block.
75 seealso:
76 - module: cisco.aci.aci_encap_pool_range
77 - module: cisco.aci.aci_vlan_pool
78 - name: APIC Management Information Model reference
79 description: More information about the internal APIC class B(fvns:EncapBlk).
80 link: https://developer.cisco.com/docs/apic-mim-ref/
81 author:
82 - Jacob McGill (@jmcgill298)
83 - Dag Wieers (@dagwieers)
84 """
85
86 EXAMPLES = r"""
87 - name: Add a new VLAN encap block
88 cisco.aci.aci_vlan_pool_encap_block:
89 host: apic
90 username: admin
91 password: SomeSecretPassword
92 pool: production
93 pool_allocation_mode: static
94 block_start: 20
95 block_end: 50
96 state: present
97 delegate_to: localhost
98
99 - name: Remove a VLAN encap block
100 cisco.aci.aci_vlan_pool_encap_block:
101 host: apic
102 username: admin
103 password: SomeSecretPassword
104 pool: production
105 pool_allocation_mode: static
106 block_start: 20
107 block_end: 50
108 state: absent
109 delegate_to: localhost
110
111 - name: Query a VLAN encap block
112 cisco.aci.aci_vlan_pool_encap_block:
113 host: apic
114 username: admin
115 password: SomeSecretPassword
116 pool: production
117 pool_allocation_mode: static
118 block_start: 20
119 block_end: 50
120 state: query
121 delegate_to: localhost
122 register: query_result
123
124 - name: Query a VLAN pool for encap blocks
125 cisco.aci.aci_vlan_pool_encap_block:
126 host: apic
127 username: admin
128 password: SomeSecretPassword
129 pool: production
130 pool_allocation_mode: static
131 state: query
132 delegate_to: localhost
133 register: query_result
134
135 - name: Query all VLAN encap blocks
136 cisco.aci.aci_vlan_pool_encap_block:
137 host: apic
138 username: admin
139 password: SomeSecretPassword
140 state: query
141 delegate_to: localhost
142 register: query_result
143 """
144
145 RETURN = r"""
146 current:
147 description: The existing configuration from the APIC after the module has finished
148 returned: success
149 type: list
150 sample:
151 [
152 {
153 "fvTenant": {
154 "attributes": {
155 "descr": "Production environment",
156 "dn": "uni/tn-production",
157 "name": "production",
158 "nameAlias": "",
159 "ownerKey": "",
160 "ownerTag": ""
161 }
162 }
163 }
164 ]
165 error:
166 description: The error information as returned from the APIC
167 returned: failure
168 type: dict
169 sample:
170 {
171 "code": "122",
172 "text": "unknown managed object class foo"
173 }
174 raw:
175 description: The raw output returned by the APIC REST API (xml or json)
176 returned: parse error
177 type: str
178 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
179 sent:
180 description: The actual/minimal configuration pushed to the APIC
181 returned: info
182 type: list
183 sample:
184 {
185 "fvTenant": {
186 "attributes": {
187 "descr": "Production environment"
188 }
189 }
190 }
191 previous:
192 description: The original configuration from the APIC before the module has started
193 returned: info
194 type: list
195 sample:
196 [
197 {
198 "fvTenant": {
199 "attributes": {
200 "descr": "Production",
201 "dn": "uni/tn-production",
202 "name": "production",
203 "nameAlias": "",
204 "ownerKey": "",
205 "ownerTag": ""
206 }
207 }
208 }
209 ]
210 proposed:
211 description: The assembled configuration from the user-provided parameters
212 returned: info
213 type: dict
214 sample:
215 {
216 "fvTenant": {
217 "attributes": {
218 "descr": "Production environment",
219 "name": "production"
220 }
221 }
222 }
223 filter_string:
224 description: The filter string used for the request
225 returned: failure or debug
226 type: str
227 sample: ?rsp-prop-include=config-only
228 method:
229 description: The HTTP method used for the request to the APIC
230 returned: failure or debug
231 type: str
232 sample: POST
233 response:
234 description: The HTTP response from the APIC
235 returned: failure or debug
236 type: str
237 sample: OK (30 bytes)
238 status:
239 description: The HTTP status from the APIC
240 returned: failure or debug
241 type: int
242 sample: 200
243 url:
244 description: The HTTP url used for the request to the APIC
245 returned: failure or debug
246 type: str
247 sample: https://10.11.12.13/api/mo/uni/tn-production.json
248 """
249
250 from ansible.module_utils.basic import AnsibleModule
251 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
252
253
254 def main():
255 argument_spec = aci_argument_spec()
256 argument_spec.update(aci_annotation_spec())
257 argument_spec.update(
258 pool=dict(type="str", aliases=["pool_name"]), # Not required for querying all objects
259 block_name=dict(type="str", aliases=["name"]), # Not required for querying all objects
260 block_end=dict(type="int", aliases=["end"]), # Not required for querying all objects
261 block_start=dict(type="int", aliases=["start"]), # Not required for querying all objects
262 allocation_mode=dict(type="str", aliases=["mode"], choices=["dynamic", "inherit", "static"]),
263 description=dict(type="str", aliases=["descr"]),
264 pool_allocation_mode=dict(type="str", aliases=["pool_mode"], choices=["dynamic", "static"]),
265 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
266 name_alias=dict(type="str"),
267 )
268
269 module = AnsibleModule(
270 argument_spec=argument_spec,
271 supports_check_mode=True,
272 required_if=[
273 ["state", "absent", ["pool", "pool_allocation_mode", "block_end", "block_start"]],
274 ["state", "present", ["pool", "pool_allocation_mode", "block_end", "block_start"]],
275 ],
276 required_together=[
277 ["pool", "pool_allocation_mode"],
278 ],
279 )
280
281 allocation_mode = module.params.get("allocation_mode")
282 description = module.params.get("description")
283 pool = module.params.get("pool")
284 pool_allocation_mode = module.params.get("pool_allocation_mode")
285 block_end = module.params.get("block_end")
286 block_name = module.params.get("block_name")
287 block_start = module.params.get("block_start")
288 state = module.params.get("state")
289 name_alias = module.params.get("name_alias")
290
291 if block_end is not None:
292 encap_end = "vlan-{0}".format(block_end)
293 else:
294 encap_end = None
295
296 if block_start is not None:
297 encap_start = "vlan-{0}".format(block_start)
298 else:
299 encap_start = None
300
301 # Collect proper mo information
302 aci_block_mo = "from-[{0}]-to-[{1}]".format(encap_start, encap_end)
303 pool_name = pool
304
305 # Validate block_end and block_start are valid for its respective encap type
306 for encap_id in block_end, block_start:
307 if encap_id is not None:
308 if not 1 <= encap_id <= 4094:
309 module.fail_json(msg="vlan pools must have 'block_start' and 'block_end' values between 1 and 4094")
310
311 if block_end is not None and block_start is not None:
312 # Validate block_start is less than block_end
313 if block_start > block_end:
314 module.fail_json(msg="The 'block_start' must be less than or equal to the 'block_end'")
315
316 elif block_end is None and block_start is None:
317 if block_name is None:
318 # Reset range managed object to None for aci util to properly handle query
319 aci_block_mo = None
320
321 # ACI Pool URL requires the allocation mode (ex: uni/infra/vlanns-[poolname]-static)
322 if pool:
323 pool_name = "[{0}]-{1}".format(pool, pool_allocation_mode)
324
325 aci = ACIModule(module)
326 aci.construct_url(
327 root_class=dict(
328 aci_class="fvnsVlanInstP",
329 aci_rn="infra/vlanns-{0}".format(pool_name),
330 module_object=pool,
331 target_filter={"name": pool},
332 ),
333 subclass_1=dict(
334 aci_class="fvnsEncapBlk",
335 aci_rn=aci_block_mo,
336 module_object=aci_block_mo,
337 target_filter={"from": encap_start, "to": encap_end, "name": block_name},
338 ),
339 )
340
341 aci.get_existing()
342
343 if state == "present":
344 aci.payload(
345 aci_class="fvnsEncapBlk",
346 class_config={
347 "allocMode": allocation_mode,
348 "descr": description,
349 "from": encap_start,
350 "name": block_name,
351 "to": encap_end,
352 "nameAlias": name_alias,
353 },
354 )
355
356 aci.get_diff(aci_class="fvnsEncapBlk")
357
358 aci.post_config()
359
360 elif state == "absent":
361 aci.delete_config()
362
363 aci.exit_json()
364
365
366 if __name__ == "__main__":
367 main()