Skip to content

Commit

Permalink
fix: crash when filtering returns 0 results
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt authored and lcaflc committed Nov 26, 2024
1 parent 075761e commit 57c6892
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/pvecontrol/actions/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

def action_nodelist(proxmox, args):
"""List proxmox nodes in the cluster using proxmoxer api"""
nodes = [ filter_keys(n.__dict__, ['node', 'status', 'allocatedcpu', 'maxcpu', 'mem', 'allocatedmem', 'maxmem']) for n in proxmox.nodes ]
print_tableoutput(nodes, sortby=args.sort_by, filters=args.filter)
columns = ['node', 'status', 'allocatedcpu', 'maxcpu', 'mem', 'allocatedmem', 'maxmem']
print_tableoutput(proxmox.nodes, columns, sortby=args.sort_by, filters=args.filter)

def action_nodeevacuate(proxmox, args):
"""Evacuate a node by migrating all it's VM out"""
Expand Down
4 changes: 2 additions & 2 deletions src/pvecontrol/actions/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


def action_tasklist(proxmox, args):
tasks = [ filter_keys(t.__dict__, ['upid', 'exitstatus', 'node', 'type', 'starttime', 'endtime', 'runningstatus', 'description']) for t in proxmox.tasks ]
print_tableoutput(tasks, sortby=args.sort_by, filters=args.filter)
columns = ['upid', 'exitstatus', 'node', 'type', 'starttime', 'endtime', 'runningstatus', 'description']
print_tableoutput(proxmox.tasks, columns, sortby=args.sort_by, filters=args.filter)

def action_taskget(proxmox, args):
print_task(proxmox, args.upid, args.follow, args.wait, show_logs = True)
5 changes: 3 additions & 2 deletions src/pvecontrol/actions/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ def action_vmmigrate(proxmox, args):

def action_vmlist(proxmox, args):
"""List VMs in the Proxmox Cluster"""
vms = [ filter_keys(n.__dict__, ['vmid', 'name', 'status', 'node', 'cpus', 'maxmem', 'maxdisk']) for n in proxmox.vms() ]
print_tableoutput(vms, sortby=args.sort_by, filters=args.filter)
vms = proxmox.vms()
columns = ['vmid', 'name', 'status', 'node', 'cpus', 'maxmem', 'maxdisk']
print_tableoutput(vms, columns, sortby=args.sort_by, filters=args.filter)
13 changes: 9 additions & 4 deletions src/pvecontrol/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

# Pretty output a table from a table of dicts
# We assume all dicts have the same keys and are sorted by key
def print_tableoutput(table, sortby=None, filters=[]):
def print_tableoutput(table, columns=[], sortby=None, filters=[]):
if len(columns) == 0:
columns = table[0].keys()
else:
table = [ filter_keys(n.__dict__, columns) for n in table ]

do_sort = not sortby is None

x = PrettyTable()
Expand All @@ -34,7 +39,7 @@ def print_tableoutput(table, sortby=None, filters=[]):
for line in table:
x.add_row( line.values() )

print(x.get_string(sortby="sortby" if do_sort else None, fields=table[0].keys()))
print(x.get_string(sortby="sortby" if do_sort else None, fields=columns))

def filter_keys(input, keys):
# Filter keys from input dict
Expand All @@ -44,8 +49,8 @@ def filter_keys(input, keys):
return output

def print_taskstatus(task):
output = [ filter_keys(task.__dict__, ['upid', 'exitstatus', 'node', 'runningstatus', 'type', 'user', 'starttime']) ]
print_tableoutput(output)
columns = ['upid', 'exitstatus', 'node', 'runningstatus', 'type', 'user', 'starttime']
print_tableoutput([task], columns)

def print_task(proxmox, upid, follow = False, wait = False, show_logs = False):
task = proxmox.find_task(upid)
Expand Down

0 comments on commit 57c6892

Please sign in to comment.