Debian Codegraph @

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

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright: (c) 2022, Akini Ross (@akinross) <[email protected]>
4 # Copyright: (c) 2025, Samita Bhattacharjee (@samiib) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 from collections import namedtuple
12
13 KVPair = namedtuple("KVPair", "key value")
14 Item = namedtuple("Item", "index details")
15
16
17 class MSOSchema:
18 def __init__(self, mso_module, schema_name, template_name=None, site_name=None, schema_id=None, template_id=None):
19 self.mso = mso_module
20 self.schema_name = schema_name
21 if schema_id:
22 self.id, self.path, self.schema = mso_module.query_schema_by_id(schema_id)
23 self.schema_name = self.schema.get("displayName")
24 else:
25 self.id, self.path, self.schema = mso_module.query_schema(schema_name)
26 self.schema_objects = {}
27 self.template_id = template_id
28 self.template_name = template_name
29 if self.template_id:
30 self.set_template_from_id(self.template_id)
31 elif self.template_name:
32 self.set_template(self.template_name)
33 if site_name and self.template_name:
34 self.set_site(self.template_name, site_name)
35
36 @staticmethod
37 def get_object_from_list(search_list, kv_list):
38 """
39 Get the first matched object from a list of mso object dictionaries.
40 :param search_list: Objects to search through -> List.
41 :param kv_list: Key/value pairs that should match in the object. -> List[KVPair(Str, Str)]
42 :return: The index and details of the object. -> Item (Named Tuple)
43 Values of provided keys of all existing objects. -> List
44 """
45
46 def kv_match(kvs, item):
47 return all((item.get(kv.key) == kv.value for kv in kvs))
48
49 match = next((Item(index, item) for index, item in enumerate(search_list) if kv_match(kv_list, item)), None)
50 existing = [item.get(kv.key) for item in search_list for kv in kv_list]
51 return match, existing
52
53 def validate_schema_objects_present(self, required_schema_objects):
54 """
55 Validate that attributes are set to a value that is not equal None.
56 :param required_schema_objects: List of schema objects to verify. -> List
57 :return: None
58 """
59 for schema_object in required_schema_objects:
60 if schema_object not in self.schema_objects.keys():
61 msg = "Required attribute '{0}' is not specified on schema instance with name {1}".format(schema_object, self.schema_name)
62 self.mso.fail_json(msg=msg)
63
64 def set_template(self, template_name, fail_module=True):
65 """
66 Get template item that matches the name of a template.
67 :param template_name: Name of the template to match. -> Str
68 :param fail_module: When match is not found fail the ansible module. -> Bool
69 :return: Template item. -> Item(Int, Dict) | None
70 """
71
72 kv_list = [KVPair("name", template_name)]
73 match, existing = self.get_object_from_list(self.schema.get("templates"), kv_list)
74 if not match and fail_module:
75 msg = "Provided template '{0}' not matching existing template(s): {1}".format(template_name, ", ".join(existing))
76 self.mso.fail_json(msg=msg)
77 self.template_id = match.details.get("templateID")
78 self.schema_objects["template"] = match
79
80 def set_template_from_id(self, template_id, fail_module=True):
81 """
82 Get template item that matches the id of a template.
83 :param template_id: ID of the template to match. -> Str
84 :param fail_module: When match is not found fail the ansible module. -> Bool
85 :return: Template item. -> Item(Int, Dict) | None
86 """
87 kv_list = [KVPair("templateID", template_id)]
88 match, existing = self.get_object_from_list(self.schema.get("templates"), kv_list)
89 if not match and fail_module:
90 msg = "Provided template ID '{0}' not matching existing template(s): {1}".format(template_id, ", ".join(existing))
91 self.mso.fail_json(msg=msg)
92 self.template_name = match.details.get("name")
93 self.schema_objects["template"] = match
94
95 def set_template_vrf(self, vrf, fail_module=True):
96 """
97 Get template VRF item that matches the name of a VRF.
98 :param vrf: Name of the VRF to match. -> Str
99 :param fail_module: When match is not found fail the ansible module. -> Bool
100 :return: Template VRF item. -> Item(Int, Dict) | None
101 """
102 self.validate_schema_objects_present(["template"])
103 kv_list = [KVPair("name", vrf)]
104 match, existing = self.get_object_from_list(self.schema_objects["template"].details.get("vrfs"), kv_list)
105 if not match and fail_module:
106 msg = "Provided VRF '{0}' not matching existing VRF(s): {1}".format(vrf, ", ".join(existing))
107 self.mso.fail_json(msg=msg)
108 self.schema_objects["template_vrf"] = match
109
110 def set_template_vrf_rp(self, ip, fail_module=True):
111 """
112 Get template VRF RP item that matches the ip of a VRF RP.
113 :param ip: IP of the RP to match. -> Str
114 :param fail_module: When match is not found fail the ansible module. -> Bool
115 :return: Template VRF RP item. -> Item(Int, Dict) | None
116 """
117 self.validate_schema_objects_present(["template_vrf"])
118 kv_list = [KVPair("ipAddress", ip)]
119 match, existing = self.get_object_from_list(self.schema_objects["template_vrf"].details.get("rpConfigs"), kv_list)
120 if not match and fail_module:
121 msg = "Provided IP '{0}' not matching existing IP(s): {1}".format(ip, ", ".join(existing))
122 self.mso.fail_json(msg=msg)
123 self.schema_objects["template_vrf_rp"] = match
124
125 def set_template_bd(self, bd, fail_module=True):
126 """
127 Get template bridge domain item that matches the name of a bd.
128 :param bd: Name of the bd to match. -> Str
129 :param fail_module: When match is not found fail the ansible module. -> Bool
130 :return: Template bd item. -> Item(Int, Dict) | None
131 """
132 self.validate_schema_objects_present(["template"])
133 kv_list = [KVPair("name", bd)]
134 match, existing = self.get_object_from_list(self.schema_objects["template"].details.get("bds"), kv_list)
135 if not match and fail_module:
136 msg = "Provided BD '{0}' not matching existing bd(s): {1}".format(bd, ", ".join(existing))
137 self.mso.fail_json(msg=msg)
138 self.schema_objects["template_bd"] = match
139
140 def set_template_bd_dhcp_relay_policy(self, relay_policy_ref, fail_module=True):
141 """
142 Get template bridge domain dhcp relay policy item that matches the name of a bd.
143 :param relay_policy_ref: Reference of the relay policy to match. -> Str
144 :param fail_module: When match is not found fail the ansible module. -> Bool
145 :return: Template bd relay policy item. -> Item(Int, Dict) | None
146 """
147 self.validate_schema_objects_present(["template"])
148 kv_list = [KVPair("ref", relay_policy_ref)]
149 match, existing = self.get_object_from_list(self.schema_objects["template_bd"].details.get("dhcpLabels"), kv_list)
150 if not match and fail_module:
151 msg = "Provided Relay Policy Reference '{0}' not matching existing relay policy reference(s): {1}".format(relay_policy_ref, ", ".join(existing))
152 self.mso.fail_json(msg=msg)
153 self.schema_objects["template_bd_dhcp_relay_policy"] = match
154
155 def set_template_anp(self, anp, anp_uuid=None, fail_module=True):
156 """
157 Get template application profile item that matches the name of an anp.
158 :param anp: Name of the anp to match. -> Str
159 :param anp: UUID of the anp to match. -> Str
160 :param fail_module: When match is not found fail the ansible module. -> Bool
161 :return: Template anp item. -> Item(Int, Dict) | None
162 """
163 self.validate_schema_objects_present(["template"])
164 kv_list = [KVPair("uuid", anp_uuid) if anp_uuid else KVPair("name", anp)]
165 match, existing = self.get_object_from_list(self.schema_objects["template"].details.get("anps"), kv_list)
166 if not match and fail_module:
167 msg = "Provided ANP '{0}' not matching existing anp(s): {1}".format(anp_uuid if anp_uuid else anp, ", ".join(existing))
168 self.mso.fail_json(msg=msg)
169 self.schema_objects["template_anp"] = match
170
171 def set_template_anp_epg(self, epg, fail_module=True):
172 """
173 Get template endpoint group item that matches the name of an epg.
174 :param epg: Name of the epg to match. -> Str
175 :param fail_module: When match is not found fail the ansible module. -> Bool
176 :return: Template epg item. -> Item(Int, Dict) | None
177 """
178 self.validate_schema_objects_present(["template_anp"])
179 kv_list = [KVPair("name", epg)]
180 match, existing = self.get_object_from_list(self.schema_objects["template_anp"].details.get("epgs"), kv_list)
181 if not match and fail_module:
182 msg = "Provided EPG '{0}' not matching existing epg(s): {1}".format(epg, ", ".join(existing))
183 self.mso.fail_json(msg=msg)
184 self.schema_objects["template_anp_epg"] = match
185
186 def set_template_anp_epg_contract(self, contract_ref, relation_type, fail_module=True):
187 """
188 Get template endpoint group contract item that matches the reference and type of an contract.
189 :param contract_ref: Reference of the contract to match. -> Str
190 :param relation_type: Relation_type of the contract to match. -> Str
191 :param fail_module: When match is not found fail the ansible module. -> Bool
192 :return: Template epg item. -> Item(Int, Dict) | None
193 """
194 self.validate_schema_objects_present(["template_anp_epg"])
195 kv_list = [KVPair("contractRef", contract_ref), KVPair("relationshipType", relation_type)]
196 match, existing = self.get_object_from_list(self.schema_objects["template_anp_epg"].details.get("contractRelationships"), kv_list)
197 if not match and fail_module:
198 msg = "Provided Contract Reference '{0}' with type '{1}' not matching existing contacts(s): {2}".format(
199 contract_ref, relation_type, ", ".join(existing)
200 )
201 self.mso.fail_json(msg=msg)
202 self.schema_objects["template_anp_epg_contract"] = match
203
204 def set_template_anp_epg_useg_attr(self, useg_attr, fail_module=True):
205 """
206 Get template endpoint group item that matches the name of an EPG uSeg Attribute.
207 :param useg_attr: Name of the EPG uSeg Attribute to match. -> Str
208 :param fail_module: When match is not found fail the ansible module. -> Bool
209 :return: Template EPG uSeg Attribute item. -> Item(Int, Dict) | None
210 """
211 self.validate_schema_objects_present(["template_anp_epg"])
212 kv_list = [KVPair("name", useg_attr)]
213 match, existing = self.get_object_from_list(self.schema_objects["template_anp_epg"].details.get("uSegAttrs"), kv_list)
214 if not match and fail_module:
215 msg = "Provided uSeg Attribute '{0}' does not match the existing uSeg Attribute(s): {1}".format(useg_attr, ", ".join(existing))
216 self.mso.fail_json(msg=msg)
217 self.schema_objects["template_anp_epg_useg_attribute"] = match
218
219 def set_template_anp_epg_annotation(self, annotation_key, fail_module=True):
220 """
221 Get template endpoint group annotation that matches the key of an EPG annotation.
222 :param useg_attr: Key of the EPG Annotation to match. -> Str
223 :param fail_module: When match is not found fail the ansible module. -> Bool
224 :return: Template EPG Annotation item. -> Item(Int, Dict) | None
225 """
226 self.validate_schema_objects_present(["template_anp_epg"])
227 kv_list = [KVPair("tagKey", annotation_key)]
228 match, existing = self.get_object_from_list(self.schema_objects["template_anp_epg"].details.get("tagAnnotations"), kv_list)
229 if not match and fail_module:
230 msg = "Provided Annotation Key '{0}' does not match the existing Annotation(s): {1}".format(annotation_key, ", ".join(existing))
231 self.mso.fail_json(msg=msg)
232 self.schema_objects["template_anp_epg_annotation"] = match
233
234 def set_template_external_epg(self, external_epg, fail_module=True):
235 """
236 Get template external epg item that matches the name of an anp.
237 :param anp: Name of the anp to match. -> Str
238 :param fail_module: When match is not found fail the ansible module. -> Bool
239 :return: Template anp item. -> Item(Int, Dict) | None
240 """
241 self.validate_schema_objects_present(["template"])
242 kv_list = [KVPair("name", external_epg)]
243 match, existing = self.get_object_from_list(self.schema_objects["template"].details.get("externalEpgs"), kv_list)
244 if not match and fail_module:
245 msg = "Provided External EPG '{0}' not matching existing external_epg(s): {1}".format(external_epg, ", ".join(existing))
246 self.mso.fail_json(msg=msg)
247 self.schema_objects["template_external_epg"] = match
248
249 def set_site(self, template_name, site_name, fail_module=True):
250 """
251 Get site item that matches the name of a site.
252 :param template_name: Name of the template to match. -> Str
253 :param site_name: Name of the site to match. -> Str
254 :param fail_module: When match is not found fail the ansible module. -> Bool
255 :return: Site item. -> Item(Int, Dict) | None
256 """
257 if not self.schema.get("sites"):
258 msg = "No sites associated with schema '{0}'. Associate the site with the schema using (M) mso_schema_site.".format(self.schema_name)
259 self.mso.fail_json(msg=msg)
260
261 kv_list = [KVPair("siteId", self.mso.lookup_site(site_name)), KVPair("templateName", template_name)]
262 match, existing = self.get_object_from_list(self.schema.get("sites"), kv_list)
263 if not match and fail_module:
264 msg = "Provided site '{0}' not associated with template '{1}'. Site is currently associated with template(s): {2}".format(
265 site_name, template_name, ", ".join(existing[1::2])
266 )
267 self.mso.fail_json(msg=msg)
268 self.schema_objects["site"] = match
269
270 def set_site_bd(self, bd_name, fail_module=True):
271 """
272 Get site bridge domain item that matches the name of a bd.
273 :param bd_name: Name of the bd to match. -> Str
274 :param fail_module: When match is not found fail the ansible module. -> Bool
275 :return: Site bd item. -> Item(Int, Dict) | None
276 """
277 self.validate_schema_objects_present(["template", "site"])
278 kv_list = [KVPair("bdRef", self.mso.bd_ref(schema_id=self.id, template=self.schema_objects["template"].details.get("name"), bd=bd_name))]
279 match, existing = self.get_object_from_list(self.schema_objects["site"].details.get("bds"), kv_list)
280 if not match and fail_module:
281 msg = "Provided BD '{0}' not matching existing site bd(s): {1}".format(bd_name, ", ".join(existing))
282 self.mso.fail_json(msg=msg)
283 self.schema_objects["site_bd"] = match
284
285 def set_site_bd_subnet(self, subnet, fail_module=True):
286 """
287 Get site bridge domain subnet item that matches the ip of a subnet.
288 :param subnet: Subnet (ip) to match. -> Str
289 :param fail_module: When match is not found fail the ansible module. -> Bool
290 :return: Site bd subnet item. -> Item(Int, Dict) | None
291 """
292 self.validate_schema_objects_present(["site_bd"])
293 kv_list = [KVPair("ip", subnet)]
294 match, existing = self.get_object_from_list(self.schema_objects["site_bd"].details.get("subnets"), kv_list)
295 if not match and fail_module:
296 msg = "Provided subnet '{0}' not matching existing site bd subnet(s): {1}".format(subnet, ", ".join(existing))
297 self.mso.fail_json(msg=msg)
298 self.schema_objects["site_bd_subnet"] = match
299
300 def set_site_anp(self, anp_name, fail_module=True):
301 """
302 Get site application profile item that matches the name of a anp.
303 :param anp_name: Name of the anp to match. -> Str
304 :param fail_module: When match is not found fail the ansible module. -> Bool
305 :return: Site anp item. -> Item(Int, Dict) | None
306 """
307 self.validate_schema_objects_present(["template_anp", "site"])
308 kv_list = [KVPair("anpRef", self.schema_objects["template_anp"].details.get("anpRef"))]
309 match, existing = self.get_object_from_list(self.schema_objects["site"].details.get("anps"), kv_list)
310 if not match and fail_module:
311 msg = "Provided ANP '{0}' not matching existing site anp(s): {1}".format(anp_name, ", ".join(existing))
312 self.mso.fail_json(msg=msg)
313 self.schema_objects["site_anp"] = match
314
315 def set_site_anp_epg(self, epg_name, fail_module=True):
316 """
317 Get site anp epg item that matches the epgs.
318 :param epg: epg to match. -> Str
319 :param fail_module: When match is not found fail the ansible module. -> Bool
320 :return: Site anp epg item. -> Item(Int, Dict) | None
321 """
322 self.validate_schema_objects_present(["site_anp", "template_anp_epg"])
323 kv_list = [KVPair("epgRef", self.schema_objects["template_anp_epg"].details.get("epgRef"))]
324 match, existing = self.get_object_from_list(self.schema_objects["site_anp"].details.get("epgs"), kv_list)
325 if not match and fail_module:
326 msg = "Provided EPG '{0}' not matching existing site anp epg(s): {1}".format(epg_name, ", ".join(existing))
327 self.mso.fail_json(msg=msg)
328 self.schema_objects["site_anp_epg"] = match
329
330 def set_site_anp_epg_useg_attr(self, useg_attr, fail_module=True):
331 """
332 Get site endpoint group item that matches the name of an EPG uSeg Attribute.
333 :param useg_attr: Name of the EPG uSeg Attribute to match. -> Str
334 :param fail_module: When match is not found fail the ansible module. -> Bool
335 :return: Site EPG uSeg Attribute item. -> Item(Int, Dict) | None
336 """
337 self.validate_schema_objects_present(["site_anp_epg"])
338 kv_list = [KVPair("name", useg_attr)]
339 match, existing = self.get_object_from_list(self.schema_objects["site_anp_epg"].details.get("uSegAttrs"), kv_list)
340 if not match and fail_module:
341 msg = "Provided Site uSeg Attribute '{0}' does not match the existing Site uSeg Attribute(s): {1}".format(useg_attr, ", ".join(existing))
342 self.mso.fail_json(msg=msg)
343 self.schema_objects["site_anp_epg_useg_attribute"] = match
344
345 def set_site_contract(self, contract_name, fail_module=True):
346 """
347 Get site contract item that matches the name of a contract.
348 :param contract_name: Name of the contract to match. -> Str
349 :param fail_module: When match is not found fail the ansible module. -> Bool
350 :return: Site contract item. -> Item(Int, Dict) | None
351 """
352 self.validate_schema_objects_present(["template", "site"])
353 kv_list = [
354 KVPair(
355 "contractRef", self.mso.contract_ref(schema_id=self.id, template=self.schema_objects["template"].details.get("name"), contract=contract_name)
356 )
357 ]
358 match, existing = self.get_object_from_list(self.schema_objects["site"].details.get("contracts"), kv_list)
359 if not match and fail_module:
360 msg = "Provided Contract '{0}' not matching existing site contract(s): {1}".format(contract_name, ", ".join(existing))
361 self.mso.fail_json(msg=msg)
362 self.schema_objects["site_contract"] = match
363
364 def set_site_service_graph(self, site_service_graph, fail_module=True):
365 """
366 Get site item that matches the name of a service graph.
367 :param service_graph: Name of the service graph to match. -> Str
368 :param fail_module: When match is not found fail the ansible module. -> Bool
369 :return: Site service graph item. -> Item(Int, Dict) | None
370 """
371 self.validate_schema_objects_present(["template", "site"])
372
373 kv_list = [
374 KVPair(
375 "serviceGraphRef",
376 self.mso.service_graph_ref(schema_id=self.id, template=self.schema_objects["template"].details.get("name"), service_graph=site_service_graph),
377 )
378 ]
379
380 site_service_graph = self.schema_objects["site"].details.get("serviceGraphs")
381 match, existing = self.get_object_from_list(site_service_graph, kv_list)
382 if not match and fail_module:
383 msg = "Provided Site Service Graph '{0}' not matching existing site service graph(s): {1}".format(site_service_graph, ", ".join(existing))
384 self.mso.fail_json(msg=msg)
385 self.schema_objects["site_service_graph"] = match
386
387 def set_site_anp_epg_static_port(self, path, fail_module=True):
388 """
389 Get site anp epg static port path item that matches the path of Static Port.
390 :param path: Path of the Static Port to match. -> Str
391 :param fail_module: When match is not found fail the ansible module. -> Bool
392 :return: Site anp epg item. -> Item(Int, Dict) | None
393 """
394 self.validate_schema_objects_present(["site_anp_epg"])
395 kv_list = [KVPair("path", path)]
396 match, existing = self.get_object_from_list(self.schema_objects["site_anp_epg"].details.get("staticPorts"), kv_list)
397 if not match and fail_module:
398 msg = "Provided Static Port Path '{0}' not matching existing static port path(s): {1}".format(path, ", ".join(existing))
399 self.mso.fail_json(msg=msg)
400 self.schema_objects["site_anp_epg_static_port"] = match