diff --git a/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py b/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py index 57ed08161a..379ad502eb 100644 --- a/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py +++ b/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py @@ -143,3 +143,18 @@ class TestConfigDHCPServer(object): 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) + diff --git a/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py b/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py index ae60611ae6..43d7fd5c8c 100644 --- a/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py +++ b/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py @@ -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, @@ -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") @@ -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):