Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_schema_site_anp_epg_domain.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2019, Nirav Katarmal (@nkatarmal-crest) <[email protected]>
5 # Copyright: (c) 2023, Mabille Florent (@fmabille09) <[email protected]>
6 # Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]>
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
14
15 DOCUMENTATION = r"""
16 ---
17 module: mso_schema_site_anp_epg_domain
18 short_description: Manage site-local EPG domains in schema template
19 description:
20 - Manage site-local EPG domains in schema template on Cisco ACI Multi-Site.
21 author:
22 - Nirav Katarmal (@nkatarmal-crest)
23 - Mabille Florent (@fmabille09)
24 - Akini Ross (@akinross)
25 options:
26 schema:
27 description:
28 - The name of the schema.
29 type: str
30 required: true
31 site:
32 description:
33 - The name of the site.
34 type: str
35 required: true
36 template:
37 description:
38 - The name of the template.
39 type: str
40 required: true
41 anp:
42 description:
43 - The name of the ANP.
44 type: str
45 required: true
46 epg:
47 description:
48 - The name of the EPG.
49 type: str
50 required: true
51 domain_association_type:
52 description:
53 - The type of domain to associate.
54 type: str
55 choices: [ vmmDomain, l3ExtDomain, l2ExtDomain, physicalDomain, fibreChannelDomain ]
56 domain_profile:
57 description:
58 - The domain profile name.
59 type: str
60 deployment_immediacy:
61 description:
62 - The deployment immediacy of the domain.
63 - C(immediate) means B(Deploy immediate).
64 - C(lazy) means B(deploy on demand).
65 type: str
66 choices: [ immediate, lazy ]
67 resolution_immediacy:
68 description:
69 - Determines when the policies should be resolved and available.
70 - Defaults to C(lazy) when unset during creation.
71 type: str
72 choices: [ immediate, lazy, pre-provision ]
73 micro_seg_vlan_type:
74 description:
75 - Virtual LAN type for microsegmentation. This attribute can only be used with VMM domain association.
76 type: str
77 choices: [ vlan ]
78 micro_seg_vlan:
79 description:
80 - Virtual LAN for microsegmentation. This attribute can only be used with VMM domain association.
81 type: int
82 port_encap_vlan_type:
83 description:
84 - Virtual LAN type for port encap. This attribute can only be used with VMM domain association.
85 type: str
86 choices: [ vlan ]
87 port_encap_vlan:
88 description:
89 - Virtual LAN type for port encap. This attribute can only be used with VMM domain association.
90 type: int
91 vlan_encap_mode:
92 description:
93 - Which VLAN enacap mode to use. This attribute can only be used with VMM domain association.
94 type: str
95 choices: [ static, dynamic ]
96 allow_micro_segmentation:
97 description:
98 - Specifies microsegmentation is enabled or not. This attribute can only be used with VMM domain association.
99 type: bool
100 switch_type:
101 description:
102 - Which switch type to use with this domain association. This attribute can only be used with VMM domain association.
103 type: str
104 switching_mode:
105 description:
106 - Which switching mode to use with this domain association. This attribute can only be used with VMM domain association.
107 type: str
108 enhanced_lagpolicy_name:
109 description:
110 - EPG enhanced lagpolicy name. This attribute can only be used with VMM domain association.
111 type: str
112 enhanced_lagpolicy_dn:
113 description:
114 - Distinguished name of EPG lagpolicy. This attribute can only be used with VMM domain association.
115 type: str
116 delimiter:
117 description:
118 - Which delimiter to use with this domain association. This attribute can only be used with VMM domain association.
119 type: str
120 choices: [ '+', '|', '~', '!', '@', '^', '=' ]
121 binding_type:
122 description:
123 - Which binding_type to use with this domain association. This attribute can only be used with VMM domain association.
124 type: str
125 default: none
126 choices: [ static, dynamic, none, ephemeral ]
127 num_ports:
128 description:
129 - Number of ports for the binding type. This attribute can only be used with VMM domain association.
130 type: int
131 port_allocation:
132 description:
133 - Port allocation for the binding type. This attribute can only be used with VMM domain association and binding type in static.
134 - Required when O(binding_type=static).
135 type: str
136 choices: [ elastic, fixed ]
137 netflow_pref:
138 description:
139 - The netflow preference state of the domain association. This attribute can only be used with VMM domain association.
140 type: str
141 default: disabled
142 choices: [ enabled, disabled ]
143 allow_promiscuous:
144 description:
145 - The allow promiscuous state of the domain association. This attribute can only be used with VMM domain association.
146 type: str
147 default: reject
148 choices: [ accept, reject ]
149 forged_transmits:
150 description:
151 - The forged transmits state of the domain association. This attribute can only be used with VMM domain association.
152 type: str
153 default: reject
154 choices: [ accept, reject ]
155 mac_changes:
156 description:
157 - The mac changes state of the domain association. This attribute can only be used with VMM domain association.
158 type: str
159 default: reject
160 choices: [ accept, reject ]
161 custom_epg_name:
162 description:
163 - Which custom_epg_name to use with this domain association. This attribute can only be used with VMM domain association.
164 type: str
165 state:
166 description:
167 - Use C(present) or C(absent) for adding or removing.
168 - Use C(query) for listing an object or multiple objects.
169 type: str
170 choices: [ absent, present, query ]
171 default: present
172 notes:
173 - The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index.
174 This can cause silent corruption on concurrent access when changing/removing on object as
175 the wrong object may be referenced. This module is affected by this deficiency.
176 seealso:
177 - module: cisco.mso.mso_schema_site_anp_epg
178 - module: cisco.mso.mso_schema_template_anp_epg
179 extends_documentation_fragment: cisco.mso.modules
180 """
181
182 EXAMPLES = r"""
183 - name: Add a new domain to a site EPG
184 cisco.mso.mso_schema_site_anp_epg_domain:
185 host: mso_host
186 username: admin
187 password: SomeSecretPassword
188 schema: Schema1
189 site: Site1
190 template: Template1
191 anp: ANP1
192 epg: EPG1
193 domain_association_type: vmmDomain
194 domain_profile: 'VMware-VMM'
195 deployment_immediacy: lazy
196 resolution_immediacy: pre-provision
197 state: present
198
199 - name: Add a new domain to a site EPG with all possible attributes set
200 cisco.mso.mso_schema_site_anp_epg_domain:
201 host: mso_host
202 username: admin
203 password: SomeSecretPassword
204 schema: Schema1
205 site: Site1
206 template: Template1
207 anp: ANP1
208 epg: EPG1
209 domain_association_type: vmmDomain
210 domain_profile: 'VMware-VMM'
211 deployment_immediacy: immediate
212 resolution_immediacy: immediate
213 micro_seg_vlan_type: vlan
214 micro_seg_vlan: 100
215 port_encap_vlan_type: vlan
216 port_encap_vlan: 100
217 vlan_encap_mode: static
218 allow_micro_segmentation: true
219 switch_type: default
220 switching_mode: native
221 enhanced_lagpolicy_name: ansible_lag_name
222 enhanced_lagpolicy_dn: ansible_lag_dn
223 delimiter: '|'
224 binding_type: static
225 num_ports: 2
226 port_allocation: elastic
227 netflow_pref: enabled
228 allow_promiscuous: accept
229 forged_transmits: accept
230 mac_changes: accept
231 custom_epg_name: ansible_custom_epg
232 state: present
233
234 - name: Query a domain associated with a specific site EPG
235 cisco.mso.mso_schema_site_anp_epg_domain:
236 host: mso_host
237 username: admin
238 password: SomeSecretPassword
239 schema: Schema1
240 site: Site1
241 template: Template1
242 anp: ANP1
243 epg: EPG1
244 domain_association_type: vmmDomain
245 domain_profile: 'VMware-VMM'
246 state: query
247 register: query_result
248
249 - name: Query all domains associated with a site EPG
250 cisco.mso.mso_schema_site_anp_epg_domain:
251 host: mso_host
252 username: admin
253 password: SomeSecretPassword
254 schema: Schema1
255 site: Site1
256 template: Template1
257 anp: ANP1
258 epg: EPG1
259 state: query
260 register: query_result
261
262 - name: Remove a domain from a site EPG
263 cisco.mso.mso_schema_site_anp_epg_domain:
264 host: mso_host
265 username: admin
266 password: SomeSecretPassword
267 schema: Schema1
268 site: Site1
269 template: Template1
270 anp: ANP1
271 epg: EPG1
272 domain_association_type: vmmDomain
273 domain_profile: 'VMware-VMM'
274 deployment_immediacy: lazy
275 resolution_immediacy: pre-provision
276 state: absent
277 """
278
279 RETURN = r"""
280 """
281
282 from ansible.module_utils.basic import AnsibleModule
283 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
284
285
286 def main():
287 argument_spec = mso_argument_spec()
288 argument_spec.update(
289 schema=dict(type="str", required=True),
290 site=dict(type="str", required=True),
291 template=dict(type="str", required=True),
292 anp=dict(type="str", required=True),
293 epg=dict(type="str", required=True),
294 domain_association_type=dict(type="str", choices=["vmmDomain", "l3ExtDomain", "l2ExtDomain", "physicalDomain", "fibreChannelDomain"]),
295 domain_profile=dict(type="str"),
296 deployment_immediacy=dict(type="str", choices=["immediate", "lazy"]),
297 resolution_immediacy=dict(type="str", choices=["immediate", "lazy", "pre-provision"]),
298 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
299 micro_seg_vlan_type=dict(type="str", choices=["vlan"]),
300 micro_seg_vlan=dict(type="int"),
301 port_encap_vlan_type=dict(type="str", choices=["vlan"]),
302 port_encap_vlan=dict(type="int"),
303 vlan_encap_mode=dict(type="str", choices=["static", "dynamic"]),
304 allow_micro_segmentation=dict(type="bool"),
305 switch_type=dict(type="str"),
306 switching_mode=dict(type="str"),
307 enhanced_lagpolicy_name=dict(type="str"),
308 enhanced_lagpolicy_dn=dict(type="str"),
309 binding_type=dict(type="str", default="none", choices=["dynamic", "ephemeral", "none", "static"]),
310 port_allocation=dict(type="str", choices=["elastic", "fixed"]),
311 num_ports=dict(type="int"),
312 netflow_pref=dict(type="str", default="disabled", choices=["enabled", "disabled"]),
313 allow_promiscuous=dict(type="str", default="reject", choices=["accept", "reject"]),
314 forged_transmits=dict(type="str", default="reject", choices=["accept", "reject"]),
315 mac_changes=dict(type="str", default="reject", choices=["accept", "reject"]),
316 delimiter=dict(type="str", choices=["+", "|", "~", "!", "@", "^", "="]),
317 custom_epg_name=dict(type="str"),
318 )
319
320 module = AnsibleModule(
321 argument_spec=argument_spec,
322 supports_check_mode=True,
323 required_if=[
324 ["state", "absent", ["domain_association_type", "domain_profile", "deployment_immediacy", "resolution_immediacy"]],
325 ["state", "present", ["domain_association_type", "domain_profile", "deployment_immediacy", "resolution_immediacy"]],
326 ["binding_type", "static", ["port_allocation"]],
327 ],
328 required_together=[
329 ("micro_seg_vlan_type", "micro_seg_vlan"),
330 ("port_encap_vlan_type", "port_encap_vlan"),
331 ("enhanced_lagpolicy_name", "enhanced_lagpolicy_dn"),
332 ],
333 )
334
335 schema = module.params.get("schema")
336 site = module.params.get("site")
337 template = module.params.get("template").replace(" ", "")
338 anp = module.params.get("anp")
339 epg = module.params.get("epg")
340 domain_association_type = module.params.get("domain_association_type")
341 domain_profile = module.params.get("domain_profile")
342 deployment_immediacy = module.params.get("deployment_immediacy")
343 resolution_immediacy = module.params.get("resolution_immediacy")
344 state = module.params.get("state")
345 micro_seg_vlan_type = module.params.get("micro_seg_vlan_type")
346 micro_seg_vlan = module.params.get("micro_seg_vlan")
347 port_encap_vlan_type = module.params.get("port_encap_vlan_type")
348 port_encap_vlan = module.params.get("port_encap_vlan")
349 vlan_encap_mode = module.params.get("vlan_encap_mode")
350 allow_micro_segmentation = module.params.get("allow_micro_segmentation")
351 switch_type = module.params.get("switch_type")
352 switching_mode = module.params.get("switching_mode")
353 enhanced_lagpolicy_name = module.params.get("enhanced_lagpolicy_name")
354 enhanced_lagpolicy_dn = module.params.get("enhanced_lagpolicy_dn")
355 binding_type = module.params.get("binding_type")
356 port_allocation = module.params.get("port_allocation")
357 num_ports = module.params.get("num_ports")
358 netflow_pref = module.params.get("netflow_pref")
359 allow_promiscuous = module.params.get("allow_promiscuous")
360 forged_transmits = module.params.get("forged_transmits")
361 mac_changes = module.params.get("mac_changes")
362 delimiter = module.params.get("delimiter")
363 custom_epg_name = module.params.get("custom_epg_name")
364
365 mso = MSOModule(module)
366
367 # Get schema objects
368 schema_id, schema_path, schema_obj = mso.query_schema(schema)
369
370 # Get template
371 templates = [t.get("name") for t in schema_obj.get("templates")]
372 if template not in templates:
373 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
374 template_idx = templates.index(template)
375
376 # Get site
377 site_id = mso.lookup_site(site)
378
379 # Get site_idx
380 if not schema_obj.get("sites"):
381 mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template))
382 sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")]
383 sites_list = [s.get("siteId") + "/" + s.get("templateName") for s in schema_obj.get("sites")]
384 if (site_id, template) not in sites:
385 mso.fail_json(
386 msg="Provided site/siteId/template '{0}/{1}/{2}' does not exist. "
387 "Existing siteIds/templates: {3}".format(site, site_id, template, ", ".join(sites_list))
388 )
389
390 # Schema-access uses indexes
391 site_idx = sites.index((site_id, template))
392 # Path-based access uses site_id-template
393 site_template = "{0}-{1}".format(site_id, template)
394
395 payload = dict()
396 ops = []
397 op_path = ""
398 domain_path = None
399
400 # Get ANP
401 anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
402 anps = [a.get("anpRef") for a in schema_obj["sites"][site_idx]["anps"]]
403 anps_in_temp = [a.get("name") for a in schema_obj["templates"][template_idx]["anps"]]
404 if anp not in anps_in_temp:
405 mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ", ".join(anps)))
406 else:
407 # Update anp index at template level
408 template_anp_idx = anps_in_temp.index(anp)
409
410 # If anp not at site level but exists at template level
411 if anp_ref not in anps:
412 op_path = "/sites/{0}/anps/-".format(site_template)
413 payload.update(
414 anpRef=dict(
415 schemaId=schema_id,
416 templateName=template,
417 anpName=anp,
418 ),
419 )
420
421 else:
422 # Update anp index at site level
423 anp_idx = anps.index(anp_ref)
424
425 # Get EPG
426 epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg)
427
428 # If anp exists at site level
429 if "anpRef" not in payload:
430 epgs = [e.get("epgRef") for e in schema_obj["sites"][site_idx]["anps"][anp_idx]["epgs"]]
431
432 # If anp already at site level AND if epg not at site level (or) anp not at site level?
433 if ("anpRef" not in payload and epg_ref not in epgs) or "anpRef" in payload:
434 epgs_in_temp = [e.get("name") for e in schema_obj["templates"][template_idx]["anps"][template_anp_idx]["epgs"]]
435
436 # If EPG not at template level - Fail
437 if epg not in epgs_in_temp:
438 mso.fail_json(msg="Provided EPG '{0}' does not exist. Existing EPGs: {1} epgref {2}".format(epg, ", ".join(epgs_in_temp), epg_ref))
439
440 # EPG at template level but not at site level. Create payload at site level for EPG
441 else:
442 new_epg = dict(
443 epgRef=dict(
444 schemaId=schema_id,
445 templateName=template,
446 anpName=anp,
447 epgName=epg,
448 )
449 )
450
451 # If anp not in payload then, anp already exists at site level. New payload will only have new EPG payload
452 if "anpRef" not in payload:
453 op_path = "/sites/{0}/anps/{1}/epgs/-".format(site_template, anp)
454 payload = new_epg
455 else:
456 # If anp in payload, anp exists at site level. Update payload with EPG payload
457 payload["epgs"] = [new_epg]
458
459 # Update index of EPG at site level
460 else:
461 epg_idx = epgs.index(epg_ref)
462
463 if domain_association_type == "vmmDomain":
464 domain_dn = "uni/vmmp-VMware/dom-{0}".format(domain_profile)
465 elif domain_association_type == "l3ExtDomain":
466 domain_dn = "uni/l3dom-{0}".format(domain_profile)
467 elif domain_association_type == "l2ExtDomain":
468 domain_dn = "uni/l2dom-{0}".format(domain_profile)
469 elif domain_association_type == "physicalDomain":
470 domain_dn = "uni/phys-{0}".format(domain_profile)
471 elif domain_association_type == "fibreChannelDomain":
472 domain_dn = "uni/fc-{0}".format(domain_profile)
473 else:
474 domain_dn = ""
475
476 # Get Domains
477 # If anp at site level and epg is at site level
478 if "anpRef" not in payload and "epgRef" not in payload:
479 domains = [dom.get("dn") for dom in schema_obj["sites"][site_idx]["anps"][anp_idx]["epgs"][epg_idx]["domainAssociations"]]
480 if domain_dn in domains:
481 domain_idx = domains.index(domain_dn)
482 domain_path = "/sites/{0}/anps/{1}/epgs/{2}/domainAssociations/{3}".format(site_template, anp, epg, domain_idx)
483 mso.existing = schema_obj["sites"][site_idx]["anps"][anp_idx]["epgs"][epg_idx]["domainAssociations"][domain_idx]
484
485 if state == "query":
486 if domain_association_type is None or domain_profile is None:
487 mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx]["domainAssociations"]
488 elif not mso.existing:
489 mso.fail_json(
490 msg="Domain association '{domain_association_type}/{domain_profile}' not found".format(
491 domain_association_type=domain_association_type, domain_profile=domain_profile
492 )
493 )
494 mso.exit_json()
495
496 domains_path = "/sites/{0}/anps/{1}/epgs/{2}/domainAssociations".format(site_template, anp, epg)
497 ops = []
498 new_domain = dict(
499 dn=domain_dn,
500 domainType=domain_association_type,
501 deploymentImmediacy=deployment_immediacy, # keeping for backworths compatibility
502 deployImmediacy=deployment_immediacy, # rename of deploymentImmediacy
503 resolutionImmediacy=resolution_immediacy,
504 )
505
506 if domain_association_type == "vmmDomain":
507 vmmDomainProperties = {}
508 if micro_seg_vlan_type and micro_seg_vlan:
509 vmmDomainProperties["microSegVlan"] = dict(vlanType=micro_seg_vlan_type, vlan=micro_seg_vlan)
510
511 if port_encap_vlan_type and port_encap_vlan:
512 vmmDomainProperties["portEncapVlan"] = dict(vlanType=port_encap_vlan_type, vlan=port_encap_vlan)
513
514 if vlan_encap_mode:
515 vmmDomainProperties["vlanEncapMode"] = vlan_encap_mode
516
517 if allow_micro_segmentation is not None:
518 vmmDomainProperties["allowMicroSegmentation"] = allow_micro_segmentation
519
520 if switch_type:
521 vmmDomainProperties["switchType"] = switch_type
522
523 if switching_mode:
524 vmmDomainProperties["switchingMode"] = switching_mode
525
526 if enhanced_lagpolicy_name and enhanced_lagpolicy_dn:
527 vmmDomainProperties["epgLagPol"] = dict(enhancedLagPol=dict(name=enhanced_lagpolicy_name, dn=enhanced_lagpolicy_dn))
528
529 if delimiter:
530 vmmDomainProperties["delimiter"] = delimiter
531
532 if binding_type:
533 vmmDomainProperties["bindingType"] = binding_type
534 vmmDomainProperties["numPorts"] = num_ports
535
536 if port_allocation:
537 vmmDomainProperties["portAllocation"] = port_allocation
538
539 if netflow_pref:
540 vmmDomainProperties["netflowPref"] = netflow_pref
541
542 if allow_promiscuous:
543 vmmDomainProperties["allowPromiscuous"] = allow_promiscuous
544
545 if forged_transmits:
546 vmmDomainProperties["forgedTransmits"] = forged_transmits
547
548 if mac_changes:
549 vmmDomainProperties["macChanges"] = mac_changes
550
551 if custom_epg_name:
552 vmmDomainProperties["customEpgName"] = custom_epg_name
553
554 if vmmDomainProperties:
555 new_domain["vmmDomainProperties"] = vmmDomainProperties
556 new_domain.update(vmmDomainProperties)
557
558 # If payload is empty, anp and EPG already exist at site level
559 if not payload:
560 op_path = domains_path + "/-"
561 payload = new_domain
562
563 # If payload exists
564 else:
565 # If anp already exists at site level...(AND payload != epg as well?)
566 if "anpRef" not in payload:
567 payload["domainAssociations"] = [new_domain]
568 else:
569 payload["epgs"][0]["domainAssociations"] = [new_domain]
570
571 mso.previous = mso.existing
572 if state == "absent":
573 if mso.existing and domain_path:
574 mso.sent = mso.existing = {}
575 ops.append(dict(op="remove", path=domain_path))
576 elif state == "present":
577 mso.sanitize(payload, collate=True)
578
579 if mso.existing and domain_path:
580 ops.append(dict(op="replace", path=domain_path, value=mso.sent))
581 else:
582 ops.append(dict(op="add", path=op_path, value=mso.sent))
583
584 mso.existing = new_domain
585
586 if not module.check_mode:
587 mso.request(schema_path, method="PATCH", data=ops)
588
589 mso.exit_json()
590
591
592 if __name__ == "__main__":
593 main()