Handle regex in a more consistent way

This commit is contained in:
Joshua Boniface 2019-03-12 23:52:59 -04:00
parent 19445205d7
commit df394c5c39
1 changed files with 12 additions and 7 deletions

View File

@ -515,20 +515,25 @@ def get_list(zk_conn, node, limit, raw):
vm_vcpu = {} vm_vcpu = {}
vm_nets = {} vm_nets = {}
# Set our limit to a sensible regex
if limit != None:
try:
# Implcitly assume fuzzy limits
if re.match('\^.*', limit) == None:
limit = '.*' + limit
if re.match('.*\$', limit) == None:
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
# If we're limited, remove other nodes' VMs # If we're limited, remove other nodes' VMs
for vm in full_vm_list: for vm in full_vm_list:
# Check we don't match the limit # Check we don't match the limit
name = zkhandler.readdata(zk_conn, '/domains/{}'.format(vm)) name = zkhandler.readdata(zk_conn, '/domains/{}'.format(vm))
vm_node[vm] = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(vm)) vm_node[vm] = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(vm))
# Handle limiting
if limit != None: if limit != None:
try: try:
# Implcitly assume fuzzy limits
if re.match('\^.*', limit) == None:
limit = '.*' + limit
if re.match('.*\$', limit) == None:
limit = limit + '.*'
if re.match(limit, vm) != None: if re.match(limit, vm) != None:
if node == None: if node == None:
vm_list.append(vm) vm_list.append(vm)