diff --git a/changelogs/fragments/t9421_vyos_interfaces_rm_rewrite.yml b/changelogs/fragments/t9421_vyos_interfaces_rm_rewrite.yml index afb7268c..2cd045fa 100644 --- a/changelogs/fragments/t9421_vyos_interfaces_rm_rewrite.yml +++ b/changelogs/fragments/t9421_vyos_interfaces_rm_rewrite.yml @@ -1,20 +1,24 @@ --- 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. + - vyos_interfaces - VXLAN interfaces (vxlanN) are recognised as an interface type; an unchanged + VXLAN configuration no longer fails with "is not a valid interface type", and changes to a + VXLAN interface render the right commands. The shared interface type helper used by other + modules now recognises VXLAN interfaces as well. trivial: - vyos_interfaces - rewritten on the resource module template framework (rm_template); options, output and behaviour are unchanged apart from the fixes above. minor_changes: - vyos_interfaces - new option tunnel, for tunnel interfaces - encapsulation, source_address, source_interface, remote, enable_multicast, and parameters.ip with key, ttl, tos, no_pmtu_discovery and ignore_df (T9422). Without the tunnel option, the tunnel definition on the device is left as it is, in every state. diff --git a/plugins/module_utils/network/vyos/utils/utils.py b/plugins/module_utils/network/vyos/utils/utils.py index 89f12773..b8f5fcec 100644 --- a/plugins/module_utils/network/vyos/utils/utils.py +++ b/plugins/module_utils/network/vyos/utils/utils.py @@ -1,346 +1,350 @@ # -*- 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) # utils from __future__ import absolute_import, division, print_function + __metaclass__ = type from ansible.module_utils.basic import missing_required_lib + try: import ipaddress HAS_IPADDRESS = True except ImportError: HAS_IPADDRESS = False def search_obj_in_list(name, lst, key="name"): if lst: for item in lst: if item[key] == name: return item return None def get_interface_type(interface): """Gets the type of interface""" if interface.startswith("eth"): return "ethernet" elif interface.startswith("bond"): return "bonding" elif interface.startswith("vti"): return "vti" elif interface.startswith("lo"): return "loopback" elif interface.startswith("vtun"): return "openvpn" elif interface.startswith("wg"): return "wireguard" elif interface.startswith("tun"): return "tunnel" elif interface.startswith("br"): return "bridge" elif interface.startswith("dum"): return "dummy" + elif interface.startswith("vxlan"): + return "vxlan" def get_interface_with_vif(interface): """Gets virtual interface if any or return as is""" vlan = None interface_real = interface if "." in interface: interface_real, vlan = interface.split(".") if vlan is not None: interface_real = interface_real + " vif " + vlan return interface_real def dict_delete(base, comparable): """ This function generates a dict containing key, value pairs for keys that are present in the `base` dict but not present in the `comparable` dict. :param base: dict object to base the diff on :param comparable: dict object to compare against base :returns: new dict object with key, value pairs that needs to be deleted. """ to_delete = dict() for key in base: if isinstance(base[key], dict): sub_diff = dict_delete(base[key], comparable.get(key, {})) if sub_diff: to_delete[key] = sub_diff else: if key not in comparable: to_delete[key] = base[key] return to_delete def diff_list_of_dicts(want, have): diff = [] set_w = set(tuple(d.items()) for d in want) set_h = set(tuple(d.items()) for d in have) difference = set_w.difference(set_h) for element in difference: diff.append(dict((x, y) for x, y in element)) return diff def get_lst_diff_for_dicts(want, have, lst): """ This function generates a list containing values that are only in want and not in list in have dict :param want: dict object to want :param have: dict object to have :param lst: list the diff on :return: new list object with values which are only in want. """ if not have: diff = want.get(lst) or [] else: want_elements = want.get(lst) or {} have_elements = have.get(lst) or {} diff = list_diff_want_only(want_elements, have_elements) return diff def get_lst_same_for_dicts(want, have, lst): """ This function generates a list containing values that are common for list in want and list in have dict :param want: dict object to want :param have: dict object to have :param lst: list the comparison on :return: new list object with values which are common in want and have. """ diff = None if want and have: want_list = want.get(lst) or {} have_list = have.get(lst) or {} diff = [i for i in want_list and have_list if i in have_list and i in want_list] return diff def list_diff_have_only(want_list, have_list): """ This function generated the list containing values that are only in have list. :param want_list: :param have_list: :return: new list with values which are only in have list """ if have_list and not want_list: diff = have_list elif not have_list: diff = None else: diff = [i for i in have_list + want_list if i in have_list and i not in want_list] return diff def list_diff_want_only(want_list, have_list): """ This function generated the list containing values that are only in want list. :param want_list: :param have_list: :return: new list with values which are only in want list """ if have_list and not want_list: diff = None elif not have_list: diff = want_list else: diff = [i for i in have_list + want_list if i in want_list and i not in have_list] return diff def search_dict_tv_in_list(d_val1, d_val2, lst, key1, key2): """ This function return the dict object if it exist in list. :param d_val1: :param d_val2: :param lst: :param key1: :param key2: :return: """ obj = next( (item for item in lst if item[key1] == d_val1 and item[key2] == d_val2), None, ) if obj: return obj else: return None def key_value_in_dict(have_key, have_value, want_dict): """ This function checks whether the key and values exist in dict :param have_key: :param have_value: :param want_dict: :return: """ for key, value in want_dict.items(): if key == have_key and value == have_value: return True return False def is_dict_element_present(dict, key): """ This function checks whether the key is present in dict. :param dict: :param key: :return: """ for item in dict: if item == key: return True return False def get_ip_address_version(address): """ This function returns the version of IP address :param address: IP address :return: """ if not HAS_IPADDRESS: raise Exception(missing_required_lib("ipaddress")) try: address = unicode(address) except NameError: address = str(address) version = ipaddress.ip_address(address.split("/")[0]).version return version def get_route_type(address): """ This function returns the route type based on IP address :param address: :return: """ version = get_ip_address_version(address) if version == 6: return "route6" elif version == 4: return "route" def _bool_to_str(val): """ This function converts the bool value into string. :param val: bool value. :return: enable/disable. """ return "enable" if str(val) == "True" else "disable" if str(val) == "False" else val def _is_w_same(w, h, key): """ This function checks whether the key value is same in desired and target config dictionary. :param w: base config. :param h: target config. :param key:attribute name. :return: True/False. """ return True if h and key in h and h[key] == w[key] else False def _in_target(h, key): """ This functi checks whether the target exist and key present in target config. :param h: target config. :param key: attribute name. :return: True/False. """ return True if h and key in h else False def in_target_not_none(h, key): """ This function checks whether the target exist,key present in target config, and the value is not None. :param h: target config. :param key: attribute name. :return: True/False. """ return True if h and key in h and h[key] is not None else False def combine(a, b, recursive=False, list_merge="replace"): """Merge dict ``b`` into dict ``a``, returning a new dict. :param a: Base dictionary. :param b: Dictionary whose values take precedence over ``a``. :param recursive: When True, nested dicts are merged recursively rather than replaced wholesale. :param list_merge: Controls how list values are combined when the same key exists in both dicts. Supported modes: - ``"replace"`` *(default)* — ``b``'s list replaces ``a``'s list. - ``"append"`` — ``b``'s list is appended to ``a``'s list (duplicates kept). - ``"prepend"`` — ``b``'s list is prepended to ``a``'s list (duplicates kept). - ``"append_rp"`` — like ``"append"`` but duplicates are removed, preserving the first occurrence (rp = remove-preserve). - ``"prepend_rp"`` — like ``"prepend"`` but duplicates are removed, preserving the first occurrence. Passing any other value raises ``ValueError``. :returns: New merged dict. :raises ValueError: If either argument is not a dict, or if an unsupported ``list_merge`` mode is given. """ if not isinstance(a, dict) or not isinstance(b, dict): raise ValueError("combine expects two dictionaries") result = a.copy() for k, v in b.items(): if k in result: # dict merge if recursive and isinstance(result[k], dict) and isinstance(v, dict): result[k] = combine(result[k], v, recursive=True, list_merge=list_merge) # list merge elif isinstance(result[k], list) and isinstance(v, list): if list_merge == "replace": result[k] = v elif list_merge == "append": result[k] = result[k] + v elif list_merge == "prepend": result[k] = v + result[k] elif list_merge == "append_rp": result[k] = list(dict.fromkeys(result[k] + v)) elif list_merge == "prepend_rp": result[k] = list(dict.fromkeys(v + result[k])) else: raise ValueError(f"Unsupported list_merge mode: {list_merge}") # everything else else: result[k] = v else: result[k] = v return result diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_interfaces_config_vxlan.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_interfaces_config_vxlan.cfg new file mode 100644 index 00000000..9b4c3ab4 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_interfaces_config_vxlan.cfg @@ -0,0 +1,3 @@ +set interfaces vxlan vxlan2 description 'overlay' +set interfaces vxlan vxlan2 mtu '1450' +set interfaces vxlan vxlan2 vni '2' diff --git a/tests/unit/modules/network/vyos/test_vyos_interfaces.py b/tests/unit/modules/network/vyos/test_vyos_interfaces.py index e5b962b3..9ef0a903 100644 --- a/tests/unit/modules/network/vyos/test_vyos_interfaces.py +++ b/tests/unit/modules/network/vyos/test_vyos_interfaces.py @@ -1,897 +1,913 @@ # (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.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=[]) def test_vyos_interfaces_parsed_quoted_vif(self): # a vif without options ends the line, and VyOS quotes its VLAN ID set_module_args( dict( running_config="set interfaces ethernet eth1 vif '10'\n" "set interfaces ethernet eth1 vif 20 description 'twenty'", state="parsed", ), ) self.assertEqual( self.execute_module(changed=False)["parsed"], [ { "name": "eth1", "enabled": True, "vifs": [ {"vlan_id": 10, "enabled": True}, {"vlan_id": 20, "description": "twenty", "enabled": True}, ], }, ], ) def test_vyos_interfaces_merged_vif_vrf(self): set_module_args( dict( config=[dict(name="eth1", vifs=[dict(vlan_id=200, vrf="green")])], state="merged", ), ) self.execute_module( changed=True, commands=["set interfaces ethernet eth1 vif 200 vrf 'green'"], ) def test_vyos_interfaces_parsed_vif_vrf(self): set_module_args( dict( running_config="set interfaces ethernet eth1 vif 200 vrf 'green'\n" "set interfaces ethernet eth1 vif 200 description 'in green'", state="parsed", ), ) self.assertEqual( self.execute_module(changed=False)["parsed"], [ { "name": "eth1", "enabled": True, "vifs": [ { "vlan_id": 200, "vrf": "green", "description": "in green", "enabled": True, }, ], }, ], ) def test_vyos_interfaces_replaced_vif_vrf_removed(self): self.fixture_path = "vyos_interfaces_config_vif_vrf.cfg" set_module_args( dict( config=[dict(name="eth1", vifs=[dict(vlan_id=200, description="in green")])], state="replaced", ), ) self.execute_module( changed=True, commands=["delete interfaces ethernet eth1 vif 200 vrf"], ) def test_vyos_interfaces_parsed_tunnel(self): set_module_args( dict( running_config="set interfaces tunnel tun0 encapsulation 'gre'\n" "set interfaces tunnel tun0 source-address '192.0.2.1'\n" "set interfaces tunnel tun0 enable-multicast\n" "set interfaces tunnel tun0 parameters ip key '1'\n" "set interfaces tunnel tun0 parameters ip no-pmtu-discovery", state="parsed", ), ) self.assertEqual( self.execute_module(changed=False)["parsed"], [ { "name": "tun0", "enabled": True, "tunnel": { "encapsulation": "gre", "source_address": "192.0.2.1", "enable_multicast": True, "parameters": {"ip": {"key": 1, "no_pmtu_discovery": True}}, }, }, ], ) def test_vyos_interfaces_rendered_tunnel(self): set_module_args( dict( config=[ dict( name="tun1", tunnel=dict( encapsulation="gre", source_interface="eth0", remote="198.51.100.1", parameters=dict(ip=dict(ttl=0, tos="inherit")), ), ), ], state="rendered", ), ) self.assertEqual( sorted(self.execute_module(changed=False)["rendered"]), [ "set interfaces tunnel tun1 encapsulation 'gre'", "set interfaces tunnel tun1 parameters ip tos 'inherit'", "set interfaces tunnel tun1 parameters ip ttl '0'", "set interfaces tunnel tun1 remote '198.51.100.1'", "set interfaces tunnel tun1 source-interface 'eth0'", ], ) def test_vyos_interfaces_merged_tunnel_idempotent(self): self.fixture_path = "vyos_interfaces_config_tunnel.cfg" set_module_args( dict( config=[ dict( name="tun0", description="dmvpn hub", tunnel=dict( encapsulation="gre", source_address="192.0.2.1", enable_multicast=True, parameters=dict(ip=dict(key=1)), ), ), ], state="merged", ), ) self.execute_module(changed=False, commands=[]) def test_vyos_interfaces_replaced_without_tunnel_keeps_tunnel(self): # no tunnel key: the tunnel definition is left as it is self.fixture_path = "vyos_interfaces_config_tunnel.cfg" set_module_args(dict(config=[dict(name="tun0", description="new")], state="replaced")) self.execute_module( changed=True, commands=["set interfaces tunnel tun0 description 'new'"], ) def test_vyos_interfaces_replaced_tunnel_options(self): # a tunnel key: options it does not list are removed self.fixture_path = "vyos_interfaces_config_tunnel.cfg" set_module_args( dict( config=[ dict( name="tun0", description="dmvpn hub", tunnel=dict(encapsulation="gre", source_address="192.0.2.1"), ), ], state="replaced", ), ) self.execute_module( changed=True, commands=[ "delete interfaces tunnel tun0 enable-multicast", "delete interfaces tunnel tun0 parameters ip key", ], ) def test_vyos_interfaces_tunnel_on_non_tunnel(self): set_module_args( dict(config=[dict(name="eth1", tunnel=dict(encapsulation="gre"))], state="merged"), ) result = self.execute_module(failed=True) self.assertEqual( result["msg"], "tunnel options are only valid for tunnel interfaces, not eth1", ) + + def test_vyos_interfaces_merged_vxlan_unchanged(self): + # an unchanged VXLAN interface: nothing to do, and no failure + self.fixture_path = "vyos_interfaces_config_vxlan.cfg" + set_module_args( + dict(config=[dict(name="vxlan2", description="overlay", mtu=1450)], state="merged"), + ) + self.execute_module(changed=False, commands=[]) + + def test_vyos_interfaces_merged_vxlan(self): + self.fixture_path = "vyos_interfaces_config_vxlan.cfg" + set_module_args(dict(config=[dict(name="vxlan2", description="new")], state="merged")) + self.execute_module( + changed=True, + commands=["set interfaces vxlan vxlan2 description 'new'"], + )