|
| 1 | + |
| 2 | +import click |
| 3 | +from swsscommon.swsscommon import ConfigDBConnector |
| 4 | +from .validated_config_db_connector import ValidatedConfigDBConnector |
| 5 | +import ipaddress |
| 6 | + |
| 7 | + |
| 8 | +ADHOC_VALIDATION = True |
| 9 | +NAMESERVERS_MAX_NUM = 3 |
| 10 | + |
| 11 | + |
| 12 | +def to_ip_address(address): |
| 13 | + """Check if the given IP address is valid""" |
| 14 | + try: |
| 15 | + ip = ipaddress.ip_address(address) |
| 16 | + |
| 17 | + if ADHOC_VALIDATION: |
| 18 | + if ip.is_reserved or ip.is_multicast or ip.is_loopback: |
| 19 | + return |
| 20 | + |
| 21 | + invalid_ips = [ |
| 22 | + ipaddress.IPv4Address('0.0.0.0'), |
| 23 | + ipaddress.IPv4Address('255.255.255.255'), |
| 24 | + ipaddress.IPv6Address("0::0"), |
| 25 | + ipaddress.IPv6Address("0::1") |
| 26 | + ] |
| 27 | + if ip in invalid_ips: |
| 28 | + return |
| 29 | + |
| 30 | + return ip |
| 31 | + except Exception: |
| 32 | + return |
| 33 | + |
| 34 | + |
| 35 | +def get_nameservers(db): |
| 36 | + nameservers = db.get_table('DNS_NAMESERVER') |
| 37 | + return [ipaddress.ip_address(ip) for ip in nameservers] |
| 38 | + |
| 39 | + |
| 40 | +# 'dns' group ('config dns ...') |
| 41 | +@click.group() |
| 42 | +@click.pass_context |
| 43 | +def dns(ctx): |
| 44 | + """Static DNS configuration""" |
| 45 | + config_db = ValidatedConfigDBConnector(ConfigDBConnector()) |
| 46 | + config_db.connect() |
| 47 | + ctx.obj = {'db': config_db} |
| 48 | + |
| 49 | + |
| 50 | +# dns nameserver config |
| 51 | +@dns.group('nameserver') |
| 52 | +@click.pass_context |
| 53 | +def nameserver(ctx): |
| 54 | + """Static DNS nameservers configuration""" |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +# dns nameserver add |
| 59 | +@nameserver.command('add') |
| 60 | +@click.argument('ip_address_str', metavar='<ip_address>', required=True) |
| 61 | +@click.pass_context |
| 62 | +def add_dns_nameserver(ctx, ip_address_str): |
| 63 | + """Add static DNS nameserver entry""" |
| 64 | + ip_address = to_ip_address(ip_address_str) |
| 65 | + if not ip_address: |
| 66 | + ctx.fail(f"{ip_address_str} invalid nameserver ip address") |
| 67 | + |
| 68 | + db = ctx.obj['db'] |
| 69 | + |
| 70 | + nameservers = get_nameservers(db) |
| 71 | + if ip_address in nameservers: |
| 72 | + ctx.fail(f"{ip_address} nameserver is already configured") |
| 73 | + |
| 74 | + if len(nameservers) >= NAMESERVERS_MAX_NUM: |
| 75 | + ctx.fail(f"The maximum number ({NAMESERVERS_MAX_NUM}) of nameservers exceeded.") |
| 76 | + |
| 77 | + db.set_entry('DNS_NAMESERVER', ip_address, {}) |
| 78 | + |
| 79 | +# dns nameserver delete |
| 80 | +@nameserver.command('del') |
| 81 | +@click.argument('ip_address_str', metavar='<ip_address>', required=True) |
| 82 | +@click.pass_context |
| 83 | +def del_dns_nameserver(ctx, ip_address_str): |
| 84 | + """Delete static DNS nameserver entry""" |
| 85 | + |
| 86 | + ip_address = to_ip_address(ip_address_str) |
| 87 | + if not ip_address: |
| 88 | + ctx.fail(f"{ip_address_str} invalid nameserver ip address") |
| 89 | + |
| 90 | + db = ctx.obj['db'] |
| 91 | + |
| 92 | + nameservers = get_nameservers(db) |
| 93 | + if ip_address not in nameservers: |
| 94 | + ctx.fail(f"DNS nameserver {ip_address} is not configured") |
| 95 | + |
| 96 | + db.set_entry('DNS_NAMESERVER', ip_address, None) |
0 commit comments