From 7c34b797f8ee43f97ebd897f0073f1a90e557b61 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Tue, 7 Dec 2021 02:24:37 +0200 Subject: [PATCH] [config] Add portchannel support for static route (#1857) * Added portchannels support for static routes. * Added check if nexthop is a portchannel and verify if this portchannel exists in config db. Signed-off-by: d-dashkov --- config/main.py | 12 +++++++++--- tests/static_routes_test.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/config/main.py b/config/main.py index 9a060056237c..4faddb1c8bd9 100644 --- a/config/main.py +++ b/config/main.py @@ -906,9 +906,15 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True): try: ipaddress.ip_network(ip_prefix) if 'nexthop' in config_entry: - nh = config_entry['nexthop'].split(',') - for ip in nh: - ipaddress.ip_address(ip) + nh_list = config_entry['nexthop'].split(',') + for nh in nh_list: + # Nexthop to portchannel + if nh.startswith('PortChannel'): + config_db = ctx.obj['config_db'] + if not nh in config_db.get_keys('PORTCHANNEL'): + ctx.fail("portchannel does not exist.") + else: + ipaddress.ip_address(nh) except ValueError: ctx.fail("ip address is not valid.") diff --git a/tests/static_routes_test.py b/tests/static_routes_test.py index c354cb97c449..5704844aef5e 100644 --- a/tests/static_routes_test.py +++ b/tests/static_routes_test.py @@ -25,6 +25,9 @@ ERROR_INVALID_IP = ''' Error: ip address is not valid. ''' +ERROR_INVALID_PORTCHANNEL = ''' +Error: portchannel does not exist. +''' class TestStaticRoutes(object): @@ -51,6 +54,17 @@ def test_simple_static_route(self): print(result.exit_code, result.output) assert not '1.2.3.4/32' in db.cfgdb.get_table('STATIC_ROUTE') + def test_invalid_portchannel_static_route(self): + db = Db() + runner = CliRunner() + obj = {'config_db':db.cfgdb} + + # config route add prefix 1.2.3.4/32 nexthop PortChannel0101 + result = runner.invoke(config.config.commands["route"].commands["add"], \ + ["prefix", "1.2.3.4/32", "nexthop", "PortChannel0101"], obj=obj) + print(result.exit_code, result.output) + assert ERROR_INVALID_PORTCHANNEL in result.output + def test_static_route_invalid_prefix_ip(self): db = Db() runner = CliRunner()