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 range cli #17262

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -15,16 +15,13 @@
"value": "dummy_value"
},
"DHCP_SERVER_IPV4_RANGE|range1": {
"ranges": [
"100.1.1.3",
"100.1.1.5"
]
"range": "100.1.1.3,100.1.1.5"
},
"DHCP_SERVER_IPV4_RANGE|range2": {
"ips": [
"100.1.1.7",
"100.1.1.8"
]
"range": "100.1.1.9,100.1.1.8"
Xichen96 marked this conversation as resolved.
Show resolved Hide resolved
},
"DHCP_SERVER_IPV4_RANGE|range3": {
"range": "100.1.1.10"
},
"DHCP_SERVER_IPV4_IP|eth0": {
"ip": "240.127.1.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,57 @@ def test_show_dhcp_server_ipv4_lease_client_not_in_fdb(self, mock_db):
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["lease"], ["Vlan1001"], 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_range_without_name(self, mock_db):
expected_stdout = """\
Range IP Start IP End IP Count
------- ---------- --------- ----------------------
range1 100.1.1.3 100.1.1.5 3
range2 100.1.1.9 100.1.1.8 range value is illegal
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["range"], [], 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_range_with_name(self, mock_db):
expected_stdout = """\
Range IP Start IP End IP Count
------- ---------- --------- ----------
range1 100.1.1.3 100.1.1.5 3
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["range"], ["range1"], 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_range_wrong_data(self, mock_db):
Xichen96 marked this conversation as resolved.
Show resolved Hide resolved
expected_stdout = """\
Range IP Start IP End IP Count
------- ---------- --------- ----------------------
range2 100.1.1.9 100.1.1.8 range value is illegal
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["range"], ["range2"], 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_range_single_ip(self, mock_db):
expected_stdout = """\
Range IP Start IP End IP Count
------- ---------- --------- ----------
range1 100.1.1.3 100.1.1.5 3
Xichen96 marked this conversation as resolved.
Show resolved Hide resolved
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["range"], ["range3"], 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

33 changes: 33 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 @@ -42,5 +42,38 @@ def lease(db, dhcp_interface):
click.echo(tabulate(table, headers=headers))


def count_ipv4(start, end):
ip1 = int(ipaddress.IPv4Address(start))
Xichen96 marked this conversation as resolved.
Show resolved Hide resolved
ip2 = int(ipaddress.IPv4Address(end))
return ip2 - ip1 + 1


@ipv4.command()
@click.argument('range_name', required=False)
@clicommon.pass_db
def range(db, range_name):
Xichen96 marked this conversation as resolved.
Show resolved Hide resolved
if not range_name:
range_name = "*"
headers = ["Range", "IP Start", "IP End", "IP Count"]
table = []
dbconn = db.db
for key in dbconn.keys("CONFIG_DB", "DHCP_SERVER_IPV4_RANGE|" + range_name):
name = key.split("|")[1]
entry = dbconn.get_all("CONFIG_DB", key)
range_ = entry["range"].split(",")
if len(range_) == 1:
start, end = range_[0], range_[0]
elif len(range_) == 2:
start, end = range_
else:
table.append([name, "", "", "range value is illegal"])
continue
count = count_ipv4(start, end)
if count < 1:
count = "range value is illegal"
table.append([name, start, end, count])
click.echo(tabulate(table, headers=headers))


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