diff --git a/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py b/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py index 780e6ae5..3a40f025 100644 --- a/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py +++ b/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py @@ -1,97 +1,104 @@ # -*- 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 vyos vpp_acl 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 ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.vpp_acl.vpp_acl import ( Vpp_aclArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.vpp_acl import ( Vpp_aclTemplate, ) def _number(item, key): return int(item[key]) def _name_key(item): - """Numeric names (parsed as int) first, in numeric order, then the others.""" + """Integer names first, in numeric order, then all others as text. + + Parsing turns names that look like Python literals into values (20 into int, + 1j into complex, True into bool); only real integers count as numeric here, + so every pair of keys is comparable. + """ name = item["name"] - return (1, str(name)) if isinstance(name, str) else (0, name) + if type(name) is int: + return (0, name, "") + return (1, 0, str(name)) class Vpp_aclFacts(object): """The vyos vpp_acl facts class""" def __init__(self, module, subspec="config", options="options"): self._module = module self.argument_spec = Vpp_aclArgs.argument_spec def get_device_data(self, connection): return connection.get("show configuration commands | grep 'vpp acl'") def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for Vpp_acl network resource :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) lines = [line.replace("'", "") for line in data.splitlines()] objs = Vpp_aclTemplate(lines=lines, module=self._module).parse() for family in ("ip", "mac"): section = objs.get(family) if not section: continue acls = [] for acl in (section.get("acls") or {}).values(): rules = [] for rule in (acl.get("rules") or {}).values(): flags = rule.get("tcp_flags") or {} for key in list(flags): flags[key] = sorted(flags[key].values()) rules.append(rule) if rules: acl["rules"] = sorted(rules, key=lambda r: _number(r, "number")) acls.append(acl) if acls: section["acls"] = sorted(acls, key=_name_key) interfaces = [] for iface in (section.get("interfaces") or {}).values(): for direction in ("input", "output"): if direction in iface: iface[direction] = sorted( iface[direction].values(), key=lambda b: _number(b, "acl_tag"), ) interfaces.append(iface) if interfaces: section["interfaces"] = sorted(interfaces, key=_name_key) facts = {"vpp_acl": {}} if objs: params = utils.validate_config(self.argument_spec, {"config": objs}) facts["vpp_acl"] = utils.remove_empties(params["config"]) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config_names.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config_names.cfg index b6eb7686..04239863 100644 --- a/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config_names.cfg +++ b/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config_names.cfg @@ -1,4 +1,6 @@ set vpp acl ip tag-name 100 rule 10 action 'permit' +set vpp acl ip tag-name 1j rule 10 action 'deny' set vpp acl ip tag-name 20 rule 10 action 'deny' +set vpp acl ip tag-name 2j rule 10 action 'deny' set vpp acl ip tag-name WEB description 'Partner web ACL' set vpp acl ip tag-name WEB rule 10 action 'permit' diff --git a/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py b/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py index 4e20ff6b..c7bff448 100644 --- a/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py +++ b/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py @@ -1,358 +1,356 @@ # -*- 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 from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_vpp_acl from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture WEB_IN = dict( name="WEB-IN", description="Inbound web traffic", rules=[ dict( number=10, action="permit", description="Allow HTTPS from partners", protocol="tcp", source=dict(prefix="192.0.2.0/24", port="1024-65535"), destination=dict(prefix="198.51.100.10/32", port="443"), tcp_flags=dict(is_set=["syn"], is_not_set=["ack"]), ), dict(number=20, action="deny"), ], ) ACLS = dict( ip=dict( acls=[ dict(name="MGMT", rules=[dict(number=10, action="permit")]), WEB_IN, dict(name="WEB-OUT", rules=[dict(number=10, action="permit-reflect")]), ], interfaces=[ dict( name="eth1", input=[dict(acl_tag=1, tag_name="WEB-IN"), dict(acl_tag=2, tag_name="MGMT")], output=[dict(acl_tag=1, tag_name="WEB-OUT")], ), ], ), mac=dict( acls=[ dict( name="MAC-FILTER", rules=[ dict( number=10, action="permit", prefix="192.0.2.0/24", mac_address="00:11:22:33:44:55", mac_mask="ff:ff:ff:00:00:00", ), ], ), ], interfaces=[dict(name="eth1", tag_name="MAC-FILTER")], ), ) class TestVyosVppAclModule(TestVyosModule): module = vyos_vpp_acl def setUp(self): super(TestVyosVppAclModule, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_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_get_device_data = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpp_acl.vpp_acl.Vpp_aclFacts.get_device_data", ) self.get_device_data = self.mock_get_device_data.start() def tearDown(self): super(TestVyosVppAclModule, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_get_resource_connection_facts.stop() self.mock_get_device_data.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_vpp_acl_config.cfg" def load_from_file(*args, **kwargs): return load_fixture(filename) self.get_device_data.side_effect = load_from_file def test_vyos_vpp_acl_merged_idempotent(self): set_module_args(dict(config=ACLS, state="merged")) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_merged_tcp_flag_order_does_not_matter(self): rule = dict(number=10, tcp_flags=dict(is_set=["syn"], is_not_set=["ack"])) set_module_args( dict(config=dict(ip=dict(acls=[dict(name="WEB-IN", rules=[rule])])), state="merged"), ) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_merged(self): set_module_args( dict( config=dict( ip=dict( acls=[ dict( name="WEB-IN", rules=[ dict(number=10, tcp_flags=dict(is_set=["syn", "fin"])), dict(number=30, action="deny", protocol="udp"), ], ), dict(name="NEW", description="New ACL with spaces"), ], interfaces=[ dict(name="eth1", input=[dict(acl_tag=3, tag_name="NEW")]), dict(name="eth2", output=[dict(acl_tag=1, tag_name="NEW")]), ], ), ), state="merged", ), ) commands = [ "set vpp acl ip tag-name NEW description 'New ACL with spaces'", "set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set fin", "set vpp acl ip tag-name WEB-IN rule 30 action deny", "set vpp acl ip tag-name WEB-IN rule 30 protocol udp", "set vpp acl ip interface eth1 input acl-tag 3 tag-name NEW", "set vpp acl ip interface eth2 output acl-tag 1 tag-name NEW", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_acl(self): set_module_args( dict( config=dict( ip=dict( acls=[ dict( name="WEB-IN", rules=[ dict( number=10, action="permit", protocol="tcp", tcp_flags=dict(is_set=["syn"]), ), ], ), ], ), ), state="replaced", ), ) commands = [ "delete vpp acl ip tag-name WEB-IN description", "delete vpp acl ip tag-name WEB-IN rule 10 description", "delete vpp acl ip tag-name WEB-IN rule 10 source prefix", "delete vpp acl ip tag-name WEB-IN rule 10 source port", "delete vpp acl ip tag-name WEB-IN rule 10 destination prefix", "delete vpp acl ip tag-name WEB-IN rule 10 destination port", "delete vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-not-set ack", "delete vpp acl ip tag-name WEB-IN rule 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_bindings(self): set_module_args( dict( config=dict( ip=dict( interfaces=[dict(name="eth1", input=[dict(acl_tag=1, tag_name="MGMT")])], ), ), state="replaced", ), ) commands = [ "set vpp acl ip interface eth1 input acl-tag 1 tag-name MGMT", "delete vpp acl ip interface eth1 input acl-tag 2", "delete vpp acl ip interface eth1 output acl-tag 1", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_mac(self): set_module_args( dict( config=dict( mac=dict( acls=[ dict( name="MAC-FILTER", rules=[ dict(number=10, action="deny", mac_address="00:11:22:33:44:55"), ], ), ], interfaces=[dict(name="eth1", tag_name="MAC-FILTER")], ), ), state="replaced", ), ) commands = [ "set vpp acl mac tag-name MAC-FILTER rule 10 action deny", "delete vpp acl mac tag-name MAC-FILTER rule 10 prefix", "delete vpp acl mac tag-name MAC-FILTER rule 10 mac-mask", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_idempotent(self): set_module_args(dict(config=ACLS, state="replaced")) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_overridden(self): set_module_args( dict( config=dict( ip=dict( acls=[dict(name="MGMT", rules=[dict(number=10, action="permit")])], interfaces=[dict(name="eth1", input=[dict(acl_tag=1, tag_name="MGMT")])], ), ), state="overridden", ), ) commands = [ "delete vpp acl ip tag-name WEB-IN", "delete vpp acl ip tag-name WEB-OUT", "set vpp acl ip interface eth1 input acl-tag 1 tag-name MGMT", "delete vpp acl ip interface eth1 input acl-tag 2", "delete vpp acl ip interface eth1 output acl-tag 1", "delete vpp acl mac", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_overridden_idempotent(self): set_module_args(dict(config=ACLS, state="overridden")) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_deleted_all(self): set_module_args(dict(state="deleted")) self.execute_module(changed=True, commands=["delete vpp acl"]) def test_vyos_vpp_acl_deleted_parts(self): set_module_args( dict( config=dict( ip=dict(acls=[dict(name="WEB-OUT")], interfaces=[dict(name="eth1")]), mac=dict(interfaces=[dict(name="eth1")]), ), state="deleted", ), ) commands = [ "delete vpp acl ip interface eth1", "delete vpp acl ip tag-name WEB-OUT", "delete vpp acl mac interface eth1", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_rendered(self): set_module_args( dict( config=dict( mac=dict( acls=[ dict( name="MAC-FILTER", description="Vendor filter", rules=[ dict(number=10, action="deny", mac_address="00:11:22:33:44:55"), ], ), ], interfaces=[dict(name="eth2", tag_name="MAC-FILTER")], ), ), state="rendered", ), ) commands = [ "set vpp acl mac tag-name MAC-FILTER description 'Vendor filter'", "set vpp acl mac tag-name MAC-FILTER rule 10 action deny", "set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:33:44:55", "set vpp acl mac interface eth2 tag-name MAC-FILTER", ] result = self.execute_module(changed=False) self.assertEqual(sorted(result["rendered"]), sorted(commands)) def test_vyos_vpp_acl_gathered(self): set_module_args(dict(state="gathered")) result = self.execute_module(changed=False) self.assertEqual(result["gathered"], ACLS) def test_vyos_vpp_acl_parsed(self): set_module_args( dict(running_config=load_fixture("vyos_vpp_acl_config.cfg"), state="parsed"), ) result = self.execute_module(changed=False) self.assertEqual(result["parsed"], ACLS) def test_vyos_vpp_acl_deleted_whole_family(self): # all MAC ACLs and bindings listed: the family node goes as a whole set_module_args( dict( config=dict( mac=dict(acls=[dict(name="MAC-FILTER")], interfaces=[dict(name="eth1")]), ), state="deleted", ), ) self.execute_module(changed=True, commands=["delete vpp acl mac"]) def test_vyos_vpp_acl_deleted_both_families(self): set_module_args( dict( config=dict( ip=dict( acls=[dict(name="MGMT"), dict(name="WEB-IN"), dict(name="WEB-OUT")], interfaces=[dict(name="eth1")], ), mac=dict(acls=[dict(name="MAC-FILTER")], interfaces=[dict(name="eth1")]), ), state="deleted", ), ) self.execute_module(changed=True, commands=["delete vpp acl"]) - def test_vyos_vpp_acl_parsed_numeric_and_text_names(self): - # numeric and text ACL names together, numeric ones first in numeric order + def test_vyos_vpp_acl_parsed_names_of_mixed_literal_types(self): + # tag names parse as int (20), complex (1j) or text (WEB); sorting them must not fail + set_module_args( - dict( - running_config=load_fixture("vyos_vpp_acl_config_names.cfg"), - state="parsed", - ), + dict(running_config=load_fixture("vyos_vpp_acl_config_names.cfg"), state="parsed"), ) acls = self.execute_module(changed=False)["parsed"]["ip"]["acls"] - self.assertEqual([acl["name"] for acl in acls], ["20", "100", "WEB"]) + self.assertEqual([acl["name"] for acl in acls], ["20", "100", "1j", "2j", "WEB"])