Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[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: mso_schema_template_anp_epg
17 short_description: Manage Endpoint Groups (EPGs) in schema templates
18 description:
19 - Manage EPGs in schema templates on Cisco ACI Multi-Site.
20 author:
21 - Dag Wieers (@dagwieers)
22 - Anvitha Jain (@anvitha-jain)
23 options:
24 schema:
25 description:
26 - The name of the schema.
27 type: str
28 required: true
29 template:
30 description:
31 - The name of the template.
32 type: str
33 required: true
34 anp:
35 description:
36 - The name of the ANP.
37 type: str
38 required: true
39 epg:
40 description:
41 - The name of the EPG to manage.
42 type: str
43 aliases: [ name ]
44 display_name:
45 description:
46 - The name as displayed on the MSO web interface.
47 type: str
48 description:
49 description:
50 - The description as displayed on the MSO web interface.
51 - The description is supported on versions of MSO that are 3.3 or greater.
52 type: str
53 # contracts:
54 # description:
55 # - A list of contracts associated to this ANP.
56 # type: list
57 bd:
58 description:
59 - The BD associated to this ANP.
60 type: dict
61 suboptions:
62 name:
63 description:
64 - The name of the BD to associate with.
65 required: true
66 type: str
67 schema:
68 description:
69 - The schema that defines the referenced BD.
70 - If this parameter is unspecified, it defaults to the current schema.
71 type: str
72 template:
73 description:
74 - The template that defines the referenced BD.
75 type: str
76 vrf:
77 description:
78 - The VRF associated to this ANP.
79 type: dict
80 suboptions:
81 name:
82 description:
83 - The name of the VRF to associate with.
84 required: true
85 type: str
86 schema:
87 description:
88 - The schema that defines the referenced VRF.
89 - If this parameter is unspecified, it defaults to the current schema.
90 type: str
91 template:
92 description:
93 - The template that defines the referenced VRF.
94 type: str
95 subnets:
96 description:
97 - The subnets associated to this ANP.
98 type: list
99 elements: dict
100 suboptions:
101 subnet:
102 description:
103 - The IP range in CIDR notation.
104 type: str
105 required: true
106 aliases: [ ip ]
107 description:
108 description:
109 - The description of this subnet.
110 type: str
111 scope:
112 description:
113 - The scope of the subnet.
114 type: str
115 default: private
116 choices: [ private, public ]
117 shared:
118 description:
119 - Whether this subnet is shared between VRFs.
120 type: bool
121 default: false
122 no_default_gateway:
123 description:
124 - Whether this subnet has a default gateway.
125 type: bool
126 default: false
127 useg_epg:
128 description:
129 - Whether this is a USEG EPG.
130 type: bool
131 # useg_epg_attributes:
132 # description:
133 # - A dictionary consisting of USEG attributes.
134 # type: dict
135 intra_epg_isolation:
136 description:
137 - Whether intra EPG isolation is enforced.
138 - When not specified, this parameter defaults to C(unenforced).
139 type: str
140 choices: [ enforced, unenforced ]
141 intersite_multicast_source:
142 description:
143 - Whether intersite multicast source is enabled.
144 - When not specified, this parameter defaults to C(no).
145 type: bool
146 proxy_arp:
147 description:
148 - Whether proxy arp is enabled.
149 - When not specified, this parameter defaults to C(no).
150 type: bool
151 preferred_group:
152 description:
153 - Whether this EPG is added to preferred group or not.
154 - When not specified, this parameter defaults to C(no).
155 type: bool
156 qos_level:
157 description:
158 - Quality of Service (QoS) allows you to classify the network traffic in the fabric.
159 - It helps prioritize and police the traffic flow to help avoid congestion in the network.
160 - The Contract QoS Level parameter is supported on versions of MSO that are 3.1 or greater.
161 choices: [ unspecified, level1, level2, level3, level4, level5, level6 ]
162 type: str
163 epg_type:
164 description:
165 - The EPG type parameter is supported on versions of MSO that are 3.3 or greater.
166 type: str
167 choices: [ application, service ]
168 deployment_type:
169 description:
170 - The deployment_type parameter indicates how and where the service is deployed.
171 - This parameter is available only when epg_type is service.
172 type: str
173 choices: [ cloud_native, cloud_native_managed, third_party ]
174 access_type:
175 description:
176 - This parameter indicates how the service will be accessed.
177 - It is only available when epg_type is service.
178 type: str
179 choices: [ private, public, public_and_private ]
180 service_type:
181 description:
182 - The service_type parameter refers to the type of cloud services.
183 - Only certain deployment types, and certain access types within each deployment type, are supported for each service type.
184 - This parameter is available only when epg_type is service.
185 type: str
186 state:
187 description:
188 - Use C(present) or C(absent) for adding or removing.
189 - Use C(query) for listing an object or multiple objects.
190 type: str
191 choices: [ absent, present, query ]
192 default: present
193 seealso:
194 - module: cisco.mso.mso_schema_template_anp
195 - module: cisco.mso.mso_schema_template_anp_epg_subnet
196 - module: cisco.mso.mso_schema_template_bd
197 - module: cisco.mso.mso_schema_template_contract_filter
198 extends_documentation_fragment: cisco.mso.modules
199 """
200
201 EXAMPLES = r"""
202 - name: Add a new EPG
203 cisco.mso.mso_schema_template_anp_epg:
204 host: mso_host
205 username: admin
206 password: SomeSecretPassword
207 schema: Schema 1
208 template: Template 1
209 anp: ANP 1
210 epg: EPG 1
211 bd:
212 name: bd1
213 vrf:
214 name: vrf1
215 state: present
216
217 - name: Add a new EPG with preferred group.
218 cisco.mso.mso_schema_template_anp_epg:
219 host: mso_host
220 username: admin
221 password: SomeSecretPassword
222 schema: Schema 1
223 template: Template 1
224 anp: ANP 1
225 epg: EPG 1
226 state: present
227 preferred_group: true
228
229 - name: Remove an EPG
230 cisco.mso.mso_schema_template_anp_epg:
231 host: mso_host
232 username: admin
233 password: SomeSecretPassword
234 schema: Schema 1
235 template: Template 1
236 anp: ANP 1
237 epg: EPG 1
238 bd:
239 name: bd1
240 vrf:
241 name: vrf1
242 state: absent
243
244 - name: Query a specific EPG
245 cisco.mso.mso_schema_template_anp_epg:
246 host: mso_host
247 username: admin
248 password: SomeSecretPassword
249 schema: Schema 1
250 template: Template 1
251 anp: ANP 1
252 epg: EPG 1
253 bd:
254 name: bd1
255 vrf:
256 name: vrf1
257 state: query
258 register: query_result
259
260 - name: Query all EPGs
261 cisco.mso.mso_schema_template_anp_epg:
262 host: mso_host
263 username: admin
264 password: SomeSecretPassword
265 schema: Schema 1
266 template: Template 1
267 anp: ANP 1
268 epg: EPG 1
269 bd:
270 name: bd1
271 vrf:
272 name: vrf1
273 state: query
274 register: query_result
275 """
276
277 RETURN = r"""
278 """
279
280 from ansible.module_utils.basic import AnsibleModule
281 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_reference_spec, mso_epg_subnet_spec
282 from ansible_collections.cisco.mso.plugins.module_utils.constants import QOS_LEVEL
283
284
285 def main():
286 argument_spec = mso_argument_spec()
287 argument_spec.update(
288 schema=dict(type="str", required=True),
289 template=dict(type="str", required=True),
290 anp=dict(type="str", required=True),
291 epg=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects
292 description=dict(type="str"),
293 bd=dict(type="dict", options=mso_reference_spec()),
294 vrf=dict(type="dict", options=mso_reference_spec()),
295 display_name=dict(type="str"),
296 useg_epg=dict(type="bool"),
297 intra_epg_isolation=dict(type="str", choices=["enforced", "unenforced"]),
298 intersite_multicast_source=dict(type="bool"),
299 proxy_arp=dict(type="bool"),
300 subnets=dict(type="list", elements="dict", options=mso_epg_subnet_spec()),
301 qos_level=dict(type="str", choices=QOS_LEVEL),
302 epg_type=dict(type="str", choices=["application", "service"]),
303 deployment_type=dict(type="str", choices=["cloud_native", "cloud_native_managed", "third_party"]),
304 service_type=dict(type="str"),
305 access_type=dict(type="str", choices=["private", "public", "public_and_private"]),
306 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
307 preferred_group=dict(type="bool"),
308 )
309
310 module = AnsibleModule(
311 argument_spec=argument_spec,
312 supports_check_mode=True,
313 required_if=[
314 ["state", "absent", ["epg"]],
315 ["state", "present", ["epg"]],
316 ],
317 )
318
319 schema = module.params.get("schema")
320 template = module.params.get("template").replace(" ", "")
321 anp = module.params.get("anp")
322 epg = module.params.get("epg")
323 description = module.params.get("description")
324 display_name = module.params.get("display_name")
325 bd = module.params.get("bd")
326 if bd is not None and bd.get("template") is not None:
327 bd["template"] = bd.get("template").replace(" ", "")
328 vrf = module.params.get("vrf")
329 if vrf is not None and vrf.get("template") is not None:
330 vrf["template"] = vrf.get("template").replace(" ", "")
331 useg_epg = module.params.get("useg_epg")
332 intra_epg_isolation = module.params.get("intra_epg_isolation")
333 intersite_multicast_source = module.params.get("intersite_multicast_source")
334 proxy_arp = module.params.get("proxy_arp")
335 subnets = module.params.get("subnets")
336 qos_level = module.params.get("qos_level")
337 epg_type = module.params.get("epg_type")
338 deployment_type = module.params.get("deployment_type")
339 service_type = module.params.get("service_type")
340 access_type = module.params.get("access_type")
341 state = module.params.get("state")
342 preferred_group = module.params.get("preferred_group")
343
344 mso = MSOModule(module)
345
346 # Get schema objects
347 schema_id, schema_path, schema_obj = mso.query_schema(schema)
348
349 # Get template
350 templates = [t.get("name") for t in schema_obj.get("templates")]
351 if template not in templates:
352 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
353 template_idx = templates.index(template)
354
355 # Get ANP
356 anps = [a.get("name") for a in schema_obj.get("templates")[template_idx]["anps"]]
357 if anp not in anps:
358 mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ", ".join(anps)))
359 anp_idx = anps.index(anp)
360
361 # Get EPG
362 epgs = [e.get("name") for e in schema_obj.get("templates")[template_idx]["anps"][anp_idx]["epgs"]]
363 if epg is not None and epg in epgs:
364 epg_idx = epgs.index(epg)
365 mso.existing = schema_obj.get("templates")[template_idx]["anps"][anp_idx]["epgs"][epg_idx]
366
367 if state == "query":
368 if epg is None:
369 mso.existing = schema_obj.get("templates")[template_idx]["anps"][anp_idx]["epgs"]
370 elif not mso.existing:
371 mso.fail_json(msg="EPG '{epg}' not found".format(epg=epg))
372
373 if "bdRef" in mso.existing:
374 mso.existing["bdRef"] = mso.dict_from_ref(mso.existing["bdRef"])
375 if "vrfRef" in mso.existing:
376 mso.existing["vrfRef"] = mso.dict_from_ref(mso.existing["vrfRef"])
377 mso.exit_json()
378
379 epgs_path = "/templates/{0}/anps/{1}/epgs".format(template, anp)
380 epg_path = "/templates/{0}/anps/{1}/epgs/{2}".format(template, anp, epg)
381 service_path = "{0}/cloudServiceEpgConfig".format(epg_path)
382 ops = []
383 cloud_service_epg_config = {}
384
385 mso.previous = mso.existing
386 if state == "absent":
387 if mso.existing:
388 mso.sent = mso.existing = {}
389 ops.append(dict(op="remove", path=epg_path))
390
391 elif state == "present":
392 bd_ref = mso.make_reference(bd, "bd", schema_id, template)
393 vrf_ref = mso.make_reference(vrf, "vrf", schema_id, template)
394 subnets = mso.make_subnets(subnets, is_bd_subnet=False)
395
396 if display_name is None and not mso.existing:
397 display_name = epg
398
399 payload = dict(
400 name=epg,
401 displayName=display_name,
402 uSegEpg=useg_epg,
403 intraEpg=intra_epg_isolation,
404 mCastSource=intersite_multicast_source,
405 proxyArp=proxy_arp,
406 # FIXME: Missing functionality
407 # uSegAttrs=[],
408 subnets=subnets,
409 bdRef=bd_ref,
410 preferredGroup=preferred_group,
411 vrfRef=vrf_ref,
412 )
413 if description is not None:
414 payload.update(description=description)
415 if qos_level is not None:
416 payload.update(prio=qos_level)
417 if epg_type is not None:
418 payload.update(epgType=epg_type)
419
420 mso.sanitize(payload, collate=True)
421
422 if mso.existing:
423 # Clean contractRef to fix api issue
424 for contract in mso.sent.get("contractRelationships"):
425 contract["contractRef"] = mso.dict_from_ref(contract.get("contractRef"))
426 ops.append(dict(op="replace", path=epg_path, value=mso.sent))
427 else:
428 ops.append(dict(op="add", path=epgs_path + "/-", value=mso.sent))
429
430 if epg_type == "service":
431 access_type_map = {
432 "private": "Private",
433 "public": "Public",
434 "public_and_private": "PublicAndPrivate",
435 }
436 deployment_type_map = {
437 "cloud_native": "CloudNative",
438 "cloud_native_managed": "CloudNativeManaged",
439 "third_party": "Third-party",
440 }
441 if cloud_service_epg_config != {}:
442 cloud_service_epg_config.update(
443 dict(deploymentType=deployment_type_map[deployment_type], serviceType=service_type, accessType=access_type_map[access_type])
444 )
445 ops.append(dict(op="replace", path=service_path, value=cloud_service_epg_config))
446 else:
447 cloud_service_epg_config.update(
448 dict(deploymentType=deployment_type_map[deployment_type], serviceType=service_type, accessType=access_type_map[access_type])
449 )
450 ops.append(dict(op="add", path=service_path, value=cloud_service_epg_config))
451
452 mso.existing = mso.proposed
453
454 if "epgRef" in mso.previous:
455 del mso.previous["epgRef"]
456 if "bdRef" in mso.previous and mso.previous["bdRef"] != "":
457 mso.previous["bdRef"] = mso.dict_from_ref(mso.previous["bdRef"])
458 if "vrfRef" in mso.previous and mso.previous["bdRef"] != "":
459 mso.previous["vrfRef"] = mso.dict_from_ref(mso.previous["vrfRef"])
460
461 if not module.check_mode and mso.proposed != mso.previous:
462 mso.request(schema_path, method="PATCH", data=ops)
463
464 mso.exit_json()
465
466
467 if __name__ == "__main__":
468 main()