diff --git a/changelogs/fragments/t9421_vyos_interfaces_rm_rewrite.yml b/changelogs/fragments/t9421_vyos_interfaces_rm_rewrite.yml new file mode 100644 index 00000000..65d658c8 --- /dev/null +++ b/changelogs/fragments/t9421_vyos_interfaces_rm_rewrite.yml @@ -0,0 +1,15 @@ +--- +bugfixes: + - vyos_interfaces - options starting with "disable", such as disable-link-detect or + disable-flow-control, no longer mark the interface as disabled. + - vyos_interfaces - the options of one vif no longer leak into another whose VLAN ID contains + it (for example vif 110 into vif 10). + - vyos_interfaces - an interface line containing "vif", such as a description, is no longer + taken for a vif line and lost. + - vyos_interfaces - re-enabling a disabled interface with state replaced or overridden deletes + disable once; before, the command was sent twice and the second one failed. + - vyos_interfaces - state deleted with an interface that has no configuration on the device no + longer fails with an AttributeError. +trivial: + - vyos_interfaces - rewritten on the resource module template framework (rm_template); options, + output and behaviour are unchanged apart from the fixes above. diff --git a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py index 71e4c0d1..12488d9e 100644 --- a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py @@ -1,340 +1,148 @@ # Copyright 2019 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The vyos_interfaces class It is in this file where the current configuration (as dict) is compared to the provided configuration (as dict) and the command set necessary to bring the current configuration to it's desired end-state is created """ from __future__ import absolute_import, division, print_function + __metaclass__ = type from copy import deepcopy -from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base import ( - ConfigBase, +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import ( + ResourceModule, ) from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( - dict_diff, - remove_empties, - to_list, + dict_merge, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.facts import Facts +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.interfaces import ( + InterfacesTemplate, +) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.utils import ( - dict_delete, get_interface_type, - search_obj_in_list, ) -class Interfaces(ConfigBase): +class Interfaces(ResourceModule): """ The vyos_interfaces class """ - gather_subset = [ - "!all", - "!min", - ] - - gather_network_resources = ["interfaces"] - def __init__(self, module): - super(Interfaces, self).__init__(module) - - def get_interfaces_facts(self, data=None): - """Get the 'facts' (the current configuration) - - :rtype: A dictionary - :returns: The current configuration as a dictionary - """ - facts, _warnings = Facts(self._module).get_facts( - self.gather_subset, - self.gather_network_resources, - data=data, + super(Interfaces, self).__init__( + empty_fact_val=[], + facts_module=Facts(module), + module=module, + resource="interfaces", + tmplt=InterfacesTemplate(), ) - interfaces_facts = facts["ansible_network_resources"].get("interfaces") - if not interfaces_facts: - return [] - return interfaces_facts + self.parsers = ["description", "mtu", "duplex", "speed", "vrf"] + self.vif_parsers = ["vifs.description", "vifs.mtu"] def execute_module(self): """Execute the module - :rtype: A dictionary - :returns: The result from module execution - """ - result = {"changed": False} - commands = list() - warnings = list() - - if self.state in self.ACTION_STATES: - existing_interfaces_facts = self.get_interfaces_facts() - - else: - existing_interfaces_facts = [] - - if self.state in self.ACTION_STATES or self.state == "rendered": - commands.extend(self.set_config(existing_interfaces_facts)) - - if commands and self.state in self.ACTION_STATES: - if not self._module.check_mode: - self._connection.edit_config(commands) - result["changed"] = True - - if self.state in self.ACTION_STATES: - result["commands"] = commands - - if self.state in self.ACTION_STATES or self.state == "gathered": - changed_interfaces_facts = self.get_interfaces_facts() - elif self.state == "rendered": - result["rendered"] = commands - elif self.state == "parsed": - running_config = self._module.params["running_config"] - if not running_config: - self._module.fail_json( - msg="value of running_config parameter must not be empty for state parsed", - ) - result["parsed"] = self.get_interfaces_facts(data=running_config) - else: - changed_interfaces_facts = [] - - if self.state in self.ACTION_STATES: - result["before"] = existing_interfaces_facts - if result["changed"]: - result["after"] = changed_interfaces_facts - elif self.state == "gathered": - result["gathered"] = changed_interfaces_facts - - result["warnings"] = warnings - return result - - def set_config(self, existing_interfaces_facts): - """Collect the configuration from the args passed to the module, - collect the current configuration (as a dict from facts) - :rtype: A list - :returns: the commands necessary to migrate the current configuration - to the desired configuration + :rtype: A dictionary + :returns: The result from module execution """ - want = self._module.params["config"] - have = existing_interfaces_facts - resp = self.set_state(want, have) - return to_list(resp) - - def set_state(self, want, have): - """Select the appropriate function based on the state provided - - :param want: the desired configuration as a dictionary - :param have: the current configuration as a dictionary - :rtype: A list - :returns: the commands necessary to migrate the current configuration - to the desired configuration + if self.state not in ["parsed", "gathered"]: + self.generate_commands() + self.run_commands() + return self.result + + def generate_commands(self): + """Generate configuration commands to send based on + want, have and desired state. """ - commands = [] - - if self.state in ("merged", "replaced", "overridden", "rendered") and not want: + if self.state in ("merged", "replaced", "overridden", "rendered") and not self.want: self._module.fail_json( msg="value of config parameter must not be empty for state {0}".format(self.state), ) - if self.state == "overridden": - commands.extend(self._state_overridden(want=want, have=have)) - - elif self.state == "deleted": - if not want: - for intf in have: - commands.extend(self._state_deleted({"name": intf["name"]}, intf)) - else: - for item in want: - obj_in_have = search_obj_in_list(item["name"], have) - commands.extend(self._state_deleted(item, obj_in_have)) - else: - for item in want: - name = item["name"] - enable_state = item["enabled"] - obj_in_have = search_obj_in_list(name, have) - if not obj_in_have: - obj_in_have = {"name": name, "enabled": enable_state} - - if self.state in ("merged", "rendered"): - commands.extend(self._state_merged(item, obj_in_have)) - - elif self.state == "replaced": - commands.extend(self._state_replaced(item, obj_in_have)) - - return commands + wantd = self._list_to_dict(self.want) + haved = self._list_to_dict(self.have) - def _state_replaced(self, want, have): - """The command generator when state is replaced - - :rtype: A list - :returns: the commands necessary to migrate the current configuration - to the desired configuration - """ - commands = [] - if have: - commands.extend(self._state_deleted(want, have)) - - commands.extend(self._state_merged(want, have)) - - return commands - - def _state_overridden(self, want, have): - """The command generator when state is overridden - - :rtype: A list - :returns: the commands necessary to migrate the current configuration - to the desired configuration - """ - commands = [] - - for intf in have: - intf_in_want = search_obj_in_list(intf["name"], want) - if not intf_in_want: - commands.extend(self._state_deleted({"name": intf["name"]}, intf)) - - for intf in want: - intf_in_have = search_obj_in_list(intf["name"], have) - if not intf_in_have: - intf_in_have = { - "name": intf["name"], - "enabled": intf["enabled"], - } - commands.extend(self._state_replaced(intf, intf_in_have)) - - return commands - - def _state_merged(self, want, have): - """The command generator when state is merged - - :rtype: A list - :returns: the commands necessary to merge the provided into - the current configuration - """ - commands = [] - want_copy = deepcopy(remove_empties(want)) - have_copy = deepcopy(have) - - want_vifs = want_copy.pop("vifs", []) - have_vifs = have_copy.pop("vifs", []) - - updates = dict_diff(have_copy, want_copy) - - if updates: - for key, value in updates.items(): - commands.append( - self._compute_commands(key=key, value=value, interface=want_copy["name"]), + for name in wantd: + if not get_interface_type(name): + self._module.fail_json( + msg="interface {0} is not a valid interface type".format(name), ) - if want_vifs: - for want_vif in want_vifs: - have_vif = search_obj_in_list(want_vif["vlan_id"], have_vifs, key="vlan_id") - if not have_vif: - have_vif = { - "vlan_id": want_vif["vlan_id"], - "enabled": True, - } - - vif_updates = dict_diff(have_vif, want_vif) - if vif_updates: - for key, value in vif_updates.items(): - commands.append( - self._compute_commands( - key=key, - value=value, - interface=want_copy["name"], - vif=want_vif["vlan_id"], - ), - ) - - return commands - - def _state_deleted(self, want, have): - """The command generator when state is deleted + if self.state == "deleted": + # reset the given interfaces, or all of them + for name in wantd or haved: + if name in haved: + self._compare(name, self._reset(name), haved[name]) + return - :rtype: A list - :returns: the commands necessary to remove the current configuration - of the provided objects - """ - commands = [] - - want_copy = deepcopy(remove_empties(want)) - have_copy = deepcopy(have) - want_vifs = want_copy.pop("vifs", []) - have_vifs = have_copy.pop("vifs", []) + if self.state == "overridden": + for name, have in haved.items(): + if name not in wantd: + self._compare(name, self._reset(name), have) + + for name, want in wantd.items(): + # an interface not on the device yet keeps its wanted enabled state + have = haved.get(name, {"name": name, "enabled": want.get("enabled", True)}) + if self.state in ("merged", "rendered"): + want = dict_merge(have, want) + self._compare(name, want, have) + + def _compare(self, name, want, have): + """Compare one interface: its options, enabled, and its vifs.""" + base = {"name": name, "type": get_interface_type(name)} + want = deepcopy(want) + have = deepcopy(have) + wvifs = want.pop("vifs", {}) or {} + hvifs = have.pop("vifs", {}) or {} + + self.compare(parsers=self.parsers, want=dict(want, **base), have=dict(have, **base)) + self._compare_enabled( + base, + "disable", + want.get("enabled", True), + have.get("enabled", True), + ) - for key in dict_delete(have_copy, want_copy).keys(): - if key == "enabled": - continue - commands.append( - self._compute_commands(key=key, interface=want_copy["name"], remove=True), + for vlan_id, wvif in wvifs.items(): + hvif = hvifs.pop(vlan_id, {"vlan_id": vlan_id, "enabled": True}) + self.compare( + parsers=self.vif_parsers, + want=dict(base, vifs=wvif), + have=dict(base, vifs=hvif), ) - if have_copy["enabled"] is False and not ( - "enabled" in want_copy and want_copy["enabled"] is False - ): - commands.append( - self._compute_commands(key="enabled", value=True, interface=want_copy["name"]), + self._compare_enabled( + dict(base, vifs={"vlan_id": vlan_id}), + "vifs.disable", + wvif.get("enabled", True), + hvif.get("enabled", True), ) - - if have_vifs: - for have_vif in have_vifs: - want_vif = search_obj_in_list(have_vif["vlan_id"], want_vifs, key="vlan_id") - if not want_vif: - commands.append( - self._compute_commands( - key="", - interface=want_copy["name"], - vif=have_vif["vlan_id"], - remove=True, - ), - ) - continue - - for key in dict_delete(have_vif, want_vif).keys(): - if key == "enabled": - continue - commands.append( - self._compute_commands( - key=key, - interface=want_copy["name"], - vif=want_vif["vlan_id"], - remove=True, - ), - ) - return commands - - def _compute_commands(self, interface, key, vif=None, value=None, remove=False): - interface_type = get_interface_type(interface) - if not interface_type: - self._module.fail_json( - msg="interface {0} is not a valid interface type".format(interface), - ) - intf_context = "interfaces {0} {1}".format(interface_type, interface) - set_cmd = "set {0}".format(intf_context) - del_cmd = "delete {0}".format(intf_context) - - if vif: - set_cmd = set_cmd + (" vif {0}".format(vif)) - del_cmd = del_cmd + (" vif {0}".format(vif)) - if key == "" or key is None: - if not remove: - command = "{0}".format(set_cmd) - else: - command = "{0}".format(del_cmd) - elif key == "enabled": - if not value: - command = "{0} disable".format(set_cmd) - else: - command = "{0} disable".format(del_cmd) - else: - if not remove: - command = "{0} {1} '{2}'".format(set_cmd, key, value) - else: - command = "{0} {1}".format(del_cmd, key) - - return command + # vifs the configuration does not list are deleted as a whole + for vlan_id in hvifs: + self.addcmd(dict(base, vifs={"vlan_id": vlan_id}), "vifs", True) + + def _compare_enabled(self, data, parser, want, have): + """enabled is the inverse of the disable flag: false sets it, true deletes it.""" + if want != have: + self.addcmd(data, parser, want) + + def _reset(self, name): + """The configuration of an interface without any options: enabled, no vifs.""" + return {"name": name, "enabled": True} + + def _list_to_dict(self, entries): + """Interfaces by name, and their vifs by VLAN ID.""" + result = {} + for entry in deepcopy(entries) or []: + vifs = entry.get("vifs") or [] + entry["vifs"] = {vif["vlan_id"]: vif for vif in vifs} + result[entry["name"]] = entry + return result diff --git a/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py b/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py index 30d619a3..40683f7b 100644 --- a/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py @@ -1,146 +1,133 @@ # # -*- coding: utf-8 -*- # Copyright 2019 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The vyos interfaces fact class It is in this file the configuration is collected from the device for a given resource, parsed, and the facts tree is populated based on the configuration. """ from __future__ import absolute_import, division, print_function -__metaclass__ = type +__metaclass__ = type -from copy import deepcopy -from re import M, findall, search from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.interfaces.interfaces import ( InterfacesArgs, ) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.interfaces import ( + InterfacesTemplate, +) class InterfacesFacts(object): """The vyos interfaces fact class""" def __init__(self, module, subspec="config", options="options"): self._module = module self.argument_spec = InterfacesArgs.argument_spec - spec = deepcopy(self.argument_spec) - if subspec: - if options: - facts_argument_spec = spec[subspec][options] - else: - facts_argument_spec = spec[subspec] - else: - facts_argument_spec = spec - - self.generated_spec = utils.generate_dict(facts_argument_spec) def get_device_data(self, connection): data = connection.get_config(flags=["| grep interfaces"]) return data def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_device_data(connection) - objs = [] - interface_names = findall( - r"^set interfaces (?:ethernet|bonding|bridge|dummy|tunnel|vti|loopback|vxlan|openvpn|wireguard) (?:\'*)(\S+)(?:\'*)", - data, - M, - ) - if interface_names: - for interface in set(interface_names): - intf_regex = r" %s (.+$)" % interface.strip("'") - cfg = findall(intf_regex, data, M) - obj = self.render_config(cfg) - obj["name"] = interface.strip("'") - if obj: - objs.append(obj) + parser = InterfacesTemplate(lines=data.splitlines(), module=self._module) + objs = list(parser.parse().values()) + for obj in objs: + # disable is the only state on the device; enabled is the default + obj.setdefault("enabled", True) + if "vifs" in obj: + for vif in obj["vifs"].values(): + vif.setdefault("enabled", True) + obj["vifs"] = sorted(obj["vifs"].values(), key=lambda vif: int(vif["vlan_id"])) + facts = {} if objs: facts["interfaces"] = [] params = utils.validate_config(self.argument_spec, {"config": objs}) for cfg in params["config"]: facts["interfaces"].append(utils.remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts - def render_config(self, conf): - """ - Render config as dictionary structure and delete keys - from spec for null values - - :param spec: The facts tree, generated from the argspec - :param conf: The configuration - :rtype: dictionary - :returns: The generated config - """ - vif_conf = "\n".join(filter(lambda x: ("vif" in x), conf)) - eth_conf = "\n".join(filter(lambda x: ("vif" not in x), conf)) - config = self.parse_attribs(["description", "speed", "mtu", "duplex", "vrf"], eth_conf) - config["vifs"] = self.parse_vifs(vif_conf) - - return utils.remove_empties(config) - - def parse_vifs(self, conf): - vif_names = findall(r"vif (?:\'*)(\d+)(?:\'*)", conf, M) - vifs_list = None - - if vif_names: - vifs_list = [] - for vif in set(vif_names): - vif_regex = r"%s (.+$)" % vif - cfg = "\n".join(findall(vif_regex, conf, M)) - obj = self.parse_attribs(["description", "mtu"], cfg) - obj["vlan_id"] = int(vif) - if obj: - vifs_list.append(obj) - vifs_list = sorted(vifs_list, key=lambda i: i["vlan_id"]) - - return vifs_list - - def parse_attribs(self, attribs, conf): - """ - Parse the attributes of a network interface. - - :param attribs: List of attribute names. - :param conf: Configuration string. - :rtype: dict - :returns: Parsed configuration dictionary. - """ - config = {} - for item in attribs: - value = utils.parse_conf_arg(conf, item) - if value and item == "mtu": - config[item] = int(value.strip("'")) - elif value: - config[item] = value.strip("'") - else: - config[item] = None - - # match only on disable next to the interface name - # there are other sub-commands that can be disabled - match = search(r"^ *disable", conf, M) - if match: - config["enabled"] = False - else: - config["enabled"] = True - - return utils.remove_empties(config) + # def render_config(self, conf): + # """ + # Render config as dictionary structure and delete keys + # from spec for null values + + # :param spec: The facts tree, generated from the argspec + # :param conf: The configuration + # :rtype: dictionary + # :returns: The generated config + # """ + # vif_conf = "\n".join(filter(lambda x: ("vif" in x), conf)) + # eth_conf = "\n".join(filter(lambda x: ("vif" not in x), conf)) + # config = self.parse_attribs(["description", "speed", "mtu", "duplex", "vrf"], eth_conf) + # config["vifs"] = self.parse_vifs(vif_conf) + + # return utils.remove_empties(config) + + # def parse_vifs(self, conf): + # vif_names = findall(r"vif (?:\'*)(\d+)(?:\'*)", conf, M) + # vifs_list = None + + # if vif_names: + # vifs_list = [] + # for vif in set(vif_names): + # vif_regex = r"%s (.+$)" % vif + # cfg = "\n".join(findall(vif_regex, conf, M)) + # obj = self.parse_attribs(["description", "mtu"], cfg) + # obj["vlan_id"] = int(vif) + # if obj: + # vifs_list.append(obj) + # vifs_list = sorted(vifs_list, key=lambda i: i["vlan_id"]) + + # return vifs_list + + # def parse_attribs(self, attribs, conf): + # """ + # Parse the attributes of a network interface. + + # :param attribs: List of attribute names. + # :param conf: Configuration string. + # :rtype: dict + # :returns: Parsed configuration dictionary. + # """ + # config = {} + # for item in attribs: + # value = utils.parse_conf_arg(conf, item) + # if value and item == "mtu": + # config[item] = int(value.strip("'")) + # elif value: + # config[item] = value.strip("'") + # else: + # config[item] = None + + # # match only on disable next to the interface name + # # there are other sub-commands that can be disabled + # match = search(r"^ *disable", conf, M) + # if match: + # config["enabled"] = False + # else: + # config["enabled"] = True + + # return utils.remove_empties(config) diff --git a/plugins/module_utils/network/vyos/rm_templates/interfaces.py b/plugins/module_utils/network/vyos/rm_templates/interfaces.py new file mode 100644 index 00000000..3bd74453 --- /dev/null +++ b/plugins/module_utils/network/vyos/rm_templates/interfaces.py @@ -0,0 +1,149 @@ +# -*- coding: utf-8 -*- +# Copyright 2026 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +""" +The Interfaces parser templates file. This contains +a list of parser definitions and associated functions that +facilitates both facts gathering and native command generation for +the given network resource. +""" + +import re + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.network_template import ( + NetworkTemplate, +) + + +# interface types the module manages +TYPES = r"ethernet|bonding|bridge|dummy|tunnel|vti|loopback|vxlan|openvpn|wireguard" + +# set interfaces ; the name is quoted when it ends the line ('lo') +INTF = ( + r""" + ^set + \s+interfaces + \s+(?P""" + + TYPES + + r""") + \s+'?(?P[^'\s]+)'? +""" +) + +# ... vif +VIF = ( + INTF + + r""" + \s+vif + \s+(?P\d+) +""" +) + +# a value, quoted ('text with spaces') or not +VALUE = r""" + \s+(?:'(?P.*)'|(?P\S+)) + \s*$ +""" + +VAL = "{{ q if q is defined else u }}" + + +def _intf_parser(name, keyword, key=None): + """An interface option with a value, e.g. description 'text'.""" + key = key or name + return { + "name": name, + "getval": re.compile(INTF + r"\s+" + keyword + VALUE, re.VERBOSE), + "setval": "interfaces {{ type }} {{ name }} " + keyword + " '{{ " + key + " }}'", + "remval": "interfaces {{ type }} {{ name }} " + keyword, + "result": {"{{ name }}": {"name": "{{ name }}", key: VAL}}, + } + + +def _vif_parser(name, keyword, key): + """A vif option with a value, e.g. vif 10 description 'text'.""" + return { + "name": name, + "getval": re.compile(VIF + r"\s+" + keyword + VALUE, re.VERBOSE), + "setval": "interfaces {{ type }} {{ name }} vif {{ vifs.vlan_id }} " + + keyword + + " '{{ vifs." + + key + + " }}'", + "remval": "interfaces {{ type }} {{ name }} vif {{ vifs.vlan_id }} " + keyword, + "compval": "vifs." + key, + "result": { + "{{ name }}": { + "name": "{{ name }}", + "vifs": {"{{ vlan }}": {"vlan_id": "{{ vlan }}", key: VAL}}, + }, + }, + } + + +class InterfacesTemplate(NetworkTemplate): + def __init__(self, lines=None, module=None): + prefix = {"set": "set", "remove": "delete"} + super(InterfacesTemplate, self).__init__( + lines=lines, + tmplt=self, + prefix=prefix, + module=module, + ) + + # The first matching parser wins for each line, so specific parsers come first, + # then the vif catch-all, then the interface catch-all. + # fmt: off + PARSERS = [ + _intf_parser("description", "description"), + _intf_parser("mtu", "mtu"), + _intf_parser("duplex", "duplex"), + _intf_parser("speed", "speed"), + _intf_parser("vrf", "vrf"), + { + "name": "disable", + "getval": re.compile(INTF + r"\s+disable\s*$", re.VERBOSE), + "setval": "interfaces {{ type }} {{ name }} disable", + "result": {"{{ name }}": {"name": "{{ name }}", "enabled": False}}, + }, + _vif_parser("vifs.description", "description", "description"), + _vif_parser("vifs.mtu", "mtu", "mtu"), + { + "name": "vifs.disable", + "getval": re.compile(VIF + r"\s+disable\s*$", re.VERBOSE), + "setval": "interfaces {{ type }} {{ name }} vif {{ vifs.vlan_id }} disable", + "result": { + "{{ name }}": { + "name": "{{ name }}", + "vifs": {"{{ vlan }}": {"vlan_id": "{{ vlan }}", "enabled": False}}, + }, + }, + }, + { + # any other vif line: the vif exists + "name": "vifs", + "getval": re.compile(VIF + r"(?:\s.*)?$", re.VERBOSE), + "setval": "interfaces {{ type }} {{ name }} vif {{ vifs.vlan_id }}", + "result": { + "{{ name }}": { + "name": "{{ name }}", + "vifs": {"{{ vlan }}": {"vlan_id": "{{ vlan }}"}}, + }, + }, + }, + { + # any other interface line: the interface exists + "name": "name", + "getval": re.compile(INTF + r"(?:\s.*)?$", re.VERBOSE), + "setval": "interfaces {{ type }} {{ name }}", + "result": {"{{ name }}": {"name": "{{ name }}"}}, + }, + ] + # fmt: on diff --git a/tests/unit/modules/network/vyos/test_vyos_interfaces.py b/tests/unit/modules/network/vyos/test_vyos_interfaces.py index a8f812c1..33241e4a 100644 --- a/tests/unit/modules/network/vyos/test_vyos_interfaces.py +++ b/tests/unit/modules/network/vyos/test_vyos_interfaces.py @@ -1,636 +1,705 @@ # (c) 2016 Red Hat Inc. # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # Make coding more python3-ish from __future__ import absolute_import, division, print_function + __metaclass__ = type from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_interfaces from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture class TestVyosInterfacesModule(TestVyosModule): module = vyos_interfaces def setUp(self): super(TestVyosInterfacesModule, self).setUp() self.mock_get_config = patch( - "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.get_config", + "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_config = self.mock_get_config.start() self.mock_load_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.load_config", ) self.load_config = self.mock_load_config.start() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_get_resource_connection_facts = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", ) self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() self.mock_execute_show_command = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos." "facts.interfaces.interfaces.InterfacesFacts.get_device_data", ) self.execute_show_command = self.mock_execute_show_command.start() # define the default fixture for the vyos_interfaces module self.fixture_path = "vyos_interfaces_config.cfg" def tearDown(self): super(TestVyosInterfacesModule, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_get_resource_connection_facts.stop() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_execute_show_command.stop() def load_fixtures(self, commands=None, filename=None): def load_from_file(*args, **kwargs): return load_fixture(self.fixture_path) self.execute_show_command.side_effect = load_from_file def test_vyos_interfaces_merged(self): set_module_args( dict( config=[ dict(name="bond1", description="Bond - 1", enabled=True), dict(name="vtun1", description="vtun - 1", enabled=True), dict(name="wg01", description="wg - 1", enabled=True), ], state="merged", ), ) commands = [ "set interfaces bonding bond1 description 'Bond - 1'", "set interfaces openvpn vtun1 description 'vtun - 1'", "set interfaces wireguard wg01 description 'wg - 1'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_merged_retain_vif(self): # we have a vif in eth1 at this point, so that should be retained self.fixture_path = "vyos_interfaces_config_vif.cfg" set_module_args( dict( config=[ dict(name="bond1", description="Bond - 1", enabled=True), dict(name="vtun1", description="vtun - 1", enabled=True), dict(name="wg01", description="wg - 1", enabled=True), ], state="merged", ), ) commands = [ "set interfaces bonding bond1 description 'Bond - 1'", "set interfaces openvpn vtun1 description 'vtun - 1'", "set interfaces wireguard wg01 description 'wg - 1'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_merged_additional_vif(self): # we have a vif in eth1 at this point, so that should be retained self.fixture_path = "vyos_interfaces_config_vif.cfg" set_module_args( dict( config=[ dict( name="eth1", vifs=[ dict( vlan_id=105, description="vlan 105", ), ], ), ], state="merged", ), ) commands = [ "set interfaces ethernet eth1 vif 105 description 'vlan 105'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_merged_idempotent(self): set_module_args( dict( config=[ dict( name="wg02", description="wire guard int 2", enabled=True, ), ], state="merged", ), ) self.execute_module(changed=False, commands=[]) def test_vyos_interfaces_merged_newinterface(self): set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict(name="eth1", description="Configured by Ansible"), ], state="merged", ), ) commands = [ "set interfaces ethernet eth1 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_replaced_newinterface(self): set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict(name="eth1", description="Configured by Ansible"), ], state="replaced", ), ) commands = [ "set interfaces ethernet eth1 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_replaced_remove_vif(self): # we have a vif in eth1 at this point, so that should be removed self.fixture_path = "vyos_interfaces_config_vif.cfg" set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict(name="eth1", description="Configured by Ansible"), ], state="replaced", ), ) commands = [ "delete interfaces ethernet eth1 vif 200", "delete interfaces ethernet eth1 vif 201", "set interfaces ethernet eth1 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_replaced_remove_vrf(self): # we have a vrf in eth2 at this point, so that should be removed self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict(name="eth2", description="Configured by Ansible"), ], state="replaced", ), ) commands = [ "delete interfaces ethernet eth2 vrf", "set interfaces ethernet eth2 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_merged_enable_vif(self): # merge in enabling vif self.fixture_path = "vyos_interfaces_config_vif.cfg" set_module_args( dict( config=[ dict( name="eth1", vifs=[ dict( vlan_id=201, enabled=True, ), ], ), ], state="merged", ), ) commands = [ "delete interfaces ethernet eth1 vif 201 disable", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_replaced_retain_vif(self): # we have a vif in eth1 at this point, so that should be removed self.fixture_path = "vyos_interfaces_config_vif.cfg" set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict( name="eth1", description="Configured by Ansible", vifs=[ dict( vlan_id=200, ), dict( vlan_id=201, description="VIF eth1.201", enabled=True, ), ], ), ], state="replaced", ), ) commands = [ "delete interfaces ethernet eth1 vif 200 description", "delete interfaces ethernet eth1 vif 201 disable", "set interfaces ethernet eth1 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_overridden_newinterface(self): set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict(name="eth1", description="Configured by Ansible"), ], state="overridden", ), ) commands = [ "set interfaces ethernet eth1 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", "delete interfaces wireguard wg02 description", "delete interfaces ethernet eth3 description", "delete interfaces ethernet eth3 disable", ] self.execute_module(changed=True, commands=commands) def test_vyos_overridden_remove_vif(self): # we have a vif in eth1 at this point, so that should be removed self.fixture_path = "vyos_interfaces_config_vif.cfg" set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict(name="eth1", description="Configured by Ansible"), ], state="overridden", ), ) commands = [ "set interfaces ethernet eth1 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", "delete interfaces wireguard wg02 description", "delete interfaces ethernet eth3 description", "delete interfaces ethernet eth3 disable", "delete interfaces ethernet eth1 vif 200", "delete interfaces ethernet eth1 vif 201", ] self.execute_module(changed=True, commands=commands) def test_vyos_overridden_remove_vrf(self): # we have a vrf in eth2 at this point, so that should be removed self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[ dict( name="eth4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), dict(name="eth2", description="Configured by Ansible"), ], state="overridden", ), ) commands = [ "set interfaces ethernet eth2 description 'Configured by Ansible'", "set interfaces ethernet eth4 description 'Ethernet 4'", "set interfaces ethernet eth4 duplex 'auto'", "set interfaces ethernet eth4 speed 'auto'", "delete interfaces ethernet eth2 vrf", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_idempotent_disable(self): set_module_args( dict( config=[ dict( name="eth3", description="Ethernet 3", enabled=False, ), ], state="merged", ), ) commands = [] self.execute_module(changed=False, commands=commands) def test_vyos_interfaces_idempotent_disable_replace(self): set_module_args( dict( config=[ dict( name="eth3", description="Ethernet 3", enabled=False, ), ], state="replaced", ), ) commands = [] self.execute_module(changed=False, commands=commands) def test_vyos_interfaces_deleted_remove_vif(self): # we have a vif in eth1 at this point, so that should be removed self.fixture_path = "vyos_interfaces_config_vif.cfg" set_module_args( dict( config=[ dict(name="eth1"), ], state="deleted", ), ) commands = [ "delete interfaces ethernet eth1 vif 200", "delete interfaces ethernet eth1 vif 201", "delete interfaces ethernet eth1 description", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_deleted_remove_vrf(self): # we have a vrf in eth2 at this point, so that should be removed self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[ dict(name="eth2"), ], state="deleted", ), ) commands = [ "delete interfaces ethernet eth2 vrf", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_deleted_remove_all(self): # we have a vif in eth1 at this point, so that should be removed set_module_args( dict( config=[], state="deleted", ), ) commands = [ "delete interfaces ethernet eth1 description", "delete interfaces ethernet eth3 description", "delete interfaces ethernet eth3 disable", "delete interfaces wireguard wg02 description", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_replaced_bad_name(self): set_module_args( dict( config=[ dict( name="int4", description="Ethernet 4", enabled=True, speed="auto", duplex="auto", ), ], state="replaced", ), ) self.execute_module(failed=True) def test_vyos_interfaces_merged_idempotent_vrf(self): self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[ dict(name="eth2", vrf="green"), ], state="merged", ), ) commands = [] self.execute_module(changed=False, commands=commands) def test_vyos_interfaces_replaced_idempotent_vrf(self): self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[ dict(name="eth2", vrf="green"), ], state="replaced", ), ) commands = [] self.execute_module(changed=False, commands=commands) def test_vyos_interfaces_overridden_idempotent_vrf(self): self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[ dict(name="eth2", vrf="green"), ], state="overridden", ), ) commands = [] self.execute_module(changed=False, commands=commands) def test_vyos_interfaces_merged_additional_vrf(self): self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[ dict( name="eth1", vrf="pink", ), ], state="merged", ), ) commands = [ "set interfaces ethernet eth1 vrf 'pink'", ] self.execute_module(changed=True, commands=commands) def test_vyos_interfaces_gathered_vrf(self): # gathered facts should surface the vrf set on eth2 self.fixture_path = "vyos_interfaces_config_vrf.cfg" set_module_args( dict( config=[], state="gathered", ), ) result = self.execute_module(changed=False) eth2_facts = [i for i in result["gathered"] if i["name"] == "eth2"][0] self.assertEqual(eth2_facts["vrf"], "green") def test_vyos_interfaces_parsed_vrf(self): # parsing a running_config string should surface vrf on eth2 set_module_args( dict( running_config="set interfaces ethernet eth2 vrf 'green'", state="parsed", ), ) result = self.execute_module(changed=False) eth2_facts = [i for i in result["parsed"] if i["name"] == "eth2"][0] self.assertEqual(eth2_facts["vrf"], "green") def test_vyos_interfaces_rendered_vrf(self): # rendered output should include the vrf set command without touching a device set_module_args( dict( config=[ dict( name="eth1", vrf="pink", ), ], state="rendered", ), ) result = self.execute_module(changed=False) commands = [ "set interfaces ethernet eth1 vrf 'pink'", ] self.assertEqual(sorted(result["rendered"]), sorted(commands)) + + def test_vyos_interfaces_parsed_disable_prefixed_options(self): + # disable-link-detect is not disable: the interface stays enabled + set_module_args( + dict( + running_config="set interfaces ethernet eth0 disable-link-detect\n" + "set interfaces ethernet eth0 hw-id '00:50:56:00:00:01'", + state="parsed", + ), + ) + self.assertEqual( + self.execute_module(changed=False)["parsed"], + [{"name": "eth0", "enabled": True}], + ) + + def test_vyos_interfaces_parsed_vif_ids_kept_apart(self): + # vif 110's lines must not end up in vif 10 + set_module_args( + dict( + running_config="set interfaces ethernet eth1 vif 10 description 'ten'\n" + "set interfaces ethernet eth1 vif 110 description 'one-ten'\n" + "set interfaces ethernet eth1 vif 110 mtu '1400'", + state="parsed", + ), + ) + self.assertEqual( + self.execute_module(changed=False)["parsed"], + [ + { + "name": "eth1", + "enabled": True, + "vifs": [ + {"vlan_id": 10, "description": "ten", "enabled": True}, + {"vlan_id": 110, "description": "one-ten", "mtu": 1400, "enabled": True}, + ], + }, + ], + ) + + def test_vyos_interfaces_parsed_vif_in_description(self): + # a description containing "vif" is an interface option, not a vif line + set_module_args( + dict( + running_config="set interfaces ethernet eth2 description 'vif uplink'\n" + "set interfaces ethernet eth2 mtu '1450'", + state="parsed", + ), + ) + self.assertEqual( + self.execute_module(changed=False)["parsed"], + [{"name": "eth2", "description": "vif uplink", "mtu": 1450, "enabled": True}], + ) + + def test_vyos_interfaces_replaced_enable_once(self): + # re-enabling a disabled interface deletes disable once, not twice + set_module_args(dict(config=[dict(name="eth3")], state="replaced")) + self.execute_module( + changed=True, + commands=[ + "delete interfaces ethernet eth3 description", + "delete interfaces ethernet eth3 disable", + ], + ) + + def test_vyos_interfaces_deleted_not_on_device(self): + # an interface without configuration on the device: nothing to delete + set_module_args(dict(config=[dict(name="eth9")], state="deleted")) + self.execute_module(changed=False, commands=[])