From cb02685bda71269e999cb25728a25bebbfc24cf1 Mon Sep 17 00:00:00 2001 From: Sabari Jaganathan <93724860+sajagana@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:55:00 +0530 Subject: [PATCH] [ignore] Removed state from ndo_l3out_node_group_policy module attribute --- plugins/module_utils/utils.py | 15 ++++++------ .../modules/ndo_l3out_node_group_policy.py | 23 ++++++------------- .../tasks/main.yml | 10 ++++---- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/plugins/module_utils/utils.py b/plugins/module_utils/utils.py index 71105ffa..5c5f5be7 100644 --- a/plugins/module_utils/utils.py +++ b/plugins/module_utils/utils.py @@ -16,13 +16,14 @@ def generate_api_endpoint(path, **kwargs): :param kwargs: Keyword arguments representing query parameters. -> Dict :return: A string representing the full API endpoint with query parameters. -> Str """ - # if not kwargs: - # return path + return path if not kwargs else "{0}?{1}".format(path, "&".join(["{0}={1}".format(key, value) for key, value in kwargs.items()])) - # query_strings = ["{0}={1}".format(key, value) for key, value in kwargs.items()] - # query_string = "&".join(query_strings) - # full_url = "{0}?{1}".format(path, query_string) - # return full_url +def check_if_all_elements_are_none(values): + """ + Checks if all the elements in the provided list are None. - return path if not kwargs else "{0}?{1}".format(path, "&".join(["{0}={1}".format(key, value) for key, value in kwargs.items()])) + :param values: List of values to check. -> List + :return: True if all elements are None, False otherwise. -> boo + """ + return all(value is None for value in values) diff --git a/plugins/modules/ndo_l3out_node_group_policy.py b/plugins/modules/ndo_l3out_node_group_policy.py index 9a1cd819..2ad57bf0 100644 --- a/plugins/modules/ndo_l3out_node_group_policy.py +++ b/plugins/modules/ndo_l3out_node_group_policy.py @@ -51,14 +51,9 @@ bfd: description: - The Bidirectional Forwarding Detection (BFD) multi-hop configuration of the L3Out Node Group Policy. + - Providing an empty dictionary will remove the O(bfd={}) from the L3Out Node Group Policy. type: dict suboptions: - state: - description: - - Use C(enabled) to configure the BFD multi-hop. - - Use C(disabled) to remove the BFD multi-hop. - type: str - choices: [ enabled, disabled ] auth: description: - The BFD multi-hop authentication of the L3Out Node Group Policy. @@ -189,7 +184,7 @@ 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.constants import TARGET_DSCP_MAP -from ansible_collections.cisco.mso.plugins.module_utils.utils import generate_api_endpoint +from ansible_collections.cisco.mso.plugins.module_utils.utils import generate_api_endpoint, check_if_all_elements_are_none def main(): @@ -203,7 +198,6 @@ def main(): bfd=dict( type="dict", options=dict( - state=dict(type="str", choices=["enabled", "disabled"]), auth=dict(type="str", choices=["enabled", "disabled"], aliases=["bfd_multi_hop_authentication"]), key_id=dict(type="int"), key=dict(type="str", no_log=True), @@ -285,10 +279,10 @@ def main(): proposed_payload["nodeRoutingPolicyRef"] = node_routing_policy if bfd: - if bfd.get("state") == "disabled" and proposed_payload.get("bfdMultiHop"): + if check_if_all_elements_are_none(list(bfd.values())) and proposed_payload.get("bfdMultiHop"): proposed_payload.pop("bfdMultiHop") ops.append(dict(op="remove", path=node_group_policy_path + "/bfdMultiHop")) - else: + elif not check_if_all_elements_are_none(list(bfd.values())): if not proposed_payload.get("bfdMultiHop"): proposed_payload["bfdMultiHop"] = dict() ops.append(dict(op="replace", path=node_group_policy_path + "/bfdMultiHop", value=dict())) @@ -314,16 +308,14 @@ def main(): ops.append(dict(op="replace", path=node_group_policy_path + "/bfdMultiHop/key/value", value=bfd.get("key"))) proposed_payload["bfdMultiHop"]["key"] = dict(value=bfd.get("key")) - if target_dscp is not None and mso.existing.get("targetDscp") != target_dscp: + if target_dscp and mso.existing.get("targetDscp") != target_dscp: ops.append(dict(op="replace", path=node_group_policy_path + "/targetDscp", value=target_dscp)) proposed_payload["targetDscp"] = target_dscp mso.sanitize(proposed_payload, collate=True) else: payload = dict(name=name) - - if description: - payload["description"] = description + payload["description"] = description if l3out_node_routing_policy_object: payload["nodeRoutingPolicyRef"] = l3out_node_routing_policy_object.details.get("uuid") @@ -343,8 +335,7 @@ def main(): if bfd_multi_hop: payload["bfdMultiHop"] = bfd_multi_hop - if target_dscp: - payload["targetDscp"] = target_dscp + payload["targetDscp"] = target_dscp mso.sanitize(payload) ops.append(dict(op="add", path=node_group_policy_path, value=payload)) diff --git a/tests/integration/targets/ndo_l3out_node_group_policy/tasks/main.yml b/tests/integration/targets/ndo_l3out_node_group_policy/tasks/main.yml index 1d640e23..92cfb77d 100644 --- a/tests/integration/targets/ndo_l3out_node_group_policy/tasks/main.yml +++ b/tests/integration/targets/ndo_l3out_node_group_policy/tasks/main.yml @@ -739,8 +739,7 @@ l3out: "l3out_2" name: "node_group_policy_5" node_routing_policy: "" - bfd: - state: disabled + bfd: {} state: present register: clear_ngp_5_bfd_config @@ -840,18 +839,17 @@ - node_group_policy_nt_with_l3out_1 is changed - node_group_policy_nt_with_l3out_1.msg == "MSO Error 400{{':'}} Invalid configuration in L3Out 'l3out_1'{{':'}} node group 'node_group_policy_nt'{{':'}} BFD Multihop is not supported with non-BGP routing protocols. Current protocol{{':'}} none" - - name: Create ngp1 with bfd state disabled + - name: Create ngp1 with bfd is an empty dict cisco.mso.ndo_l3out_node_group_policy: <<: *mso_info template: '{{ ansible_l3out_template | default("ansible_test") }}' l3out: "l3out_1" name: "ngp1" - bfd: - state: disabled + bfd: {} state: present register: create_ngp1_bfd_disabled - - name: Assertion check for create ngp1 with bfd state disabled + - name: Assertion check for create ngp1 with bfd is an empty dict ansible.builtin.assert: that: - create_ngp1_bfd_disabled is changed