Skip to content

Commit 6d1ed6c

Browse files
authored
[config] Prevent deleting VLAN with IP addresses. (sonic-net#1429)
Warn user while deleting VLAN if it has IP addresses. Signed-off-by: d-dashkov <[email protected]>
1 parent 327b292 commit 6d1ed6c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

config/vlan.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def del_vlan(db, vid):
4646
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
4747
ctx.fail("{} does not exist".format(vlan))
4848

49+
intf_table = db.cfgdb.get_table('VLAN_INTERFACE')
50+
for intf_key in intf_table:
51+
if ((type(intf_key) is str and intf_key == 'Vlan{}'.format(vid)) or
52+
(type(intf_key) is tuple and intf_key[0] == 'Vlan{}'.format(vid))):
53+
ctx.fail("{} can not be removed. First remove IP addresses assigned to this VLAN".format(vlan))
54+
4955
keys = [ (k, v) for k, v in db.cfgdb.get_table('VLAN_MEMBER') if k == 'Vlan{}'.format(vid) ]
5056
for k in keys:
5157
db.cfgdb.set_entry('VLAN_MEMBER', k, None)

tests/vlan_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,23 @@ def test_config_vlan_add_rif_portchannel_member(self):
319319
def test_config_vlan_del_vlan(self):
320320
runner = CliRunner()
321321
db = Db()
322+
obj = {'config_db':db.cfgdb}
323+
324+
# del vlan with IP
325+
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
326+
print(result.exit_code)
327+
print(result.output)
328+
assert result.exit_code != 0
329+
assert "Error: Vlan1000 can not be removed. First remove IP addresses assigned to this VLAN" in result.output
330+
331+
# remove vlan IP`s
332+
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Vlan1000", "192.168.0.1/21"], obj=obj)
333+
print(result.exit_code, result.output)
334+
assert result.exit_code != 0
335+
336+
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Vlan1000", "fc02:1000::1/64"], obj=obj)
337+
print(result.exit_code, result.output)
338+
assert result.exit_code != 0
322339

323340
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
324341
print(result.exit_code)

0 commit comments

Comments
 (0)