diff --git a/plugins/module_utils/constants.py b/plugins/module_utils/constants.py index 9f0262a1..ca6d4392 100644 --- a/plugins/module_utils/constants.py +++ b/plugins/module_utils/constants.py @@ -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", } diff --git a/plugins/module_utils/utils.py b/plugins/module_utils/utils.py index 477a5126..68f8bbff 100644 --- a/plugins/module_utils/utils.py +++ b/plugins/module_utils/utils.py @@ -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.") diff --git a/plugins/modules/ndo_tenant_custom_qos_policy.py b/plugins/modules/ndo_tenant_custom_qos_policy.py index 0c771046..7e14de01 100644 --- a/plugins/modules/ndo_tenant_custom_qos_policy.py +++ b/plugins/modules/ndo_tenant_custom_qos_policy.py @@ -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, ) @@ -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"], ), ), @@ -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"], ), ), @@ -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)