Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Dag Wieers <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_domain_to_encap_pool
16 short_description: Bind Domain to Encap Pools (infra:RsVlanNs)
17 description:
18 - Bind Domain to Encap Pools on Cisco ACI fabrics.
19 notes:
20 - The C(domain) and C(encap_pool) parameters should exist before using this module.
21 The M(cisco.aci.aci_domain) and M(cisco.aci.aci_encap_pool) can be used for these.
22 options:
23 domain:
24 description:
25 - Name of the domain being associated with the Encap Pool.
26 type: str
27 aliases: [ domain_name, domain_profile ]
28 domain_type:
29 description:
30 - Determines if the Domain is physical (phys) or virtual (vmm).
31 type: str
32 required: true
33 choices: [ fc, l2dom, l3dom, phys, vmm ]
34 pool:
35 description:
36 - The name of the pool.
37 type: str
38 aliases: [ pool_name ]
39 pool_allocation_mode:
40 description:
41 - The method used for allocating encaps to resources.
42 - Only vlan and vsan support allocation modes.
43 type: str
44 choices: [ dynamic, static]
45 aliases: [ allocation_mode, mode ]
46 pool_type:
47 description:
48 - The encap type of C(pool).
49 type: str
50 required: true
51 choices: [ vlan, vsan, vxlan ]
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 vm_provider:
60 description:
61 - The VM platform for VMM Domains.
62 - Support for Kubernetes was added in ACI v3.0.
63 - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
64 type: str
65 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware, nutanix ]
66 extends_documentation_fragment:
67 - cisco.aci.aci
68 - cisco.aci.annotation
69
70 seealso:
71 - module: cisco.aci.aci_domain
72 - module: cisco.aci.aci_encap_pool
73 - name: APIC Management Information Model reference
74 description: More information about the internal APIC class B(infra:RsVlanNs).
75 link: https://developer.cisco.com/docs/apic-mim-ref/
76 author:
77 - Dag Wieers (@dagwieers)
78 """
79
80 EXAMPLES = r"""
81 - name: Add domain to VLAN pool binding
82 cisco.aci.aci_domain_to_encap_pool:
83 host: apic
84 username: admin
85 password: SomeSecretPassword
86 domain: phys_dom
87 domain_type: phys
88 pool: test_pool
89 pool_type: vlan
90 pool_allocation_mode: dynamic
91 state: present
92 delegate_to: localhost
93
94 - name: Remove domain to VLAN pool binding
95 cisco.aci.aci_domain_to_encap_pool:
96 host: apic
97 username: admin
98 password: SomeSecretPassword
99 domain: phys_dom
100 domain_type: phys
101 pool: test_pool
102 pool_type: vlan
103 pool_allocation_mode: dynamic
104 state: absent
105 delegate_to: localhost
106
107 - name: Query our domain to VLAN pool binding
108 cisco.aci.aci_domain_to_encap_pool:
109 host: apic
110 username: admin
111 password: SomeSecretPassword
112 domain: phys_dom
113 pool: test_pool
114 pool_type: vlan
115 pool_allocation_mode: dynamic
116 state: query
117 delegate_to: localhost
118 register: query_result
119
120 - name: Query all domain to VLAN pool bindings
121 cisco.aci.aci_domain_to_encap_pool:
122 host: apic
123 username: admin
124 password: SomeSecretPassword
125 domain_type: phys
126 pool_type: vlan
127 pool_allocation_mode: dynamic
128 state: query
129 delegate_to: localhost
130 register: query_result
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 from ansible.module_utils.basic import AnsibleModule
239 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
240 from ansible_collections.cisco.aci.plugins.module_utils.constants import VM_PROVIDER_MAPPING
241
242
243 POOL_MAPPING = dict(
244 vlan=dict(
245 aci_mo="uni/infra/vlanns-{0}",
246 child_class="infraRsVlanNs",
247 ),
248 vxlan=dict(
249 aci_mo="uni/infra/vxlanns-{0}",
250 child_class="vmmRsVxlanNs",
251 ),
252 vsan=dict(
253 aci_mo="uni/infra/vsanns-{0}",
254 child_class="fcRsVsanNs",
255 ),
256 )
257
258
259 def main():
260 argument_spec = aci_argument_spec()
261 argument_spec.update(aci_annotation_spec())
262 argument_spec.update(
263 domain_type=dict(type="str", required=True, choices=["fc", "l2dom", "l3dom", "phys", "vmm"]),
264 pool_type=dict(type="str", required=True, choices=["vlan", "vsan", "vxlan"]),
265 domain=dict(type="str", aliases=["domain_name", "domain_profile"]), # Not required for querying all objects
266 pool=dict(type="str", aliases=["pool_name"]), # Not required for querying all objects
267 pool_allocation_mode=dict(type="str", aliases=["allocation_mode", "mode"], choices=["dynamic", "static"]),
268 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
269 vm_provider=dict(type="str", choices=list(VM_PROVIDER_MAPPING)),
270 )
271
272 module = AnsibleModule(
273 argument_spec=argument_spec,
274 supports_check_mode=True,
275 required_if=[
276 ["domain_type", "vmm", ["vm_provider"]],
277 ["state", "absent", ["domain", "domain_type", "pool", "pool_type"]],
278 ["state", "present", ["domain", "domain_type", "pool", "pool_type"]],
279 ],
280 )
281
282 domain = module.params.get("domain")
283 domain_type = module.params.get("domain_type")
284 pool = module.params.get("pool")
285 pool_allocation_mode = module.params.get("pool_allocation_mode")
286 pool_type = module.params.get("pool_type")
287 vm_provider = module.params.get("vm_provider")
288 state = module.params.get("state")
289
290 # Report when vm_provider is set when type is not virtual
291 if domain_type != "vmm" and vm_provider is not None:
292 module.fail_json(msg="Domain type '{0}' cannot have a 'vm_provider'".format(domain_type))
293
294 # ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
295 pool_name = pool
296 if pool_type != "vxlan" and pool is not None:
297 if pool_allocation_mode is not None:
298 pool_name = "[{0}]-{1}".format(pool, pool_allocation_mode)
299 else:
300 module.fail_json(msg="ACI requires the 'pool_allocation_mode' for 'pool_type' of 'vlan' and 'vsan' when 'pool' is provided")
301
302 # Vxlan pools do not support allocation modes
303 if pool_type == "vxlan" and pool_allocation_mode is not None:
304 module.fail_json(msg="vxlan pools do not support setting the allocation_mode; please remove this parameter from the task")
305
306 # Compile the full domain for URL building
307 if domain_type == "fc":
308 domain_class = "fcDomP"
309 domain_mo = "uni/fc-{0}".format(domain)
310 domain_rn = "fc-{0}".format(domain)
311 elif domain_type == "l2dom":
312 domain_class = "l2extDomP"
313 domain_mo = "uni/l2dom-{0}".format(domain)
314 domain_rn = "l2dom-{0}".format(domain)
315 elif domain_type == "l3dom":
316 domain_class = "l3extDomP"
317 domain_mo = "uni/l3dom-{0}".format(domain)
318 domain_rn = "l3dom-{0}".format(domain)
319 elif domain_type == "phys":
320 domain_class = "physDomP"
321 domain_mo = "uni/phys-{0}".format(domain)
322 domain_rn = "phys-{0}".format(domain)
323 elif domain_type == "vmm":
324 domain_class = "vmmDomP"
325 domain_mo = "uni/vmmp-{0}/dom-{1}".format(VM_PROVIDER_MAPPING[vm_provider], domain)
326 domain_rn = "vmmp-{0}/dom-{1}".format(VM_PROVIDER_MAPPING[vm_provider], domain)
327
328 # Ensure that querying all objects works when only domain_type is provided
329 if domain is None:
330 domain_mo = None
331
332 pool_mo = POOL_MAPPING[pool_type]["aci_mo"].format(pool_name)
333 child_class = POOL_MAPPING[pool_type]["child_class"]
334
335 aci = ACIModule(module)
336 aci.construct_url(
337 root_class=dict(
338 aci_class=domain_class,
339 aci_rn=domain_rn,
340 module_object=domain_mo,
341 target_filter={"name": domain},
342 ),
343 child_classes=[child_class],
344 )
345
346 aci.get_existing()
347
348 if state == "present":
349 # Filter out module params with null values
350 aci.payload(
351 aci_class=domain_class,
352 class_config=dict(name=domain),
353 child_configs=[
354 {child_class: {"attributes": {"tDn": pool_mo}}},
355 ],
356 )
357
358 # Generate config diff which will be used as POST request body
359 aci.get_diff(aci_class=domain_class)
360
361 # Submit changes if module not in check_mode and the proposed is different than existing
362 aci.post_config()
363
364 elif state == "absent":
365 aci.delete_config()
366
367 aci.exit_json()
368
369
370 if __name__ == "__main__":
371 main()