| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2022, Akini Ross (@akinross) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import TEMPLATE_TYPES, DOMAIN_TYPE_MAP, VM_DOMAIN_PROVIDER_MAP |
| 11 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import generate_api_endpoint |
| 12 |
from collections import namedtuple |
| 13 |
|
| 14 |
KVPair = namedtuple("KVPair", "key value") |
| 15 |
Item = namedtuple("Item", "index details") |
| 16 |
SearchQuery = namedtuple("SearchQuery", "key kv_pairs") |
| 17 |
|
| 18 |
|
| 19 |
class MSOTemplate: |
| 20 |
def __init__(self, mso_module, template_type=None, template_name=None, template_id=None, schema_name=None, schema_id=None, fail_module=False): |
| 21 |
self.mso = mso_module |
| 22 |
self.templates_path = "templates" |
| 23 |
self.summaries_path = "{0}/summaries".format(self.templates_path) |
| 24 |
self.template = {} |
| 25 |
self.template_path = "" |
| 26 |
self.template_name = template_name |
| 27 |
self.template_id = template_id |
| 28 |
self.template_type = template_type |
| 29 |
self.template_summary = {} |
| 30 |
self.template_objects_cache = {} |
| 31 |
self.schema_path = None |
| 32 |
self.schema_name = schema_name |
| 33 |
self.schema_id = schema_id |
| 34 |
|
| 35 |
if template_id: |
| 36 |
# Checking if the template with id exists to avoid error: MSO Error 400: Template ID 665da24b95400f375928f195 invalid |
| 37 |
self.template_summary = self.mso.get_obj(self.summaries_path, templateId=self.template_id) |
| 38 |
if self.template_summary: |
| 39 |
self.template_path = "{0}/{1}".format(self.templates_path, self.template_id) |
| 40 |
self.template = self.mso.query_obj(self.template_path) |
| 41 |
self.template_name = self.template.get("displayName") |
| 42 |
self.template_type = self.template.get("templateType") |
| 43 |
self._set_deployment_properties() |
| 44 |
if self.template_type == "application": |
| 45 |
self._set_schema_properties() |
| 46 |
else: |
| 47 |
self.mso.fail_json( |
| 48 |
msg="Provided template id '{0}' does not exist. Existing templates: {1}".format( |
| 49 |
self.template_id, |
| 50 |
[ |
| 51 |
"Template '{0}' with id '{1}'".format(template.get("templateName"), template.get("templateId")) |
| 52 |
for template in self.mso.query_objs(self.summaries_path) |
| 53 |
], |
| 54 |
) |
| 55 |
) |
| 56 |
elif template_name: |
| 57 |
if not template_type: |
| 58 |
self.mso.fail_json(msg="Template type must be provided when using template name.") |
| 59 |
self.template_summary = self.mso.get_obj( |
| 60 |
self.summaries_path, |
| 61 |
templateName=self.template_name, |
| 62 |
templateType=TEMPLATE_TYPES[template_type]["template_type"], |
| 63 |
schemaName=self.schema_name, |
| 64 |
schemaId=self.schema_id, |
| 65 |
) |
| 66 |
if self.template_summary: |
| 67 |
self.template_path = "{0}/{1}".format(self.templates_path, self.template_summary.get("templateId")) |
| 68 |
self.template = self.mso.query_obj(self.template_path) |
| 69 |
self.template_id = self.template.get("templateId") |
| 70 |
self.template_type = self.template.get("templateType") |
| 71 |
self._set_deployment_properties() |
| 72 |
if template_type == "application": |
| 73 |
self._set_schema_properties() |
| 74 |
|
| 75 |
if fail_module and not self.template: |
| 76 |
self.mso.fail_json( |
| 77 |
msg="Provided template name '{0}' does not exist. Existing templates: {1}".format( |
| 78 |
self.template_name, |
| 79 |
[ |
| 80 |
"Template '{0}' with id '{1}'".format(template.get("templateName"), template.get("templateId")) |
| 81 |
for template in self.mso.query_objs(self.summaries_path, templateType=TEMPLATE_TYPES[template_type]["template_type"]) |
| 82 |
], |
| 83 |
) |
| 84 |
) |
| 85 |
|
| 86 |
elif template_type: |
| 87 |
self.template = self.mso.query_objs(self.summaries_path, templateType=TEMPLATE_TYPES[template_type]["template_type"]) |
| 88 |
else: |
| 89 |
self.template = self.mso.query_objs(self.summaries_path) |
| 90 |
|
| 91 |
# Remove unwanted keys from existing object for better output and diff compares |
| 92 |
if isinstance(self.template, dict): |
| 93 |
for key in ["_updateVersion", "version"]: |
| 94 |
self.template.pop(key, None) |
| 95 |
|
| 96 |
def _set_schema_properties(self): |
| 97 |
self.schema_name = self.template_summary.get("schemaName") |
| 98 |
self.schema_id = self.template_summary.get("schemaId") |
| 99 |
self.schema_path = "schemas/{0}".format(self.schema_id) |
| 100 |
|
| 101 |
def _set_deployment_properties(self): |
| 102 |
# "deploySummmary" is a typo in the API |
| 103 |
# Checking both keys to be safe |
| 104 |
deploy_summary = self.template_summary.get("deploySummmary", {}) or self.template_summary.get("deploySummary", {}) |
| 105 |
self.deploy_task_id = deploy_summary.get("teTaskId") |
| 106 |
self.deployed_site_ids = [summary.get("siteId") for summary in deploy_summary.get("siteDeploymentSummaries", []) if summary.get("siteId") is not None] |
| 107 |
|
| 108 |
@staticmethod |
| 109 |
def get_object_from_list(search_list, kv_list): |
| 110 |
""" |
| 111 |
Get the first matched object from a list of mso object dictionaries. |
| 112 |
:param search_list: Objects to search through -> List. |
| 113 |
:param kv_list: Key/value pairs that should match in the object. -> List[KVPair(Str, Str)] |
| 114 |
:return: The index and details of the object. -> Item (Named Tuple) |
| 115 |
Values of provided keys of all existing objects. -> List |
| 116 |
""" |
| 117 |
|
| 118 |
# Sometimes the attribute returned by api might be None |
| 119 |
# If search_list is None, iterating over it will throw an error |
| 120 |
# Thus we need to return the match of None and without existing values |
| 121 |
if search_list is None: |
| 122 |
return None, [] |
| 123 |
|
| 124 |
def kv_match(kvs, item): |
| 125 |
return all((item.get(kv.key) == kv.value for kv in kvs)) |
| 126 |
|
| 127 |
match = next((Item(index, item) for index, item in enumerate(search_list) if kv_match(kv_list, item)), None) |
| 128 |
existing = [item.get(kv.key) for item in search_list for kv in kv_list if item.get(kv.key) is not None] |
| 129 |
return match, existing |
| 130 |
|
| 131 |
def validate_template(self, template_type): |
| 132 |
""" |
| 133 |
Validate that attributes are set to a value that is not equal None. |
| 134 |
:return: None |
| 135 |
""" |
| 136 |
if not self.template or not isinstance(self.template, dict): |
| 137 |
self.mso.fail_json(msg="Template '{0}' not found.".format(self.template_name)) |
| 138 |
if self.template.get("templateType") != template_type: |
| 139 |
self.mso.fail_json(msg="Template type must be '{0}'.".format(template_type)) |
| 140 |
|
| 141 |
def get_object_by_key_value_pairs(self, object_description, search_list, kv_list, fail_module=False): |
| 142 |
""" |
| 143 |
Get the object from a list of mso object dictionaries by name. |
| 144 |
:param object_description: Description of the object to search for -> Str |
| 145 |
:param search_list: Objects to search through -> List. |
| 146 |
:param kv_list: Key/value pairs that should match in the object. -> List[KVPair(Str, Str)] |
| 147 |
:param fail_module: When match is not found fail the ansible module. -> Bool |
| 148 |
:return: The object. -> Dict | None |
| 149 |
""" |
| 150 |
match, existing = self.get_object_from_list(search_list, kv_list) |
| 151 |
if not match and fail_module: |
| 152 |
msg = "Provided {0} with '{1}' not matching existing object(s): {2}".format(object_description, kv_list, ", ".join(existing)) |
| 153 |
self.mso.fail_json(msg=msg) |
| 154 |
return match |
| 155 |
|
| 156 |
def get_object_by_uuid(self, object_description, search_list, uuid, fail_module=False): |
| 157 |
""" |
| 158 |
Get the object from a list of mso object dictionaries by uuid. |
| 159 |
:param object_description: Description of the object to search for -> Str |
| 160 |
:param search_list: Objects to search through -> List. |
| 161 |
:param uuid: UUID of the object to search for -> Str |
| 162 |
:param fail_module: When match is not found fail the ansible module. -> Bool |
| 163 |
:return: The object. -> Dict | None |
| 164 |
""" |
| 165 |
kv_list = [KVPair("uuid", uuid)] |
| 166 |
return self.get_object_by_key_value_pairs(object_description, search_list, kv_list, fail_module) |
| 167 |
|
| 168 |
def get_vlan_pool_uuid(self, vlan_pool_name): |
| 169 |
""" |
| 170 |
Get the UUID of a VLAN pool by name. |
| 171 |
:param vlan_pool_name: Name of the VLAN pool to search for -> Str |
| 172 |
:return: UUID of the VLAN pool. -> Str |
| 173 |
""" |
| 174 |
existing_vlan_pools = self.template.get("fabricPolicyTemplate", {}).get("template", {}).get("vlanPools", []) |
| 175 |
kv_list = [KVPair("name", vlan_pool_name)] |
| 176 |
match = self.get_object_by_key_value_pairs("VLAN Pool", existing_vlan_pools, kv_list, fail_module=True) |
| 177 |
return match.details.get("uuid") |
| 178 |
|
| 179 |
def get_vlan_pool_name(self, vlan_pool_uuid): |
| 180 |
""" |
| 181 |
Get the UUID of a VLAN pool by name. |
| 182 |
:param vlan_pool_name: Name of the VLAN pool to search for -> Str |
| 183 |
:return: UUID of the VLAN pool. -> Str |
| 184 |
""" |
| 185 |
existing_vlan_pools = self.template.get("fabricPolicyTemplate", {}).get("template", {}).get("vlanPools", []) |
| 186 |
kv_list = [KVPair("uuid", vlan_pool_uuid)] |
| 187 |
match = self.get_object_by_key_value_pairs("VLAN Pool", existing_vlan_pools, kv_list, fail_module=True) |
| 188 |
return match.details.get("name") |
| 189 |
|
| 190 |
def get_route_map(self, attr_name, tenant_id, tenant_name, route_map, route_map_objects): |
| 191 |
""" |
| 192 |
Retrieves the details of a specific route map object based on the provided attributes. |
| 193 |
:param attr_name: The attribute name for error messaging. -> Str |
| 194 |
:param tenant_id: The ID of the tenant. -> Str |
| 195 |
:param tenant_name: The name of the tenant. -> Str |
| 196 |
:param route_map: The name of the route map. -> Str |
| 197 |
:param route_map_objects: The list of route map objects to search from. -> List |
| 198 |
:return: The details of the route map object if found, otherwise an empty dictionary. -> Dict |
| 199 |
""" |
| 200 |
if route_map and tenant_id and route_map_objects: |
| 201 |
route_map_object = self.get_object_from_list( |
| 202 |
route_map_objects, |
| 203 |
[KVPair("name", route_map), KVPair("tenantId", tenant_id)], |
| 204 |
) |
| 205 |
if route_map_object[0]: |
| 206 |
return route_map_object[0].details |
| 207 |
else: |
| 208 |
self.mso.fail_json(msg="Provided Route Map {0}: {1} with the tenant: {2} not found.".format(attr_name, route_map, tenant_name)) |
| 209 |
else: |
| 210 |
return {} |
| 211 |
|
| 212 |
def get_vrf_object(self, vrf_dict, tenant_id, templates_objects_path): |
| 213 |
""" |
| 214 |
Get VRF object based on provided parameters. |
| 215 |
:param vrf_dict: Dictionary containing VRF details. -> Dict |
| 216 |
:param tenant_id: Id of the tenant. -> Str |
| 217 |
:param templates_objects_path: Path to the templates objects. -> Str |
| 218 |
:return: VRF object if found, otherwise fail with an error message. -> Dict |
| 219 |
""" |
| 220 |
|
| 221 |
vrf_path = generate_api_endpoint(templates_objects_path, **{"type": "vrf", "tenant-id": tenant_id, "include-common": "true"}) |
| 222 |
vrf_objects = self.mso.query_objs(vrf_path) |
| 223 |
vrf_kv_list = [ |
| 224 |
KVPair("name", vrf_dict.get("name")), |
| 225 |
KVPair("templateId", vrf_dict.get("template_id")) if vrf_dict.get("template_id") else KVPair("templateName", vrf_dict.get("template")), |
| 226 |
KVPair("schemaId", vrf_dict.get("schema_id")) if vrf_dict.get("schema_id") else KVPair("schemaName", vrf_dict.get("schema")), |
| 227 |
KVPair("tenantId", tenant_id), |
| 228 |
] |
| 229 |
|
| 230 |
vrf_object = self.get_object_from_list(vrf_objects, vrf_kv_list) |
| 231 |
|
| 232 |
if vrf_object[0]: |
| 233 |
return vrf_object[0] |
| 234 |
else: |
| 235 |
self.mso.fail_json(msg="Provided VRF {0} not found.".format(vrf_dict.get("name"))) |
| 236 |
|
| 237 |
def get_bd_object(self, bd_dict, tenant_id, templates_objects_path): |
| 238 |
""" |
| 239 |
Get Bridge Domain object based on provided parameters. |
| 240 |
:param bd_dict: Dictionary containing Bridge Domain details. -> Dict |
| 241 |
:param tenant_id: Id of the tenant. -> Str |
| 242 |
:param templates_objects_path: Path to the templates objects. -> Str |
| 243 |
:return: Bridge Domain object if found, otherwise fail with an error message. -> Dict |
| 244 |
""" |
| 245 |
|
| 246 |
bd_path = generate_api_endpoint(templates_objects_path, **{"type": "bd", "tenant-id": tenant_id, "include-common": "true"}) |
| 247 |
bd_objects = self.mso.query_objs(bd_path) |
| 248 |
bd_kv_list = [ |
| 249 |
KVPair("name", bd_dict.get("name")), |
| 250 |
KVPair("templateId", bd_dict.get("template_id")) if bd_dict.get("template_id") else KVPair("templateName", bd_dict.get("template")), |
| 251 |
KVPair("schemaId", bd_dict.get("schema_id")) if bd_dict.get("schema_id") else KVPair("schemaName", bd_dict.get("schema")), |
| 252 |
KVPair("tenantId", tenant_id), |
| 253 |
] |
| 254 |
return self.get_object_by_key_value_pairs("Bridge Domain", bd_objects, bd_kv_list, fail_module=True) |
| 255 |
|
| 256 |
def get_l3out_node_routing_policy_object(self, uuid=None, name=None, fail_module=False): |
| 257 |
""" |
| 258 |
Get the L3Out Node Routing Policy by UUID or Name. |
| 259 |
:param uuid: UUID of the L3Out Node Routing Policy to search for -> Str |
| 260 |
:param name: Name of the L3Out Node Routing Policy to search for -> Str |
| 261 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 262 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 263 |
When the UUID | Name is existing in the search list -> Dict |
| 264 |
When the UUID | Name is not existing in the search list -> None |
| 265 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 266 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 267 |
""" |
| 268 |
existing_l3out_node_routing_policy = self.template.get("tenantPolicyTemplate", {}).get("template", {}).get("l3OutNodePolGroups", []) |
| 269 |
if uuid or name: # Query a specific object |
| 270 |
return self.get_object_by_key_value_pairs( |
| 271 |
"L3Out Node Routing Policy", existing_l3out_node_routing_policy, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module |
| 272 |
) |
| 273 |
return existing_l3out_node_routing_policy # Query all objects |
| 274 |
|
| 275 |
def get_interface_policy_group_uuid(self, interface_policy_group): |
| 276 |
""" |
| 277 |
Get the UUID of an Interface Policy Group by name. |
| 278 |
:param interface_policy_group: Name of the Interface Policy Group to search for -> Str |
| 279 |
:return: UUID of the Interface Policy Group. -> Str |
| 280 |
""" |
| 281 |
existing_policy_groups = self.template.get("fabricPolicyTemplate", {}).get("template", {}).get("interfacePolicyGroups", []) |
| 282 |
kv_list = [KVPair("name", interface_policy_group)] |
| 283 |
match = self.get_object_by_key_value_pairs("Interface Policy Groups", existing_policy_groups, kv_list, fail_module=True) |
| 284 |
return match.details.get("uuid") |
| 285 |
|
| 286 |
def get_ipsla_monitoring_policy(self, uuid=None, name=None, fail_module=False): |
| 287 |
""" |
| 288 |
Get the IPSLA Monitoring Policy by UUID or Name. |
| 289 |
:param uuid: UUID of the IPSLA Monitoring Policy to search for -> Str |
| 290 |
:param name: Name of the IPSLA Monitoring Policy to search for -> Str |
| 291 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 292 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 293 |
When the UUID | Name is existing in the search list -> Dict |
| 294 |
When the UUID | Name is not existing in the search list -> None |
| 295 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 296 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 297 |
""" |
| 298 |
existing_ipsla_policies = self.template.get("tenantPolicyTemplate", {}).get("template", {}).get("ipslaMonitoringPolicies", []) |
| 299 |
if name or uuid: |
| 300 |
return self.get_object_by_key_value_pairs( |
| 301 |
"IPSLA Monitoring Policy", |
| 302 |
existing_ipsla_policies, |
| 303 |
[KVPair("uuid", uuid) if uuid else KVPair("name", name)], |
| 304 |
fail_module=fail_module, |
| 305 |
) |
| 306 |
return existing_ipsla_policies |
| 307 |
|
| 308 |
def get_l3out_object(self, uuid=None, name=None, fail_module=False, search_object=None): |
| 309 |
""" |
| 310 |
Get the L3Out by uuid or name. |
| 311 |
:param uuid: UUID of the L3Out to search for -> Str |
| 312 |
:param name: Name of the L3Out to search for -> Str |
| 313 |
:param search_object: The object to search in -> Dict |
| 314 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 315 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 316 |
When the UUID | Name is existing in the search list -> Dict |
| 317 |
When the UUID | Name is not existing in the search list -> None |
| 318 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 319 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 320 |
""" |
| 321 |
if not search_object: |
| 322 |
search_object = self.template |
| 323 |
existing_l3outs = search_object.get("l3outTemplate", {}).get("l3outs", []) |
| 324 |
if uuid or name: # Query a specific object |
| 325 |
return self.get_object_by_key_value_pairs("L3Out", existing_l3outs, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 326 |
return existing_l3outs # Query all objects |
| 327 |
|
| 328 |
def get_l3out_node_group(self, name, l3out_object, fail_module=False): |
| 329 |
""" |
| 330 |
Get the L3Out Node Group Policy by name. |
| 331 |
:param name: Name of the L3Out Node Group Policy to search for -> Str |
| 332 |
:param l3out_object: L3Out object to search Node Group Policy -> Dict |
| 333 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 334 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 335 |
When the Name is existing in the search list -> Dict |
| 336 |
When the Name is not existing in the search list -> None |
| 337 |
When the Name is None, and the search list is not empty -> List[Dict] |
| 338 |
When the Name is None, and the search list is empty -> List[] |
| 339 |
""" |
| 340 |
existing_l3out_node_groups = l3out_object.get("nodeGroups", []) |
| 341 |
if name: # Query a specific object |
| 342 |
return self.get_object_by_key_value_pairs("L3Out Node Group Policy", existing_l3out_node_groups, [KVPair("name", name)], fail_module) |
| 343 |
return existing_l3out_node_groups # Query all objects |
| 344 |
|
| 345 |
def get_port_channel_match(self, port_channel, mso_templates): |
| 346 |
""" |
| 347 |
Get the port channel from the provided port channel reference or uuid. |
| 348 |
:param port_channel: The port channel object containing reference or uuid to search for -> ndo_l3out_port_channel_spec |
| 349 |
:param mso_templates: MSO Templates object to search for referenced templates -> MSOTemplates |
| 350 |
:return: The matched port channel object or None if not found -> Dict | None |
| 351 |
""" |
| 352 |
port_channel_match = None |
| 353 |
if port_channel: |
| 354 |
port_channel_uuid = port_channel.get("uuid") |
| 355 |
if port_channel_uuid: |
| 356 |
port_channel_match = self.get_template_object_by_uuid("portChannel", port_channel_uuid, True) |
| 357 |
else: |
| 358 |
fabric_resource_mso_template = mso_templates.get_template( |
| 359 |
"fabric_resource", |
| 360 |
port_channel.get("reference").get("template"), |
| 361 |
port_channel.get("reference").get("template_id"), |
| 362 |
fail_module=True, |
| 363 |
) |
| 364 |
port_channel_match = fabric_resource_mso_template.get_port_channel( |
| 365 |
None, |
| 366 |
port_channel.get("reference").get("name"), |
| 367 |
fail_module=True, |
| 368 |
).details |
| 369 |
return port_channel_match |
| 370 |
|
| 371 |
def get_port_channel(self, uuid=None, name=None, fail_module=False): |
| 372 |
""" |
| 373 |
Get the port channel by uuid or name. |
| 374 |
:param uuid: UUID of the Port Channel to search for -> Str |
| 375 |
:param name: Name of the Port Channel to search for -> Str |
| 376 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 377 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 378 |
When the UUID | Name is existing in the search list -> Dict |
| 379 |
When the UUID | Name is not existing in the search list -> None |
| 380 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 381 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 382 |
""" |
| 383 |
existing_port_channels = self.template.get("fabricResourceTemplate", {}).get("template", {}).get("portChannels", []) |
| 384 |
if uuid or name: # Query a specific object |
| 385 |
return self.get_object_by_key_value_pairs( |
| 386 |
"Port Channel", existing_port_channels, [KVPair("uuid", uuid)] if uuid else [KVPair("name", name)], fail_module=fail_module |
| 387 |
) |
| 388 |
return existing_port_channels |
| 389 |
|
| 390 |
def get_virtual_port_channel_match(self, virtual_port_channel, mso_templates): |
| 391 |
""" |
| 392 |
Get the virtual port channel from the provided virtual port channel reference or uuid. |
| 393 |
:param virtual_port_channel: The virtual port channel object containing reference or uuid to search for -> ndo_l3out_virtual_port_channel_spec |
| 394 |
:param mso_templates: MSO Templates object to search for referenced templates -> MSOTemplates |
| 395 |
:return: The matched virtual port channel object or None if not found -> Dict | None |
| 396 |
""" |
| 397 |
virtual_port_channel_match = None |
| 398 |
if virtual_port_channel: |
| 399 |
virtual_port_channel_uuid = virtual_port_channel.get("uuid") |
| 400 |
if virtual_port_channel_uuid: |
| 401 |
virtual_port_channel_match = self.get_template_object_by_uuid("virtualPortChannel", virtual_port_channel_uuid, True) |
| 402 |
else: |
| 403 |
fabric_resource_mso_template = mso_templates.get_template( |
| 404 |
"fabric_resource", |
| 405 |
virtual_port_channel.get("reference").get("template"), |
| 406 |
virtual_port_channel.get("reference").get("template_id"), |
| 407 |
fail_module=True, |
| 408 |
) |
| 409 |
virtual_port_channel_match = fabric_resource_mso_template.get_virtual_port_channel( |
| 410 |
virtual_port_channel_uuid, |
| 411 |
virtual_port_channel.get("reference").get("name"), |
| 412 |
fail_module=True, |
| 413 |
).details |
| 414 |
return virtual_port_channel_match |
| 415 |
|
| 416 |
def get_virtual_port_channel(self, uuid=None, name=None, fail_module=False): |
| 417 |
""" |
| 418 |
Get the virtual port channel by uuid or name. |
| 419 |
:param uuid: UUID of the Virtual Port Channel to search for -> Str |
| 420 |
:param name: Name of the Virtual Port Channel to search for -> Str |
| 421 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 422 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 423 |
When the UUID | Name is existing in the search list -> Dict |
| 424 |
When the UUID | Name is not existing in the search list -> None |
| 425 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 426 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 427 |
""" |
| 428 |
existing_virtual_port_channels = self.template.get("fabricResourceTemplate", {}).get("template", {}).get("virtualPortChannels", []) |
| 429 |
if uuid or name: # Query a specific object |
| 430 |
return self.get_object_by_key_value_pairs( |
| 431 |
"Virtual Port Channel", existing_virtual_port_channels, [KVPair("uuid", uuid)] if uuid else [KVPair("name", name)], fail_module=fail_module |
| 432 |
) |
| 433 |
return existing_virtual_port_channels |
| 434 |
|
| 435 |
def get_l3out_node(self, l3out_object, pod_id, node_id, fail_module=False): |
| 436 |
""" |
| 437 |
Get the L3Out Node by pod_id and node_id. |
| 438 |
:param l3out_object: L3Out object to search for the Node -> Dict |
| 439 |
:param pod_id: Pod ID of the Node to search for -> Str |
| 440 |
:param node_id: Node ID of the Node to search for -> Str |
| 441 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 442 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 443 |
When the pod_id | node_id is existing in the search list -> Dict |
| 444 |
When the pod_id | node_id is not existing in the search list -> None |
| 445 |
When both pod_id and node_id are None, and the search list is not empty -> List[Dict] |
| 446 |
When both pod_id and node_id are None, and the search list is empty -> List[] |
| 447 |
""" |
| 448 |
existing_l3out_nodes = l3out_object.get("nodes", []) |
| 449 |
if pod_id and node_id: # Query a specific object |
| 450 |
return self.get_object_by_key_value_pairs("L3Out Node", existing_l3out_nodes, [KVPair("podID", pod_id), KVPair("nodeID", node_id)], fail_module) |
| 451 |
return existing_l3out_nodes # Query all objects |
| 452 |
|
| 453 |
def get_l3out_node_static_route(self, node_object, prefix, fail_module=False): |
| 454 |
""" |
| 455 |
Get the L3Out Node Static Route by prefix. |
| 456 |
:param node_object: L3Out Node object to search for the Static Route -> Dict |
| 457 |
:param prefix: Prefix of the Static Route to search for -> Str |
| 458 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 459 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 460 |
When the prefix is existing in the search list -> Dict |
| 461 |
When the prefix is not existing in the search list -> None |
| 462 |
When the prefix is None, and the search list is not empty -> List[Dict] |
| 463 |
When the prefix is None, and the search list is empty -> List[] |
| 464 |
""" |
| 465 |
existing_l3out_static_routes = node_object.get("staticRoutes", []) |
| 466 |
if prefix: # Query a specific object |
| 467 |
return self.get_object_by_key_value_pairs("L3Out Node Static Route", existing_l3out_static_routes, [KVPair("prefix", prefix)], fail_module) |
| 468 |
return existing_l3out_static_routes # Query all objects |
| 469 |
|
| 470 |
def get_l3out_node_static_route_next_hop(self, static_route_object, ip, fail_module=False): |
| 471 |
""" |
| 472 |
Get the L3Out Node Static Route Next Hop by IP. |
| 473 |
:param static_route_object: L3Out Node Static Route object to search for the Next Hop -> Dict |
| 474 |
:param ip: IP of the Static Route Next Hop to search for -> Str |
| 475 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 476 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 477 |
When the IP is existing in the search list -> Dict |
| 478 |
When the IP is not existing in the search list -> None |
| 479 |
When the IP is None, and the search list is not empty -> List[Dict] |
| 480 |
When the IP is None, and the search list is empty -> List[] |
| 481 |
""" |
| 482 |
existing_l3out_static_route_next_hops = static_route_object.get("nextHops", []) |
| 483 |
if ip: # Query a specific object |
| 484 |
return self.get_object_by_key_value_pairs( |
| 485 |
"L3Out Node Static Route Next Hop", existing_l3out_static_route_next_hops, [KVPair("nextHopIP", ip)], fail_module |
| 486 |
) |
| 487 |
return existing_l3out_static_route_next_hops # Query all objects |
| 488 |
|
| 489 |
def get_ipsla_track_list(self, uuid=None, name=None, fail_module=False): |
| 490 |
""" |
| 491 |
Get the IPSLA Track List by uuid or name. |
| 492 |
:param uuid: UUID of the IPSLA Track List to search for -> Str |
| 493 |
:param name: Name of the IPSLA Track List to search for -> Str |
| 494 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 495 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 496 |
When the UUID | Name is existing in the search list -> Dict |
| 497 |
When the UUID | Name is not existing in the search list -> None |
| 498 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 499 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 500 |
""" |
| 501 |
existing_ipsla_track_lists = self.template.get("tenantPolicyTemplate", {}).get("template", {}).get("ipslaTrackLists", []) |
| 502 |
if uuid or name: # Query a specific object |
| 503 |
return self.get_object_by_key_value_pairs( |
| 504 |
"IPSLA Track List", |
| 505 |
existing_ipsla_track_lists, |
| 506 |
[KVPair("uuid", uuid)] if uuid else [KVPair("name", name)], |
| 507 |
fail_module=fail_module, |
| 508 |
) |
| 509 |
return existing_ipsla_track_lists # Query all objects |
| 510 |
|
| 511 |
def get_l3out_secondary_address(self, parent_object, parent_type, secondary_address, side_b, fail_module=False): |
| 512 |
""" |
| 513 |
Get the L3Out Secondary Address by address. |
| 514 |
:param parent_object: The parent object to search for the secondary IP address -> Dict |
| 515 |
:param secondary_address: The secondary address to search for -> Str |
| 516 |
:param side_b: The side indicator for the SVI VPC parent object. |
| 517 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 518 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 519 |
When the address is existing in the search list -> Dict |
| 520 |
When the address is not existing in the search list -> None |
| 521 |
When the address is None, and the search list is not empty -> List[Dict] |
| 522 |
When the address is None, and the search list is empty -> List[] |
| 523 |
""" |
| 524 |
if parent_type == "floating_svi_path_attributes": |
| 525 |
existing_secondary_address = parent_object.get("secondaryAddresses", []) |
| 526 |
else: |
| 527 |
existing_secondary_address = parent_object.get("sideBAddresses" if side_b else "addresses", {}).get("secondary", []) |
| 528 |
|
| 529 |
if secondary_address: # Query a specific object |
| 530 |
kv_list = [KVPair("address", secondary_address)] |
| 531 |
return self.get_object_by_key_value_pairs("L3Out Secondary IP Address", existing_secondary_address, kv_list, fail_module) |
| 532 |
return existing_secondary_address # Query all objects |
| 533 |
|
| 534 |
def get_l3out_routed_interface(self, l3out_object, pod_id, node_id, path, path_ref, fail_module=False): |
| 535 |
""" |
| 536 |
Get the L3Out Routed Interface by pod_id, node_id, path, and path_ref. |
| 537 |
:param l3out_object: L3Out object to search for the Routed Interface -> Dict |
| 538 |
:param pod_id: Pod ID of the Routed Interface to search for -> Str |
| 539 |
:param node_id: Node ID of the Routed Interface to search for -> Str |
| 540 |
:param path: Path of the Routed Interface to search for -> Str |
| 541 |
:param path_ref: Path reference of the Routed Interface to search for -> Str |
| 542 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 543 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 544 |
When the pod_id, node_id, path | path_ref is existing in the search list -> Dict |
| 545 |
When the pod_id, node_id, path | path_ref is not existing in the search list -> None |
| 546 |
When both pod_id, node_id, path and path_ref are None, and the search list is not empty -> List[Dict] |
| 547 |
When both pod_id, node_id, path and path_ref are None, and the search list is empty -> List[] |
| 548 |
""" |
| 549 |
existing_l3out_interfaces = l3out_object.get("interfaces", []) |
| 550 |
if (pod_id and node_id and path) or path_ref: # Query a specific object |
| 551 |
if path_ref: |
| 552 |
kv_list = [KVPair("pathRef", path_ref)] |
| 553 |
else: |
| 554 |
kv_list = [KVPair("podID", pod_id), KVPair("nodeID", node_id), KVPair("path", path)] |
| 555 |
|
| 556 |
return self.get_object_by_key_value_pairs("L3Out Interface", existing_l3out_interfaces, kv_list, fail_module) |
| 557 |
return existing_l3out_interfaces # Query all objects |
| 558 |
|
| 559 |
def get_l3out_routed_sub_interface(self, l3out_object, pod_id, node_id, path, path_ref, encap, fail_module=False): |
| 560 |
""" |
| 561 |
Get the L3Out Routed Sub-Interface by pod_id, node_id, path, and path_ref. |
| 562 |
:param l3out_object: L3Out object to search for the Routed Sub-Interface -> Dict |
| 563 |
:param pod_id: Pod ID of the Routed Sub-Interface to search for -> Str |
| 564 |
:param node_id: Node ID of the Routed Sub-Interface to search for -> Str |
| 565 |
:param path: Path of the Routed Sub-Interface to search for -> Str |
| 566 |
:param path_ref: Path reference of the Routed Sub-Interface to search for -> Str |
| 567 |
:param encap: Encapsulation details of the Routed Sub-Interface to search for -> Dict |
| 568 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 569 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 570 |
When the pod_id, node_id, path | path_ref with encap is existing in the search list -> Dict |
| 571 |
When the pod_id, node_id, path | path_ref with encap is not existing in the search list -> None |
| 572 |
When both pod_id, node_id, path, path_ref, and encap are None, and the search list is not empty -> List[Dict] |
| 573 |
When both pod_id, node_id, path, path_ref, and encap are None, and the search list is empty -> List[] |
| 574 |
""" |
| 575 |
existing_l3out_interfaces = l3out_object.get("subInterfaces", []) |
| 576 |
if ((pod_id and node_id and path) or path_ref) and encap: # Query a specific object |
| 577 |
if path_ref: |
| 578 |
kv_list = [KVPair("pathRef", path_ref), KVPair("encap", encap)] |
| 579 |
else: |
| 580 |
kv_list = [KVPair("podID", pod_id), KVPair("nodeID", node_id), KVPair("path", path), KVPair("encap", encap)] |
| 581 |
|
| 582 |
return self.get_object_by_key_value_pairs("L3Out Sub-Interface", existing_l3out_interfaces, kv_list, fail_module) |
| 583 |
return existing_l3out_interfaces # Query all objects |
| 584 |
|
| 585 |
def get_l3out_svi_interface(self, l3out_object, pod_id, node_id, path, encap, path_ref, fail_module=False): |
| 586 |
""" |
| 587 |
Get the L3Out SVI Interface by pod_id, node_id, path, and path_ref. |
| 588 |
:param l3out_object: L3Out object to search for the SVI Interface -> Dict |
| 589 |
:param pod_id: Pod ID of the SVI Interface to search for -> Str |
| 590 |
:param node_id: Node ID of the SVI Interface to search for -> Str |
| 591 |
:param path: Path of the SVI Interface to search for -> Str |
| 592 |
:param encap: Encapsulation details of the Floating SVI Interface to search for -> Dict |
| 593 |
:param path_ref: Path reference of the SVI Interface to search for -> Str |
| 594 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 595 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 596 |
When the pod_id, node_id, path | path_ref is existing in the search list -> Dict |
| 597 |
When the pod_id, node_id, path | path_ref is not existing in the search list -> None |
| 598 |
When both pod_id, node_id, path and path_ref are None, and the search list is not empty -> List[Dict] |
| 599 |
When both pod_id, node_id, path and path_ref are None, and the search list is empty -> List[] |
| 600 |
""" |
| 601 |
existing_l3out_svi_interfaces = l3out_object.get("sviInterfaces", []) |
| 602 |
if encap and ((pod_id and node_id and path) or path_ref): # Query a specific object |
| 603 |
if path_ref: |
| 604 |
kv_list = [KVPair("pathRef", path_ref), KVPair("encap", encap)] |
| 605 |
else: |
| 606 |
kv_list = [KVPair("podID", pod_id), KVPair("nodeID", node_id), KVPair("path", path), KVPair("encap", encap)] |
| 607 |
|
| 608 |
return self.get_object_by_key_value_pairs("L3Out SVI Interface", existing_l3out_svi_interfaces, kv_list, fail_module) |
| 609 |
return existing_l3out_svi_interfaces # Query all objects |
| 610 |
|
| 611 |
def get_l3out_floating_svi_interface(self, l3out_object, pod_id, node_id, encap, fail_module=False): |
| 612 |
""" |
| 613 |
Get the L3Out Floating SVI Interface by pod_id and node_id. |
| 614 |
:param l3out_object: L3Out object to search for the Floating SVI Interface -> Dict |
| 615 |
:param pod_id: Pod ID of the Floating SVI Interface to search for -> Str |
| 616 |
:param node_id: Node ID of the Floating SVI Interface to search for -> Str |
| 617 |
:param encap: Encapsulation details of the Floating SVI Interface to search for -> Dict |
| 618 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 619 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 620 |
When the pod_id, node_id and encap are existing in the search list -> Dict |
| 621 |
When the pod_id, node_id and encap are not existing in the search list -> None |
| 622 |
When both pod_id, node_id, and encap are None, and the search list is not empty -> List[Dict] |
| 623 |
When both pod_id, node_id, and encap are None, and the search list is empty -> List[] |
| 624 |
""" |
| 625 |
existing_l3out_floating_svi_interfaces = l3out_object.get("floatingSviInterfaces", []) |
| 626 |
if pod_id and node_id and encap: # Query a specific object |
| 627 |
kv_list = [KVPair("podID", pod_id), KVPair("nodeID", node_id), KVPair("encap", encap)] |
| 628 |
|
| 629 |
return self.get_object_by_key_value_pairs("L3Out Floating SVI Interface", existing_l3out_floating_svi_interfaces, kv_list, fail_module) |
| 630 |
return existing_l3out_floating_svi_interfaces # Query all objects |
| 631 |
|
| 632 |
def get_l3out_floating_svi_interface_path_attributes(self, l3out_floating_svi_interface_object, domain_type, domain, fail_module=False): |
| 633 |
""" |
| 634 |
Get the L3Out Floating SVI Interface Path Attributes by domain_type and domain. |
| 635 |
:param l3out_floating_svi_interface_object: L3Out Floating SVI Interface object to search for the Path Attributes -> Dict |
| 636 |
:param domain_type: Domain type of the Path Attributes to search for -> Str |
| 637 |
:param domain: Domain of the Path Attributes to search for -> Str |
| 638 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 639 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 640 |
When the domain_type and domain are existing in the search list -> Dict |
| 641 |
When the domain_type and domain are not existing in the search list -> None |
| 642 |
When both domain_type and domain are None, and the search list is not empty -> List[Dict] |
| 643 |
When both domain_type and domain are None, and the search list is empty -> List[] |
| 644 |
""" |
| 645 |
existing_path_attributes = l3out_floating_svi_interface_object.get("svi", {}).get("floatingPathAttributes", []) |
| 646 |
if domain_type and domain: # Query a specific object |
| 647 |
kv_list = [KVPair("domainType", domain_type), KVPair("domain", domain)] |
| 648 |
return self.get_object_by_key_value_pairs("L3Out Floating SVI Interface Path Attributes", existing_path_attributes, kv_list, fail_module) |
| 649 |
return existing_path_attributes # Query all objects |
| 650 |
|
| 651 |
def get_node_settings_object(self, uuid=None, name=None, fail_module=False): |
| 652 |
""" |
| 653 |
Get the Fabric Node Settings by uuid or name. |
| 654 |
:param uuid: UUID of the Node Setting to search for -> Str |
| 655 |
:param name: Name of the Node Setting to search for -> Str |
| 656 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 657 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 658 |
When the UUID | Name is existing in the search list -> Dict |
| 659 |
When the UUID | Name is not existing in the search list -> None |
| 660 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 661 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 662 |
""" |
| 663 |
existing_objects = self.template.get("fabricPolicyTemplate", {}).get("template", {}).get("nodePolicyGroups", []) |
| 664 |
if uuid or name: # Query a specific object |
| 665 |
return self.get_object_by_key_value_pairs("Node Settings", existing_objects, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 666 |
return existing_objects # Query all objects |
| 667 |
|
| 668 |
def get_pod_profile_object(self, uuid=None, name=None, search_object=None, fail_module=False): |
| 669 |
""" |
| 670 |
Get the Pod Profile by uuid or name. |
| 671 |
:param uuid: UUID of the Pod Profile to search for -> Str |
| 672 |
:param name: Name of the Pod Profile to search for -> Str |
| 673 |
:param search_object: The object to search in -> Dict |
| 674 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 675 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 676 |
When the UUID | Name is existing in the search list -> Dict |
| 677 |
When the UUID | Name is not existing in the search list -> None |
| 678 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 679 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 680 |
""" |
| 681 |
if not search_object: |
| 682 |
search_object = self.template |
| 683 |
existing_objects = search_object.get("fabricResourceTemplate", {}).get("template", {}).get("podProfiles", []) |
| 684 |
if uuid or name: # Query a specific object |
| 685 |
return self.get_object_by_key_value_pairs("Pod Profile", existing_objects, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 686 |
return existing_objects # Query all objects |
| 687 |
|
| 688 |
def get_pod_settings_object(self, uuid=None, name=None, search_object=None, fail_module=False): |
| 689 |
""" |
| 690 |
Get the Pod Settings by uuid or name. |
| 691 |
:param uuid: UUID of the Pod Settings to search for -> Str |
| 692 |
:param name: Name of the Pod Settings to search for -> Str |
| 693 |
:param search_object: The object to search in -> Dict |
| 694 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 695 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 696 |
When the UUID | Name is existing in the search list -> Dict |
| 697 |
When the UUID | Name is not existing in the search list -> None |
| 698 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 699 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 700 |
""" |
| 701 |
if not search_object: |
| 702 |
search_object = self.template |
| 703 |
existing_objects = search_object.get("fabricPolicyTemplate", {}).get("template", {}).get("podPolicyGroups", []) |
| 704 |
if uuid or name: # Query a specific object |
| 705 |
return self.get_object_by_key_value_pairs("Pod Settings", existing_objects, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 706 |
return existing_objects # Query all objects |
| 707 |
|
| 708 |
def get_ntp_policy_object(self, uuid=None, name=None, search_object=None, fail_module=False): |
| 709 |
""" |
| 710 |
Get the NTP Policy by uuid or name. |
| 711 |
:param uuid: UUID of the NTP Policy to search for -> Str |
| 712 |
:param name: Name of the NTP Policy to search for -> Str |
| 713 |
:param search_object: The object to search in -> Dict |
| 714 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 715 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 716 |
When the UUID | Name is existing in the search list -> Dict |
| 717 |
When the UUID | Name is not existing in the search list -> None |
| 718 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 719 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 720 |
""" |
| 721 |
if not search_object: |
| 722 |
search_object = self.template |
| 723 |
existing_objects = search_object.get("fabricPolicyTemplate", {}).get("template", {}).get("ntpPolicies", []) |
| 724 |
if uuid or name: # Query a specific object |
| 725 |
return self.get_object_by_key_value_pairs("NTP Policy", existing_objects, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 726 |
return existing_objects # Query all objects |
| 727 |
|
| 728 |
def get_ptp_policy_profile_object(self, uuid=None, name=None, fail_module=False): |
| 729 |
# This object is an exception where the template can only contain a single PTP Policy, thus an additional layer of nested query is done |
| 730 |
""" |
| 731 |
Get the PTP Policy Profile by UUID or Name. |
| 732 |
:param uuid: UUID of the PTP Profile to search for -> Str |
| 733 |
:param name: Name of the PTP Profile to search for -> Str |
| 734 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 735 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 736 |
When the UUID | Name is existing in the search list -> Dict |
| 737 |
When the UUID | Name is not existing in the search list -> None |
| 738 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 739 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 740 |
""" |
| 741 |
existing_ptp_profile = self.template.get("fabricPolicyTemplate", {}).get("template", {}).get("ptpPolicy", {}).get("profiles", []) |
| 742 |
if uuid or name: # Query a specific object |
| 743 |
return self.get_object_by_key_value_pairs( |
| 744 |
"PTP Policy Profile", existing_ptp_profile, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module |
| 745 |
) |
| 746 |
return existing_ptp_profile # Query all objects |
| 747 |
|
| 748 |
def get_macsec_policy_object(self, uuid=None, name=None, search_object=None, fail_module=False): |
| 749 |
""" |
| 750 |
Get the MACsec Policy by uuid or name. |
| 751 |
:param uuid: UUID of the MACsec Policy to search for -> Str |
| 752 |
:param name: Name of the MACsec Policy to search for -> Str |
| 753 |
:param search_object: The object to search in -> Dict |
| 754 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 755 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 756 |
When the UUID | Name is existing in the search list -> Dict |
| 757 |
When the UUID | Name is not existing in the search list -> None |
| 758 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 759 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 760 |
""" |
| 761 |
if not search_object: |
| 762 |
search_object = self.template |
| 763 |
existing_objects = search_object.get("fabricPolicyTemplate", {}).get("template", {}).get("macsecPolicies", []) |
| 764 |
if uuid or name: # Query a specific object |
| 765 |
return self.get_object_by_key_value_pairs("MACsec Policy", existing_objects, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 766 |
return existing_objects # Query all objects |
| 767 |
|
| 768 |
def get_l3out_interface_routing_policy_object(self, uuid=None, name=None, fail_module=False): |
| 769 |
""" |
| 770 |
Get the L3Out Interface Routing Policy by UUID or Name. |
| 771 |
:param uuid: UUID of the L3Out Interface Routing Policy to search for -> Str |
| 772 |
:param name: Name of the L3Out Interface Routing Policy to search for -> Str |
| 773 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 774 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 775 |
When the UUID | Name is existing in the search list -> Dict |
| 776 |
When the UUID | Name is not existing in the search list -> None |
| 777 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 778 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 779 |
""" |
| 780 |
existing_l3out_interface_routing_policy = self.template.get("tenantPolicyTemplate", {}).get("template", {}).get("l3OutIntfPolGroups", []) |
| 781 |
if uuid or name: # Query a specific object |
| 782 |
return self.get_object_by_key_value_pairs( |
| 783 |
"L3Out Interface Routing Policy", |
| 784 |
existing_l3out_interface_routing_policy, |
| 785 |
[KVPair("uuid", uuid) if uuid else KVPair("name", name)], |
| 786 |
fail_module, |
| 787 |
) |
| 788 |
return existing_l3out_interface_routing_policy # Query all objects |
| 789 |
|
| 790 |
def get_match_rule_policy_object(self, uuid=None, name=None, search_object=None, fail_module=False): |
| 791 |
""" |
| 792 |
Get the Match Rule Policy by uuid or name. |
| 793 |
:param uuid: UUID of the Match Rule Policy to search for -> Str |
| 794 |
:param name: Name of the Match Rule Policy to search for -> Str |
| 795 |
:param search_object: The object to search in -> Dict |
| 796 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 797 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 798 |
When the UUID | Name is existing in the search list -> Dict |
| 799 |
When the UUID | Name is not existing in the search list -> None |
| 800 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 801 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 802 |
""" |
| 803 |
if not search_object: |
| 804 |
search_object = self.template |
| 805 |
existing_objects = search_object.get("tenantPolicyTemplate", {}).get("template", {}).get("matchRulePolicies", []) |
| 806 |
if uuid or name: # Query a specific object |
| 807 |
return self.get_object_by_key_value_pairs( |
| 808 |
"Match Rule Policy", |
| 809 |
existing_objects, |
| 810 |
[KVPair("uuid", uuid) if uuid else KVPair("name", name)], |
| 811 |
fail_module, |
| 812 |
) |
| 813 |
return existing_objects # Query all objects |
| 814 |
|
| 815 |
def get_endpoint_mac_tag_policy_object(self, uuid=None, mac=None, bd_uuid=None, vrf_uuid=None, search_object=None, fail_module=False): |
| 816 |
""" |
| 817 |
Get the Endpoint MAC Tag Policy by uuid or mac and bd_uuid or vrf_uuid. |
| 818 |
:param uuid: UUID of the Endpoint MAC Tag Policy to search for -> Str |
| 819 |
:param mac: The MAC address of the Endpoint MAC Tag Policy to search for -> Str |
| 820 |
:param bd_uuid: UUID of the Bridge Domain referenced by the Endpoint MAC Tag Policy to search for -> Str |
| 821 |
:param vrf_uuid: UUID of the VRF referenced by the Endpoint MAC Tag Policy to search for -> Str |
| 822 |
:param search_object: The object to search in -> Dict |
| 823 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 824 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 825 |
When the UUID | MAC address and (VRF UUID | Bridge Domain UUID) are existing in the search list -> Dict |
| 826 |
When the UUID | MAC address and (VRF UUID | Bridge Domain UUID) are not existing in the search list -> None |
| 827 |
When UUID is None and MAC address | (VRF UUID and Bridge Domain UUID) are None, and the search list is not empty -> List[Dict] |
| 828 |
When UUID and MAC address | (VRF UUID and Bridge Domain UUID) are None, and the search list is empty -> List[] |
| 829 |
""" |
| 830 |
kv_list = [] |
| 831 |
if not search_object: |
| 832 |
search_object = self.template |
| 833 |
existing_objects = search_object.get("tenantPolicyTemplate", {}).get("template", {}).get("endpointMacTagPolicies", []) |
| 834 |
if uuid: |
| 835 |
kv_list = [KVPair("uuid", uuid)] |
| 836 |
elif mac and bd_uuid: |
| 837 |
kv_list = [KVPair("mac", mac), KVPair("bdRef", bd_uuid)] |
| 838 |
elif mac and vrf_uuid: |
| 839 |
kv_list = [KVPair("mac", mac), KVPair("vrfRef", vrf_uuid)] |
| 840 |
if kv_list: |
| 841 |
return self.get_object_by_key_value_pairs("Endpoint MAC Tag Policy", existing_objects, kv_list, fail_module) |
| 842 |
return existing_objects # Query all objects |
| 843 |
|
| 844 |
def get_endpoint_ip_tag_policy_object(self, uuid=None, ip=None, vrf_uuid=None, search_object=None, fail_module=False): |
| 845 |
""" |
| 846 |
Get the Endpoint IP Tag Policy by uuid or ip and vrf_uuid. |
| 847 |
:param uuid: UUID of the Endpoint IP Tag Policy to search for -> Str |
| 848 |
:param ip: The IP address of the Endpoint IP Tag Policy to search for -> Str |
| 849 |
:param vrf_uuid: UUID of the VRF referenced by the Endpoint IP Tag Policy to search for -> Str |
| 850 |
:param search_object: The object to search in -> Dict |
| 851 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 852 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 853 |
When the UUID | IP address and VRF UUID are existing in the search list -> Dict |
| 854 |
When the UUID | IP address and VRF UUID are not existing in the search list -> None |
| 855 |
When UUID is None and IP address | VRF UUID is None, and the search list is not empty -> List[Dict] |
| 856 |
When UUID is None and IP address | VRF UUID is None, and the search list is empty -> List[] |
| 857 |
""" |
| 858 |
if not search_object: |
| 859 |
search_object = self.template |
| 860 |
existing_objects = search_object.get("tenantPolicyTemplate", {}).get("template", {}).get("endpointIPTagPolicies", []) |
| 861 |
if uuid or (ip and vrf_uuid): |
| 862 |
if uuid: |
| 863 |
kv_list = [KVPair("uuid", uuid)] |
| 864 |
else: |
| 865 |
kv_list = [KVPair("ip", ip), KVPair("vrfRef", vrf_uuid)] |
| 866 |
return self.get_object_by_key_value_pairs("Endpoint IP Tag Policy", existing_objects, kv_list, fail_module) |
| 867 |
return existing_objects # Query all objects |
| 868 |
|
| 869 |
def get_direct_child_object(self, parent_object, description, endpoint, identifiers=None, fail_module=False): |
| 870 |
""" |
| 871 |
Get the direct child object using its identifiers and its parent object. |
| 872 |
:param parent_object: Parent object data where to search the direct child object -> Dict |
| 873 |
:param description: Description of the child object to search for -> Str |
| 874 |
:param endpoint: NDO API child object's endpoint -> Str |
| 875 |
:param identifiers: child object's identifiers with coresponding identifier's name and value -> Dict |
| 876 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 877 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 878 |
When one of the child object identifiers is existing in the search list -> Dict |
| 879 |
When the child object identifiers are not existing in the search list -> None |
| 880 |
When the child object identifiers are None, and the search list is not empty -> List[Dict] |
| 881 |
When the child object identifiers ae None, and the search list is empty -> List[] |
| 882 |
""" |
| 883 |
if isinstance(identifiers, dict) and identifiers.values(): # Query a specific object |
| 884 |
for key, value in identifiers.items(): |
| 885 |
if value: |
| 886 |
child_object_kvpair = KVPair(key, value) |
| 887 |
return self.get_object_by_key_value_pairs( |
| 888 |
description, |
| 889 |
parent_object.details.get(endpoint, []), |
| 890 |
[child_object_kvpair], |
| 891 |
fail_module, |
| 892 |
) |
| 893 |
return parent_object.details.get(endpoint, []) # Query all objects |
| 894 |
|
| 895 |
def get_template_policy_uuid(self, template_type, policy_name, policy_type): |
| 896 |
""" |
| 897 |
Get the UUID of a Tenant Policy by name. |
| 898 |
:param template_type: The type of template -> Str |
| 899 |
:param policy_name: Name of the policy -> Str |
| 900 |
:param policy_type: The type of the policy specified in the API response -> Str |
| 901 |
:return: UUID of the tenant policy -> Str |
| 902 |
""" |
| 903 |
existing_policies = self.template.get(TEMPLATE_TYPES[template_type]["template_type_container"], {}).get("template", {}).get(policy_type, []) |
| 904 |
match = self.get_object_by_key_value_pairs(policy_type, existing_policies, [KVPair("name", policy_name)], fail_module=True) |
| 905 |
return match.details.get("uuid") |
| 906 |
|
| 907 |
def clear_template_objects_cache(self): |
| 908 |
self.template_objects_cache = {} |
| 909 |
|
| 910 |
def get_template_object_name_by_uuid(self, object_type, uuid, fail_module=True): |
| 911 |
""" |
| 912 |
Retrieve the name of a specific object type in the MSO template using its UUID. |
| 913 |
:param mso: An instance of the MSO class, which provides methods for making API requests -> MSO Class instance |
| 914 |
:param object_type: The type of the object to retrieve the name for -> Str |
| 915 |
:param uuid: The UUID of the object to retrieve the name for -> Str |
| 916 |
:return: Str | None: The processed result which could be: |
| 917 |
When the UUID is existing, returns object name -> Str |
| 918 |
When the UUID is not existing -> None |
| 919 |
""" |
| 920 |
response_object = self.get_template_object_by_uuid(object_type, uuid, fail_module) |
| 921 |
if response_object: |
| 922 |
return response_object.get("name") |
| 923 |
|
| 924 |
def get_template_object_by_uuid(self, object_type, uuid, fail_module=True, use_cache=False): |
| 925 |
""" |
| 926 |
Retrieve a specific object type in the MSO template using its UUID. |
| 927 |
:param object_type: The type of the object to retrieve -> Str |
| 928 |
:param uuid: The UUID of the object to retrieve -> Str |
| 929 |
:param use_cache: Use the cached result of the templates/objects API for the UUID -> Bool |
| 930 |
:return: Dict | None: The processed result which could be: |
| 931 |
When the UUID is existing, returns object -> Dict |
| 932 |
When the UUID is not existing -> None |
| 933 |
""" |
| 934 |
response_object = None |
| 935 |
if use_cache and uuid in self.template_objects_cache.keys(): |
| 936 |
response_object = self.template_objects_cache[uuid] |
| 937 |
else: |
| 938 |
response_object = self.mso.request("templates/objects?type={0}&uuid={1}".format(object_type, uuid), "GET") |
| 939 |
self.template_objects_cache[uuid] = response_object |
| 940 |
if not response_object and fail_module: |
| 941 |
msg = "Provided {0} with UUID of '{1}' not found.".format(object_type, uuid) |
| 942 |
self.mso.fail_json(msg=msg) |
| 943 |
return response_object |
| 944 |
|
| 945 |
def update_config_with_template_and_references(self, config_data, reference_collections=None, set_template=True, use_cache=False): |
| 946 |
""" |
| 947 |
Return the updated config_data with the template values and reference_collections if provided |
| 948 |
:param config_data: The original config_data that requires to be updated -> Dict |
| 949 |
:param reference_collections: A dict containing the object type, references and the corresponding names -> Dict |
| 950 |
:param set_template: Adds the templateId and templateName to the config_data -> Bool |
| 951 |
:param use_cache: Use the cached result of the templates/objects API for the ref UUID -> Bool |
| 952 |
:return: Updated config_data with names for references -> Dict |
| 953 |
Example 1: |
| 954 |
reference_collections = { |
| 955 |
"qos": { |
| 956 |
"name": "qosName", |
| 957 |
"reference": "qosRef", |
| 958 |
"type": "qos", |
| 959 |
"template": "qosTemplateName", |
| 960 |
"templateId": "qosTemplateId", |
| 961 |
}, |
| 962 |
"interfaceRoutingPolicy": { |
| 963 |
"name": "interfaceRoutingPolicyName", |
| 964 |
"reference": "interfaceRoutingPolicyRef", |
| 965 |
"type": "l3OutIntfPolGroup", |
| 966 |
"template": "interfaceRoutingPolicyTemplateName", |
| 967 |
"templateId": "interfaceRoutingPolicyTemplateId", |
| 968 |
}, |
| 969 |
} |
| 970 |
config_data = { |
| 971 |
"qosRef": "unique-qos-id", |
| 972 |
"interfaceRoutingPolicyRef": "unique-interface-id" |
| 973 |
} |
| 974 |
updated_config_data = mso_template_object.update_config_with_template_and_references(mso_instance, config_data, reference_collections) |
| 975 |
Expected Output: |
| 976 |
{ "templateName": "template_name", |
| 977 |
"templateId": "unique-template-id", |
| 978 |
"qosRef": "unique-qos-id", |
| 979 |
"interfaceRoutingPolicyRef": "unique-interface-id", |
| 980 |
"qosName": "Resolved QoS Name", |
| 981 |
"qosTemplateName": "Resolved QoS Template Name", |
| 982 |
"qosTemplateId": "Resolved QoS Template ID", |
| 983 |
"interfaceRoutingPolicyName": "Resolved Interface Routing Policy Name", |
| 984 |
"interfaceRoutingPolicyTemplateName": "Resolved Interface Routing Policy Template Name", |
| 985 |
"interfaceRoutingPolicyTemplateId": "Resolved Interface Routing Policy Template ID" |
| 986 |
} |
| 987 |
Example 2: |
| 988 |
reference_collections = { |
| 989 |
"stateLimitRouteMap": { |
| 990 |
"name": "stateLimitRouteMapName", |
| 991 |
"reference": "stateLimitRouteMapRef", |
| 992 |
"type": "mcastRouteMap" |
| 993 |
}, |
| 994 |
"reportPolicyRouteMap": { |
| 995 |
"name": "reportPolicyRouteMapName", |
| 996 |
"reference": "reportPolicyRouteMapRef", |
| 997 |
"type": "mcastRouteMap" |
| 998 |
}, |
| 999 |
"staticReportRouteMap": { |
| 1000 |
"name": "staticReportRouteMapName", |
| 1001 |
"reference": "staticReportRouteMapRef", |
| 1002 |
"type": "mcastRouteMap" |
| 1003 |
}, |
| 1004 |
} |
| 1005 |
config_data = { |
| 1006 |
"stateLimitRouteMapRef": "unique-state-limit-id", |
| 1007 |
"reportPolicyRouteMapRef": "unique-report-policy-id" |
| 1008 |
} |
| 1009 |
updated_config_data = mso_template_object.update_config_with_template_and_references(mso_instance, config_data, reference_collections) |
| 1010 |
Expected Output: |
| 1011 |
{ "templateName": "template_name", |
| 1012 |
"templateId": "unique-template-id", |
| 1013 |
"stateLimitRouteMapRef": "unique-state-limit-id", |
| 1014 |
"reportPolicyRouteMapRef": "unique-report-policy-id", |
| 1015 |
"stateLimitRouteMapName": "Resolved State Limit Route Map Name", |
| 1016 |
"reportPolicyRouteMapName": "Resolved Report Policy Route Map Name" |
| 1017 |
} |
| 1018 |
""" |
| 1019 |
|
| 1020 |
# Set template ID and template name if available |
| 1021 |
if set_template: |
| 1022 |
if self.template_id: |
| 1023 |
config_data["templateId"] = self.template_id |
| 1024 |
if self.template_name: |
| 1025 |
config_data["templateName"] = self.template_name |
| 1026 |
if self.schema_id: |
| 1027 |
config_data["schemaId"] = self.schema_id |
| 1028 |
if self.schema_name: |
| 1029 |
config_data["schemaName"] = self.schema_name |
| 1030 |
|
| 1031 |
# Update config data with reference names if reference_collections is provided |
| 1032 |
if reference_collections: |
| 1033 |
for reference_details in reference_collections.values(): |
| 1034 |
if config_data.get(reference_details.get("reference")): |
| 1035 |
template_object = self.get_template_object_by_uuid( |
| 1036 |
reference_details.get("type"), config_data.get(reference_details.get("reference")), True, use_cache |
| 1037 |
) |
| 1038 |
config_data[reference_details.get("name")] = template_object.get("name") |
| 1039 |
if reference_details.get("template"): |
| 1040 |
config_data[reference_details.get("template")] = template_object.get("templateName") |
| 1041 |
if reference_details.get("templateId"): |
| 1042 |
config_data[reference_details.get("templateId")] = template_object.get("templateId") |
| 1043 |
if reference_details.get("schemaId"): |
| 1044 |
config_data[reference_details.get("schemaId")] = template_object.get("schemaId") |
| 1045 |
if reference_details.get("schema"): |
| 1046 |
config_data[reference_details.get("schema")] = template_object.get("schemaName") |
| 1047 |
for config_value in config_data.values(): |
| 1048 |
if isinstance(config_value, dict): |
| 1049 |
self.update_config_with_template_and_references(config_value, reference_collections, False, use_cache) |
| 1050 |
elif isinstance(config_value, list): |
| 1051 |
for item in config_value: |
| 1052 |
if isinstance(item, dict): |
| 1053 |
self.update_config_with_template_and_references(item, reference_collections, False, use_cache) |
| 1054 |
return config_data |
| 1055 |
|
| 1056 |
def update_config_with_port_channel_references(self, update_object): |
| 1057 |
if update_object: |
| 1058 |
reference_details = None |
| 1059 |
if update_object.get("pathRef"): |
| 1060 |
if update_object.get("pathType") == "vpc": |
| 1061 |
reference_details = { |
| 1062 |
"virtual_port_channel_reference": { |
| 1063 |
"name": "virtualPortChannelName", |
| 1064 |
"reference": "pathRef", |
| 1065 |
"type": "virtualPortChannel", |
| 1066 |
"template": "virtualPortChannelTemplateName", |
| 1067 |
"templateId": "virtualPortChannelTemplateId", |
| 1068 |
} |
| 1069 |
} |
| 1070 |
elif update_object.get("pathType") == "pc": |
| 1071 |
reference_details = { |
| 1072 |
"port_channel_reference": { |
| 1073 |
"name": "portChannelName", |
| 1074 |
"reference": "pathRef", |
| 1075 |
"type": "portChannel", |
| 1076 |
"template": "portChannelTemplateName", |
| 1077 |
"templateId": "portChannelTemplateId", |
| 1078 |
} |
| 1079 |
} |
| 1080 |
self.update_config_with_template_and_references( |
| 1081 |
update_object, |
| 1082 |
reference_details, |
| 1083 |
True, |
| 1084 |
) |
| 1085 |
|
| 1086 |
def update_config_with_node_references(self, interface, l3out_object): |
| 1087 |
|
| 1088 |
pod_id = interface.get("podID") |
| 1089 |
node_id = interface.get("nodeID") |
| 1090 |
node_id_2 = None |
| 1091 |
|
| 1092 |
if interface.get("pathType") == "pc": |
| 1093 |
interface_details = self.mso.get_site_interface_details( |
| 1094 |
self.template.get("l3outTemplate", {}).get("siteId"), |
| 1095 |
port_channel_uuid=interface.get("pathRef"), |
| 1096 |
) |
| 1097 |
pod_id = interface_details.get("pod") |
| 1098 |
node_id = interface_details.get("node") |
| 1099 |
elif interface.get("pathType") == "vpc": |
| 1100 |
interface_details = self.mso.get_site_interface_details( |
| 1101 |
self.template.get("l3outTemplate", {}).get("siteId"), |
| 1102 |
virtual_port_channel_uuid=interface.get("pathRef"), |
| 1103 |
) |
| 1104 |
pod_id = interface_details.get("pod") |
| 1105 |
node_id = interface_details.get("node1") |
| 1106 |
node_id_2 = interface_details.get("node2") |
| 1107 |
|
| 1108 |
node = self.get_l3out_node(l3out_object.details, pod_id, node_id) |
| 1109 |
if node and not isinstance(node, list): |
| 1110 |
interface["node"] = node.details |
| 1111 |
|
| 1112 |
if node_id_2: |
| 1113 |
node_2 = self.get_l3out_node(l3out_object.details, pod_id, node_id_2) |
| 1114 |
if node_2 and not isinstance(node_2, list): |
| 1115 |
interface["sideBNode"] = node_2.details |
| 1116 |
|
| 1117 |
def update_config_with_ptp_references(self, routed_interface, mso_templates): |
| 1118 |
ptp = routed_interface.get("ptpConfig") |
| 1119 |
if ptp: |
| 1120 |
reference_details = { |
| 1121 |
"ptp_profile_reference": { |
| 1122 |
"name": "ptpProfileName", |
| 1123 |
"reference": "ptpProfileRef", |
| 1124 |
"type": "ptpProfile", |
| 1125 |
"template": "ptpProfileTemplateName", |
| 1126 |
"templateId": "ptpProfileTemplateId", |
| 1127 |
} |
| 1128 |
} |
| 1129 |
self.update_config_with_template_and_references( |
| 1130 |
ptp, |
| 1131 |
reference_details, |
| 1132 |
False, |
| 1133 |
) |
| 1134 |
# The PTP policy details cannot be updated via update_config_with_template_and_references because the object type is not supported. |
| 1135 |
# The PTP policy details cannot be updated because the uuid is unknown and can only be retrieved from the template. |
| 1136 |
# Adding additional logic to populate the parent details in output. |
| 1137 |
ptpPolicy = ( |
| 1138 |
mso_templates.get_template( |
| 1139 |
"fabric_policy", |
| 1140 |
ptp.get("ptpProfileTemplateName"), |
| 1141 |
ptp.get("ptpProfileTemplateId"), |
| 1142 |
fail_module=True, |
| 1143 |
) |
| 1144 |
.template.get("fabricPolicyTemplate", {}) |
| 1145 |
.get("template", {}) |
| 1146 |
.get("ptpPolicy", {}) |
| 1147 |
) |
| 1148 |
routed_interface["ptpConfig"]["ptpPolicyName"] = ptpPolicy.get("name") |
| 1149 |
routed_interface["ptpConfig"]["ptpPolicyRef"] = ptpPolicy.get("uuid") |
| 1150 |
|
| 1151 |
def update_match_rule_policy_child_object_with_template_and_parent(self, match_rule_policy, config_data): |
| 1152 |
""" |
| 1153 |
Return the updated Match Rule Policy child object config_data with the template and policy values |
| 1154 |
:param config_data: The Match Rule Policy data -> Dict |
| 1155 |
:param config_data: The original config_data that requires to be updated -> Dict |
| 1156 |
:return: Updated config_data with names and ids for template and Match Rule Policy -> Dict |
| 1157 |
""" |
| 1158 |
self.update_config_with_template_and_references(config_data) |
| 1159 |
if match_rule_policy.get("uuid"): |
| 1160 |
config_data["matchRulePolicyUuid"] = match_rule_policy["uuid"] |
| 1161 |
if match_rule_policy.get("name"): |
| 1162 |
config_data["matchRulePolicyName"] = match_rule_policy["name"] |
| 1163 |
return config_data |
| 1164 |
|
| 1165 |
def get_route_map_policy_for_multicast_uuid(self, route_map_policy_for_multicast_name): |
| 1166 |
""" |
| 1167 |
Get the UUID of an Route Map Policy for Multicast by name. |
| 1168 |
:param route_map_policy_for_multicast_name: Name of the Route Map Policy for Multicast to search for -> Str |
| 1169 |
:return: UUID of the Route Map Policy for Multicast. -> Str |
| 1170 |
""" |
| 1171 |
existing_route_map_policies = self.template.get("tenantPolicyTemplate", {}).get("template", {}).get("mcastRouteMapPolicies", []) |
| 1172 |
kv_list = [KVPair("name", route_map_policy_for_multicast_name)] |
| 1173 |
match = self.get_object_by_key_value_pairs("Route Map Policy for Multicast", existing_route_map_policies, kv_list, fail_module=True) |
| 1174 |
return match.details.get("uuid") |
| 1175 |
|
| 1176 |
def get_fabric_template_object_by_key_value(self, object_type, object_description, kv_list, fail_module=False): |
| 1177 |
""" |
| 1178 |
Get the Fabric Policy by policy type and search criteria. |
| 1179 |
The search criteria could be name and UUID of the object. |
| 1180 |
:param object_type: The type of the object to retrieve the name for -> Str |
| 1181 |
:param object_description: Description of the object to search for -> Str |
| 1182 |
:param kv_list: Key/value pairs that should match in the object. -> List[KVPair(Str, Str)] |
| 1183 |
:param fail_module: When match is not found fail the ansible module. -> Bool |
| 1184 |
:return: Dict | None: The processed result which could be: |
| 1185 |
When the object is existing in the search list -> Dict |
| 1186 |
When the object is not existing -> None |
| 1187 |
""" |
| 1188 |
response_object = self.mso.request("getfabricpolicies?type={0}".format(object_type), "GET") |
| 1189 |
search_list = response_object.get("items", [{"spec": {"policies": []}}])[0].get("spec", {}).get("policies", []) |
| 1190 |
match = self.get_object_by_key_value_pairs(object_description, search_list, kv_list, fail_module) |
| 1191 |
if match: |
| 1192 |
return match.details |
| 1193 |
|
| 1194 |
def get_fabric_span_session(self, uuid=None, name=None, fail_module=False): |
| 1195 |
""" |
| 1196 |
Get the Fabric SPAN Session by uuid or name. |
| 1197 |
:param uuid: UUID of the Fabric SPAN Session to search for -> Str |
| 1198 |
:param name: Name of the Fabric SPAN Session to search for -> Str |
| 1199 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 1200 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 1201 |
When the UUID | Name is existing in the search list -> Dict |
| 1202 |
When the UUID | Name is not existing in the search list -> None |
| 1203 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 1204 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 1205 |
""" |
| 1206 |
existing_objects = self.template.get("monitoringTemplate", {}).get("template", {}).get("spanSessions", []) |
| 1207 |
if uuid or name: # Query a specific object |
| 1208 |
return self.get_object_by_key_value_pairs("SPAN Session", existing_objects, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 1209 |
return existing_objects # Query all objects |
| 1210 |
|
| 1211 |
def get_fabric_span_session_source(self, name, search_list, fail_module=False): |
| 1212 |
""" |
| 1213 |
Get the Fabric SPAN Session Source by name. |
| 1214 |
:param name: Name of the Fabric SPAN Session Source to search for -> Str |
| 1215 |
:param search_list: Objects to search through -> List. |
| 1216 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 1217 |
:return: Dict | List[Dict] | List[]: The processed result which could be: |
| 1218 |
When the Name is existing in the search list -> Dict |
| 1219 |
When both Name is None, and the search list is not empty -> List[Dict] |
| 1220 |
When both Name is None, and the search list is empty -> List[] |
| 1221 |
""" |
| 1222 |
if name and search_list: # Query a specific object |
| 1223 |
return self.get_object_by_key_value_pairs("SPAN Session Source", search_list, [KVPair("name", name)], fail_module) |
| 1224 |
return search_list # Query all objects |
| 1225 |
|
| 1226 |
def get_application_template_contract(self, uuid=None, name=None, fail_module=False): |
| 1227 |
""" |
| 1228 |
Get the Application Template Contract by uuid or name. |
| 1229 |
:param uuid: UUID of the Contract to search for -> Str |
| 1230 |
:param name: Name of the Contract to search for -> Str |
| 1231 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 1232 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 1233 |
When the UUID | Name is existing in the search list -> Dict |
| 1234 |
When the UUID | Name is not existing in the search list -> None |
| 1235 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 1236 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 1237 |
""" |
| 1238 |
existing_objects = self.template.get("appTemplate", {}).get("template", {}).get("contracts", []) |
| 1239 |
if uuid or name: # Query a specific object |
| 1240 |
return self.get_object_by_key_value_pairs( |
| 1241 |
"Template Contract", existing_objects, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module |
| 1242 |
) |
| 1243 |
return existing_objects # Query all objects |
| 1244 |
|
| 1245 |
def get_parent_details_for_nested_object_in_l3out(self, mso_templates, l3out_object): |
| 1246 |
""" |
| 1247 |
Get the parent details for a nested object in the L3Out object. |
| 1248 |
:param mso_templates: The MSO templates to search through -> List[Dict] |
| 1249 |
:param l3out_object: The L3Out object to find the parent for -> Dict |
| 1250 |
:return: The parent details for the nested object -> Dict, Str | None, None |
| 1251 |
""" |
| 1252 |
port_channel_uuid = virtual_port_channel_uuid = pod_id = encap = parent_match = parent_path = None |
| 1253 |
parent_type = self.mso.params.get("parent_type") |
| 1254 |
node_group = self.mso.params.get("node_group") |
| 1255 |
node_id = self.mso.params.get("node_id") |
| 1256 |
path = self.mso.params.get("path") |
| 1257 |
port_channel = self.get_port_channel_match(self.mso.params.get("port_channel"), mso_templates) |
| 1258 |
if port_channel: |
| 1259 |
port_channel_uuid = port_channel.get("uuid") |
| 1260 |
virtual_port_channel = self.get_virtual_port_channel_match(self.mso.params.get("virtual_port_channel"), mso_templates) |
| 1261 |
if virtual_port_channel: |
| 1262 |
virtual_port_channel_uuid = virtual_port_channel.get("uuid") |
| 1263 |
encapsulation_type = self.mso.params.get("encapsulation_type") |
| 1264 |
encapsulation_value = self.mso.params.get("encapsulation_value") |
| 1265 |
domain_type = DOMAIN_TYPE_MAP.get(self.mso.params.get("domain_type")) |
| 1266 |
domain_provider = self.mso.params.get("domain_provider") |
| 1267 |
if domain_type == "physicalDomain": |
| 1268 |
domain = "uni/phys-{0}".format(self.mso.params.get("domain")) |
| 1269 |
elif domain_type == "vmmDomain": |
| 1270 |
domain = "uni/vmmp-{0}/dom-{1}".format(VM_DOMAIN_PROVIDER_MAP.get(self.mso.params.get("domain_provider")), self.mso.params.get("domain")) |
| 1271 |
|
| 1272 |
if node_id or path or port_channel or virtual_port_channel: |
| 1273 |
pod_id = self.mso.get_site_interface_details( |
| 1274 |
site_id=self.template.get("l3outTemplate", {}).get("siteId"), |
| 1275 |
node=node_id, |
| 1276 |
port=path, |
| 1277 |
port_channel_uuid=port_channel_uuid, |
| 1278 |
virtual_port_channel_uuid=virtual_port_channel_uuid, |
| 1279 |
) |
| 1280 |
if path or port_channel or virtual_port_channel: |
| 1281 |
pod_id = pod_id.get("pod") |
| 1282 |
|
| 1283 |
if encapsulation_type and encapsulation_value: |
| 1284 |
encap = {"encapType": encapsulation_type, "value": encapsulation_value} |
| 1285 |
|
| 1286 |
if parent_type == "node_group": |
| 1287 |
parent_match = self.get_l3out_node_group(node_group, l3out_object.details, fail_module=True) |
| 1288 |
parent_path = "/l3outTemplate/l3outs/{0}/nodeGroups/{1}".format(l3out_object.index, parent_match.index if parent_match else "-") |
| 1289 |
elif parent_type == "floating_svi": |
| 1290 |
parent_match = self.get_l3out_floating_svi_interface(l3out_object.details, pod_id, node_id, encap, fail_module=True) |
| 1291 |
parent_path = "/l3outTemplate/l3outs/{0}/floatingSviInterfaces/{1}".format(l3out_object.index, parent_match.index if parent_match else "-") |
| 1292 |
elif parent_type == "routed": |
| 1293 |
parent_match = self.get_l3out_routed_interface(l3out_object.details, pod_id, node_id, path, port_channel_uuid, fail_module=True) |
| 1294 |
parent_path = "/l3outTemplate/l3outs/{0}/interfaces/{1}".format(l3out_object.index, parent_match.index if parent_match else "-") |
| 1295 |
elif parent_type == "routed_sub": |
| 1296 |
parent_match = self.get_l3out_routed_sub_interface(l3out_object.details, pod_id, node_id, path, port_channel_uuid, encap, fail_module=True) |
| 1297 |
parent_path = "/l3outTemplate/l3outs/{0}/subInterfaces/{1}".format(l3out_object.index, parent_match.index if parent_match else "-") |
| 1298 |
elif parent_type == "svi": |
| 1299 |
parent_match = self.get_l3out_svi_interface( |
| 1300 |
l3out_object.details, pod_id, node_id, path, encap, port_channel_uuid or virtual_port_channel_uuid, fail_module=True |
| 1301 |
) |
| 1302 |
parent_path = "/l3outTemplate/l3outs/{0}/sviInterfaces/{1}".format(l3out_object.index, parent_match.index if parent_match else "-") |
| 1303 |
elif parent_type == "floating_svi_path_attributes": |
| 1304 |
floating_svi_object = self.get_l3out_floating_svi_interface(l3out_object.details, pod_id, node_id, encap, True) |
| 1305 |
parent_match = self.get_l3out_floating_svi_interface_path_attributes(floating_svi_object.details, domain_type, domain, fail_module=True) |
| 1306 |
parent_path = "/l3outTemplate/l3outs/{0}/floatingSviInterfaces/{1}/svi/floatingPathAttributes/{2}".format( |
| 1307 |
l3out_object.index, floating_svi_object.index, parent_match.index if parent_match else "-" |
| 1308 |
) |
| 1309 |
|
| 1310 |
return parent_match, parent_path |
| 1311 |
|
| 1312 |
def set_parent_details_for_nested_object_in_l3out(self, parent_type, parent_object, update_object): |
| 1313 |
""" |
| 1314 |
Set the parent details for a nested object in the L3Out object. |
| 1315 |
:param parent_type: The type of the parent object -> Str |
| 1316 |
:param parent_object: The parent object details -> Dict |
| 1317 |
:param update_object: The object to update with parent details -> Dict |
| 1318 |
""" |
| 1319 |
parent_output_prepend = "" |
| 1320 |
if parent_type == "node_group": |
| 1321 |
parent_output_prepend = "nodeGroup" |
| 1322 |
elif parent_type == "floating_svi": |
| 1323 |
parent_output_prepend = "floatingSviInterface" |
| 1324 |
elif parent_type == "routed": |
| 1325 |
parent_output_prepend = "routedInterface" |
| 1326 |
elif parent_type == "routed_sub": |
| 1327 |
parent_output_prepend = "routedSubInterface" |
| 1328 |
elif parent_type == "svi": |
| 1329 |
parent_output_prepend = "sviInterface" |
| 1330 |
elif parent_type == "floating_svi_path_attributes": |
| 1331 |
parent_output_prepend = "floatingPathAttributes" |
| 1332 |
|
| 1333 |
pod_id = parent_object.get("podID") |
| 1334 |
if pod_id: |
| 1335 |
update_object["{0}PodID".format(parent_output_prepend)] = pod_id |
| 1336 |
|
| 1337 |
node_id = parent_object.get("nodeID") |
| 1338 |
if node_id: |
| 1339 |
update_object["{0}NodeID".format(parent_output_prepend)] = node_id |
| 1340 |
|
| 1341 |
path_type = parent_object.get("pathType") |
| 1342 |
if path_type: |
| 1343 |
update_object["{0}PathType".format(parent_output_prepend)] = path_type |
| 1344 |
|
| 1345 |
encap = parent_object.get("encap") |
| 1346 |
if encap: |
| 1347 |
update_object["{0}Encap".format(parent_output_prepend)] = encap |
| 1348 |
|
| 1349 |
path_ref = parent_object.get("pathRef") |
| 1350 |
if path_ref: |
| 1351 |
# Add pathType and pathRef to the update_object in order to resolve the port_channel or virtual_port_channel |
| 1352 |
update_object["pathType"] = path_type |
| 1353 |
update_object["pathRef"] = path_ref |
| 1354 |
self.update_config_with_port_channel_references(update_object) |
| 1355 |
update_object.pop("pathType", None) |
| 1356 |
update_object.pop("pathRef", None) |
| 1357 |
update_object["{0}PathRef".format(parent_output_prepend)] = path_ref |
| 1358 |
|
| 1359 |
path = parent_object.get("path") |
| 1360 |
if path: |
| 1361 |
update_object["{0}Path".format(parent_output_prepend)] = path |
| 1362 |
|
| 1363 |
name = parent_object.get("name") |
| 1364 |
if name: |
| 1365 |
update_object["{0}Name".format(parent_output_prepend)] = name |
| 1366 |
|
| 1367 |
domain = parent_object.get("domain") |
| 1368 |
if domain: |
| 1369 |
update_object["{0}Domain".format(parent_output_prepend)] = domain |
| 1370 |
|
| 1371 |
def get_route_map_policy(self, uuid=None, name=None, template_object=None, fail_module=False): |
| 1372 |
""" |
| 1373 |
Get the Route Map Policy for Route Control by UUID or Name. |
| 1374 |
:param uuid: UUID of the Route Map Policy for Route Control to search for -> Str |
| 1375 |
:param name: Name of the Route Map Policy for Route Control to search for -> Str |
| 1376 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 1377 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 1378 |
When the UUID | Name is existing in the search list -> Dict |
| 1379 |
When the UUID | Name is not existing in the search list -> None |
| 1380 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 1381 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 1382 |
""" |
| 1383 |
template_object = template_object if template_object else self.template |
| 1384 |
match = template_object.get("tenantPolicyTemplate", {}).get("template", {}).get("routeMapPolicies", []) |
| 1385 |
if uuid or name: # Query a specific object |
| 1386 |
return self.get_object_by_key_value_pairs( |
| 1387 |
"Route Map Policy for Route Control", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module |
| 1388 |
) |
| 1389 |
return match # Query all objects |
| 1390 |
|
| 1391 |
def get_set_rule_policy_object(self, uuid=None, name=None, search_object=None, fail_module=False): |
| 1392 |
""" |
| 1393 |
Get the Set Rule Policy by uuid or name. |
| 1394 |
:param uuid: UUID of the Set Rule Policy to search for -> Str |
| 1395 |
:param name: Name of the Set Rule Policy to search for -> Str |
| 1396 |
:param search_object: The object to search in -> Dict |
| 1397 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 1398 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 1399 |
When the UUID | Name is existing in the search list -> Dict |
| 1400 |
When the UUID | Name is not existing in the search list -> None |
| 1401 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 1402 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 1403 |
""" |
| 1404 |
if not search_object: |
| 1405 |
search_object = self.template |
| 1406 |
|
| 1407 |
existing_objects = search_object.get("tenantPolicyTemplate", {}).get("template", {}).get("setRulePolicies", []) |
| 1408 |
if uuid or name: # Query a specific object |
| 1409 |
return self.get_object_by_key_value_pairs( |
| 1410 |
"Set Rule Policy", |
| 1411 |
existing_objects, |
| 1412 |
[KVPair("uuid", uuid) if uuid else KVPair("name", name)], |
| 1413 |
fail_module, |
| 1414 |
) |
| 1415 |
|
| 1416 |
return existing_objects # Query all objects |
| 1417 |
|
| 1418 |
def get_netflow_record(self, uuid=None, name=None, search_object=None, fail_module=False): |
| 1419 |
""" |
| 1420 |
Get the NetFlow Record by uuid or name. |
| 1421 |
:param uuid: UUID of the NetFlow Record to search for -> Str |
| 1422 |
:param name: Name of the NetFlow Record to search for -> Str |
| 1423 |
:param search_object: The object to search in -> Dict |
| 1424 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 1425 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 1426 |
When the UUID | Name is existing in the search list -> Dict |
| 1427 |
When the UUID | Name is not existing in the search list -> None |
| 1428 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 1429 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 1430 |
""" |
| 1431 |
search_object = search_object if search_object else self.template |
| 1432 |
match = search_object.get("tenantPolicyTemplate", {}).get("template", {}).get("netFlowRecords", []) |
| 1433 |
if uuid or name: # Query a specific object |
| 1434 |
return self.get_object_by_key_value_pairs("NetFlow Record", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 1435 |
return match # Query all objects |