Skip to content

Commit

Permalink
[ignore] Modify format_list_dict function in utils.py. Slightly modif…
Browse files Browse the repository at this point in the history
…y ndo_tenant_custom_qos_policy module's documentation.
  • Loading branch information
gmicol committed Dec 6, 2024
1 parent 3a931b6 commit 3cda83e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
63 changes: 56 additions & 7 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,55 @@ def format_list_dict(list_dict, keys_map):
Convert an 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. -> List[Dict]
:param keys_map: the mapping from the Ansible argument's keys to NDO API keys. Can also include the map between values -> Dict
:return: The formatted dictionary. -> Dict
:param list_dict: The Python list of dictionaries to format. Can be an empty List or None -> List
:param keys_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_KEYS_FORMAT_MAP = {
"dot1p_from": ["dot1pFrom", REDUCED_TARGET_COS_MAP],
"dot1p_to": ["dot1pTo", REDUCED_TARGET_COS_MAP],
"dscp_target": ["dscpTarget", REDUCED_TARGET_DSCP_MAP],
"target_cos": ["targetCos", REDUCED_TARGET_COS_MAP],
"qos_priority": "priority",
}
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_KEYS_FORMAT_MAP)
Output Data:
---------------------
{
"dot1pFrom": "cos0",
"dot1pTo": "cos1",
"dscpTarget": "voiceAdmit",
"targetCos": "cos2",
"priority": "level1",
}
"""
if isinstance(list_dict, list):
if isinstance(list_dict, list) and isinstance(keys_map, dict):

def format_dict(d):
def format_dict(d): # format individual dictionary to its equivalent NDO API format
formatted_dict = {}
if isinstance(d, dict):
for key, value in keys_map.items():
Expand All @@ -167,7 +209,14 @@ def format_dict(d):
formatted_dict[value[0]] = value[1].get(d.get(key))
else:
formatted_dict[value] = d.get(key)
else:
raise TypeError("items in list_dict must be dictionaries.")
return formatted_dict

formatted_list = [format_dict(d) for d in list_dict]
return formatted_list
return [format_dict(d) for d in list_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(keys_map, dict):
raise TypeError("keys_map must be a dictionary.")
18 changes: 10 additions & 8 deletions plugins/modules/ndo_tenant_custom_qos_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
description:
- The Differentiated Services Code Point (DSCP) mappings of the Custom QoS Policy.
- Both O(dscp_mappings.dscp_from) and O(dscp_mappings.dscp_to) cannot be set to C(unspecified).
- Providing a new list of O(dscp_mappings) will completely replace an existing one from the Custom QoS Policy.
- Providing an empty list will remove the O(cos_mappings=[]) from the Custom QoS Policy.
type: list
elements: dict
Expand Down Expand Up @@ -158,6 +159,7 @@
description:
- The Class of Service (CoS) mappings of the Custom QoS Policy.
- Both O(cos_mappings.dot1p_from) and O(cos_mappings.dot1p_to) cannot be set to C(unspecified).
- Providing a new list of O(cos_mappings) will completely replace an existing one from the Custom QoS Policy.
- Providing an empty list will remove the O(cos_mappings=[]) from the Custom QoS Policy.
type: list
elements: dict
Expand Down Expand Up @@ -343,10 +345,10 @@ def main():
type="list",
elements="dict",
options=dict(
dscp_from=dict(type="str", choices=list(TARGET_DSCP_MAP.keys()), aliases=["from"]),
dscp_to=dict(type="str", choices=list(TARGET_DSCP_MAP.keys()), aliases=["to"]),
dscp_target=dict(type="str", choices=list(TARGET_DSCP_MAP.keys()), aliases=["target"]),
target_cos=dict(type="str", choices=list(TARGET_COS_MAP.keys())),
dscp_from=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["from"]),
dscp_to=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["to"]),
dscp_target=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["target"]),
target_cos=dict(type="str", choices=list(TARGET_COS_MAP)),
qos_priority=dict(
type="str",
choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"],
Expand All @@ -358,10 +360,10 @@ def main():
type="list",
elements="dict",
options=dict(
dot1p_from=dict(type="str", choices=list(TARGET_COS_MAP.keys()), aliases=["from"]),
dot1p_to=dict(type="str", choices=list(TARGET_COS_MAP.keys()), aliases=["to"]),
dscp_target=dict(type="str", choices=list(TARGET_DSCP_MAP.keys()), aliases=["target"]),
target_cos=dict(type="str", choices=list(TARGET_COS_MAP.keys())),
dot1p_from=dict(type="str", choices=list(TARGET_COS_MAP), aliases=["from"]),
dot1p_to=dict(type="str", choices=list(TARGET_COS_MAP), aliases=["to"]),
dscp_target=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["target"]),
target_cos=dict(type="str", choices=list(TARGET_COS_MAP)),
qos_priority=dict(
type="str",
choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"],
Expand Down

0 comments on commit 3cda83e

Please sign in to comment.