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

Commit

Permalink
[VCDA-549] Fix query service to handle special characters in filter v…
Browse files Browse the repository at this point in the history
…alues (#239)

As a fallout of vmware-archive/pyvcloud#282, we need to make sure that we url encode filter values properly in vcd-cli.

Changed qfilter to equality_filter wherever possible, since equality_filter url-encodes the value properly.

Tested the following commands to make sure that no regressions were introduced
vcd catalog list 
vcd info adminVApp 
vcd vapp list
vcd vapp list

Also replaced hard-coded strings to their ResourceType enum counterparts.
  • Loading branch information
rocknes authored Sep 6, 2018
1 parent 2d23ba2 commit bbef5ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
9 changes: 6 additions & 3 deletions vcd_cli/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import click
from pyvcloud.vcd.client import QueryResultFormat
from pyvcloud.vcd.client import ResourceType
from pyvcloud.vcd.exceptions import AccessForbiddenException
from pyvcloud.vcd.org import Org
from pyvcloud.vcd.utils import access_settings_to_dict
Expand Down Expand Up @@ -171,12 +172,14 @@ def list_catalogs_or_items(ctx, catalog_name):
result = org.list_catalogs()
else:
result = []
resource_type = \
'adminCatalogItem' if is_sysadmin(ctx) else 'catalogItem'
if is_sysadmin(ctx):
resource_type = ResourceType.ADMIN_CATALOG_ITEM.value
else:
resource_type = ResourceType.CATALOG_ITEM.value
q = client.get_typed_query(
resource_type,
query_result_format=QueryResultFormat.ID_RECORDS,
qfilter='catalogName==%s' % catalog_name)
equality_filter=('catalogName', catalog_name))
records = list(q.execute())
if len(records) == 0:
result = 'not found'
Expand Down
2 changes: 1 addition & 1 deletion vcd_cli/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def info(ctx, resource_type, resource_id):
q = client.get_typed_query(
to_camel_case(resource_type, RESOURCE_TYPES),
query_result_format=QueryResultFormat.REFERENCES,
qfilter='id==%s' % resource_id)
equality_filter=('id', resource_id))
records = list(q.execute())
if len(records) == 0:
raise Exception('not found')
Expand Down
22 changes: 16 additions & 6 deletions vcd_cli/vapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import click
from pyvcloud.vcd.client import QueryResultFormat
from pyvcloud.vcd.client import ResourceType
from pyvcloud.vcd.org import Org
from pyvcloud.vcd.utils import access_settings_to_dict
from pyvcloud.vcd.utils import to_dict
Expand Down Expand Up @@ -241,23 +242,32 @@ def list_vapps(ctx, name):
client = ctx.obj['client']
result = []
if name is None:
resource_type = 'adminVApp' if is_sysadmin(ctx) else 'vApp'
qfilter = None
if is_sysadmin(ctx):
resource_type = ResourceType.ADMIN_VAPP.value
else:
resource_type = ResourceType.VAPP.value
name_filter = None
attributes = None
else:
resource_type = 'adminVm' if is_sysadmin(ctx) else 'vm'
qfilter = 'containerName==%s' % name
if is_sysadmin(ctx):
resource_type = ResourceType.ADMIN_VM.value
else:
resource_type = ResourceType.VM.value
name_filter = ('containerName', name)
attributes = [
'name', 'containerName', 'ipAddress', 'status', 'memoryMB',
'numberOfCpus'
]
q = client.get_typed_query(
resource_type,
query_result_format=QueryResultFormat.ID_RECORDS,
qfilter=qfilter)
equality_filter=name_filter)
records = list(q.execute())
if len(records) == 0:
result = 'not found'
if name is None:
result = 'No vApps were found.'
else:
result = 'No vms were found.'
else:
for r in records:
result.append(
Expand Down

0 comments on commit bbef5ad

Please sign in to comment.