Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Improve 'vcd search' #575

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Search - sort on a 2nd field
Add option --sort-next to add sort on a second field.
This option must be used with --sort-asc or --sort-desc

Signed-off-by: Olivier DRAGHI <[email protected]>
odraghi committed Oct 10, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit ab00c7a777c5cff0fd81e1d11caec91d5972bcfd
37 changes: 33 additions & 4 deletions vcd_cli/search.py
Original file line number Diff line number Diff line change
@@ -63,7 +63,13 @@
required=False,
metavar='[field]',
help='sort in descending order on a field')
def search(ctx, resource_type, query_filter, fields, show_id, sort_asc, sort_desc):
@click.option(
'--sort-next',
'sort_next',
required=False,
metavar='[field]',
help='--sort_asc or --sort-desc on a second field')
def search(ctx, resource_type, query_filter, fields, show_id, sort_asc, sort_desc, sort_next):
"""Search for resources in vCloud Director.

\b
@@ -121,11 +127,14 @@ def search(ctx, resource_type, query_filter, fields, show_id, sort_asc, sort_des
vcd search vm --fields 'containerName as containerName(vapp),name,ownerName as owner,isAutoNature as standalone' \\
--sort-asc containerName --filter 'isVAppTemplate==false' --hide-id
Search for virtual machines, show only some fields, use 'as' to customize field name.
\b
vcd search vm --fields 'containerName as vapp,name' --sort-asc containerName --sort-next name --hide-id
Search for virtual machines and show only some fields.
"""
return query(ctx, resource_type, query_filter, fields, show_id, sort_asc, sort_desc)
return query(ctx, resource_type, query_filter, fields, show_id, sort_asc, sort_desc, sort_next)


def query(ctx, resource_type=None, query_filter=None, fields=None, show_id=True, sort_asc=None, sort_desc=None):
def query(ctx, resource_type=None, query_filter=None, fields=None, show_id=True, sort_asc=None, sort_desc=None, sort_next=None):
try:
if resource_type is None:
click.secho(ctx.get_help())
@@ -144,7 +153,7 @@ def query(ctx, resource_type=None, query_filter=None, fields=None, show_id=True,
if label:
label = label.pop()
headers[field] = label
fields = ','.join( headers.keys() )
fields = ','.join(headers.keys())
q = client.get_typed_query(
resource_type_cc,
query_result_format=QueryResultFormat.ID_RECORDS,
@@ -166,6 +175,26 @@ def query(ctx, resource_type=None, query_filter=None, fields=None, show_id=True,
d_with_custom_header[label] = d.pop(field)
d = d_with_custom_header
result.append(d)
if sort_next and (sort_asc or sort_desc):
if sort_asc:
reverse=False
sort_key1 = sort_asc
if sort_desc:
reverse=True
sort_key1 = sort_desc
sort_key2=sort_next
if sort_key1 in headers:
sort_key1 = headers[sort_key1]
if sort_key2 in headers:
sort_key2 = headers[sort_key2]
keys = list(result[0].keys())
if sort_key1 not in keys:
raise Exception('sort key \'%s\' not in %s' % (sort_key1, keys))
if sort_key2 not in keys:
raise Exception('sort_next \'%s\' not in %s' % (sort_key2, keys))
result=sorted(result, key=lambda d: (d[sort_key1], d[sort_key2]), reverse=reverse)
elif sort_next:
raise Exception('sort_next must be used with sort_asc or sort_desc')
stdout(result, ctx, show_id=show_id, sort_headers=False)
except Exception as e:
stderr(e, ctx)