Add better handling of listing leases and handling static reservations

This commit is contained in:
Joshua Boniface 2018-10-03 19:23:46 -04:00
parent e5ed3d2c44
commit 503680d5b2
2 changed files with 71 additions and 52 deletions

View File

@ -740,11 +740,39 @@ def net_dhcp():
""" """
pass pass
###############################################################################
# pvc network dhcp list
###############################################################################
@click.command(name='list', short_help='List active DHCP leases.')
@click.argument(
'net'
)
@click.argument(
'limit', default=None, required=False
)
def net_dhcp_list(net, limit):
"""
List all DHCP leases in virtual network NET; optionally only match elements matching regex LIMIT; NET can be either a VNI or description.
"""
zk_conn = pvc_common.startZKConnection(zk_host)
retcode, retmsg = pvc_network.get_list_dhcp(zk_conn, net, limit, only_static=False)
cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
# pvc network dhcp add # pvc network dhcp static
############################################################################### ###############################################################################
@click.command(name='add', short_help='Add a static DHCP lease to a virtual network.') @click.group(name='static', short_help='Manage DHCP static reservations in a PVC virtual network.', context_settings=CONTEXT_SETTINGS)
def net_dhcp_static():
"""
Manage host DHCP static reservations of a VXLAN network in the PVC cluster.
"""
pass
###############################################################################
# pvc network dhcp static add
###############################################################################
@click.command(name='add', short_help='Add a DHCP static reservation.')
@click.argument( @click.argument(
'net' 'net'
) )
@ -757,9 +785,9 @@ def net_dhcp():
@click.argument( @click.argument(
'macaddr' 'macaddr'
) )
def net_dhcp_add(net, ipaddr, macaddr, hostname): def net_dhcp_static_add(net, ipaddr, macaddr, hostname):
""" """
Add a new static DHCP lease of IP address IPADDR with hostname HOSTNAME for MAC address MACADDR to virtual network NET; NET can be either a VNI or description. Add a new DHCP static reservation of IP address IPADDR with hostname HOSTNAME for MAC address MACADDR to virtual network NET; NET can be either a VNI or description.
""" """
zk_conn = pvc_common.startZKConnection(zk_host) zk_conn = pvc_common.startZKConnection(zk_host)
@ -767,45 +795,41 @@ def net_dhcp_add(net, ipaddr, macaddr, hostname):
cleanup(retcode, retmsg, zk_conn) cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
# pvc network dhcp remove # pvc network dhcp static remove
############################################################################### ###############################################################################
@click.command(name='remove', short_help='Remove a DHCP lease from a virtual network.') @click.command(name='remove', short_help='Remove a DHCP static reservation.')
@click.argument( @click.argument(
'net' 'net'
) )
@click.argument( @click.argument(
'lease' 'reservation'
) )
def net_dhcp_remove(net, lease): def net_dhcp_static_remove(net, reservation):
""" """
Remove a DHCP lease LEASE from virtual network NET; LEASE can be either a MAC address, an IP address, or a hostname; NET can be either a VNI or description. Remove a DHCP static reservation RESERVATION from virtual network NET; RESERVATION can be either a MAC address, an IP address, or a hostname; NET can be either a VNI or description.
""" """
zk_conn = pvc_common.startZKConnection(zk_host) zk_conn = pvc_common.startZKConnection(zk_host)
retcode, retmsg = pvc_network.remove_dhcp_lease(zk_conn, net, lease) retcode, retmsg = pvc_network.remove_dhcp_reservation(zk_conn, net, reservation)
cleanup(retcode, retmsg, zk_conn) cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
# pvc network dhcp list # pvc network dhcp static list
############################################################################### ###############################################################################
@click.command(name='list', short_help='List DHCP lease objects.') @click.command(name='list', short_help='List DHCP static reservations.')
@click.option(
'-s', '--static', 'only_static', is_flag=True, default=False,
help='Show only static leases.'
)
@click.argument( @click.argument(
'net' 'net'
) )
@click.argument( @click.argument(
'limit', default=None, required=False 'limit', default=None, required=False
) )
def net_dhcp_list(net, limit, only_static): def net_dhcp_static_list(net, limit):
""" """
List all DHCP leases in virtual network NET; optionally only match elements matching regex LIMIT; NET can be either a VNI or description. List all DHCP static reservations in virtual network NET; optionally only match elements matching regex LIMIT; NET can be either a VNI or description.
""" """
zk_conn = pvc_common.startZKConnection(zk_host) zk_conn = pvc_common.startZKConnection(zk_host)
retcode, retmsg = pvc_network.get_list_dhcp_leases(zk_conn, net, limit, only_static=only_static) retcode, retmsg = pvc_network.get_list_dhcp(zk_conn, net, limit, only_static=True)
cleanup(retcode, retmsg, zk_conn) cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
@ -918,9 +942,12 @@ cli_network.add_command(net_list)
cli_network.add_command(net_dhcp) cli_network.add_command(net_dhcp)
cli_network.add_command(net_acl) cli_network.add_command(net_acl)
net_dhcp.add_command(net_dhcp_add)
net_dhcp.add_command(net_dhcp_remove)
net_dhcp.add_command(net_dhcp_list) net_dhcp.add_command(net_dhcp_list)
net_dhcp.add_command(net_dhcp_static)
net_dhcp_static.add_command(net_dhcp_static_add)
net_dhcp_static.add_command(net_dhcp_static_remove)
net_dhcp_static.add_command(net_dhcp_static_list)
cli.add_command(cli_node) cli.add_command(cli_node)
cli.add_command(cli_router) cli.add_command(cli_router)

View File

@ -106,13 +106,8 @@ def getNetworkDHCPLeases(zk_conn, vni):
return sorted(dhcp_leases) return sorted(dhcp_leases)
def getNetworkDHCPReservations(zk_conn, vni): def getNetworkDHCPReservations(zk_conn, vni):
# Get a list of VNIs by listing the children of /networks/<vni>/dhcp_leases # Get a list of DHCP reservations by listing the children of /networks/<vni>/dhcp_reservations
dhcp_reservations = [] dhcp_reservations = zk_conn.get_children('/networks/{}/dhcp_reservations'.format(vni))
dhcp_leases = getNetworkDHCPLeases(zk_conn, vni)
for lease in dhcp_leases:
timestamp = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}'.format (vni, lease))
if timestamp == 'static':
dhcp_reservations.append(lease)
return sorted(dhcp_reservations) return sorted(dhcp_reservations)
def getNetworkFirewallRules(zk_conn, vni): def getNetworkFirewallRules(zk_conn, vni):
@ -132,7 +127,10 @@ def getNetworkInformation(zk_conn, vni):
def getDHCPLeaseInformation(zk_conn, vni, mac_address): def getDHCPLeaseInformation(zk_conn, vni, mac_address):
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/hostname'.format(vni, mac_address)) hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/hostname'.format(vni, mac_address))
ip_address = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/ipaddr'.format(vni, mac_address)) ip_address = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/ipaddr'.format(vni, mac_address))
timestamp = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}'.format(vni, mac_address)) try:
timestamp = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/expiry'.format(vni, mac_address))
except:
timestamp = 'static'
return hostname, ip_address, mac_address, timestamp return hostname, ip_address, mac_address, timestamp
def formatNetworkInformation(zk_conn, vni, long_output): def formatNetworkInformation(zk_conn, vni, long_output):
@ -423,6 +421,7 @@ def add_network(zk_conn, vni, description, domain, ip_network, ip_gateway, dhcp_
'/networks/{}/dhcp_start'.format(vni): dhcp_start, '/networks/{}/dhcp_start'.format(vni): dhcp_start,
'/networks/{}/dhcp_end'.format(vni): dhcp_end, '/networks/{}/dhcp_end'.format(vni): dhcp_end,
'/networks/{}/dhcp_leases'.format(vni): '', '/networks/{}/dhcp_leases'.format(vni): '',
'/networks/{}/dhcp_reservations'.format(vni): '',
'/networks/{}/firewall_rules'.format(vni): '' '/networks/{}/firewall_rules'.format(vni): ''
}) })
@ -558,14 +557,18 @@ def get_list(zk_conn, limit):
return True, '' return True, ''
def get_list_dhcp_leases(zk_conn, network, limit, only_static=False): def get_list_dhcp(zk_conn, network, limit, only_static=False):
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
net_vni = getNetworkVNI(zk_conn, network) net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None: if net_vni == None:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network) return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
dhcp_leases_list = [] dhcp_list = []
full_dhcp_leases_list = getNetworkDHCPLeases(zk_conn, net_vni)
if only_static:
full_dhcp_list = getNetworkDHCPReservations(zk_conn, net_vni)
else:
full_dhcp_list = getNetworkDHCPLeases(zk_conn, net_vni)
if limit: if limit:
try: try:
@ -578,31 +581,20 @@ def get_list_dhcp_leases(zk_conn, network, limit, only_static=False):
return False, 'Regex Error: {}'.format(e) return False, 'Regex Error: {}'.format(e)
for lease in full_dhcp_leases_list: for lease in full_dhcp_list:
valid_lease = False valid_lease = False
if only_static: if limit:
lease_timestamp = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}'.format(net_vni, lease)) if re.match(limit, lease) != None:
if lease_timestamp == 'static':
if limit:
if re.match(limit, lease) != None:
valid_lease = True
if re.match(limit, lease) != None:
valid_lease = True
else:
valid_lease = True
else:
if limit:
if re.match(limit, lease) != None:
valid_lease = True
if re.match(limit, lease) != None:
valid_lease = True
else:
valid_lease = True valid_lease = True
if re.match(limit, lease) != None:
valid_lease = True
else:
valid_lease = True
if valid_lease: if valid_lease:
dhcp_leases_list.append(lease) dhcp_list.append(lease)
output_string = formatDHCPLeaseList(zk_conn, net_vni, dhcp_leases_list) output_string = formatDHCPLeaseList(zk_conn, net_vni, dhcp_list)
click.echo(output_string) click.echo(output_string)
return True, '' return True, ''