Debian Codegraph @

ansible_collections/cisco/mso/plugins/module_utils/templates.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.template import MSOTemplate
6 from ansible_collections.cisco.mso.plugins.module_utils.constants import TEMPLATE_TYPES
7 from ansible_collections.cisco.mso.plugins.module_utils.utils import check_if_all_elements_are_none
8
9
10 class MSOTemplates:
11 def __init__(self, mso_module):
12 self.mso = mso_module
13 self.templates_by_id = {}
14 self.templates_by_name = {}
15
16 def get_template(self, template_type, template_name, template_id, refresh=False, fail_module=False):
17 if not refresh:
18 if template_id in self.templates_by_id:
19 return self.templates_by_id[template_id]
20 elif (template_name, TEMPLATE_TYPES[template_type]["template_type"]) in self.templates_by_name:
21 return self.templates_by_name[(template_name, TEMPLATE_TYPES[template_type]["template_type"])]
22
23 new_template = MSOTemplate(self.mso, template_type, template_name, template_id, fail_module=fail_module)
24 self.templates_by_id[new_template.template_id] = new_template
25 self.templates_by_name[(new_template.template_name, new_template.template_type)] = new_template
26 return new_template
27
28 def get_object_uuid_from_template(self, template_type, object_type, uuid, obj, refresh=False):
29 if uuid:
30 return uuid
31
32 is_empty = check_if_all_elements_are_none(obj.values()) if obj and isinstance(obj, dict) else True
33 if not is_empty:
34 name = obj.get("name")
35 template_name = obj.get("template")
36 template_id = obj.get("template_id")
37
38 template = self.get_template(template_type, template_name, template_id, refresh)
39
40 return template.get_template_policy_uuid(template_type, name, object_type)