Skip to content

Commit

Permalink
[ignore] Refactor ipsla_track_list to use existing funtions and creat…
Browse files Browse the repository at this point in the history
…ed shared ipsla monitoring policy function.
  • Loading branch information
samiib committed Jan 26, 2025
1 parent 02aa230 commit 8f03fc8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 59 deletions.
22 changes: 22 additions & 0 deletions plugins/module_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 5 additions & 11 deletions plugins/modules/ndo_ipsla_monitoring_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
60 changes: 13 additions & 47 deletions plugins/modules/ndo_ipsla_track_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = []

Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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"),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f03fc8

Please sign in to comment.