Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dhcp_server] add show dhcp server info #17468

Merged
merged 5 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"lease_time": "3600",
"mode": "PORT",
"netmask": "255.255.255.0",
"customized_options": [
"option60"
],
"customized_options": "option60",
"state": "enabled"
},
"DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS|option60": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,42 @@ def test_show_dhcp_server_ipv4_range_single_ip(self, mock_db):
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert result.stdout == expected_stdout

def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db):
expected_stdout = """\
Interface Mode Gateway Netmask Lease Time(s) State
----------- ------ --------- ------------- --------------- -------
Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["info"], [], obj=db)
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert result.stdout == expected_stdout

def test_show_dhcp_server_ipv4_info_with_intf(self, mock_db):
expected_stdout = """\
Interface Mode Gateway Netmask Lease Time(s) State
----------- ------ --------- ------------- --------------- -------
Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["info"], ["Vlan100"], obj=db)
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert result.stdout == expected_stdout

def test_show_dhcp_server_ipv4_info_with_customized_options(self, mock_db):
expected_stdout = """\
Interface Mode Gateway Netmask Lease Time(s) State Customized Options
----------- ------ --------- ------------- --------------- ------- --------------------
Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled option60
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["info"], ["Vlan100", "--with_customized_options"], obj=db)
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert result.stdout == expected_stdout

21 changes: 21 additions & 0 deletions dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,26 @@ def range(db, range_name):
click.echo(tabulate(table, headers=headers))


@ipv4.command()
@click.argument('dhcp_interface', required=False)
@click.option('--with_customized_options', default=False, is_flag=True)
@clicommon.pass_db
def info(db, dhcp_interface, with_customized_options):
if not dhcp_interface:
dhcp_interface = "*"
headers = ["Interface", "Mode", "Gateway", "Netmask", "Lease Time(s)", "State"]
if with_customized_options:
headers.append("Customized Options")
table = []
dbconn = db.db
for key in dbconn.keys("CONFIG_DB", "DHCP_SERVER_IPV4|" + dhcp_interface):
entry = dbconn.get_all("CONFIG_DB", key)
interface = key.split("|")[1]
table.append([interface, entry["mode"], entry["gateway"], entry["netmask"], entry["lease_time"], entry["state"]])
if with_customized_options:
table[-1].append(entry["customized_options"])
click.echo(tabulate(table, headers=headers))


def register(cli):
cli.add_command(dhcp_server)
Loading