Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Manuel Widmer <[email protected]>
5 # Copyright: (c) 2021, Anvitha Jain (@anvitha-jain) <[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": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_vmm_vswitch_policy
17 short_description: Manage vSwitch policy for VMware virtual domains profiles (vmm:VSwitchPolicyCont)
18 description:
19 - Manage vSwitch policy for VMware VMM domains on Cisco ACI fabrics.
20 options:
21 port_channel_policy:
22 description:
23 - Name of the fabric access port-channel policy.
24 type: str
25 lldp_policy:
26 description:
27 - Name of the fabric access LLDP policy.
28 type: str
29 cdp_policy:
30 description:
31 - Name of the fabric access CDP policy.
32 type: str
33 mtu_policy:
34 description:
35 - VMWare only.
36 - Name of the fabric access MTU policy.
37 type: str
38 domain:
39 description:
40 - Name of the virtual domain profile.
41 type: str
42 aliases: [ domain_name, domain_profile ]
43 enhanced_lag:
44 description:
45 - List of enhanced LAG policies if vSwitch needs to be connected via VPC.
46 type: list
47 elements: dict
48 suboptions:
49 name:
50 description:
51 - Name of the enhanced Lag policy.
52 type: str
53 required: true
54 lacp_mode:
55 description:
56 - LACP port channel mode.
57 type: str
58 choices: [ active, passive ]
59 load_balancing_mode:
60 description:
61 - Load balancing mode of the port channel.
62 - See also https://pubhub.devnetcloud.com/media/apic-mim-ref-421/docs/MO-lacpEnhancedLagPol.html.
63 type: str
64 choices:
65 - dst-ip
66 - dst-ip-l4port
67 - dst-ip-vlan
68 - dst-ip-l4port-vlan
69 - dst-mac
70 - dst-l4port
71 - src-ip
72 - src-ip-l4port
73 - src-ip-vlan
74 - src-ip-l4port-vlan
75 - src-mac
76 - src-l4port
77 - src-dst-ip
78 - src-dst-ip-l4port
79 - src-dst-ip-vlan
80 - src-dst-ip-l4port-vlan
81 - src-dst-mac
82 - src-dst-l4port
83 - src-port-id
84 - vlan
85 number_uplinks:
86 description:
87 - Number of uplinks, must be between 2 and 8.
88 type: int
89 stp_policy:
90 description:
91 - SCVMM only.
92 - Name of the STP policy.
93 type: str
94 netflow_exporter:
95 description:
96 - Parameters for the netflow exporter policy
97 type: dict
98 suboptions:
99 name:
100 description:
101 - Name of the netflow exporter policy
102 type: str
103 required: true
104 active_flow_timeout:
105 description:
106 - Specifies the delay in seconds that NetFlow waits after the active flow is initiated, after which NetFlow sends the collected data.
107 - The range is from 60 to 3600. The default value is 60
108 type: int
109 idle_flow_timeout:
110 description:
111 - Specifies the delay in seconds that NetFlow waits after the idle flow is initiated, after which NetFlow sends the collected data.
112 - The range is from 10 to 600. The default value is 15.
113 type: int
114 sampling_rate:
115 description:
116 - (VDS only) Specifies how many packets that NetFlow will drop after every collected packet.
117 If you specify a value of 0, then NetFlow does not drop any packets.
118 - The range is from 0 to 1000. The default value is 0.
119 type: int
120 state:
121 description:
122 - Use C(present) or C(absent) for adding or removing.
123 - Use C(query) for listing an object or multiple objects.
124 type: str
125 choices: [ absent, present, query ]
126 default: present
127 vm_provider:
128 description:
129 - The VM platform for VMM Domains.
130 - Support for Kubernetes was added in ACI v3.0.
131 - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
132 type: str
133 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware ]
134 extends_documentation_fragment:
135 - cisco.aci.aci
136 - cisco.aci.annotation
137 - cisco.aci.owner
138
139 seealso:
140 - module: cisco.aci.aci_domain
141 - name: APIC Management Information Model reference
142 description: More information about the internal APIC classes B(vmm:VSwitchPolicyCont).
143 link: https://developer.cisco.com/docs/apic-mim-ref/
144 author:
145 - Manuel Widmer (@lumean)
146 - Anvitha Jain (@anvitha-jain)
147 """
148
149 EXAMPLES = r"""
150 - name: Add a vSwitch policy with LLDP
151 cisco.aci.aci_vmm_vswitch_policy:
152 host: apic
153 username: admin
154 password: SomeSecretPassword
155 lldp_policy: LLDP_ENABLED
156 domain: vmware_dom
157 vm_provider: vmware
158 state: present
159
160 - name: Add a vSwitch policy with link aggregation
161 cisco.aci.aci_vmm_vswitch_policy:
162 host: apic
163 username: admin
164 password: SomeSecretPassword
165 port_channel_policy: LACP_ACTIVE
166 lldp_policy: LLDP_ENABLED
167 domain: vmware_dom
168 vm_provider: vmware
169 enhanced_lag:
170 - name: my_lacp_uplink
171 lacp_mode: active
172 load_balancing_mode: src-dst-ip
173 number_uplinks: 2
174 state: present
175
176 - name: Remove vSwitch Policy from VMware VMM domain
177 cisco.aci.aci_vmm_vswitch_policy:
178 host: apic
179 username: admin
180 password: SomeSecretPassword
181 domain: vmware_dom
182 vm_provider: vmware
183 state: absent
184
185 - name: Query the vSwitch policy of the VMWare domain
186 cisco.aci.aci_vmm_vswitch_policy:
187 host: apic
188 username: admin
189 password: SomeSecretPassword
190 domain: vmware_dom
191 vm_provider: vmware
192 state: query
193 delegate_to: localhost
194 register: query_result
195 """
196
197 RETURN = r"""
198 current:
199 description: The existing configuration from the APIC after the module has finished
200 returned: success
201 type: list
202 sample:
203 [
204 {
205 "fvTenant": {
206 "attributes": {
207 "descr": "Production environment",
208 "dn": "uni/tn-production",
209 "name": "production",
210 "nameAlias": "",
211 "ownerKey": "",
212 "ownerTag": ""
213 }
214 }
215 }
216 ]
217 error:
218 description: The error information as returned from the APIC
219 returned: failure
220 type: dict
221 sample:
222 {
223 "code": "122",
224 "text": "unknown managed object class foo"
225 }
226 raw:
227 description: The raw output returned by the APIC REST API (xml or json)
228 returned: parse error
229 type: str
230 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
231 sent:
232 description: The actual/minimal configuration pushed to the APIC
233 returned: info
234 type: list
235 sample:
236 {
237 "fvTenant": {
238 "attributes": {
239 "descr": "Production environment"
240 }
241 }
242 }
243 previous:
244 description: The original configuration from the APIC before the module has started
245 returned: info
246 type: list
247 sample:
248 [
249 {
250 "fvTenant": {
251 "attributes": {
252 "descr": "Production",
253 "dn": "uni/tn-production",
254 "name": "production",
255 "nameAlias": "",
256 "ownerKey": "",
257 "ownerTag": ""
258 }
259 }
260 }
261 ]
262 proposed:
263 description: The assembled configuration from the user-provided parameters
264 returned: info
265 type: dict
266 sample:
267 {
268 "fvTenant": {
269 "attributes": {
270 "descr": "Production environment",
271 "name": "production"
272 }
273 }
274 }
275 filter_string:
276 description: The filter string used for the request
277 returned: failure or debug
278 type: str
279 sample: ?rsp-prop-include=config-only
280 method:
281 description: The HTTP method used for the request to the APIC
282 returned: failure or debug
283 type: str
284 sample: POST
285 response:
286 description: The HTTP response from the APIC
287 returned: failure or debug
288 type: str
289 sample: OK (30 bytes)
290 status:
291 description: The HTTP status from the APIC
292 returned: failure or debug
293 type: int
294 sample: 200
295 url:
296 description: The HTTP url used for the request to the APIC
297 returned: failure or debug
298 type: str
299 sample: https://10.11.12.13/api/mo/uni/tn-production.json
300 """
301
302 from ansible.module_utils.basic import AnsibleModule
303 from ansible_collections.cisco.aci.plugins.module_utils.aci import (
304 ACIModule,
305 aci_argument_spec,
306 enhanced_lag_spec,
307 netflow_spec,
308 aci_annotation_spec,
309 aci_owner_spec,
310 )
311 from ansible_collections.cisco.aci.plugins.module_utils.constants import (
312 VM_PROVIDER_MAPPING,
313 )
314
315 # via UI vSwitch Policy can only be added for VMware and Microsoft vmm domains
316 # behavior for other domains is currently untested.
317
318 # enhanced_lag_spec = dict(
319 # name=dict(type='str', required=True),
320 # lacp_mode=dict(type='str', choices=['active', 'passive']),
321 # load_balancing_mode=dict(
322 # type='str',
323 # choices=['dst-ip', 'dst-ip-l4port', 'dst-ip-vlan', 'dst-ip-l4port-vlan', 'dst-mac', 'dst-l4port',
324 # 'src-ip', 'src-ip-l4port', 'src-ip-vlan', 'src-ip-l4port-vlan', 'src-mac', 'src-l4port',
325 # 'src-dst-ip', 'src-dst-ip-l4port', 'src-dst-ip-vlan', 'src-dst-ip-l4port-vlan', 'src-dst-mac',
326 # 'src-dst-l4port', 'src-port-id', 'vlan']),
327 # number_uplinks=dict(type='int'),
328 # )
329 # netflow_spec = dict(
330 # name=dict(type='str', required=True),
331 # active_flow_timeout=dict(type='int'),
332 # idle_flow_timeout=dict(type='int'),
333 # sampling_rate=dict(type='int'),
334 # )
335
336
337 def main():
338
339 # Remove nutanix from VM_PROVIDER_MAPPING as it is not supported
340 CLEAN_VM_PROVIDER_MAPPING = VM_PROVIDER_MAPPING.copy()
341 CLEAN_VM_PROVIDER_MAPPING.pop("nutanix")
342
343 argument_spec = aci_argument_spec()
344 argument_spec.update(aci_annotation_spec())
345 argument_spec.update(aci_owner_spec())
346 argument_spec.update(
347 port_channel_policy=dict(type="str"),
348 lldp_policy=dict(type="str"),
349 cdp_policy=dict(type="str"),
350 mtu_policy=dict(type="str"),
351 stp_policy=dict(type="str"),
352 enhanced_lag=dict(type="list", elements="dict", options=enhanced_lag_spec()),
353 netflow_exporter=dict(type="dict", options=netflow_spec()),
354 domain=dict(type="str", aliases=["domain_name", "domain_profile"]),
355 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
356 vm_provider=dict(type="str", choices=list(CLEAN_VM_PROVIDER_MAPPING)),
357 )
358
359 module = AnsibleModule(
360 argument_spec=argument_spec,
361 supports_check_mode=True,
362 required_if=[
363 ["state", "absent", ["domain", "vm_provider"]],
364 ["state", "present", ["domain", "vm_provider"]],
365 ],
366 )
367
368 port_channel_policy = module.params.get("port_channel_policy")
369 lldp_policy = module.params.get("lldp_policy")
370 cdp_policy = module.params.get("cdp_policy")
371 mtu_policy = module.params.get("mtu_policy")
372 stp_policy = module.params.get("stp_policy")
373 netflow_exporter = module.params.get("netflow_exporter")
374 enhanced_lag = module.params.get("enhanced_lag")
375 domain = module.params.get("domain")
376 state = module.params.get("state")
377 vm_provider = module.params.get("vm_provider")
378
379 aci = ACIModule(module)
380 vswitch_class = "vmmVSwitchPolicyCont"
381
382 child_classes = ["vmmRsVswitchOverrideLldpIfPol", "vmmRsVswitchOverrideLacpPol", "vmmRsVswitchOverrideCdpIfPol", "lacpEnhancedLagPol"]
383 if mtu_policy is not None:
384 child_classes.append("vmmRsVswitchOverrideMtuPol")
385
386 if stp_policy is not None:
387 child_classes.append("vmmRsVswitchOverrideStpPol")
388
389 if isinstance(netflow_exporter, dict):
390 child_classes.append("vmmRsVswitchExporterPol")
391
392 aci.construct_url(
393 root_class=dict(
394 aci_class="vmmProvP",
395 aci_rn="vmmp-{0}".format(CLEAN_VM_PROVIDER_MAPPING.get(vm_provider)),
396 module_object=vm_provider,
397 target_filter={"name": vm_provider},
398 ),
399 subclass_1=dict(
400 aci_class="vmmDomP",
401 aci_rn="dom-{0}".format(domain),
402 module_object=domain,
403 target_filter={"name": domain},
404 ),
405 subclass_2=dict(
406 aci_class="vmmVSwitchPolicyCont",
407 aci_rn="vswitchpolcont",
408 module_object="vswitchpolcont",
409 target_filter={"name": "vswitchpolcont"},
410 ),
411 child_classes=child_classes,
412 )
413
414 aci.get_existing()
415
416 if state == "present":
417 children = list()
418
419 if port_channel_policy is not None:
420 children.append(dict(vmmRsVswitchOverrideLacpPol=dict(attributes=dict(tDn="uni/infra/lacplagp-{0}".format(port_channel_policy)))))
421
422 if lldp_policy is not None:
423 children.append(dict(vmmRsVswitchOverrideLldpIfPol=dict(attributes=dict(tDn="uni/infra/lldpIfP-{0}".format(lldp_policy)))))
424
425 if cdp_policy is not None:
426 children.append(dict(vmmRsVswitchOverrideCdpIfPol=dict(attributes=dict(tDn="uni/infra/cdpIfP-{0}".format(cdp_policy)))))
427
428 if mtu_policy is not None:
429 children.append(dict(vmmRsVswitchOverrideMtuPol=dict(attributes=dict(tDn="uni/fabric/l2pol-{0}".format(mtu_policy)))))
430
431 if stp_policy is not None:
432 children.append(dict(vmmRsVswitchOverrideStpPol=dict(attributes=dict(tDn="uni/infra/ifPol-{0}".format(stp_policy)))))
433
434 if isinstance(netflow_exporter, dict):
435 children.append(
436 dict(
437 vmmRsVswitchExporterPol=dict(
438 attributes=dict(
439 tDn="uni/infra/vmmexporterpol-{0}".format(netflow_exporter["name"]),
440 activeFlowTimeOut=netflow_exporter["active_flow_timeout"],
441 idleFlowTimeOut=netflow_exporter["idle_flow_timeout"],
442 samplingRate=netflow_exporter["sampling_rate"],
443 )
444 )
445 )
446 )
447
448 if isinstance(enhanced_lag, list):
449 for lag_dict in enhanced_lag:
450 children.append(
451 dict(
452 lacpEnhancedLagPol=dict(
453 attributes=dict(
454 name=lag_dict["name"],
455 mode=lag_dict["lacp_mode"],
456 lbmode=lag_dict["load_balancing_mode"],
457 numLinks=lag_dict["number_uplinks"],
458 )
459 )
460 )
461 )
462
463 aci.payload(aci_class=vswitch_class, class_config=dict(rn="vswitchpolcont"), child_configs=children)
464
465 aci.get_diff(aci_class=vswitch_class)
466
467 aci.post_config()
468
469 elif state == "absent":
470 aci.delete_config()
471
472 aci.exit_json()
473
474
475 if __name__ == "__main__":
476 main()