Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_encap_pool.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
15 short_description: Manage encap pools (fvns:VlanInstP, fvns:VxlanInstP, and fvns:VsanInstP)
16 description:
17 - Manage vlan, vxlan, and vsan pools on Cisco ACI fabrics.
18 options:
19 description:
20 description:
21 - Description for the C(pool).
22 type: str
23 aliases: [ descr ]
24 pool:
25 description:
26 - The name of the pool.
27 type: str
28 aliases: [ name, pool_name ]
29 pool_allocation_mode:
30 description:
31 - The method used for allocating encaps to resources.
32 - Only vlan and vsan support allocation modes.
33 type: str
34 choices: [ dynamic, static ]
35 aliases: [ allocation_mode, mode ]
36 pool_type:
37 description:
38 - The encap type of C(pool).
39 type: str
40 required: true
41 aliases: [ type ]
42 choices: [ vlan, vsan, vxlan ]
43 state:
44 description:
45 - Use C(present) or C(absent) for adding or removing.
46 - Use C(query) for listing an object or multiple objects.
47 type: str
48 choices: [ absent, present, query ]
49 default: present
50 name_alias:
51 description:
52 - The alias for the current object. This relates to the nameAlias field in ACI.
53 type: str
54 extends_documentation_fragment:
55 - cisco.aci.aci
56 - cisco.aci.annotation
57 - cisco.aci.owner
58
59 seealso:
60 - module: cisco.aci.aci_encap_pool_range
61 - module: cisco.aci.aci_vlan_pool
62 - name: APIC Management Information Model reference
63 description: More information about the internal APIC classes B(fvns:VlanInstP),
64 B(fvns:VxlanInstP) and B(fvns:VsanInstP)
65 link: https://developer.cisco.com/docs/apic-mim-ref/
66 author:
67 - Jacob McGill (@jmcgill298)
68 """
69
70 EXAMPLES = r"""
71 - name: Add a new vlan pool
72 cisco.aci.aci_encap_pool:
73 host: apic
74 username: admin
75 password: SomeSecretPassword
76 pool: production
77 pool_type: vlan
78 description: Production VLANs
79 state: present
80 delegate_to: localhost
81
82 - name: Remove a vlan pool
83 cisco.aci.aci_encap_pool:
84 host: apic
85 username: admin
86 password: SomeSecretPassword
87 pool: production
88 pool_type: vlan
89 state: absent
90 delegate_to: localhost
91
92 - name: Query a vlan pool
93 cisco.aci.aci_encap_pool:
94 host: apic
95 username: admin
96 password: SomeSecretPassword
97 pool: production
98 pool_type: vlan
99 state: query
100 delegate_to: localhost
101 register: query_result
102
103 - name: Query all vlan pools
104 cisco.aci.aci_encap_pool:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 pool_type: vlan
109 state: query
110 delegate_to: localhost
111 register: query_result
112 """
113
114 RETURN = r"""
115 current:
116 description: The existing configuration from the APIC after the module has finished
117 returned: success
118 type: list
119 sample:
120 [
121 {
122 "fvTenant": {
123 "attributes": {
124 "descr": "Production environment",
125 "dn": "uni/tn-production",
126 "name": "production",
127 "nameAlias": "",
128 "ownerKey": "",
129 "ownerTag": ""
130 }
131 }
132 }
133 ]
134 error:
135 description: The error information as returned from the APIC
136 returned: failure
137 type: dict
138 sample:
139 {
140 "code": "122",
141 "text": "unknown managed object class foo"
142 }
143 raw:
144 description: The raw output returned by the APIC REST API (xml or json)
145 returned: parse error
146 type: str
147 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
148 sent:
149 description: The actual/minimal configuration pushed to the APIC
150 returned: info
151 type: list
152 sample:
153 {
154 "fvTenant": {
155 "attributes": {
156 "descr": "Production environment"
157 }
158 }
159 }
160 previous:
161 description: The original configuration from the APIC before the module has started
162 returned: info
163 type: list
164 sample:
165 [
166 {
167 "fvTenant": {
168 "attributes": {
169 "descr": "Production",
170 "dn": "uni/tn-production",
171 "name": "production",
172 "nameAlias": "",
173 "ownerKey": "",
174 "ownerTag": ""
175 }
176 }
177 }
178 ]
179 proposed:
180 description: The assembled configuration from the user-provided parameters
181 returned: info
182 type: dict
183 sample:
184 {
185 "fvTenant": {
186 "attributes": {
187 "descr": "Production environment",
188 "name": "production"
189 }
190 }
191 }
192 filter_string:
193 description: The filter string used for the request
194 returned: failure or debug
195 type: str
196 sample: ?rsp-prop-include=config-only
197 method:
198 description: The HTTP method used for the request to the APIC
199 returned: failure or debug
200 type: str
201 sample: POST
202 response:
203 description: The HTTP response from the APIC
204 returned: failure or debug
205 type: str
206 sample: OK (30 bytes)
207 status:
208 description: The HTTP status from the APIC
209 returned: failure or debug
210 type: int
211 sample: 200
212 url:
213 description: The HTTP url used for the request to the APIC
214 returned: failure or debug
215 type: str
216 sample: https://10.11.12.13/api/mo/uni/tn-production.json
217 """
218
219 from ansible.module_utils.basic import AnsibleModule
220 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
221
222 ACI_POOL_MAPPING = dict(
223 vlan=dict(
224 aci_class="fvnsVlanInstP",
225 aci_mo="infra/vlanns-",
226 ),
227 vxlan=dict(
228 aci_class="fvnsVxlanInstP",
229 aci_mo="infra/vxlanns-",
230 ),
231 vsan=dict(
232 aci_class="fvnsVsanInstP",
233 aci_mo="infra/vsanns-",
234 ),
235 )
236
237
238 def main():
239 argument_spec = aci_argument_spec()
240 argument_spec.update(aci_annotation_spec())
241 argument_spec.update(aci_owner_spec())
242 argument_spec.update(
243 pool_type=dict(type="str", required=True, aliases=["type"], choices=["vlan", "vsan", "vxlan"]),
244 description=dict(type="str", aliases=["descr"]),
245 pool=dict(type="str", aliases=["name", "pool_name"]), # Not required for querying all objects
246 pool_allocation_mode=dict(type="str", aliases=["allocation_mode", "mode"], choices=["dynamic", "static"]),
247 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
248 name_alias=dict(type="str"),
249 )
250
251 module = AnsibleModule(
252 argument_spec=argument_spec,
253 supports_check_mode=True,
254 required_if=[
255 ["state", "absent", ["pool"]],
256 ["state", "present", ["pool"]],
257 ],
258 )
259
260 description = module.params.get("description")
261 pool = module.params.get("pool")
262 pool_type = module.params.get("pool_type")
263 pool_allocation_mode = module.params.get("pool_allocation_mode")
264 state = module.params.get("state")
265 name_alias = module.params.get("name_alias")
266
267 aci_class = ACI_POOL_MAPPING[pool_type]["aci_class"]
268 aci_mo = ACI_POOL_MAPPING[pool_type]["aci_mo"]
269 pool_name = pool
270
271 # ACI Pool URL requires the pool_allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
272 if pool_type != "vxlan" and pool is not None:
273 if pool_allocation_mode is not None:
274 pool_name = "[{0}]-{1}".format(pool, pool_allocation_mode)
275 else:
276 module.fail_json(msg="ACI requires parameter 'pool_allocation_mode' for 'pool_type' of 'vlan' and 'vsan' when parameter 'pool' is provided")
277
278 # Vxlan pools do not support pool allocation modes
279 if pool_type == "vxlan" and pool_allocation_mode is not None:
280 module.fail_json(msg="vxlan pools do not support setting the 'pool_allocation_mode'; please remove this parameter from the task")
281
282 aci = ACIModule(module)
283 aci.construct_url(
284 root_class=dict(
285 aci_class=aci_class,
286 aci_rn="{0}{1}".format(aci_mo, pool_name),
287 module_object=pool,
288 target_filter={"name": pool},
289 ),
290 )
291
292 aci.get_existing()
293
294 if state == "present":
295 # Filter out module parameters with null values
296 aci.payload(
297 aci_class=aci_class,
298 class_config=dict(
299 allocMode=pool_allocation_mode,
300 descr=description,
301 name=pool,
302 nameAlias=name_alias,
303 ),
304 )
305
306 # Generate config diff which will be used as POST request body
307 aci.get_diff(aci_class=aci_class)
308
309 # Submit changes if module not in check_mode and the proposed is different than existing
310 aci.post_config()
311
312 elif state == "absent":
313 aci.delete_config()
314
315 aci.exit_json()
316
317
318 if __name__ == "__main__":
319 main()