Skip to content

Commit

Permalink
[config] Prevent deleting VLAN with IP addresses. (sonic-net#1429)
Browse files Browse the repository at this point in the history
Warn user while deleting VLAN if it has IP addresses.
Signed-off-by: d-dashkov <Dmytro_Dashkov@Jabil.com>
  • Loading branch information
d-dashkov committed Feb 16, 2021
1 parent 327b292 commit 6d1ed6c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def del_vlan(db, vid):
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
ctx.fail("{} does not exist".format(vlan))

intf_table = db.cfgdb.get_table('VLAN_INTERFACE')
for intf_key in intf_table:
if ((type(intf_key) is str and intf_key == 'Vlan{}'.format(vid)) or
(type(intf_key) is tuple and intf_key[0] == 'Vlan{}'.format(vid))):
ctx.fail("{} can not be removed. First remove IP addresses assigned to this VLAN".format(vlan))

keys = [ (k, v) for k, v in db.cfgdb.get_table('VLAN_MEMBER') if k == 'Vlan{}'.format(vid) ]
for k in keys:
db.cfgdb.set_entry('VLAN_MEMBER', k, None)
Expand Down
17 changes: 17 additions & 0 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ def test_config_vlan_add_rif_portchannel_member(self):
def test_config_vlan_del_vlan(self):
runner = CliRunner()
db = Db()
obj = {'config_db':db.cfgdb}

# del vlan with IP
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Vlan1000 can not be removed. First remove IP addresses assigned to this VLAN" in result.output

# remove vlan IP`s
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Vlan1000", "192.168.0.1/21"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0

result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Vlan1000", "fc02:1000::1/64"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0

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

0 comments on commit 6d1ed6c

Please sign in to comment.