Skip to content

Commit

Permalink
[ignore] Remove format_list_dict function from utils.py and replace i…
Browse files Browse the repository at this point in the history
…t with dict comprehension in ndo_tenant_custom_qos_policy module.
  • Loading branch information
gmicol committed Jan 21, 2025
1 parent 3f1e69f commit 855d85f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 133 deletions.
38 changes: 8 additions & 30 deletions plugins/module_utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,34 +226,12 @@
"unspecified": "cos8",
}

COS_CONVERSION_MAP = {
"keys_map": {
"dot1p_from": "dot1pFrom",
"dot1p_to": "dot1pTo",
"dscp_target": "dscpTarget",
"target_cos": "targetCos",
"qos_priority": "priority",
},
"values_map": {
"dot1p_from": TARGET_COS_MAP,
"dot1p_to": TARGET_COS_MAP,
"dscp_target": TARGET_DSCP_MAP,
"target_cos": TARGET_COS_MAP,
},
}

DSCP_CONVERSION_MAP = {
"keys_map": {
"dscp_from": "dscpFrom",
"dscp_to": "dscpTo",
"dscp_target": "dscpTarget",
"target_cos": "targetCos",
"qos_priority": "priority",
},
"values_map": {
"dscp_from": TARGET_DSCP_MAP,
"dscp_to": TARGET_DSCP_MAP,
"dscp_target": TARGET_DSCP_MAP,
"target_cos": TARGET_COS_MAP,
},
DSCP_COS_KEY_MAP = {
"dscp_from": "dscpFrom",
"dscp_to": "dscpTo",
"dot1p_from": "dot1pFrom",
"dot1p_to": "dot1pTo",
"dscp_target": "dscpTarget",
"target_cos": "targetCos",
"qos_priority": "priority",
}
95 changes: 0 additions & 95 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,98 +146,3 @@ def check_if_all_elements_are_none(values):
:return: True if all elements are None, False otherwise. -> boo
"""
return all(value is None for value in values)


def format_list_dict(list_dict, conversion_map):
"""
Convert a Python list of dictionaries into its equivalent NDO API format.
All keys must be defined in the keys map even if no conversion is needed for some keys.
:param list_dict: The Python list of dictionaries to format. Can be an empty List or None -> List
:param conversion_map: The mapping from the Ansible argument's keys to NDO API keys. Can also include the map between values -> Dict
:return: The formatted list of dictionaries -> Dict
Sample Input Data:
---------------------
REDUCED_TARGET_COS_MAP = {
"background": "cos0",
"best_effort": "cos1",
"excellent_effort": "cos2",
}
REDUCED_TARGET_DSCP_MAP = {
"af11": "af11",
"cs0": "cs0",
"voice_admit": "voiceAdmit",
}
COS_CONVERSION_MAP = {
"keys_map": {
"dot1p_from": "dot1pFrom",
"dot1p_to": "dot1pTo",
"dscp_target": "dscpTarget",
"target_cos": "targetCos",
"qos_priority": "priority",
},
"values_map": {
"dot1p_from": REDUCED_TARGET_COS_MAP,
"dot1p_to": REDUCED_TARGET_COS_MAP,
"dscp_target": REDUCED_TARGET_DSCP_MAP,
"target_cos": REDUCED_TARGET_COS_MAP,
},
}
ansible_cos_mappings = [
{
"dot1p_from": "background",
"dot1p_to": "best_effort",
"dscp_target": "voice_admit",
"target_cos": "excellent_effort",
"qos_priority": "level1",
}
]
formatted_cos_mappings = format_list_dict(ansible_cos_mappings, COS_CONVERSION_MAP)
Output Data:
---------------------
[
{
"dot1pFrom": "cos0",
"dot1pTo": "cos1",
"dscpTarget": "voiceAdmit",
"targetCos": "cos2",
"priority": "level1",
}
]
"""
if isinstance(list_dict, list) and isinstance(conversion_map, dict):
keys_map, values_map = conversion_map.get("keys_map"), conversion_map.get("values_map")
if isinstance(keys_map, dict) and isinstance(values_map, dict):

def format_dict(d): # format individual dictionary to its equivalent NDO API format
formatted_dict = {}
if isinstance(d, dict):
for key, value in d.items():
json_key = keys_map.get(key, "unknownKey") # retrieve the equilavent NDO API formatted key
if not isinstance(json_key, str):
raise TypeError("the associated json key must be of type string, got:{0}".format(type(json_key)))
values_mapping = values_map.get(key) # Check if there is a mapping between values associated with the current key
if values_mapping and isinstance(values_mapping, dict):
formatted_dict[json_key] = values_map[key].get(value, value)
else:
formatted_dict[json_key] = value # in case there is no mapping between values
else:
raise TypeError("items in list_dict must be dictionaries.")
return formatted_dict

return [format_dict(d) for d in list_dict]

else:
raise TypeError("keys_map and values_map must be of type dict.")

elif list_dict is not None and not isinstance(list_dict, list):
raise TypeError("list_dict can either be a list of dictionaries, an empty List or None.")

elif not isinstance(conversion_map, dict):
raise TypeError("conversion_map must be a dictionary.")
27 changes: 19 additions & 8 deletions plugins/modules/ndo_tenant_custom_qos_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,12 @@
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, format_list_dict
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
from ansible_collections.cisco.mso.plugins.module_utils.constants import (
TARGET_DSCP_MAP,
TARGET_COS_MAP,
DSCP_CONVERSION_MAP,
COS_CONVERSION_MAP,
QOS_PRIORITY_VALUES,
DSCP_COS_KEY_MAP,
QOS_LEVEL,
)


Expand All @@ -433,7 +432,7 @@ def main():
target_cos=dict(type="str", choices=COS_MAP_CHOICES),
qos_priority=dict(
type="str",
choices=QOS_PRIORITY_VALUES,
choices=QOS_LEVEL,
aliases=["priority", "prio"],
),
),
Expand All @@ -448,7 +447,7 @@ def main():
target_cos=dict(type="str", choices=COS_MAP_CHOICES),
qos_priority=dict(
type="str",
choices=QOS_PRIORITY_VALUES,
choices=QOS_LEVEL,
aliases=["priority", "prio"],
),
),
Expand All @@ -471,8 +470,20 @@ def main():
name = module.params.get("name")
uuid = module.params.get("uuid")
description = module.params.get("description")
dscp_mappings = format_list_dict(module.params.get("dscp_mappings"), DSCP_CONVERSION_MAP)
cos_mappings = format_list_dict(module.params.get("cos_mappings"), COS_CONVERSION_MAP)
dscp_mappings = module.params.get("dscp_mappings")
if dscp_mappings:
dscp_mappings = [
{
DSCP_COS_KEY_MAP.get(k): TARGET_DSCP_MAP.get(v) if k in ("dscp_from", "dscp_to", "dscp_target") else TARGET_COS_MAP.get(v, v)
for k, v in d.items()
}
for d in dscp_mappings
]
cos_mappings = module.params.get("cos_mappings")
if cos_mappings:
cos_mappings = [
{DSCP_COS_KEY_MAP.get(k): TARGET_DSCP_MAP.get(v) if k == "dscp_target" else TARGET_COS_MAP.get(v, v) for k, v in d.items()} for d in cos_mappings
]
state = module.params.get("state")

template_object = MSOTemplate(mso, "tenant", template)
Expand Down

0 comments on commit 855d85f

Please sign in to comment.