Debian Codegraph @

ansible_collections/cisco/mso/plugins/module_utils/schemas.py

raw
1 from __future__ import absolute_import, division, print_function
2
3 __metaclass__ = type
4
5 from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema
6
7
8 class MSOSchemas:
9 def __init__(self, mso_module):
10 self.mso = mso_module
11 self.schemas_by_id = {}
12 self.schemas_by_name = {}
13
14 def get_template_from_schema(self, schema_name, schema_id, template_name, template_id, refresh=False):
15 if not refresh:
16 existing_schema = None
17
18 if schema_id in self.schemas_by_id:
19 existing_schema = self.schemas_by_id[schema_id]
20 elif schema_name in self.schemas_by_name:
21 existing_schema = self.schemas_by_name[schema_name]
22
23 if existing_schema:
24 if template_id:
25 existing_schema.set_template_from_id(template_id)
26 elif template_name:
27 existing_schema.set_template(template_name)
28 return existing_schema
29
30 new_schema = MSOSchema(self.mso, schema_name, template_name, None, schema_id, template_id)
31 self.schemas_by_id[new_schema.id] = new_schema
32 self.schemas_by_name[new_schema.schema_name] = new_schema
33 return new_schema
34
35 def get_epg_uuid(self, epg_obj, epg_uuid):
36 """
37 Returns the EPG UUID in a given schema, template and ANP.
38
39 :param epg_obj: A dictionary containing the epg reference object. -> Dict
40 :param epg_uuid: The EPG UUID to return if specified. -> Str
41 :return: The EPG UUID if found, otherwise it fails the module. -> Str
42 """
43 if epg_uuid:
44 return epg_uuid
45 schema = self.get_template_from_schema(
46 epg_obj.get("schema"),
47 epg_obj.get("schema_id"),
48 epg_obj.get("template"),
49 epg_obj.get("template_id"),
50 )
51 schema.set_template_anp(epg_obj.get("anp"), epg_obj.get("anp_uuid"), fail_module=True)
52 schema.set_template_anp_epg(epg_obj.get("name"), fail_module=True)
53 return schema.schema_objects.get("template_anp_epg").details.get("uuid")