Skip to content

Commit

Permalink
add config dhcp_server disable (sonic-net#17689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xichen96 committed Jan 10, 2024
1 parent b2ca36a commit 5987dcf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,18 @@ def test_config_dhcp_server_ipv4_enable_does_not_exist(self, mock_db):
result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["enable"], ["Vlan200"], obj=db)
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)

def test_config_dhcp_server_ipv4_disable_already_exist(self, mock_db):
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["disable"], ["Vlan100"], obj=db)
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert mock_db.get("CONFIG_DB", "DHCP_SERVER_IPV4|Vlan300", "state") == "disabled"

def test_config_dhcp_server_ipv4_disable_does_not_exist(self, mock_db):
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["disable"], ["Vlan200"], obj=db)
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)

21 changes: 17 additions & 4 deletions dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def dhcp_server_ipv4_add(db, mode, lease_time, dup_gw_nm, gateway, netmask, dhcp
ctx.fail("gateway and netmask must be valid ipv4 string")
key = "DHCP_SERVER_IPV4|" + dhcp_interface
if dbconn.exists("CONFIG_DB", key):
ctx.fail("Dhcp_interface %s already exist".format(dhcp_interface))
ctx.fail("Dhcp_interface {} already exist".format(dhcp_interface))
else:
dbconn.hmset("CONFIG_DB", key, {
"mode": mode,
Expand All @@ -110,10 +110,10 @@ def dhcp_server_ipv4_del(db, dhcp_interface):
dbconn = db.db
key = "DHCP_SERVER_IPV4|" + dhcp_interface
if dbconn.exists("CONFIG_DB", key):
click.echo("Dhcp interface %s exists in config db, proceed to delete".format(dhcp_interface))
click.echo("Dhcp interface {} exists in config db, proceed to delete".format(dhcp_interface))
dbconn.delete("CONFIG_DB", key)
else:
ctx.fail("Dhcp interface %s does not exist in config db".format(dhcp_interface))
ctx.fail("Dhcp interface {} does not exist in config db".format(dhcp_interface))


@dhcp_server_ipv4.command(name="enable")
Expand All @@ -126,7 +126,20 @@ def dhcp_server_ipv4_enable(db, dhcp_interface):
if dbconn.exists("CONFIG_DB", key):
dbconn.set("CONFIG_DB", key, "state", "enabled")
else:
ctx.fail("Failed to enable, dhcp interface %s does not exist".format(dhcp_interface))
ctx.fail("Failed to enable, dhcp interface {} does not exist".format(dhcp_interface))


@dhcp_server_ipv4.command(name="disable")
@click.argument("dhcp_interface", required=True)
@clicommon.pass_db
def dhcp_server_ipv4_disable(db, dhcp_interface):
ctx = click.get_current_context()
dbconn = db.db
key = "DHCP_SERVER_IPV4|" + dhcp_interface
if dbconn.exists("CONFIG_DB", key):
dbconn.set("CONFIG_DB", key, "state", "disabled")
else:
ctx.fail("Failed to disable, dhcp interface {} does not exist".format(dhcp_interface))


def register(cli):
Expand Down

0 comments on commit 5987dcf

Please sign in to comment.