diff --git a/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py b/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py index 2c69ee29..2ca67cdf 100644 --- a/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py +++ b/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py @@ -1,435 +1,433 @@ # # -*- coding: utf-8 -*- # Copyright 2021 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 import re __metaclass__ = type """ The vyos_bgp_global config file. 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 its desired end-state is created. """ 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_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.bgp_global import ( Bgp_globalTemplate, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.bgp_global_14 import ( Bgp_globalTemplate14, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.version import ( LooseVersion, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import get_os_version class Bgp_global(ResourceModule): """ The vyos_bgp_global config class """ def __init__(self, module): super(Bgp_global, self).__init__( empty_fact_val={}, facts_module=Facts(module), module=module, resource="bgp_global", tmplt=Bgp_globalTemplate(), ) self.parsers = [] def _validate_template(self): version = get_os_version(self._module) if LooseVersion(version) >= LooseVersion("1.4"): self._tmplt = Bgp_globalTemplate14() else: self._tmplt = Bgp_globalTemplate() def parse(self): """override parse to check template""" self._validate_template() return super().parse() def get_parser(self, name): """get_parsers""" self._validate_template() return super().get_parser(name) def execute_module(self): """Execute the module :rtype: A dictionary :returns: The result from module execution """ version = get_os_version(self._module) if LooseVersion(version) >= LooseVersion("1.4"): self._asn_mod = "" else: self._asn_mod = " " + str(self.have.get("as_number")) self._validate_template() 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. """ wantd = {} haved = {} if self.want and self.want.get("as_number") is None and self.state != "deleted": self._module.fail_json( msg="as_number is required: VyOS needs a system-as for every BGP " "instance, including the BGP instance of each VRF", ) if ( self.want.get("as_number") == self.have.get("as_number") or not self.have or LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4") ): if self.want: wantd = {self.want["as_number"]: self.want} if self.have: haved = {self.have["as_number"]: self.have} else: self._module.fail_json(msg="Only one bgp instance is allowed per device") # turn all lists of dicts into dicts prior to merge for entry in wantd, haved: self._bgp_global_list_to_dict(entry) # if state is merged, merge want onto have and then compare if self.state == "merged": wantd = dict_merge(haved, wantd) # if state is deleted, empty out wantd and set haved to wantd if self.state == "purged": h_del = {} for k, v in haved.items(): if k in wantd or not wantd: h_del.update({k: v}) for num, entry in h_del.items(): self.commands.append(self._tmplt.render({"as_number": num}, "router", True)) wantd = {} if self.state == "deleted": self._compare(want={}, have=self.have) wantd = {} for k, want in wantd.items(): self._compare(want=want, have=haved.pop(k, {})) def _compare(self, want, have): """Leverages the base class `compare()` method and populates the list of commands to be run by comparing the `want` and `have` data with the `parsers` defined for the Bgp_global network resource. """ if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): self._compare_asn(want, have) parsers = ["maximum_paths", "timers"] self._compare_neighbor(want, have) self._compare_bgp_params(want, have) for name, entry in want.items(): if name != "as_number": self.compare( parsers=parsers, want={"as_number": want["as_number"], name: entry}, have={ "as_number": want["as_number"], name: have.pop(name, {}), }, ) for name, entry in have.items(): if name != "as_number": self.compare( parsers=parsers, want={}, have={"as_number": have["as_number"], name: entry}, ) # Do the negation first command_set = [] for cmd in self.commands: if cmd not in command_set: if "delete" in cmd: command_set.insert(0, cmd) else: command_set.append(cmd) self.commands = command_set def _compare_neighbor(self, want, have): parsers = [ "neighbor.advertisement_interval", "neighbor.allowas_in", "neighbor.as_override", "neighbor.attribute_unchanged.as_path", "neighbor.attribute_unchanged.med", "neighbor.attribute_unchanged.next_hop", "neighbor.capability_dynamic", "neighbor.capability_orf", "neighbor.default_originate", "neighbor.description", "neighbor.disable_capability_negotiation", "neighbor.disable_connected_check", "neighbor.disable_send_community", "neighbor.distribute_list", "neighbor.ebgp_multihop", "neighbor.filter_list", "neighbor.local_as", "neighbor.maximum_prefix", "neighbor.nexthop_self", "neighbor.override_capability", "neighbor.passive", "neighbor.password", "neighbor.peer_group_name", "neighbor.port", "neighbor.prefix_list", "neighbor.remote_as", "neighbor.remove_private_as", "neighbor.route_map", "neighbor.route_reflector_client", "neighbor.route_server_client", "neighbor.shutdown", "neighbor.soft_reconfiguration", "neighbor.strict_capability_match", "neighbor.unsuppress_map", "neighbor.update_source", "neighbor.weight", "neighbor.ttl_security", "neighbor.timers", ] wneigh = want.pop("neighbor", {}) hneigh = have.pop("neighbor", {}) peer_groups = [n for d in (wneigh, hneigh) for n, e in d.items() if e.get("peer_group")] first = len(self.commands) self._compare_neigh_lists(wneigh, hneigh) for name, entry in wneigh.items(): for k, v in entry.items(): if k == "address": continue if hneigh.get(name): h = {"address": name, k: hneigh[name].pop(k, {})} else: h = {} self.compare( parsers=parsers, want={ "as_number": want["as_number"], "neighbor": {"address": name, k: v}, }, have={"as_number": want["as_number"], "neighbor": h}, ) for name, entry in hneigh.items(): if name not in wneigh.keys(): if self._check_af(name): msg = "Use the _bgp_address_family module to delete the address_family under neighbor {0}, before replacing/deleting the neighbor.".format( name, ) self._module.fail_json(msg=msg) else: self.commands.append( "delete protocols bgp" + self._asn_mod + " neighbor " + name, ) continue for k, v in entry.items(): self.compare( parsers=parsers, want={}, have={ "as_number": have["as_number"], "neighbor": {"address": name, k: v}, }, ) # peer-group definitions are compared like neighbors, then renamed for i, cmd in enumerate(self.commands[first:], first): for name in peer_groups: cmd = re.sub( r"^(\S+ protocols bgp )neighbor " + re.escape(name) + r"(?= |$)", r"\1peer-group " + name, cmd, ) self.commands[i] = cmd def _compare_bgp_params(self, want, have): parsers = [ "bgp_params.always_compare_med", "bgp_params.bestpath.as_path", "bgp_params.bestpath.compare_routerid", "bgp_params.bestpath.med", "bgp_params.cluster_id", "bgp_params.confederation", "bgp_params.dampening_half_life", "bgp_params.dampening_max_suppress_time", "bgp_params.dampening_re_use", "bgp_params.dampening_start_suppress_time", "bgp_params.default", "bgp_params.deterministic_med", "bgp_params.disbale_network_import_check", "bgp_params.enforce_first_as", "bgp_params.graceful_restart", "bgp_params.log_neighbor_changes", "bgp_params.no_client_to_client_reflection", "bgp_params.no_fast_external_failover", "bgp_params.routerid", "bgp_params.scan_time", ] wbgp = want.pop("bgp_params", {}) hbgp = have.pop("bgp_params", {}) for name, entry in wbgp.items(): if name == "confederation": if entry != hbgp.pop(name, {}): self.addcmd( { "as_number": want["as_number"], "bgp_params": {name: entry}, }, "bgp_params.confederation", False, ) elif name == "distance": if entry != hbgp.pop(name, {}): distance_parsers = [ "bgp_params.distance.global", "bgp_params.distance.prefix", ] for distance_type in entry: self.compare( parsers=distance_parsers, want={ "as_number": want["as_number"], "bgp_params": {name: distance_type}, }, have={ "as_number": want["as_number"], "bgp_params": {name: hbgp.pop(name, {})}, }, ) else: self.compare( parsers=parsers, want={ "as_number": want["as_number"], "bgp_params": {name: entry}, }, have={ "as_number": want["as_number"], "bgp_params": {name: hbgp.pop(name, {})}, }, ) if not wbgp and hbgp: self.commands.append("delete protocols bgp" + self._asn_mod + " parameters") hbgp = {} for name, entry in hbgp.items(): if name == "confederation": self.commands.append( "delete protocols bgp" + self._asn_mod + " parameters confederation", ) elif name == "distance": distance_parsers = [ "bgp_params.distance.global", "bgp_params.distance.prefix", ] self.compare( parsers=distance_parsers, want={}, have={ "as_number": have["as_number"], "bgp_params": {name: entry[0]}, }, ) else: self.compare( parsers=parsers, want={}, have={ "as_number": have["as_number"], "bgp_params": {name: entry}, }, ) def _compare_neigh_lists(self, want, have): for attrib in [ "distribute_list", "filter_list", "prefix_list", "route_map", ]: wdict = want.pop(attrib, {}) hdict = have.pop(attrib, {}) for key, entry in wdict.items(): if entry != hdict.pop(key, {}): self.addcmd(entry, "neighbor.{0}".format(attrib), False) # remove remaining items in have for replaced for entry in hdict.values(): self.addcmd(entry, "neighbor.{0}".format(attrib), True) def _bgp_global_list_to_dict(self, entry): for name, proc in entry.items(): if "neighbor" in proc: neigh_dict = {} for entry in proc.get("neighbor", []): neigh_dict.update({entry["address"]: entry}) proc["neighbor"] = neigh_dict if "network" in proc: network_dict = {} for entry in proc.get("network", []): network_dict.update({entry["address"]: entry}) proc["network"] = network_dict if "aggregate_address" in proc: agg_dict = {} for entry in proc.get("aggregate_address", []): agg_dict.update({entry["prefix"]: entry}) proc["aggregate_address"] = agg_dict if "redistribute" in proc: redis_dict = {} for entry in proc.get("redistribute", []): redis_dict.update({entry["protocol"]: entry}) proc["redistribute"] = redis_dict def _compare_asn(self, want, have): if want.get("as_number") and not have.get("as_number"): self.commands.append( "set protocols bgp " + "system-as" + " " + str(want.get("as_number")), ) def _check_af(self, neighbor): - af_present = False - if self._connection: - config_lines = self._get_config(self._connection).splitlines() - for line in config_lines: - if neighbor in line: - if "address-family" in line: - af_present = True - return af_present + if not self._connection: + return False + af_line = re.compile( + r"^set protocols bgp (?:\d+ )?(?:neighbor|peer-group) " + + re.escape(neighbor) + + r" address-family\b", + ) + return any(af_line.match(line) for line in self._get_config(self._connection).splitlines()) def _get_config(self, connection): - return connection.get( - 'show configuration commands | match "set protocols bgp .*neighbor"', - ) + return connection.get('show configuration commands | match "set protocols bgp"') diff --git a/tests/integration/targets/vyos_bgp_global/tests/cli/replaced_af.yaml b/tests/integration/targets/vyos_bgp_global/tests/cli/replaced_af.yaml index 3054ea1f..7ba9a504 100644 --- a/tests/integration/targets/vyos_bgp_global/tests/cli/replaced_af.yaml +++ b/tests/integration/targets/vyos_bgp_global/tests/cli/replaced_af.yaml @@ -1,47 +1,72 @@ --- - debug: msg: START vyos_bgp_global replaced integration tests on connection={{ ansible_connection }} - include_tasks: _remove_config.yaml - include_tasks: _populate_af.yaml - block: - name: Replace the existing af configuration with the provided running configuration register: result ignore_errors: true vyos.vyos.vyos_bgp_global: config: as_number: "65536" bgp_params: always_compare_med: true dampening: start_suppress_time: 5 max_suppress_time: 20 half_life: 33 re_use: 60 bestpath: as_path: confed compare_routerid: true neighbor: - address: 192.0.2.43 disable_connected_check: true advertisement_interval: 72 capability: dynamic: true timers: holdtime: 30 keepalive: 10 remote_as: 102 - address: 203.0.113.0 capability: dynamic: true remote_as: 102 state: replaced - assert: that: - result.msg == 'Use the _bgp_address_family module to delete the address_family under neighbor 5001::64, before replacing/deleting the neighbor.' + - name: Peer group with an address family (1.4 and later) + when: vyos_version is version('1.4.0', '>=', version_type='semver') + block: + - name: Add a peer group with an address family + vyos.vyos.vyos_config: + lines: + - set protocols bgp peer-group RR-CLIENTS remote-as '65536' + - set protocols bgp peer-group RR-CLIENTS address-family 'ipv4-unicast' + vars: + ansible_connection: ansible.netcommon.network_cli + + - name: Replace without the peer group that has an address family + register: result + ignore_errors: true + vyos.vyos.vyos_bgp_global: + config: + as_number: "65536" + neighbor: + - address: 5001::64 + state: replaced + + - assert: + that: + - result.msg == 'Use the _bgp_address_family module to delete the address_family under neighbor RR-CLIENTS, before replacing/deleting the neighbor.' + always: - include_tasks: _remove_config.yaml diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_bgp_global_peer_group_af_config_14.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_global_peer_group_af_config_14.cfg new file mode 100644 index 00000000..44648b1b --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_global_peer_group_af_config_14.cfg @@ -0,0 +1,4 @@ +set protocols bgp system-as '65001' +set protocols bgp peer-group PG1 remote-as '65001' +set protocols bgp peer-group PG10 remote-as '65001' +set protocols bgp peer-group PG10 address-family 'ipv4-unicast' diff --git a/tests/unit/modules/network/vyos/test_vyos_bgp_global14.py b/tests/unit/modules/network/vyos/test_vyos_bgp_global14.py index d8268504..c553f567 100644 --- a/tests/unit/modules/network/vyos/test_vyos_bgp_global14.py +++ b/tests/unit/modules/network/vyos/test_vyos_bgp_global14.py @@ -1,468 +1,500 @@ # (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_bgp_global from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture class TestVyosBgpglobalModule(TestVyosModule): module = vyos_bgp_global def setUp(self): super(TestVyosBgpglobalModule, 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_execute_show_command_config = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_global.bgp_global.Bgp_global._get_config", ) self.execute_show_command_config = self.mock_execute_show_command_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.bgp_global.bgp_global.Bgp_globalFacts.get_device_data", ) self.execute_show_command = self.mock_execute_show_command.start() self.mock_get_os_version = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_global.bgp_global.get_os_version", ) self.test_version = "1.4" self.get_os_version = self.mock_get_os_version.start() self.get_os_version.return_value = self.test_version self.mock_facts_get_os_version = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_global.bgp_global.get_os_version", ) self.get_facts_os_version = self.mock_facts_get_os_version.start() self.get_facts_os_version.return_value = self.test_version self.maxDiff = None def tearDown(self): super(TestVyosBgpglobalModule, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_get_resource_connection_facts.stop() self.mock_execute_show_command.stop() self.mock_execute_show_command_config.stop() self.mock_get_os_version.stop() self.mock_facts_get_os_version.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_bgp_global_config_14.cfg" def load_from_file(*args, **kwargs): output = load_fixture(filename) return output self.execute_show_command.side_effect = load_from_file self.execute_show_command_config.side_effect = load_from_file def test_vyos_bgp_global_merged_idempotent(self): set_module_args( dict( config=dict( as_number="65536", neighbor=[ dict( address="10.0.0.4", disable_connected_check=True, timers=dict(holdtime=30, keepalive=10), ), dict( address="192.168.0.2", ebgp_multihop=2, remote_as="65535", update_source="192.168.0.1", ), dict( address="2001:db8::2", ebgp_multihop=2, remote_as="65535", update_source="2001:db8::1", ), ], bgp_params=dict( bestpath=dict(as_path="confed", compare_routerid=True), default=dict(no_ipv4_unicast=True), router_id="10.1.1.1", ), ), state="merged", ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_global_merged(self): set_module_args( dict( config=dict( as_number="65536", neighbor=[ dict( address="2001:db8::2", ebgp_multihop=2, remote_as="65535", update_source="2001:db8::1", ), ], bgp_params=dict( confederation=[dict(peers=20), dict(identifier=66)], router_id="10.1.1.1", ), ), state="merged", ), ) commands = [ "set protocols bgp parameters confederation peers 20", "set protocols bgp parameters confederation identifier 66", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_global_replaced_idempotent(self): set_module_args( dict( config=dict( as_number="65536", neighbor=[ dict( address="10.0.0.4", disable_connected_check=True, timers=dict(holdtime=30, keepalive=10), ), dict( address="192.168.0.2", ebgp_multihop=2, remote_as="65535", update_source="192.168.0.1", ), dict( address="2001:db8::2", ebgp_multihop=2, remote_as="65535", update_source="2001:db8::1", ), ], bgp_params=dict( bestpath=dict(as_path="confed", compare_routerid=True), default=dict(no_ipv4_unicast=True), router_id="10.1.1.1", ), ), state="replaced", ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_global_replaced(self): set_module_args( dict( config=dict( as_number="65536", timers=dict(holdtime=30, keepalive=10), neighbor=[ dict( address="200.11.155.3", ), dict( address="2001:db8::2", remote_as="65535", default_originate="map01", ), ], bgp_params=dict( log_neighbor_changes=True, no_client_to_client_reflection=True, confederation=[dict(peers=20), dict(identifier=66)], router_id="10.1.1.1", ), ), state="replaced", ), ) commands = [ "delete protocols bgp parameters default", "delete protocols bgp parameters bestpath compare-routerid", "delete protocols bgp parameters bestpath as-path confed", "delete protocols bgp neighbor 2001:db8::2 update-source 2001:db8::1", "delete protocols bgp neighbor 2001:db8::2 ebgp-multihop 2", "delete protocols bgp neighbor 192.168.0.2", "delete protocols bgp neighbor 10.0.0.4", "set protocols bgp parameters log-neighbor-changes", "set protocols bgp parameters no-client-to-client-reflection", "set protocols bgp parameters confederation peers 20", "set protocols bgp parameters confederation identifier 66", "set protocols bgp timers holdtime 30", "set protocols bgp timers keepalive 10", ] self.execute_module(changed=True, commands=commands) # def test_vyos_bgp_global_purged(self): set_module_args(dict(config=dict(as_number="65536"), state="purged")) # commands = ["delete protocols bgp"] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_global_replaced_af(self): set_module_args( dict( config=dict( as_number="65536", timers=dict(holdtime=30, keepalive=10), neighbor=[ dict( address="200.11.155.3", ), dict( address="2001:db8::2", remote_as="65535", default_originate="map01", ), ], bgp_params=dict( log_neighbor_changes=True, no_client_to_client_reflection=True, confederation=[dict(peers=20), dict(identifier=66)], router_id="10.1.1.1", ), ), state="replaced", ), ) result = self.execute_module(failed=True, filename="vyos_bgp_global_af_config_14.cfg") self.assertIn( "Use the _bgp_address_family module to delete the address_family under neighbor 5001::64, before replacing/deleting the neighbor.", result["msg"], ) def test_vyos_bgp_global_missing_as_number(self): set_module_args( dict( config=dict(neighbor=[dict(address="192.0.2.1", remote_as=65002)]), state="merged", ), ) result = self.execute_module(failed=True) self.assertIn("as_number is required", result["msg"]) def test_vyos_bgp_global_rendered(self): set_module_args( dict( config=dict( as_number="65536", neighbor=[ dict( address="10.0.0.4", disable_connected_check=True, timers=dict(holdtime=30, keepalive=10), ), dict( address="192.168.0.2", ebgp_multihop=2, remote_as="65535", update_source="192.168.0.1", ), dict( address="2001:db8::2", ebgp_multihop=2, remote_as="65535", update_source="2001:db8::1", ), ], bgp_params=dict( bestpath=dict(as_path="confed", compare_routerid=True), default=dict(no_ipv4_unicast=True), router_id="10.1.1.1", ), ), state="rendered", ), ) rendered_cmds = [ "set protocols bgp system-as 65536", "set protocols bgp neighbor 10.0.0.4 disable-connected-check", "set protocols bgp neighbor 10.0.0.4 timers holdtime 30", "set protocols bgp neighbor 10.0.0.4 timers keepalive 10", "set protocols bgp neighbor 192.168.0.2 ebgp-multihop 2", "set protocols bgp neighbor 192.168.0.2 remote-as 65535", "set protocols bgp neighbor 192.168.0.2 update-source 192.168.0.1", "set protocols bgp neighbor 2001:db8::2 ebgp-multihop 2", "set protocols bgp neighbor 2001:db8::2 remote-as 65535", "set protocols bgp neighbor 2001:db8::2 update-source 2001:db8::1", "set protocols bgp parameters bestpath as-path confed", "set protocols bgp parameters bestpath compare-routerid", "set protocols bgp parameters default no-ipv4-unicast", "set protocols bgp parameters router-id 10.1.1.1", ] result = self.execute_module(changed=False) self.assertEqual( sorted(result["rendered"]), sorted(rendered_cmds), result["rendered"], ) def test_vyos_bgp_global_parsed(self): commands = [ "set protocols bgp system-as 65536", "set protocols bgp neighbor 10.0.0.4 disable-connected-check", "set protocols bgp neighbor 10.0.0.4 timers holdtime 30", "set protocols bgp neighbor 10.0.0.4 timers keepalive 10", "set protocols bgp neighbor 192.168.0.2 ebgp-multihop 2", "set protocols bgp neighbor 192.168.0.2 remote-as 65535", "set protocols bgp neighbor 192.168.0.2 update-source 192.168.0.1", "set protocols bgp neighbor 2001:db8::2 ebgp-multihop 2", "set protocols bgp neighbor 2001:db8::2 remote-as 65535", "set protocols bgp neighbor 2001:db8::2 update-source 2001:db8::1", "set protocols bgp parameters bestpath as-path confed", "set protocols bgp parameters bestpath compare-routerid", "set protocols bgp parameters default no-ipv4-unicast", "set protocols bgp parameters router-id 10.1.1.1", ] parsed_str = "\n".join(commands) set_module_args(dict(running_config=parsed_str, state="parsed")) result = self.execute_module(changed=False) parsed_list = { "as_number": 65536, "bgp_params": { "bestpath": {"as_path": "confed", "compare_routerid": True}, "default": {"no_ipv4_unicast": True}, "router_id": "10.1.1.1", }, "neighbor": [ { "address": "10.0.0.4", "disable_connected_check": True, "timers": {"holdtime": 30, "keepalive": 10}, }, { "address": "192.168.0.2", "ebgp_multihop": 2, "remote_as": 65535, "update_source": "192.168.0.1", }, { "address": "2001:db8::2", "ebgp_multihop": 2, "remote_as": 65535, "update_source": "2001:db8::1", }, ], } self.assertEqual(sorted(parsed_list), sorted(result["parsed"])) def test_vyos_bgp_global_gathered(self): set_module_args(dict(state="gathered")) result = self.execute_module(changed=False) gather_list = { "as_number": 65536, "bgp_params": { "bestpath": {"as_path": "confed", "compare_routerid": True}, "default": {"no_ipv4_unicast": True}, "router_id": "10.1.1.1", }, "neighbor": [ { "address": "10.0.0.4", "disable_connected_check": True, "timers": {"holdtime": 30, "keepalive": 10}, }, { "address": "192.168.0.2", "ebgp_multihop": 2, "remote_as": 65535, "update_source": "192.168.0.1", }, { "address": "2001:db8::2", "ebgp_multihop": 2, "remote_as": 65535, "update_source": "2001:db8::1", }, ], } self.assertEqual(sorted(gather_list), sorted(result["gathered"])) def test_vyos_bgp_global_reflection_flags_independent(self): self.get_os_version.return_value = "1.5" set_module_args( dict( config=dict(as_number=65001, bgp_params=dict(log_neighbor_changes=True)), state="rendered", ), ) cmds = self.execute_module(changed=False)["rendered"] self.assertIn("set protocols bgp parameters log-neighbor-changes", cmds) self.assertFalse(any("no-client-to-client-reflection" in c for c in cmds)) set_module_args( dict( config=dict(as_number=65001, bgp_params=dict(no_client_to_client_reflection=True)), state="rendered", ), ) cmds = self.execute_module(changed=False)["rendered"] self.assertIn("set protocols bgp parameters no-client-to-client-reflection", cmds) self.assertFalse(any("log-neighbor-changes" in c for c in cmds)) def test_vyos_bgp_global_peer_group_rendered(self): self.get_os_version.return_value = "1.5" group = dict(address="RR-CLIENTS", peer_group=True, remote_as=65001, update_source="lo") member = dict(address="192.0.2.1", peer_group_name="RR-CLIENTS") set_module_args( dict(config=dict(as_number=65001, neighbor=[group, member]), state="rendered"), ) self.assertEqual( sorted(self.execute_module(changed=False)["rendered"]), [ "set protocols bgp neighbor 192.0.2.1 peer-group RR-CLIENTS", "set protocols bgp peer-group RR-CLIENTS remote-as 65001", "set protocols bgp peer-group RR-CLIENTS update-source lo", "set protocols bgp system-as 65001", ], ) def test_vyos_bgp_global_peer_group_replaced(self): self.get_os_version.return_value = "1.5" self.get_facts_os_version.return_value = "1.5" group = dict(address="RR-CLIENTS", peer_group=True, remote_as=65001) member = dict(address="192.0.2.1", peer_group_name="RR-CLIENTS") set_module_args( dict(config=dict(as_number=65001, neighbor=[group, member]), state="replaced"), ) self.execute_module( changed=True, commands=["delete protocols bgp peer-group RR-CLIENTS update-source lo"], filename="vyos_bgp_global_peer_group_config_14.cfg", ) + + def test_vyos_bgp_global_peer_group_af_replaced(self): + set_module_args( + dict( + config=dict( + as_number=65001, + neighbor=[dict(address="PG1", peer_group=True, remote_as=65001)], + ), + state="replaced", + ), + ) + result = self.execute_module( + failed=True, + filename="vyos_bgp_global_peer_group_af_config_14.cfg", + ) + self.assertIn("address_family under neighbor PG10", result["msg"]) + + def test_vyos_bgp_global_peer_group_af_exact_name(self): + set_module_args( + dict( + config=dict( + as_number=65001, + neighbor=[dict(address="PG10", peer_group=True, remote_as=65001)], + ), + state="replaced", + ), + ) + self.execute_module( + changed=True, + commands=["delete protocols bgp peer-group PG1"], + filename="vyos_bgp_global_peer_group_af_config_14.cfg", + )