-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dns] Implement config and show commands for static DNS. (#2737)
- What I did Implement config and show commands for static DNS feature. According to sonic-net/SONiC#1262 HLD. - How I did it Static DNS config commands are implemented in the new config/dns.py file. DNS config commands are available under config dns ... sub-command. Show commands are implemented in the new show/dns.py file. DNS show commands are available under show dns ... sub-command. - How to verify it Compile sonic-utilities package. The unit tests will run automatically during the compilation. Coverage for config/dns.py : 94% Coverage for show/dns.py : 86% - Previous command output (if the output of a command-line utility has changed) - New command output (if the output of a command-line utility has changed) # config dns nameserver add 1.1.1.1 # config dns nameserver add 8.8.8.8 # show dns nameserver Nameserver ------------ 1.1.1.1 8.8.8.8
- Loading branch information
1 parent
8414a70
commit dc2945b
Showing
6 changed files
with
381 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
|
||
import click | ||
from swsscommon.swsscommon import ConfigDBConnector | ||
from .validated_config_db_connector import ValidatedConfigDBConnector | ||
import ipaddress | ||
|
||
|
||
ADHOC_VALIDATION = True | ||
NAMESERVERS_MAX_NUM = 3 | ||
|
||
|
||
def to_ip_address(address): | ||
"""Check if the given IP address is valid""" | ||
try: | ||
ip = ipaddress.ip_address(address) | ||
|
||
if ADHOC_VALIDATION: | ||
if ip.is_reserved or ip.is_multicast or ip.is_loopback: | ||
return | ||
|
||
invalid_ips = [ | ||
ipaddress.IPv4Address('0.0.0.0'), | ||
ipaddress.IPv4Address('255.255.255.255'), | ||
ipaddress.IPv6Address("0::0"), | ||
ipaddress.IPv6Address("0::1") | ||
] | ||
if ip in invalid_ips: | ||
return | ||
|
||
return ip | ||
except Exception: | ||
return | ||
|
||
|
||
def get_nameservers(db): | ||
nameservers = db.get_table('DNS_NAMESERVER') | ||
return [ipaddress.ip_address(ip) for ip in nameservers] | ||
|
||
|
||
# 'dns' group ('config dns ...') | ||
@click.group() | ||
@click.pass_context | ||
def dns(ctx): | ||
"""Static DNS configuration""" | ||
config_db = ValidatedConfigDBConnector(ConfigDBConnector()) | ||
config_db.connect() | ||
ctx.obj = {'db': config_db} | ||
|
||
|
||
# dns nameserver config | ||
@dns.group('nameserver') | ||
@click.pass_context | ||
def nameserver(ctx): | ||
"""Static DNS nameservers configuration""" | ||
pass | ||
|
||
|
||
# dns nameserver add | ||
@nameserver.command('add') | ||
@click.argument('ip_address_str', metavar='<ip_address>', required=True) | ||
@click.pass_context | ||
def add_dns_nameserver(ctx, ip_address_str): | ||
"""Add static DNS nameserver entry""" | ||
ip_address = to_ip_address(ip_address_str) | ||
if not ip_address: | ||
ctx.fail(f"{ip_address_str} invalid nameserver ip address") | ||
|
||
db = ctx.obj['db'] | ||
|
||
nameservers = get_nameservers(db) | ||
if ip_address in nameservers: | ||
ctx.fail(f"{ip_address} nameserver is already configured") | ||
|
||
if len(nameservers) >= NAMESERVERS_MAX_NUM: | ||
ctx.fail(f"The maximum number ({NAMESERVERS_MAX_NUM}) of nameservers exceeded.") | ||
|
||
db.set_entry('DNS_NAMESERVER', ip_address, {}) | ||
|
||
# dns nameserver delete | ||
@nameserver.command('del') | ||
@click.argument('ip_address_str', metavar='<ip_address>', required=True) | ||
@click.pass_context | ||
def del_dns_nameserver(ctx, ip_address_str): | ||
"""Delete static DNS nameserver entry""" | ||
|
||
ip_address = to_ip_address(ip_address_str) | ||
if not ip_address: | ||
ctx.fail(f"{ip_address_str} invalid nameserver ip address") | ||
|
||
db = ctx.obj['db'] | ||
|
||
nameservers = get_nameservers(db) | ||
if ip_address not in nameservers: | ||
ctx.fail(f"DNS nameserver {ip_address} is not configured") | ||
|
||
db.set_entry('DNS_NAMESERVER', ip_address, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import click | ||
import utilities_common.cli as clicommon | ||
from natsort import natsorted | ||
from tabulate import tabulate | ||
|
||
from swsscommon.swsscommon import ConfigDBConnector | ||
from utilities_common.cli import pass_db | ||
|
||
|
||
# 'dns' group ("show dns ...") | ||
@click.group(cls=clicommon.AliasedGroup) | ||
@click.pass_context | ||
def dns(ctx): | ||
"""Show details of the static DNS configuration """ | ||
config_db = ConfigDBConnector() | ||
config_db.connect() | ||
ctx.obj = {'db': config_db} | ||
|
||
|
||
# 'nameserver' subcommand ("show dns nameserver") | ||
@dns.command() | ||
@click.pass_context | ||
def nameserver(ctx): | ||
""" Show static DNS configuration """ | ||
header = ["Nameserver"] | ||
db = ctx.obj['db'] | ||
|
||
nameservers = db.get_table('DNS_NAMESERVER') | ||
|
||
click.echo(tabulate([(ns,) for ns in nameservers.keys()], header, tablefmt='simple', stralign='right')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.