Skip to content

Commit

Permalink
filter vms belonging to a given network
Browse files Browse the repository at this point in the history
  • Loading branch information
karmab committed Feb 14, 2025
1 parent 6cc4261 commit 48d2072
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions kvirt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,11 @@ def _filter_list(_list, overrides={}):
for vm in _list:
match = True
for key in overrides:
if (overrides[key] is None and vm.get(key) is not None)\
or (overrides[key] is not None and vm.get(key) is None)\
or vm[key] != overrides[key]:
override = overrides[key]
value = vm.get(key)
if key == 'network' and override in [n['net'] for n in vm.get('nets')]:
continue
elif (overrides is None) != (value is None) or value != override:
match = False
break
if match:
Expand Down Expand Up @@ -791,7 +793,7 @@ def _parse_vms_list(_list, overrides={}):
def list_vm(args):
output = args.global_output or args.output
overrides = handle_parameters(args.param, args.paramfile)
filter_keys = ['name', 'ip', 'status', 'image', 'plan', 'profile']
filter_keys = ['name', 'ip', 'status', 'image', 'network', 'plan', 'profile']
if [key for key in overrides if key not in filter_keys]:
overrides = {}
if args.client is not None and args.client == 'all':
Expand Down
7 changes: 5 additions & 2 deletions kvirt/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,9 +848,12 @@
# Combine filters
$ kcli list vm -P plan=prod -P status=up
# Get vms whose ip starts with 192.168.122
# Filter vms whose ip start with 192.168.122
$ kcli list vm -P ip=192.168.122
# Get vms whose name starts with myclu
# Filter vms with a name that starts with myclu
$ kcli list vm -P name=myclu
# Filter vms which belong to a specific network
$ kcli list vm -P network=baremetal
"""

0 comments on commit 48d2072

Please sign in to comment.