diff options
author | Gaige B Paulsen <gaige@cluetrust.com> | 2025-02-07 14:11:19 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-07 11:11:19 -0800 |
commit | d75becb8e46d764552b0df605ef9192592ae2633 (patch) | |
tree | 1a8d98cfd62ee956017302685d6e0d0453833764 /plugins/module_utils/network | |
parent | 4b1cc399b285fbf3f2535424b444d8d3282515c5 (diff) | |
download | vyos.vyos-d75becb8e46d764552b0df605ef9192592ae2633.tar.gz vyos.vyos-d75becb8e46d764552b0df605ef9192592ae2633.zip |
T7011: fix: lldp global integration (#385)
* T7011: fix: lldp global integration
* T7011: Fix lldp interfaces integration
Diffstat (limited to 'plugins/module_utils/network')
6 files changed, 78 insertions, 127 deletions
diff --git a/plugins/module_utils/network/vyos/argspec/lldp_global/lldp_global.py b/plugins/module_utils/network/vyos/argspec/lldp_global/lldp_global.py index 5c33d674..d79de617 100644 --- a/plugins/module_utils/network/vyos/argspec/lldp_global/lldp_global.py +++ b/plugins/module_utils/network/vyos/argspec/lldp_global/lldp_global.py @@ -42,6 +42,10 @@ class Lldp_globalArgs(object): # pylint: disable=R0903 "config": { "options": { "address": {"type": "str"}, + "addresses": { + "elements": "str", + "type": "list", + }, "enable": {"type": "bool"}, "legacy_protocols": { "choices": [ @@ -56,6 +60,7 @@ class Lldp_globalArgs(object): # pylint: disable=R0903 "snmp": {"type": "str"}, }, "type": "dict", + "mutually_exclusive": [["addresses", "address"]], }, "running_config": {"type": "str"}, "state": { diff --git a/plugins/module_utils/network/vyos/argspec/lldp_interfaces/lldp_interfaces.py b/plugins/module_utils/network/vyos/argspec/lldp_interfaces/lldp_interfaces.py index 37a6afb0..fd6c6271 100644 --- a/plugins/module_utils/network/vyos/argspec/lldp_interfaces/lldp_interfaces.py +++ b/plugins/module_utils/network/vyos/argspec/lldp_interfaces/lldp_interfaces.py @@ -45,29 +45,6 @@ class Lldp_interfacesArgs(object): # pylint: disable=R0903 "enable": {"default": True, "type": "bool"}, "location": { "options": { - "civic_based": { - "options": { - "ca_info": { - "elements": "dict", - "options": { - "ca_type": { - "required": True, - "type": "int", - }, - "ca_value": { - "required": True, - "type": "str", - }, - }, - "type": "list", - }, - "country_code": { - "required": True, - "type": "str", - }, - }, - "type": "dict", - }, "coordinate_based": { "options": { "altitude": {"type": "int"}, diff --git a/plugins/module_utils/network/vyos/config/lldp_global/lldp_global.py b/plugins/module_utils/network/vyos/config/lldp_global/lldp_global.py index 1afda782..1dfd25e4 100644 --- a/plugins/module_utils/network/vyos/config/lldp_global/lldp_global.py +++ b/plugins/module_utils/network/vyos/config/lldp_global/lldp_global.py @@ -42,7 +42,8 @@ class Lldp_global(ConfigBase): "lldp_global", ] - params = ["enable", "address", "snmp", "legacy_protocols"] + # address intentionally omitted since it's coerced to addresses + params = ["enable", "addresses", "snmp", "legacy_protocols"] def __init__(self, module): super(Lldp_global, self).__init__(module) @@ -73,6 +74,12 @@ class Lldp_global(ConfigBase): warnings = list() commands = list() + # fix for new name/type + if self._module.params["config"]: + temp_have_address = self._module.params["config"].pop("address", None) + if temp_have_address: + self._module.params["config"]["addresses"] = [temp_have_address] + if self.state in self.ACTION_STATES: existing_lldp_global_facts = self.get_lldp_global_facts() else: @@ -184,14 +191,19 @@ class Lldp_global(ConfigBase): for item in Lldp_global.params: if item == "legacy_protocols": commands.extend(self._update_lldp_protocols(want, have)) + if item == "addresses": + commands.extend(self._update_management_addresses(want, have)) elif have.get(item) and not want.get(item) and item != "enable": commands.append(Lldp_global.del_cmd + item) elif have: + if have.get("enable"): + commands.append(self._compute_command(remove=True)) + return commands for item in Lldp_global.params: if have.get(item): if item == "legacy_protocols": commands.append(self._compute_command("legacy-protocols", remove=True)) - elif item == "address": + elif item == "addresses": commands.append(self._compute_command("management-address", remove=True)) elif item == "snmp": commands.append(self._compute_command(item, remove=True)) @@ -202,9 +214,17 @@ class Lldp_global(ConfigBase): commands = [] if have: temp_have_legacy_protos = have.pop("legacy_protocols", None) + temp_have_addreses = have.pop("addresses", None) + temp_have_address = have.pop("address", None) + if temp_have_address: + temp_have_addresses = [temp_have_address] else: have = {} temp_want_legacy_protos = want.pop("legacy_protocols", None) + temp_want_addreses = want.pop("addresses", None) + temp_want_address = want.pop("address", None) + if temp_want_address: + temp_want_addresses = [temp_want_address] updates = dict_diff(have, want) @@ -212,16 +232,23 @@ class Lldp_global(ConfigBase): have["legacy_protocols"] = temp_have_legacy_protos if not have and temp_want_legacy_protos: want["legacy_protocols"] = temp_want_legacy_protos + if have and temp_have_addreses: + have["addresses"] = temp_have_addreses + if not have and temp_want_addreses: + want["addresses"] = temp_want_addreses commands.extend(self._add_lldp_protocols(want, have)) + commands.extend(self._add_management_addresses(want, have)) if updates: for key, value in iteritems(updates): - if value: + if value is not None: if key == "enable": - commands.append(self._compute_command()) - elif key == "address": - commands.append(self._compute_command("management-address", str(value))) + if value is False: + commands.append(self._compute_command(remove=True)) + return commands + else: + commands.append(self._compute_command()) elif key == "snmp": if value == "disable": commands.append(self._compute_command(key, remove=True)) @@ -232,8 +259,17 @@ class Lldp_global(ConfigBase): def _add_lldp_protocols(self, want, have): commands = [] diff_members = get_lst_diff_for_dicts(want, have, "legacy_protocols") - for key in diff_members: - commands.append(self._compute_command("legacy-protocols", key)) + if diff_members: + for key in diff_members: + commands.append(self._compute_command("legacy-protocols", key)) + return commands + + def _add_management_addresses(self, want, have): + commands = [] + diff_members = get_lst_diff_for_dicts(want, have, "addresses") + if diff_members: + for key in diff_members: + commands.append(self._compute_command("management-address", key)) return commands def _update_lldp_protocols(self, want_item, have_item): @@ -247,6 +283,17 @@ class Lldp_global(ConfigBase): commands.append(self._compute_command("legacy-protocols", member, remove=True)) return commands + def _update_management_addresses(self, want_item, have_item): + commands = [] + want_addresses = want_item.get("addresses") or [] + have_addresses = have_item.get("addresses") or [] + + members_diff = list_diff_have_only(want_addresses, have_addresses) + if members_diff: + for member in members_diff: + commands.append(self._compute_command("management-address", member, remove=True)) + return commands + def _compute_command(self, key=None, value=None, remove=False): if remove: cmd = "delete service lldp" diff --git a/plugins/module_utils/network/vyos/config/lldp_interfaces/lldp_interfaces.py b/plugins/module_utils/network/vyos/config/lldp_interfaces/lldp_interfaces.py index 53e9aed0..2fd6a548 100644 --- a/plugins/module_utils/network/vyos/config/lldp_interfaces/lldp_interfaces.py +++ b/plugins/module_utils/network/vyos/config/lldp_interfaces/lldp_interfaces.py @@ -29,7 +29,6 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.facts from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.utils import ( is_dict_element_present, key_value_in_dict, - search_dict_tv_in_list, search_obj_in_list, ) @@ -290,31 +289,6 @@ class Lldp_interfaces(ConfigBase): if value: commands.append(self._compute_command(set_cmd + location_type, key, str(value))) - elif want_location_type["civic_based"]: - location_type = "civic-based" - want_dict = want_location_type.get("civic_based") or {} - want_ca = want_dict.get("ca_info") or [] - if is_dict_element_present(have_location_type, "civic_based"): - have_dict = have_location_type.get("civic_based") or {} - have_ca = have_dict.get("ca_info") or [] - if want_dict["country_code"] != have_dict["country_code"]: - commands.append( - self._compute_command( - set_cmd + location_type, - "country-code", - str(want_dict["country_code"]), - ), - ) - else: - commands.append( - self._compute_command( - set_cmd + location_type, - "country-code", - str(want_dict["country_code"]), - ), - ) - commands.extend(self._add_civic_address(name, want_ca, have_ca)) - elif want_location_type["elin"]: location_type = "elin" if is_dict_element_present(have_location_type, "elin"): @@ -354,16 +328,6 @@ class Lldp_interfaces(ConfigBase): else: commands.append(self._compute_command(del_cmd, remove=True)) - elif want_location_type["civic_based"]: - want_dict = want_location_type.get("civic_based") or {} - want_ca = want_dict.get("ca_info") or [] - if is_dict_element_present(have_location_type, "civic_based"): - have_dict = have_location_type.get("civic_based") or {} - have_ca = have_dict.get("ca_info") - commands.extend(self._update_civic_address(name, want_ca, have_ca)) - else: - commands.append(self._compute_command(del_cmd, remove=True)) - else: if is_dict_element_present(have_location_type, "elin"): if want_location_type.get("elin") != have_location_type.get("elin"): @@ -372,39 +336,6 @@ class Lldp_interfaces(ConfigBase): commands.append(self._compute_command(del_cmd, remove=True)) return commands - def _add_civic_address(self, name, want, have): - commands = [] - for item in want: - ca_type = item["ca_type"] - ca_value = item["ca_value"] - obj_in_have = search_dict_tv_in_list(ca_type, ca_value, have, "ca_type", "ca_value") - if not obj_in_have: - commands.append( - self._compute_command( - key=name + " location civic-based ca-type", - attrib=str(ca_type) + " ca-value", - value=ca_value, - ), - ) - return commands - - def _update_civic_address(self, name, want, have): - commands = [] - for item in have: - ca_type = item["ca_type"] - ca_value = item["ca_value"] - in_want = search_dict_tv_in_list(ca_type, ca_value, want, "ca_type", "ca_value") - if not in_want: - commands.append( - self._compute_command( - name, - "location civic-based ca-type", - str(ca_type), - remove=True, - ), - ) - return commands - def _compute_command(self, key, attrib=None, value=None, remove=False): if remove: cmd = "delete service lldp interface " diff --git a/plugins/module_utils/network/vyos/facts/lldp_global/lldp_global.py b/plugins/module_utils/network/vyos/facts/lldp_global/lldp_global.py index b2afe590..7a6e9b8e 100644 --- a/plugins/module_utils/network/vyos/facts/lldp_global/lldp_global.py +++ b/plugins/module_utils/network/vyos/facts/lldp_global/lldp_global.py @@ -41,6 +41,9 @@ class Lldp_globalFacts(object): self.generated_spec = utils.generate_dict(facts_argument_spec) + def get_config(self, connection): + return connection.get("show configuration commands | grep lldp") + def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for lldp_global :param connection: the device connection @@ -50,7 +53,7 @@ class Lldp_globalFacts(object): :returns: facts """ if not data: - data = connection.get_config() + data = self.get_config(connection) objs = {} lldp_output = findall(r"^set service lldp (\S+)", data, M) @@ -85,9 +88,11 @@ class Lldp_globalFacts(object): :returns: The generated config """ protocol_conf = "\n".join(filter(lambda x: ("legacy-protocols" in x), conf)) - att_conf = "\n".join(filter(lambda x: ("legacy-protocols" not in x), conf)) - config = self.parse_attribs(["snmp", "address"], att_conf) + att_conf = "\n".join(filter(lambda x: ("snmp" in x), conf)) + addr_conf = "\n".join(filter(lambda x: ("management-address" in x), conf)) + config = self.parse_attribs(["snmp"], att_conf) config["legacy_protocols"] = self.parse_protocols(protocol_conf) + config["addresses"] = self.parse_addresses(addr_conf) return utils.remove_empties(config) def parse_protocols(self, conf): @@ -100,6 +105,16 @@ class Lldp_globalFacts(object): protocol_support.append(protocol.strip("'")) return protocol_support + def parse_addresses(self, conf): + management_addresses = None + if conf: + addresses = findall(r"^.*management-address (.+)", conf, M) + if addresses: + management_addresses = [] + for protocol in addresses: + management_addresses.append(protocol.strip("'")) + return management_addresses + def parse_attribs(self, attribs, conf): config = {} for item in attribs: diff --git a/plugins/module_utils/network/vyos/facts/lldp_interfaces/lldp_interfaces.py b/plugins/module_utils/network/vyos/facts/lldp_interfaces/lldp_interfaces.py index 5f439e78..e029b47a 100644 --- a/plugins/module_utils/network/vyos/facts/lldp_interfaces/lldp_interfaces.py +++ b/plugins/module_utils/network/vyos/facts/lldp_interfaces/lldp_interfaces.py @@ -85,7 +85,6 @@ class Lldp_interfacesFacts(object): config = {} location = {} - civic_conf = "\n".join(filter(lambda x: ("civic-based" in x), conf)) elin_conf = "\n".join(filter(lambda x: ("elin" in x), conf)) coordinate_conf = "\n".join(filter(lambda x: ("coordinate-based" in x), conf)) disable = "\n".join(filter(lambda x: ("disable" in x), conf)) @@ -95,15 +94,11 @@ class Lldp_interfacesFacts(object): coordinate_conf, ) elin_based_conf = self.parse_lldp_elin_based(elin_conf) - civic_based_conf = self.parse_lldp_civic_based(civic_conf) if disable: config["enable"] = False if coordinate_conf: location["coordinate_based"] = coordinate_based_conf config["location"] = location - elif civic_based_conf: - location["civic_based"] = civic_based_conf - config["location"] = location elif elin_conf: location["elin"] = elin_based_conf config["location"] = location @@ -123,25 +118,6 @@ class Lldp_interfacesFacts(object): config[item] = None return utils.remove_empties(config) - def parse_lldp_civic_based(self, conf): - civic_based = None - if conf: - civic_info_list = [] - civic_add_list = findall(r"^.*civic-based ca-type (.+)", conf, M) - if civic_add_list: - for civic_add in civic_add_list: - ca = civic_add.split(" ") - c_add = {} - c_add["ca_type"] = int(ca[0].strip("'")) - c_add["ca_value"] = ca[2].strip("'") - civic_info_list.append(c_add) - - country_code = search(r"^.*civic-based country-code (.+)", conf, M) - civic_based = {} - civic_based["ca_info"] = civic_info_list - civic_based["country_code"] = country_code.group(1).strip("'") - return civic_based - def parse_lldp_elin_based(self, conf): elin_based = None if conf: |