*-list command shows only limited fields normally.

Bug #1036051

We add list_columns in list commands to limit the output columns.
The behaviour is overriden if we use -c in command.

Change-Id: I0fa6c73cd7270d86aff01d3790d59c8d4e8a235a
This commit is contained in:
yong sheng gong 2012-09-01 11:38:45 +08:00
parent 5e2f6fd375
commit 62f508939e
11 changed files with 73 additions and 19 deletions

View File

@ -74,11 +74,11 @@ tenant_id=tenant_a
tenant_id_b=tenant_b
quantum quota-update --tenant_id $tenant_id --network 30 || die "fail to update quota for tenant $tenant_id"
quantum quota-update --tenant_id $tenant_id_b --network 20 || die "fail to update quota for tenant $tenant_id"
networks=`quantum quota-list | grep $tenant_id | awk '{print $2}'`
networks=`quantum quota-list -c network -c tenant_id | grep $tenant_id | awk '{print $2}'`
if [ $networks -ne 30 ]; then
die "networks quota should be 30"
fi
networks=`quantum quota-list | grep $tenant_id_b | awk '{print $2}'`
networks=`quantum quota-list -c network -c tenant_id | grep $tenant_id_b | awk '{print $2}'`
if [ $networks -ne 20 ]; then
die "networks quota should be 20"
fi
@ -100,7 +100,7 @@ if [ "t$NOAUTH" = "t" ]; then
die "ports quota should be 99"
fi
ports=`quantum quota-list | grep 99 | awk '{print $4}'`
ports=`quantum quota-list -c port | grep 99 | awk '{print $2}'`
if [ $ports -ne 99 ]; then
die "ports quota should be 99"
fi

View File

@ -339,6 +339,7 @@ class ListCommand(QuantumCommand, lister.Lister):
resource = None
log = None
_formatters = None
list_columns = []
def get_parser(self, prog_name):
parser = super(ListCommand, self).get_parser(prog_name)
@ -374,7 +375,13 @@ class ListCommand(QuantumCommand, lister.Lister):
info = data[collection]
_columns = len(info) > 0 and sorted(info[0].keys()) or []
if not _columns:
# clean the parsed_args.columns so that cliff will not break
parsed_args.columns = []
elif not parsed_args.columns and self.list_columns:
# if no -c(s) by user and list_columns, we use columns in
# both list_columns and returned resource.
# Also Keep their order the same as in list_columns
_columns = [x for x in self.list_columns if x in _columns]
return (_columns, (utils.get_item_properties(
s, _columns, formatters=self._formatters, )
for s in info), )

View File

@ -38,6 +38,7 @@ class ListNetwork(ListCommand):
resource = 'network'
log = logging.getLogger(__name__ + '.ListNetwork')
_formatters = {'subnets': _format_subnets, }
list_columns = ['id', 'name', 'tenant_id', 'subnets']
class ShowNetwork(ShowCommand):

View File

@ -40,6 +40,7 @@ class ListPort(ListCommand):
resource = 'port'
log = logging.getLogger(__name__ + '.ListPort')
_formatters = {'fixed_ips': _format_fixed_ips, }
list_columns = ['id', 'name', 'mac_address', 'fixed_ips']
class ShowPort(ShowCommand):

View File

@ -18,6 +18,7 @@
import argparse
import logging
from quantumclient.common import exceptions
from quantumclient.common import utils
from quantumclient.quantum import v2_0 as quantumv20
from quantumclient.quantum.v2_0 import CreateCommand
@ -59,6 +60,7 @@ class ListSubnet(ListCommand):
_formatters = {'allocation_pools': _format_allocation_pools,
'dns_nameservers': _format_dns_nameservers,
'host_routes': _format_host_routes, }
list_columns = ['id', 'name', 'cidr', 'allocation_pools']
class ShowSubnet(ShowCommand):

View File

@ -20,7 +20,6 @@ Command-line interface to the Quantum APIs
"""
import argparse
import gettext
import itertools
import logging
import os
import sys
@ -424,15 +423,8 @@ class QuantumShell(App):
return
def itertools_compressdef(data, selectors):
# patch 2.6 compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
return (d for d, s in itertools.izip(data, selectors) if s)
def main(argv=sys.argv[1:]):
try:
if not getattr(itertools, 'compress', None):
setattr(itertools, 'compress', itertools_compressdef)
return QuantumShell(QUANTUM_API_VERSION).run(argv)
except exc.QuantumClientException:
return 1

View File

@ -25,6 +25,7 @@ from mox import Comparator
from quantumclient.quantum import v2_0 as quantumv20
from quantumclient.v2_0.client import Client
API_VERSION = "2.0"
FORMAT = 'json'
TOKEN = 'testtoken'
@ -172,6 +173,27 @@ class CLITestV20Base(unittest.TestCase):
self.assertTrue(myid in _str)
self.assertTrue(name in _str)
def _test_list_columns(self, cmd, resources_collection,
resources_out, args=['-f', 'json']):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
resstr = self.client.serialize(resources_out)
path = getattr(self.client, resources_collection + "_path")
self.client.httpclient.request(
end_url(path), 'GET',
body=None,
headers=ContainsKeyValue('X-Auth-Token',
TOKEN)).AndReturn((MyResp(200), resstr))
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("list_" + resources_collection)
parsed_args = cmd_parser.parse_args(args)
cmd.run(parsed_args)
self.mox.VerifyAll()
self.mox.UnsetStubs()
def _test_list_resources(self, resources, cmd, detail=False, tags=[],
fields_1=[], fields_2=[]):
self.mox.StubOutWithMock(cmd, "get_client")

View File

@ -18,6 +18,7 @@
import sys
from quantumclient.common import exceptions
from quantumclient.common import utils
from quantumclient.tests.unit import test_cli20
from quantumclient.tests.unit.test_cli20 import CLITestV20Base
from quantumclient.tests.unit.test_cli20 import MyApp
@ -92,7 +93,7 @@ class CLITestV20Network(CLITestV20Base):
position_names, position_values,
admin_state_up=False)
def test_lsit_nets_empty_with_column(self):
def test_list_nets_empty_with_column(self):
resources = "networks"
cmd = ListNetwork(MyApp(sys.stdout), None)
self.mox.StubOutWithMock(cmd, "get_client")
@ -146,6 +147,38 @@ class CLITestV20Network(CLITestV20Base):
self._test_list_resources(resources, cmd,
fields_1=['a', 'b'], fields_2=['c', 'd'])
def test_list_nets_defined_column(self):
resources = 'networks'
cmd = ListNetwork(MyApp(sys.stdout), None)
returned_body = {"networks": [{"name": "buildname3",
"id": "id3",
"tenant_id": "tenant_3",
"subnets": []}]}
self._test_list_columns(cmd, resources, returned_body,
args=['-f', 'json', '-c', 'id'])
_str = self.fake_stdout.make_string()
returned_networks = utils.loads(_str)
self.assertEquals(1, len(returned_networks))
network = returned_networks[0]
self.assertEquals(1, len(network))
self.assertEquals("id", network.keys()[0])
def test_list_nets_with_default_column(self):
resources = 'networks'
cmd = ListNetwork(MyApp(sys.stdout), None)
returned_body = {"networks": [{"name": "buildname3",
"id": "id3",
"tenant_id": "tenant_3",
"subnets": []}]}
self._test_list_columns(cmd, resources, returned_body)
_str = self.fake_stdout.make_string()
returned_networks = utils.loads(_str)
self.assertEquals(1, len(returned_networks))
network = returned_networks[0]
self.assertEquals(4, len(network))
self.assertEquals(0, len(set(network) ^
set(cmd.list_columns)))
def test_update_network_exception(self):
"""Update net: myid."""
resource = 'network'

View File

@ -439,7 +439,7 @@ class Client(object):
# Add format and tenant_id
action += ".%s" % self.format
action = self.action_prefix + action
if type(params) is dict:
if type(params) is dict and params:
action += '?' + urllib.urlencode(params, doseq=1)
if body:
body = self.serialize(body)

View File

@ -1,4 +1,4 @@
cliff>=0.6.0
cliff>=1.2.1
argparse
httplib2
prettytable>=0.6.0

View File

@ -1,9 +1,5 @@
distribute>=0.6.24
cliff>=0.6.0
argparse
httplib2
prettytable>=0.6.0
simplejson
cliff-tablib>=1.0
mox
nose
nose-exclude