From 8f03fc8867db9554186bce1f0dfa36ab8e8d58bc Mon Sep 17 00:00:00 2001 From: samitab Date: Sun, 26 Jan 2025 20:39:58 +1000 Subject: [PATCH] [ignore] Refactor ipsla_track_list to use existing funtions and created shared ipsla monitoring policy function. --- plugins/module_utils/template.py | 22 +++++++ .../modules/ndo_ipsla_monitoring_policy.py | 16 ++--- plugins/modules/ndo_ipsla_track_list.py | 60 ++++--------------- .../ndo_ipsla_track_list/tasks/main.yml | 2 +- 4 files changed, 41 insertions(+), 59 deletions(-) diff --git a/plugins/module_utils/template.py b/plugins/module_utils/template.py index aaaab21da..f71d6e420 100644 --- a/plugins/module_utils/template.py +++ b/plugins/module_utils/template.py @@ -219,6 +219,28 @@ def get_interface_policy_group_uuid(self, interface_policy_group): match = self.get_object_by_key_value_pairs("Interface Policy Groups", existing_policy_groups, kv_list, fail_module=True) return match.details.get("uuid") + def get_ipsla_monitoring_policy(self, uuid=None, name=None, fail_module=False): + """ + Get the IPSLA Monitoring Policy by UUID or Name. + :param uuid: UUID of the IPSLA Monitoring Policy to search for -> Str + :param name: Name of the IPSLA Monitoring Policy to search for -> Str + :param fail_module: When match is not found fail the ansible module -> Bool + :return: Dict | None | List[Dict] | List[]: The processed result which could be: + When the UUID | Name is existing in the search list -> Dict + When the UUID | Name is not existing in the search list -> None + When both UUID and Name are None, and the search list is not empty -> List[Dict] + When both UUID and Name are None, and the search list is empty -> List[] + """ + existing_ipsla_policies = self.template.get("tenantPolicyTemplate", {}).get("template", {}).get("ipslaMonitoringPolicies", []) + if name or uuid: + return self.get_object_by_key_value_pairs( + "IPSLA Monitoring Policy", + existing_ipsla_policies, + [(KVPair("uuid", uuid) if uuid else KVPair("name", name))], + fail_module=fail_module, + ) + return existing_ipsla_policies + def get_l3out_object(self, uuid=None, name=None, fail_module=False): """ Get the L3Out by uuid or name. diff --git a/plugins/modules/ndo_ipsla_monitoring_policy.py b/plugins/modules/ndo_ipsla_monitoring_policy.py index 2413c4b05..a6e0b7b3e 100644 --- a/plugins/modules/ndo_ipsla_monitoring_policy.py +++ b/plugins/modules/ndo_ipsla_monitoring_policy.py @@ -296,17 +296,11 @@ def main(): object_description = "IPSLA Monitoring Policy" path = "/tenantPolicyTemplate/template/ipslaMonitoringPolicies" - existing_ipsla_policies = mso_template.template.get("tenantPolicyTemplate", {}).get("template", {}).get("ipslaMonitoringPolicies", []) - if ipsla_monitoring_policy or ipsla_monitoring_policy_uuid: - match = mso_template.get_object_by_key_value_pairs( - object_description, - existing_ipsla_policies, - [KVPair("uuid", ipsla_monitoring_policy_uuid) if ipsla_monitoring_policy_uuid else KVPair("name", ipsla_monitoring_policy)], - ) - if match: - mso.existing = mso.previous = copy.deepcopy(match.details) - else: - mso.existing = mso.previous = existing_ipsla_policies + match = mso_template.get_ipsla_monitoring_policy(ipsla_monitoring_policy_uuid, ipsla_monitoring_policy) + if isinstance(match, list): # Query all policies + mso.existing = mso.previous = match + elif match is not None: # Query a specific policy + mso.existing = mso.previous = copy.deepcopy(match.details) if state == "present": mso_values = dict( diff --git a/plugins/modules/ndo_ipsla_track_list.py b/plugins/modules/ndo_ipsla_track_list.py index 9310c1fe1..b00ce6a16 100644 --- a/plugins/modules/ndo_ipsla_track_list.py +++ b/plugins/modules/ndo_ipsla_track_list.py @@ -268,6 +268,7 @@ MSOModule, mso_argument_spec, ) +from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema from ansible_collections.cisco.mso.plugins.module_utils.template import ( MSOTemplate, KVPair, @@ -434,43 +435,6 @@ def main(): mso.exit_json() -def get_bd_uuid(mso, schema_obj, template, bd): - # Get template - templates = [t.get("name") for t in schema_obj.get("templates")] - if template not in templates: - mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates))) - template_idx = templates.index(template) - # Get BD - bds = [b.get("name") for b in schema_obj.get("templates")[template_idx]["bds"]] - if bd not in bds: - mso.fail_json(msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format(bd, ", ".join(bds))) - return schema_obj.get("templates")[template_idx]["bds"][bds.index(bd)].get("uuid") - - -def get_l3out_uuid(l3out_template_object, name): - l3outs = l3out_template_object.template.get("l3outTemplate", {}).get("l3outs", []) - match = l3out_template_object.get_object_by_key_value_pairs( - "L3Out", - l3outs, - [KVPair("name", name)], - fail_module=True, - ) - if match: - return match.details.get("uuid") - - -def get_ipsla_monitoring_policy_uuid(tenant_template_obj, uuid, name): - existing_ipsla_policies = tenant_template_obj.template.get("tenantPolicyTemplate", {}).get("template", {}).get("ipslaMonitoringPolicies", []) - match = tenant_template_obj.get_object_by_key_value_pairs( - "IPSLA Monitoring Policy", - existing_ipsla_policies, - [(KVPair("uuid", uuid) if uuid else KVPair("name", name))], - fail_module=True, - ) - if match: - return match.details.get("uuid") - - def format_track_list_members(mso, mso_template, members, obj_cache): track_list_members = [] @@ -485,12 +449,14 @@ def get_scope_obj_uuid(scope_type, uuid, obj): schema_name = obj.get("schema") if not schema_name: mso.fail_json(msg="A member scope_type is bd and scope is used but the schema option is missing.") - key = "schema-{0}-{1}".format(scope_type, schema_name) - schema_obj = obj_cache.get(key) - if not schema_obj: - id, path, schema_obj = mso.query_schema(schema_name) - obj_cache[key] = schema_obj - return get_bd_uuid(mso, schema_obj, template, name) + key = "schema-{0}-{1}-{2}".format(scope_type, schema_name, template) + mso_schema = obj_cache.get(key) + if not mso_schema: + mso_schema = MSOSchema(mso, schema_name, template) + obj_cache[key] = mso_schema + mso_schema.set_template(template, fail_module=True) + mso_schema.set_template_bd(name, fail_module=True) + return mso_schema.schema_objects.get("template_bd").details.get("uuid") if scope_type == "l3out": key = "template-{0}-{1}".format(scope_type, template) @@ -499,7 +465,7 @@ def get_scope_obj_uuid(scope_type, uuid, obj): mso_template = MSOTemplate(mso, scope_type, template) mso_template.validate_template(scope_type) obj_cache[key] = mso_template - return get_l3out_uuid(mso_template, name) + return mso_template.get_l3out_object(name=name, fail_module=True).details.get("uuid") for member in members: scope_type = member.get("scope_type") @@ -508,11 +474,11 @@ def get_scope_obj_uuid(scope_type, uuid, obj): "destIP": member.get("destination_ip"), "scope": get_scope_obj_uuid(scope_type, member.get("scope_uuid"), member.get("scope")), "scopeType": scope_type, - "ipslaMonitoringRef": get_ipsla_monitoring_policy_uuid( - mso_template, + "ipslaMonitoringRef": mso_template.get_ipsla_monitoring_policy( member.get("ipsla_monitoring_policy_uuid"), member.get("ipsla_monitoring_policy"), - ), + fail_module=True, + ).details.get("uuid"), }, "weight": member.get("weight"), } diff --git a/tests/integration/targets/ndo_ipsla_track_list/tasks/main.yml b/tests/integration/targets/ndo_ipsla_track_list/tasks/main.yml index 1a77fce93..288ad25a7 100644 --- a/tests/integration/targets/ndo_ipsla_track_list/tasks/main.yml +++ b/tests/integration/targets/ndo_ipsla_track_list/tasks/main.yml @@ -610,7 +610,7 @@ ansible.builtin.assert: that: - err_invalid_bd is failed - - err_invalid_bd.msg == "Provided BD 'ansible_test_bd_non_existing' does not exist. Existing BDs{{":"}} ansible_test_bd" + - err_invalid_bd.msg == "Provided BD 'ansible_test_bd_non_existing' not matching existing bd(s){{":"}} ansible_test_bd" - err_bd_no_schema is failed - err_bd_no_schema.msg == "A member scope_type is bd and scope is used but the schema option is missing." - err_bd_name_and_uuid is failed