[dhcp_server] add show range cli (#17262)
* add show range * add support for single ip
This commit is contained in:
parent
2e072beb41
commit
5992765d94
@ -1,4 +1,7 @@
|
|||||||
{
|
{
|
||||||
|
"FEATURE|dhcp_server": {
|
||||||
|
"state": "enabled"
|
||||||
|
},
|
||||||
"DHCP_SERVER_IPV4|Vlan100": {
|
"DHCP_SERVER_IPV4|Vlan100": {
|
||||||
"gateway": "100.1.1.1",
|
"gateway": "100.1.1.1",
|
||||||
"lease_time": "3600",
|
"lease_time": "3600",
|
||||||
@ -15,16 +18,13 @@
|
|||||||
"value": "dummy_value"
|
"value": "dummy_value"
|
||||||
},
|
},
|
||||||
"DHCP_SERVER_IPV4_RANGE|range1": {
|
"DHCP_SERVER_IPV4_RANGE|range1": {
|
||||||
"ranges": [
|
"range": "100.1.1.3,100.1.1.5"
|
||||||
"100.1.1.3",
|
|
||||||
"100.1.1.5"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"DHCP_SERVER_IPV4_RANGE|range2": {
|
"DHCP_SERVER_IPV4_RANGE|range2": {
|
||||||
"ips": [
|
"range": "100.1.1.9,100.1.1.8"
|
||||||
"100.1.1.7",
|
},
|
||||||
"100.1.1.8"
|
"DHCP_SERVER_IPV4_RANGE|range3": {
|
||||||
]
|
"range": "100.1.1.10"
|
||||||
},
|
},
|
||||||
"DHCP_SERVER_IPV4_IP|eth0": {
|
"DHCP_SERVER_IPV4_IP|eth0": {
|
||||||
"ip": "240.127.1.2"
|
"ip": "240.127.1.2"
|
||||||
|
@ -55,3 +55,58 @@ Vlan1001|<Unknown> 10:70:fd:b6:13:02 192.168.0.3 2023-03-01 03:16:21 2023-03
|
|||||||
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["lease"], ["Vlan1001"], obj=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.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
|
||||||
assert result.stdout == expected_stdout
|
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
|
||||||
|
range3 100.1.1.10 100.1.1.10 1
|
||||||
|
"""
|
||||||
|
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):
|
||||||
|
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
|
||||||
|
------- ---------- ---------- ----------
|
||||||
|
range3 100.1.1.10 100.1.1.10 1
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
|
||||||
|
@ -12,9 +12,13 @@ def ts_to_str(ts):
|
|||||||
|
|
||||||
|
|
||||||
@click.group(cls=clicommon.AliasedGroup)
|
@click.group(cls=clicommon.AliasedGroup)
|
||||||
def dhcp_server():
|
@clicommon.pass_db
|
||||||
|
def dhcp_server(db):
|
||||||
"""Show dhcp_server related info"""
|
"""Show dhcp_server related info"""
|
||||||
pass
|
ctx = click.get_current_context()
|
||||||
|
dbconn = db.db
|
||||||
|
if dbconn.get("CONFIG_DB", "FEATURE|dhcp_server", "state") != "enabled":
|
||||||
|
ctx.fail("Feature dhcp_server is not enabled")
|
||||||
|
|
||||||
|
|
||||||
@dhcp_server.group(cls=clicommon.AliasedGroup)
|
@dhcp_server.group(cls=clicommon.AliasedGroup)
|
||||||
@ -42,5 +46,38 @@ def lease(db, dhcp_interface):
|
|||||||
click.echo(tabulate(table, headers=headers))
|
click.echo(tabulate(table, headers=headers))
|
||||||
|
|
||||||
|
|
||||||
|
def count_ipv4(start, end):
|
||||||
|
ip1 = int(ipaddress.IPv4Address(start))
|
||||||
|
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):
|
||||||
|
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):
|
def register(cli):
|
||||||
cli.add_command(dhcp_server)
|
cli.add_command(dhcp_server)
|
||||||
|
Loading…
Reference in New Issue
Block a user