Skip to content

Commit

Permalink
*-list command shows only limited fields normally.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yong sheng gong committed Sep 1, 2012
1 parent 5e2f6fd commit 62f5089
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 19 deletions.
6 changes: 3 additions & 3 deletions quantum_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions quantumclient/quantum/v2_0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -374,7 +375,13 @@ def get_data(self, parsed_args):
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), )
Expand Down
1 change: 1 addition & 0 deletions quantumclient/quantum/v2_0/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions quantumclient/quantum/v2_0/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions quantumclient/quantum/v2_0/subnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 0 additions & 8 deletions quantumclient/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"""
import argparse
import gettext
import itertools
import logging
import os
import sys
Expand Down Expand Up @@ -424,15 +423,8 @@ def configure_logging(self):
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
Expand Down
22 changes: 22 additions & 0 deletions quantumclient/tests/unit/test_cli20.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from quantumclient.quantum import v2_0 as quantumv20
from quantumclient.v2_0.client import Client


API_VERSION = "2.0"
FORMAT = 'json'
TOKEN = 'testtoken'
Expand Down Expand Up @@ -172,6 +173,27 @@ def _test_create_resource(self, resource, cmd,
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")
Expand Down
35 changes: 34 additions & 1 deletion quantumclient/tests/unit/test_cli20_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,7 +93,7 @@ def test_create_network_state(self):
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")
Expand Down Expand Up @@ -146,6 +147,38 @@ def test_list_nets_fields(self):
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'
Expand Down
2 changes: 1 addition & 1 deletion quantumclient/v2_0/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def do_request(self, method, action, body=None, headers=None, params=None):
# 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)
Expand Down
2 changes: 1 addition & 1 deletion tools/pip-requires
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cliff>=0.6.0
cliff>=1.2.1
argparse
httplib2
prettytable>=0.6.0
Expand Down
6 changes: 1 addition & 5 deletions tools/test-requires
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 62f5089

Please sign in to comment.