Improve limit handling

This commit is contained in:
Joshua Boniface 2018-09-25 02:20:32 -04:00
parent 615169cd69
commit 7daed1b0d8
4 changed files with 66 additions and 22 deletions

View File

@ -217,16 +217,28 @@ def get_info(zk_conn, network, long_output):
def get_list(zk_conn, limit): def get_list(zk_conn, limit):
net_list = [] net_list = []
full_net_list = zk_conn.get_children('/networks') full_net_list = zk_conn.get_children('/networks')
for net in full_net_list: for net in full_net_list:
description = zkhandler.readdata(zk_conn, '/networks/{}'.format(net)) description = zkhandler.readdata(zk_conn, '/networks/{}'.format(net))
if limit != None: if limit != None:
try: try:
if not re.match(limit, net) or not re.match(limit, description): # Implcitly assume fuzzy limits
continue if re.match('\^.*', limit) == None:
limit = '.*' + limit
if re.match('.*\$', limit) == None:
limit = limit + '.*'
if re.match(limit, net) != None:
net_list.append(net)
if re.match(limit, description) != None:
net_list.append(net)
except Exception as e: except Exception as e:
return False, 'Regex Error: {}'.format(e) return False, 'Regex Error: {}'.format(e)
else:
net_list.append(net) net_list.append(net)
print(net_list)
net_list_output = [] net_list_output = []
description = {} description = {}
ip_network = {} ip_network = {}

View File

@ -169,10 +169,17 @@ def get_list(zk_conn, limit):
for node in full_node_list: for node in full_node_list:
if limit != None: if limit != None:
try: try:
if re.match(limit, node) == None: # Implcitly assume fuzzy limits
continue if re.match('\^.*', limit) == None:
limit = '.*' + limit
if re.match('.*\$', limit) == None:
limit = limit + '.*'
if re.match(limit, node) != None:
node_list.append(node)
except Exception as e: except Exception as e:
return False, 'Regex Error: {}'.format(e) return False, 'Regex Error: {}'.format(e)
else:
node_list.append(node) node_list.append(node)
node_list_output = [] node_list_output = []

View File

@ -121,11 +121,17 @@ def get_list(zk_conn, limit):
for router in full_router_list: for router in full_router_list:
if limit != None: if limit != None:
try: try:
if re.match(limit, router) == None: # Implcitly assume fuzzy limits
continue if re.match('\^.*', limit) == None:
limit = '.*' + limit
if re.match('.*\$', limit) == None:
limit = limit + '.*'
if re.match(limit, router) != None:
router_list.append(router)
except Exception as e: except Exception as e:
common.stopZKConnection(zk_conn)
return False, 'Regex Error: {}'.format(e) return False, 'Regex Error: {}'.format(e)
else:
router_list.append(router) router_list.append(router)
router_list_output = [] router_list_output = []

View File

@ -35,6 +35,7 @@ import configparser
import kazoo.client import kazoo.client
import client_lib.ansiiprint as ansiiprint import client_lib.ansiiprint as ansiiprint
import client_lib.zkhandler as zkhandler
import client_lib.common as common import client_lib.common as common
# #
@ -466,7 +467,7 @@ def get_list(zk_conn, hypervisor, limit):
# Verify node is valid # Verify node is valid
common.verifyNode(zk_conn, hypervisor) common.verifyNode(zk_conn, hypervisor)
vm_list_raw = zk_conn.get_children('/domains') full_vm_list = zk_conn.get_children('/domains')
vm_list = [] vm_list = []
vm_list_output = [] vm_list_output = []
@ -480,18 +481,36 @@ def get_list(zk_conn, hypervisor, limit):
vm_vcpu = {} vm_vcpu = {}
# If we're limited, remove other nodes' VMs # If we're limited, remove other nodes' VMs
for vm in vm_list_raw: for vm in full_vm_list:
# Check we don't match the limit # Check we don't match the limit
name = zk_conn.get('/domains/{}'.format(vm))[0].decode('ascii') name = zkhandler.readdata(zk_conn, '/domains/{}'.format(vm))
vm_hypervisor[vm] = zkhandler.readdata(zk_conn, '/domains/{}/hypervisor'.format(vm))
if limit != None: if limit != None:
try: try:
if re.match(limit, name) == None: # Implcitly assume fuzzy limits
continue if re.match('\^.*', limit) == None:
limit = '.*' + limit
if re.match('.*\$', limit) == None:
limit = limit + '.*'
if re.match(limit, vm) != None:
if hypervisor == None:
vm_list.append(vm)
else:
if vm_hypervisor[vm] == hypervisor:
vm_list.append(vm)
if re.match(limit, name) != None:
if hypervisor == None:
vm_list.append(vm)
else:
if vm_hypervisor[vm] == hypervisor:
vm_list.append(vm)
except Exception as e: except Exception as e:
click.echo('Regex Error: {}'.format(e)) return False, 'Regex Error: {}'.format(e)
exit(1) else:
# Check hypervisor to avoid unneeded ZK calls # Check hypervisor to avoid unneeded ZK calls
vm_hypervisor[vm] = zk_conn.get('/domains/{}/hypervisor'.format(vm))[0].decode('ascii')
if hypervisor == None: if hypervisor == None:
vm_list.append(vm) vm_list.append(vm)
else: else: