Support domain names in networks

This commit is contained in:
Joshua Boniface 2018-09-28 20:42:24 -04:00
parent 5d35adb4fc
commit 63d48a3821
2 changed files with 28 additions and 9 deletions

View File

@ -581,7 +581,12 @@ def cli_network():
@click.option( @click.option(
'-d', '--description', 'description', '-d', '--description', 'description',
default="", default="",
help='Description of the network. Should not contain whitespace.' help='Description of the network; should not contain whitespace.'
)
@click.option(
'-n', '--domain', 'domain',
required=True,
help='Domain name of the network.'
) )
@click.option( @click.option(
'-i', '--ipnet', 'ip_network', '-i', '--ipnet', 'ip_network',
@ -602,16 +607,16 @@ def cli_network():
@click.argument( @click.argument(
'vni' 'vni'
) )
def net_add(vni, description, ip_network, ip_gateway, dhcp_flag): def net_add(vni, description, domain, ip_network, ip_gateway, dhcp_flag):
""" """
Add a new virtual network with VXLAN identifier VNI to the cluster. Add a new virtual network with VXLAN identifier VNI to the cluster.
Example: Example:
pvc network add 1001 --ipnet 10.1.1.0/24 --gateway 10.1.1.1 --dhcp pvc network add 1001 --domain test.local --ipnet 10.1.1.0/24 --gateway 10.1.1.1 --dhcp
""" """
zk_conn = pvc_common.startZKConnection(zk_host) zk_conn = pvc_common.startZKConnection(zk_host)
retcode, retmsg = pvc_network.add_network(zk_conn, vni, description, ip_network, ip_gateway, dhcp_flag) retcode, retmsg = pvc_network.add_network(zk_conn, vni, description, domain, ip_network, ip_gateway, dhcp_flag)
cleanup(retcode, retmsg, zk_conn) cleanup(retcode, retmsg, zk_conn)
############################################################################### ###############################################################################
@ -621,7 +626,7 @@ def net_add(vni, description, ip_network, ip_gateway, dhcp_flag):
@click.option( @click.option(
'-d', '--description', 'description', '-d', '--description', 'description',
default=None, default=None,
help='Description of the network. Should not contain whitespace.' help='Description of the network; should not contain whitespace.'
) )
@click.option( @click.option(
'-i', '--ipnet', 'ip_network', '-i', '--ipnet', 'ip_network',
@ -735,7 +740,7 @@ def net_dhcp():
@click.option( @click.option(
'-d', '--description', 'description', '-d', '--description', 'description',
default=None, default=None,
help='Description of the DHCP reservation; defaults to MACADDR if unspecified. Should not contain whitespace.' help='Description of the DHCP reservation; defaults to MACADDR if unspecified; should not contain whitespace.'
) )
@click.argument( @click.argument(
'net' 'net'

View File

@ -111,10 +111,11 @@ def getNetworkFirewallRules(zk_conn, vni):
def getNetworkInformation(zk_conn, vni): def getNetworkInformation(zk_conn, vni):
description = zk_conn.get('/networks/{}'.format(vni))[0].decode('ascii') description = zk_conn.get('/networks/{}'.format(vni))[0].decode('ascii')
domain = zk_conn.get('/networks/{}/domain'.format(vni))[0].decode('ascii')
ip_network = zk_conn.get('/networks/{}/ip_network'.format(vni))[0].decode('ascii') ip_network = zk_conn.get('/networks/{}/ip_network'.format(vni))[0].decode('ascii')
ip_gateway = zk_conn.get('/networks/{}/ip_gateway'.format(vni))[0].decode('ascii') ip_gateway = zk_conn.get('/networks/{}/ip_gateway'.format(vni))[0].decode('ascii')
dhcp_flag = zk_conn.get('/networks/{}/dhcp_flag'.format(vni))[0].decode('ascii') dhcp_flag = zk_conn.get('/networks/{}/dhcp_flag'.format(vni))[0].decode('ascii')
return description, ip_network, ip_gateway, dhcp_flag return description, domain, ip_network, ip_gateway, dhcp_flag
def getDHCPReservationInformation(zk_conn, vni, reservation): def getDHCPReservationInformation(zk_conn, vni, reservation):
description = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_reservations/{}'.format(vni, reservation)) description = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_reservations/{}'.format(vni, reservation))
@ -165,6 +166,7 @@ def formatNetworkInformation(zk_conn, vni, long_output):
def formatNetworkList(zk_conn, net_list): def formatNetworkList(zk_conn, net_list):
net_list_output = [] net_list_output = []
description = {} description = {}
domain = {}
ip_network = {} ip_network = {}
ip_gateway = {} ip_gateway = {}
dhcp_flag = {} dhcp_flag = {}
@ -174,7 +176,7 @@ def formatNetworkList(zk_conn, net_list):
# Gather information for printing # Gather information for printing
for net in net_list: for net in net_list:
# get info # get info
description[net], ip_network[net], ip_gateway[net], dhcp_flag[net] = getNetworkInformation(zk_conn, net) description[net], domain[net], ip_network[net], ip_gateway[net], dhcp_flag[net] = getNetworkInformation(zk_conn, net)
if dhcp_flag[net]: if dhcp_flag[net]:
dhcp_flag_colour[net] = ansiiprint.green() dhcp_flag_colour[net] = ansiiprint.green()
else: else:
@ -184,6 +186,7 @@ def formatNetworkList(zk_conn, net_list):
# Dynamic columns: node_name, hypervisor, migrated # Dynamic columns: node_name, hypervisor, migrated
net_vni_length = 5 net_vni_length = 5
net_description_length = 13 net_description_length = 13
net_domain_length = 8
net_ip_network_length = 12 net_ip_network_length = 12
net_ip_gateway_length = 9 net_ip_gateway_length = 9
for net in net_list: for net in net_list:
@ -195,6 +198,10 @@ def formatNetworkList(zk_conn, net_list):
_net_description_length = len(description[net]) + 1 _net_description_length = len(description[net]) + 1
if _net_description_length > net_description_length: if _net_description_length > net_description_length:
net_description_length = _net_description_length net_description_length = _net_description_length
# domain column
_net_domain_length = len(domain[net]) + 1
if _net_domain_length > net_domain_length:
net_domain_length = _net_domain_length
# ip_network column # ip_network column
_net_ip_network_length = len(ip_network[net]) + 1 _net_ip_network_length = len(ip_network[net]) + 1
if _net_ip_network_length > net_ip_network_length: if _net_ip_network_length > net_ip_network_length:
@ -208,6 +215,7 @@ def formatNetworkList(zk_conn, net_list):
net_list_output_header = '{bold}\ net_list_output_header = '{bold}\
{net_vni: <{net_vni_length}} \ {net_vni: <{net_vni_length}} \
{net_description: <{net_description_length}} \ {net_description: <{net_description_length}} \
{net_domain: <{net_domain_length}} \
{net_ip_network: <{net_ip_network_length}} \ {net_ip_network: <{net_ip_network_length}} \
{net_ip_gateway: <{net_ip_gateway_length}} \ {net_ip_gateway: <{net_ip_gateway_length}} \
{net_dhcp_flag: <8}\ {net_dhcp_flag: <8}\
@ -216,10 +224,12 @@ def formatNetworkList(zk_conn, net_list):
end_bold=ansiiprint.end(), end_bold=ansiiprint.end(),
net_vni_length=net_vni_length, net_vni_length=net_vni_length,
net_description_length=net_description_length, net_description_length=net_description_length,
net_domain_length=net_domain_length,
net_ip_network_length=net_ip_network_length, net_ip_network_length=net_ip_network_length,
net_ip_gateway_length=net_ip_gateway_length, net_ip_gateway_length=net_ip_gateway_length,
net_vni='VNI', net_vni='VNI',
net_description='Description', net_description='Description',
net_domain='Domain',
net_ip_network='Network', net_ip_network='Network',
net_ip_gateway='Gateway', net_ip_gateway='Gateway',
net_dhcp_flag='DHCP' net_dhcp_flag='DHCP'
@ -230,6 +240,7 @@ def formatNetworkList(zk_conn, net_list):
'{bold}\ '{bold}\
{net_vni: <{net_vni_length}} \ {net_vni: <{net_vni_length}} \
{net_description: <{net_description_length}} \ {net_description: <{net_description_length}} \
{net_domain: <{net_domain_length}} \
{net_ip_network: <{net_ip_network_length}} \ {net_ip_network: <{net_ip_network_length}} \
{net_ip_gateway: <{net_ip_gateway_length}} \ {net_ip_gateway: <{net_ip_gateway_length}} \
{dhcp_flag_colour}{net_dhcp_flag: <8}{colour_off}\ {dhcp_flag_colour}{net_dhcp_flag: <8}{colour_off}\
@ -238,10 +249,12 @@ def formatNetworkList(zk_conn, net_list):
end_bold='', end_bold='',
net_vni_length=net_vni_length, net_vni_length=net_vni_length,
net_description_length=net_description_length, net_description_length=net_description_length,
net_domain_length=net_domain_length,
net_ip_network_length=net_ip_network_length, net_ip_network_length=net_ip_network_length,
net_ip_gateway_length=net_ip_gateway_length, net_ip_gateway_length=net_ip_gateway_length,
net_vni=net, net_vni=net,
net_description=description[net], net_description=description[net],
net_domain=domain[net],
net_ip_network=ip_network[net], net_ip_network=ip_network[net],
net_ip_gateway=ip_gateway[net], net_ip_gateway=ip_gateway[net],
net_dhcp_flag=dhcp_flag[net], net_dhcp_flag=dhcp_flag[net],
@ -349,7 +362,7 @@ def isValidIP(ipaddr):
# #
# Direct functions # Direct functions
# #
def add_network(zk_conn, vni, description, ip_network, ip_gateway, dhcp_flag): def add_network(zk_conn, vni, description, domain, ip_network, ip_gateway, dhcp_flag):
if description == '': if description == '':
description = vni description = vni
@ -360,6 +373,7 @@ def add_network(zk_conn, vni, description, ip_network, ip_gateway, dhcp_flag):
# Add the new network to Zookeeper # Add the new network to Zookeeper
transaction = zk_conn.transaction() transaction = zk_conn.transaction()
transaction.create('/networks/{}'.format(vni), description.encode('ascii')) transaction.create('/networks/{}'.format(vni), description.encode('ascii'))
transaction.create('/networks/{}/domain'.format(vni), domain.encode('ascii'))
transaction.create('/networks/{}/ip_network'.format(vni), ip_network.encode('ascii')) transaction.create('/networks/{}/ip_network'.format(vni), ip_network.encode('ascii'))
transaction.create('/networks/{}/ip_gateway'.format(vni), ip_gateway.encode('ascii')) transaction.create('/networks/{}/ip_gateway'.format(vni), ip_gateway.encode('ascii'))
transaction.create('/networks/{}/dhcp_flag'.format(vni), str(dhcp_flag).encode('ascii')) transaction.create('/networks/{}/dhcp_flag'.format(vni), str(dhcp_flag).encode('ascii'))