diff --git a/changelogs/fragments/t9431_bgp_address_family_fixes.yml b/changelogs/fragments/t9431_bgp_address_family_fixes.yml index 9687fc69..04b2591b 100644 --- a/changelogs/fragments/t9431_bgp_address_family_fixes.yml +++ b/changelogs/fragments/t9431_bgp_address_family_fixes.yml @@ -1,10 +1,15 @@ --- minor_changes: - vyos_bgp_address_family - neighbor address families accept ipv4-vpn and ipv6-vpn next to ipv4 and ipv6 (protocols bgp neighbor
address-family ipv4-vpn|ipv6-vpn), for example for route-reflector-client and nexthop-self on an L3VPN route reflector. They need VyOS 1.4 or later; older versions fail with a clear message. bugfixes: - vyos_bgp_address_family - a neighbor address family other than unicast or VPN on the device, such as ipv4-labeled-unicast, no longer makes the module fail; before, it was read as afi ipv4-labeled, which the argument spec rejected. + - vyos_bgp_address_family - a neighbor address family without options (afi only) activates the + neighbor in that family (protocols bgp neighbor
address-family ); before, it + rendered nothing, so on VyOS 1.4 and later, where FRR always has "no bgp default + ipv4-unicast", such a neighbor carried no routes. With state replaced or overridden, a family + without options on the device that the configuration no longer lists is now deleted. diff --git a/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py b/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py index 29a923e7..43f24386 100644 --- a/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py +++ b/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py @@ -1,420 +1,443 @@ # # -*- 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 __metaclass__ = type """ The vyos_bgp_address_family 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. """ import re 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_address_family import ( Bgp_address_familyTemplate, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.bgp_address_family_14 import ( Bgp_address_familyTemplate14, ) 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_address_family(ResourceModule): """ The vyos_bgp_address_family config class """ def __init__(self, module): super(Bgp_address_family, self).__init__( empty_fact_val={}, facts_module=Facts(module), module=module, resource="bgp_address_family", tmplt=Bgp_address_familyTemplate(), ) self.parsers = [] def _validate_template(self): version = get_os_version(self._module) if LooseVersion(version) >= LooseVersion("1.4"): self._tmplt = Bgp_address_familyTemplate14() else: self._tmplt = Bgp_address_familyTemplate() 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 """ 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 = {} # The VPN address families (ipv4-vpn, ipv6-vpn) only exist on VyOS 1.4 and later. # The version is only checked when one of them is used. vpn = sorted( { af["afi"] for neighbor in self.want.get("neighbors", []) for af in neighbor.get("address_family", []) if af.get("afi", "").endswith("-vpn") }, ) if vpn and LooseVersion(get_os_version(self._module)) < LooseVersion("1.4"): self._module.fail_json( msg="address family {0} needs VyOS 1.4 or later".format(", ".join(vpn)), ) 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_af_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 == "deleted": for k, have in haved.items(): self._delete_af(wantd, have) wantd = {} if self.state == "overridden": for k, have in haved.items(): if k not in wantd: self._compare(want={}, have=have) 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_address_family network resource. """ if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): self._compare_asn(want, have) self._compare_af(want, have) self._compare_neighbors(want, have) # 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_af(self, want, have): waf = want.get("address_family", {}) haf = have.get("address_family", {}) for name, entry in waf.items(): self._compare_lists( entry, have=haf.get(name, {}), as_number=want["as_number"], afi=name, ) for name, entry in haf.items(): if name not in waf.keys() and self.state == "replaced": continue self._compare_lists({}, entry, as_number=have["as_number"], afi=name) def _delete_af(self, want, have): for as_num, entry in want.items(): for afi, af_entry in entry.get("address_family", {}).items(): if have.get("address_family"): for hafi, hentry in have["address_family"].items(): if hafi == afi: self.commands.append( self._tmplt.render( { "as_number": as_num, "address_family": {"afi": afi}, }, "address_family", True, ), ) for neigh, neigh_entry in entry.get("neighbors", {}).items(): if have.get("neighbors"): for hneigh, hnentry in have["neighbors"].items(): if hneigh == neigh: if not neigh_entry.get("address_family"): self.commands.append( self._tmplt.render( { "as_number": as_num, "neighbors": {"neighbor_address": neigh}, }, "neighbors", True, ), ) else: for k in neigh_entry["address_family"].keys(): if ( hnentry.get("address_family") and k in hnentry["address_family"].keys() ): self.commands.append( self._tmplt.render( { "as_number": as_num, "neighbors": { "neighbor_address": neigh, "address_family": {"afi": k}, }, }, "neighbors.address_family", True, ), ) def _compare_neighbors(self, want, have): parsers = [ "neighbors.allowas_in", "neighbors.as_override", "neighbors.attribute_unchanged.as_path", "neighbors.attribute_unchanged.med", "neighbors.attribute_unchanged.next_hop", "neighbors.capability_dynamic", "neighbors.capability_orf", "neighbors.default_originate", "neighbors.distribute_list", "neighbors.prefix_list", "neighbors.filter_list", "neighbors.maximum_prefix", "neighbors.nexthop_local", "neighbors.nexthop_self", "neighbors.peer_group", "neighbors.remove_private_as", "neighbors.route_map", "neighbors.route_reflector_client", "neighbors.route_server_client", "neighbors.soft_reconfiguration", "neighbors.unsuppress_map", "neighbors.weight", ] wneigh = want.get("neighbors", {}) hneigh = have.get("neighbors", {}) for name, entry in wneigh.items(): for afi, af_entry in entry.get("address_family").items(): + if set(af_entry) == {"afi"} and not hneigh.get(name, {}).get( + "address_family", + {}, + ).get(afi): + self.addcmd( + { + "as_number": want["as_number"], + "neighbors": {"neighbor_address": name, "address_family": {"afi": afi}}, + }, + "neighbors.address_family", + False, + ) for k, val in af_entry.items(): w = { "as_number": want["as_number"], "neighbors": { "neighbor_address": name, "address_family": {"afi": afi, k: val}, }, } h = {} if hneigh.get(name): if hneigh[name]["address_family"].get(afi): if hneigh[name]["address_family"][afi].get(k): h = { "as_number": want["as_number"], "neighbors": { "neighbor_address": name, "address_family": { "afi": afi, k: hneigh[name]["address_family"][afi].pop(k, {}), }, }, } self.compare( parsers=parsers, want=w, have=h, ) for name, entry in hneigh.items(): if name not in wneigh.keys(): # remove surplus config for overridden and replaced if self.state != "replaced": self.commands.append( self._tmplt.render( { "as_number": have["as_number"], "neighbors": {"neighbor_address": name}, }, "neighbors", True, ), ) continue for hafi, haf_entry in entry.get("address_family").items(): - # remove surplus configs for given neighbor - replace and overridden + if set(haf_entry) == {"afi"} and hafi not in wneigh[name].get("address_family", {}): + self.addcmd( + { + "as_number": have["as_number"], + "neighbors": { + "neighbor_address": name, + "address_family": {"afi": hafi}, + }, + }, + "neighbors.address_family", + True, + ) for k, val in haf_entry.items(): h = { "as_number": have["as_number"], "neighbors": { "neighbor_address": name, "address_family": {"afi": hafi, k: val}, }, } self.compare(parsers=parsers, want={}, have=h) def _compare_lists(self, want, have, as_number, afi): parsers = [ "aggregate_address", "network", "network.backdoor", "network.path_limit", "network.route_map", "redistribute", "redistribute.metric", "redistribute.route_map", "redistribute.table", ] if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): delete_asn = "" else: delete_asn = " " + str(as_number) for attrib in ["redistribute", "networks", "aggregate_address"]: wdict = want.pop(attrib, {}) hdict = have.pop(attrib, {}) for key, entry in wdict.items(): if entry != hdict.get(key, {}): self.compare( parsers=parsers, want={ "as_number": as_number, "address_family": {"afi": afi, attrib: entry}, }, have={ "as_number": as_number, "address_family": { "afi": afi, attrib: hdict.pop(key, {}), }, }, ) hdict.pop(key, {}) # remove remaining items in have for replaced if not wdict and hdict: attrib = re.sub("_", "-", attrib) attrib = re.sub("networks", "network", attrib) self.commands.append( "delete protocols bgp" + delete_asn + " " + "address-family " + afi + " " + attrib, ) hdict = {} for key, entry in hdict.items(): self.compare( parsers=parsers, want={}, have={ "as_number": as_number, "address_family": {"afi": afi, attrib: entry}, }, ) # de-duplicate child commands if parent command is present for val in self.commands: for val2 in self.commands: if val != val2 and val2.startswith(val): self.commands.remove(val2) 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 _bgp_af_list_to_dict(self, entry): for name, proc in entry.items(): if "address_family" in proc: af_dict = {} for entry in proc.get("address_family"): if "networks" in entry: network_dict = {} for n_entry in entry.get("networks", []): network_dict.update({n_entry["prefix"]: n_entry}) entry["networks"] = network_dict if "aggregate_address" in entry: agg_dict = {} for a_entry in entry.get("aggregate_address", []): agg_dict.update({a_entry["prefix"]: a_entry}) entry["aggregate_address"] = agg_dict if "redistribute" in entry: redis_dict = {} for r_entry in entry.get("redistribute", []): proto_key = r_entry.get("protocol", "table") redis_dict.update({proto_key: r_entry}) entry["redistribute"] = redis_dict for af in proc.get("address_family"): af_dict.update({af["afi"]: af}) proc["address_family"] = af_dict if "neighbors" in proc: neigh_dict = {} for entry in proc.get("neighbors", []): neigh_dict.update({entry["neighbor_address"]: entry}) proc["neighbors"] = neigh_dict self._bgp_af_list_to_dict(proc["neighbors"]) diff --git a/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml b/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml index 0bf57e67..4997f2eb 100644 --- a/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml +++ b/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml @@ -1,118 +1,160 @@ --- - debug: msg: START vyos_bgp_address_family merged integration tests on connection={{ ansible_connection }} - include_tasks: _remove_config.yaml - include_tasks: _preconfig.yaml - block: - name: Merge the provided configuration with the existing running configuration register: result vyos.vyos.vyos_bgp_address_family: &id001 config: as_number: "65536" address_family: - afi: ipv6 redistribute: - protocol: static metric: 50 neighbors: - neighbor_address: 203.0.113.5 address_family: - afi: ipv6 allowas_in: 4 attribute_unchanged: med: true default_originate: map01 distribute_list: - action: export acl: 10 - neighbor_address: 192.0.2.25 address_family: - afi: ipv6 maximum_prefix: 45 nexthop_self: true route_map: - action: export route_map: map01 - action: import route_map: map01 state: merged - vyos.vyos.vyos_facts: gather_network_resources: bgp_address_family - assert: that: - result.commands|length == merged.commands|length - result.changed == true - result.commands|symmetric_difference(merged.commands) == [] - result.after == ansible_facts['network_resources']['bgp_address_family'] - result.before == merged.before - result.after == merged.after - name: Merge the provided configuration with the existing running configuration (IDEMPOTENT) register: result vyos.vyos.vyos_bgp_address_family: *id001 - name: Assert that the previous task was idempotent assert: that: - result['changed'] == false always: - include_tasks: _remove_config.yaml - include_tasks: _get_version.yaml - block: - name: setup an iBGP neighbor vyos.vyos.vyos_config: lines: - set protocols bgp system-as 65536 - set protocols bgp neighbor 192.0.2.50 remote-as internal vars: ansible_connection: ansible.netcommon.network_cli - name: Merge VPN address families for a route-reflector client register: result vyos.vyos.vyos_bgp_address_family: &id010 config: as_number: 65536 neighbors: - neighbor_address: 192.0.2.50 address_family: - afi: ipv4-vpn route_reflector_client: true nexthop_self: true - afi: ipv6-vpn route_reflector_client: true state: merged - vyos.vyos.vyos_facts: gather_network_resources: bgp_address_family - assert: that: - result.changed == true - result.commands | symmetric_difference(expected) == [] - result.after == ansible_facts['network_resources']['bgp_address_family'] vars: expected: - set protocols bgp neighbor 192.0.2.50 address-family ipv4-vpn route-reflector-client - set protocols bgp neighbor 192.0.2.50 address-family ipv4-vpn nexthop-self - set protocols bgp neighbor 192.0.2.50 address-family ipv6-vpn route-reflector-client - name: Merge VPN address families for a route-reflector client (IDEMPOTENT) register: result vyos.vyos.vyos_bgp_address_family: *id010 - name: Assert that the previous task was idempotent assert: that: - result['changed'] == false when: vyos_version is version('1.4.0', '>=', version_type='semver') always: - include_tasks: _remove_config.yaml + +- block: + - name: setup a neighbor + vyos.vyos.vyos_config: + lines: + - set protocols bgp system-as 65536 + - set protocols bgp neighbor 192.0.2.60 remote-as 65002 + vars: + ansible_connection: ansible.netcommon.network_cli + + - name: Merge an address family without options (activate the neighbor) + register: result + vyos.vyos.vyos_bgp_address_family: &id011 + config: + as_number: 65536 + neighbors: + - neighbor_address: 192.0.2.60 + address_family: + - afi: ipv4 + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: bgp_address_family + + - assert: + that: + - result.changed == true + - result.commands == ['set protocols bgp neighbor 192.0.2.60 address-family ipv4-unicast'] + - result.after == ansible_facts['network_resources']['bgp_address_family'] + + - name: Merge an address family without options (IDEMPOTENT) + register: result + vyos.vyos.vyos_bgp_address_family: *id011 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + when: vyos_version is version('1.4.0', '>=', version_type='semver') + always: + - include_tasks: _remove_config.yaml diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_bare_config_14.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_bare_config_14.cfg new file mode 100644 index 00000000..e3e1fada --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_bare_config_14.cfg @@ -0,0 +1,2 @@ +set protocols bgp system-as '65536' +set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast diff --git a/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py b/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py index 0e9eef98..89a363c5 100644 --- a/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py +++ b/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py @@ -1,1023 +1,1068 @@ # (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_address_family from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture class TestVyosBgpafModule14(TestVyosModule): module = vyos_bgp_address_family def setUp(self): super(TestVyosBgpafModule14, 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 = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts." + "bgp_address_family.bgp_address_family.Bgp_address_familyFacts.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_address_family.bgp_address_family.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_address_family.bgp_address_family.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(TestVyosBgpafModule14, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_execute_show_command.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_address_family_config_14.cfg" def load_from_file(*args, **kwargs): output = load_fixture(filename) return output self.execute_show_command.side_effect = load_from_file def test_vyos_bgp_address_family_merged_idempotent(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_merged(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", summary_only=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv4", filter_list=[ dict(action="export", path_list="list01"), ], capability=dict(orf="send"), ), ], ), ], ), ), ) commands = [ "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-setipv4-unicast aggregate-address 192.0.2.0/24 summary-only", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast filter-list export list01", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast capability prefix-list send", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_replaced_idempotent(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_replaced(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", summary_only=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="import", route_map="map01")], ), dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv4", filter_list=[ dict(action="export", path_list="list01"), ], capability=dict(orf="send"), ), ], ), ], ), ), ) commands = [ "delete protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged", "delete protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration", "delete protocols bgp address-family ipv6-unicast redistribute ripng", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 summary-only", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp neighbor 192.10.21.25 address-family ipv4-unicast route-map import map01", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast filter-list export list01", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast capability prefix-list send", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden_idempotent(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_overridden(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="import", route_map="map01")], ), dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), ], ), ), ) commands = [ "delete protocols bgp neighbor 203.0.113.5 address-family", "delete protocols bgp neighbor 192.0.2.25 address-family", "delete protocols bgp address-family ipv6-unicast redistribute ripng", "delete protocols bgp address-family ipv4 aggregate-address", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp neighbor 192.10.21.25 address-family ipv4-unicast route-map import map01", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_deleted(self): set_module_args( dict( state="deleted", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", ), ], ), dict( neighbor_address="203.0.113.5", ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv4-unicast", "delete protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast", "delete protocols bgp neighbor 203.0.113.5 address-family", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_rendered(self): set_module_args( dict( state="rendered", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) rendered_cmds = [ "set protocols bgp system-as 65536", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] result = self.execute_module(changed=False) self.assertEqual( sorted(result["rendered"]), sorted(rendered_cmds), result["rendered"], ) def test_vyos_bgp_address_family_parsed(self): commands = [ "set protocols bgp system-as 65536", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] 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, "address_family": [ { "afi": "ipv4", "networks": [ {"prefix": "192.1.13.0/24", "route_map": "map01"}, {"prefix": "192.2.13.0/24", "backdoor": True}, ], "aggregate_address": [{"prefix": "192.0.2.0/24", "as_set": True}], }, { "afi": "ipv6", "redistribute": [{"protocol": "ripng", "metric": 20}], }, ], "neighbors": [ { "neighbor_address": "192.0.2.25", "address_family": [ {"afi": "ipv4", "soft_reconfiguration": True}, ], }, { "neighbor_address": "203.0.113.5", "address_family": [ { "afi": "ipv6", "attribute_unchanged": {"next_hop": True}, }, ], }, ], } self.assertEqual(sorted(parsed_list), sorted(result["parsed"])) def test_vyos_bgp_address_family_gathered(self): set_module_args(dict(state="gathered")) result = self.execute_module(changed=False) gather_list = { "as_number": 65536, "address_family": [ { "afi": "ipv4", "networks": [ {"prefix": "192.1.13.0/24", "route_map": "map01"}, {"prefix": "192.2.13.0/24", "backdoor": True}, ], "aggregate_address": [{"prefix": "192.0.2.0/24", "as_set": True}], }, { "afi": "ipv6", "redistribute": [{"protocol": "ripng", "metric": 20}], }, ], "neighbors": [ { "neighbor_address": "192.0.2.25", "address_family": [ {"afi": "ipv4", "soft_reconfiguration": True}, ], }, { "neighbor_address": "203.0.113.5", "address_family": [ { "afi": "ipv6", "attribute_unchanged": {"next_hop": True}, }, ], }, ], } self.assertEqual(sorted(gather_list), sorted(result["gathered"])) def test_vyos_bgp_address_family_replaced_asn(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_overridden_asn(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_replaced_asn2(self): set_module_args( dict( state="replaced", config=dict( as_number=65540, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) commands = [ "set protocols bgp system-as 65540", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden_asn2(self): set_module_args( dict( state="overridden", config=dict( as_number=65540, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv4 aggregate-address", "delete protocols bgp address-family ipv4 network", "delete protocols bgp address-family ipv6 redistribute", "delete protocols bgp neighbor 192.0.2.25 address-family", "delete protocols bgp neighbor 203.0.113.5 address-family", "set protocols bgp system-as 65540", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_af_neighbor_vpn_rendered(self): set_module_args( dict( config=dict( as_number=65536, neighbors=[ dict( neighbor_address="192.0.2.1", address_family=[ dict( afi="ipv4-vpn", route_reflector_client=True, nexthop_self=True, ), dict(afi="ipv6-vpn", route_reflector_client=True), ], ), ], ), state="rendered", ), ) self.assertEqual( sorted(self.execute_module(changed=False)["rendered"]), [ "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn nexthop-self", "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn route-reflector-client", "set protocols bgp neighbor 192.0.2.1 address-family ipv6-vpn route-reflector-client", "set protocols bgp system-as 65536", ], ) def test_vyos_bgp_af_neighbor_vpn_parsed(self): set_module_args( dict( running_config="set protocols bgp system-as 65536\n" "set protocols bgp neighbor 192.0.2.1 address-family ipv4-labeled-unicast\n" "set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast route-reflector-client\n" "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn nexthop-self\n" "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn route-reflector-client", state="parsed", ), ) self.assertEqual( self.execute_module(changed=False)["parsed"], { "as_number": 65536, "neighbors": [ { "neighbor_address": "192.0.2.1", "address_family": [ {"afi": "ipv4", "route_reflector_client": True}, { "afi": "ipv4-vpn", "nexthop_self": True, "route_reflector_client": True, }, ], }, ], }, ) + def test_vyos_bgp_af_neighbor_bare_family_rendered(self): + set_module_args( + dict( + config=dict( + as_number=65536, + neighbors=[ + dict( + neighbor_address="192.0.2.1", + address_family=[dict(afi="ipv4"), dict(afi="ipv4-vpn")], + ), + ], + ), + state="rendered", + ), + ) + self.assertEqual( + sorted(self.execute_module(changed=False)["rendered"]), + [ + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast", + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn", + "set protocols bgp system-as 65536", + ], + ) + + def test_vyos_bgp_af_neighbor_bare_family_replaced(self): + set_module_args( + dict( + config=dict( + as_number=65536, + neighbors=[ + dict(neighbor_address="192.0.2.1", address_family=[dict(afi="ipv6")]), + ], + ), + state="replaced", + ), + ) + self.execute_module( + changed=True, + commands=[ + "delete protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast", + "set protocols bgp neighbor 192.0.2.1 address-family ipv6-unicast", + ], + filename="vyos_bgp_address_family_bare_config_14.cfg", + ) + class TestVyosBgpafOpsModule14(TestVyosModule): module = vyos_bgp_address_family def setUp(self): super(TestVyosBgpafOpsModule14, 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 = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts." + "bgp_address_family.bgp_address_family.Bgp_address_familyFacts.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_address_family.bgp_address_family.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_address_family.bgp_address_family.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(TestVyosBgpafOpsModule14, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_execute_show_command.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_af_ops_config_14.cfg" def load_from_file(*args, **kwargs): output = load_fixture(filename) return output self.execute_show_command.side_effect = load_from_file def test_vyos_bgp_address_family_merged(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.3.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], ), ), ) commands = [ "set protocols bgp address-family ipv4-unicast network 192.3.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_replaced(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", backdoor=True), ], redistribute=[ dict(protocol="ospf", metric=25), ], ), dict( afi="ipv6", redistribute=[ dict(protocol="ospfv3", metric=20), dict(protocol="ripng"), ], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv6-unicast redistribute ripng metric", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "delete protocols bgp address-family ipv4-unicast redistribute rip", "set protocols bgp address-family ipv4-unicast redistribute ospf metric 25", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", backdoor=True), ], redistribute=[ dict(protocol="ospf", metric=25), ], ), dict( afi="ipv6", redistribute=[ dict(protocol="ospfv3", metric=20), dict(protocol="ripng"), ], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv6-unicast redistribute ripng metric", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "delete protocols bgp address-family ipv4-unicast redistribute rip", "set protocols bgp address-family ipv4-unicast redistribute ospf metric 25", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_deleted(self): set_module_args( dict( state="deleted", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.2.13.0/24"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng")], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv4-unicast", "delete protocols bgp address-family ipv6-unicast", ] self.execute_module(changed=True, commands=commands)