Use print_list and print_dict functions from common code

Remove 'print_list' and 'print_dict' functions from
manilaclient.utils and use same functions from cliutils instead.

Partially implements: blueprint use-common-code

Change-Id: I5f7ac638403bfc23b855219f6e11469e23481fbc
This commit is contained in:
Sun Jun 2015-01-09 13:25:21 +08:00
parent 6773889fd6
commit 2f8a58acf4
4 changed files with 68 additions and 113 deletions

View File

@ -25,10 +25,10 @@ from manilaclient import client
from manilaclient.common import constants
from manilaclient import exceptions
from manilaclient.openstack.common.apiclient import utils as apiclient_utils
from manilaclient.openstack.common import cliutils
from manilaclient import shell
from manilaclient.tests.unit import utils as test_utils
from manilaclient.tests.unit.v1 import fakes
from manilaclient import utils
from manilaclient.v1 import client as client_v1
from manilaclient.v1 import shell as shell_v1
@ -478,29 +478,29 @@ class ShellTest(test_utils.TestCase):
expected = {'os-reset_status': {'status': 'error'}}
self.assert_called('POST', '/snapshots/1234/action', body=expected)
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list(self):
self.run_command('share-network-list')
self.assert_called(
'GET',
'/share-networks/detail',
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_all_tenants(self):
self.run_command('share-network-list --all-tenants')
self.assert_called(
'GET',
'/share-networks/detail?all_tenants=1',
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
@mock.patch.object(shell_v1, '_find_security_service', mock.Mock())
def test_share_network_list_filter_by_security_service(self):
ss = type('FakeSecurityService', (object,), {'id': 'fake-ss-id'})
@ -514,11 +514,11 @@ class ShellTest(test_utils.TestCase):
'/share-networks/detail?security_service_id=%s' % ss.id,
)
shell_v1._find_security_service.assert_called_with(mock.ANY, ss.id)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_project_id_aliases(self):
for command in ['--project-id', '--project_id']:
self.run_command('share-network-list %s 1234' % command)
@ -526,11 +526,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?project_id=1234',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_created_before_aliases(self):
for command in ['--created-before', '--created_before']:
self.run_command('share-network-list %s 2001-01-01' % command)
@ -538,11 +538,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?created_before=2001-01-01',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_created_since_aliases(self):
for command in ['--created-since', '--created_since']:
self.run_command('share-network-list %s 2001-01-01' % command)
@ -550,11 +550,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?created_since=2001-01-01',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_neutron_net_id_aliases(self):
for command in ['--neutron-net-id', '--neutron-net_id',
'--neutron_net-id', '--neutron_net_id']:
@ -563,11 +563,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?neutron_net_id=fake-id',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_neutron_subnet_id_aliases(self):
for command in ['--neutron-subnet-id', '--neutron-subnet_id',
'--neutron_subnet-id', '--neutron_subnet_id']:
@ -576,11 +576,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?neutron_subnet_id=fake-id',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_network_type_aliases(self):
for command in ['--network_type', '--network-type']:
self.run_command('share-network-list %s local' % command)
@ -588,11 +588,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?network_type=local',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_segmentation_id_aliases(self):
for command in ['--segmentation-id', '--segmentation_id']:
self.run_command('share-network-list %s 1234' % command)
@ -600,11 +600,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?segmentation_id=1234',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_ip_version_aliases(self):
for command in ['--ip-version', '--ip_version']:
self.run_command('share-network-list %s 4' % command)
@ -612,11 +612,11 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?ip_version=4',
)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_share_network_list_all_filters(self):
filters = {
'name': 'fake-name',
@ -643,7 +643,7 @@ class ShellTest(test_utils.TestCase):
'GET',
'/share-networks/detail?%s' % query,
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name'])
@ -784,18 +784,18 @@ class ShellTest(test_utils.TestCase):
cmd.split.assert_called_once_with()
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_security_service_list(self):
self.run_command('security-service-list')
self.assert_called(
'GET',
'/security-services',
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name', 'status', 'type'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
@mock.patch.object(shell_v1, '_find_share_network', mock.Mock())
def test_security_service_list_filter_share_network(self):
class FakeShareNetwork:
@ -811,33 +811,33 @@ class ShellTest(test_utils.TestCase):
'/security-services?share_network_id=%s' % sn.id,
)
shell_v1._find_share_network.assert_called_with(mock.ANY, sn.id)
utils.print_list.assert_called_with(
cliutils.print_list.assert_called_with(
mock.ANY,
fields=['id', 'name', 'status', 'type'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_security_service_list_detailed(self):
self.run_command('security-service-list --detailed')
self.assert_called(
'GET',
'/security-services/detail',
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name', 'status', 'type', 'share_networks'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_security_service_list_all_tenants(self):
self.run_command('security-service-list --all-tenants')
self.assert_called(
'GET',
'/security-services?all_tenants=1',
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name', 'status', 'type'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_security_service_list_all_filters(self):
filters = {
'status': 'new',
@ -861,18 +861,18 @@ class ShellTest(test_utils.TestCase):
'&name=fake-name&offset=10&server=fake-server&status=new'
'&type=ldap&user=fake-user',
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name', 'status', 'type'])
@mock.patch.object(utils, 'print_list', mock.Mock())
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_security_service_list_filter_by_dns_ip_alias(self):
self.run_command('security-service-list --dns_ip 1.1.1.1')
self.assert_called(
'GET',
'/security-services?dns_ip=1.1.1.1',
)
utils.print_list.assert_called_once_with(
cliutils.print_list.assert_called_once_with(
mock.ANY,
fields=['id', 'name', 'status', 'type'])

View File

@ -10,50 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import print_function
import sys
from oslo.utils import encodeutils
import prettytable
import six
def _print(pt, order=None):
output = pt.get_string(sortby=order) if order else pt.get_string()
if sys.version_info >= (3, 0):
print(output)
else:
print(encodeutils.safe_encode(output))
def print_list(objs, fields, formatters={}, order_by=None):
mixed_case_fields = ['serverId']
pt = prettytable.PrettyTable([f for f in fields], caching=False)
pt.aligns = ['l' for f in fields]
for o in objs:
row = []
for field in fields:
if field in formatters:
row.append(formatters[field](o))
else:
if field in mixed_case_fields:
field_name = field.replace(' ', '_')
else:
field_name = field.lower().replace(' ', '_')
data = getattr(o, field_name, '')
row.append(data)
pt.add_row(row)
_print(pt, order_by)
def print_dict(d, property="Property"):
pt = prettytable.PrettyTable([property, 'Value'], caching=False)
pt.aligns = ['l', 'l']
[pt.add_row(list(r)) for r in six.iteritems(d)]
_print(pt, property)
class HookableMixin(object):
"""Mixin so classes can register and run hooks."""

View File

@ -16,7 +16,6 @@
from manilaclient import base
from manilaclient.openstack.common.apiclient import base as common_base
from manilaclient.openstack.common import cliutils
from manilaclient import utils
class ListExtResource(common_base.Resource):
@ -44,4 +43,4 @@ def do_list_extensions(client, _args):
"""List all the os-api extensions that are available."""
extensions = client.list_extensions.show_all()
fields = ["Name", "Summary", "Alias", "Updated"]
utils.print_list(extensions, fields)
cliutils.print_list(extensions, fields)

View File

@ -23,7 +23,6 @@ from manilaclient.common import constants
from manilaclient import exceptions
from manilaclient.openstack.common.apiclient import utils as apiclient_utils
from manilaclient.openstack.common import cliutils
from manilaclient import utils
from manilaclient.v1 import quotas
@ -64,7 +63,7 @@ def _find_share(cs, share):
def _print_share(cs, share):
info = share._info.copy()
utils.print_dict(info)
cliutils.print_dict(info)
def _find_share_snapshot(cs, snapshot):
@ -75,7 +74,7 @@ def _find_share_snapshot(cs, snapshot):
def _print_share_snapshot(cs, snapshot):
info = snapshot._info.copy()
info.pop('links')
utils.print_dict(info)
cliutils.print_dict(info)
def _find_share_network(cs, share_network):
@ -131,14 +130,14 @@ def do_endpoints(cs, args):
"""Discover endpoints that get returned from the authenticate services."""
catalog = cs.client.service_catalog.catalog
for e in catalog['access']['serviceCatalog']:
utils.print_dict(e['endpoints'][0], e['name'])
cliutils.print_dict(e['endpoints'][0], e['name'])
def do_credentials(cs, args):
"""Show user credentials returned from auth."""
catalog = cs.client.service_catalog.catalog
utils.print_dict(catalog['access']['user'], "User Credentials")
utils.print_dict(catalog['access']['token'], "Token")
cliutils.print_dict(catalog['access']['user'], "User Credentials")
cliutils.print_dict(catalog['access']['token'], "Token")
_quota_resources = ['shares', 'snapshots', 'gigabytes', 'share_networks']
@ -147,7 +146,7 @@ def _quota_show(quotas):
quota_dict = {}
for resource in _quota_resources:
quota_dict[resource] = getattr(quotas, resource, None)
utils.print_dict(quota_dict)
cliutils.print_dict(quota_dict)
def _quota_update(manager, identifier, args):
@ -320,7 +319,7 @@ def do_absolute_limits(cs, args):
"""Print a list of absolute limits for a user."""
limits = cs.limits.get().absolute
columns = ['Name', 'Value']
utils.print_list(limits, columns)
cliutils.print_list(limits, columns)
@cliutils.service_type('share')
@ -328,7 +327,7 @@ def do_rate_limits(cs, args):
"""Print a list of rate limits for a user."""
limits = cs.limits.get().rate
columns = ['Verb', 'URI', 'Value', 'Remain', 'Unit', 'Next_Available']
utils.print_list(limits, columns)
cliutils.print_list(limits, columns)
@cliutils.arg(
@ -428,7 +427,7 @@ def do_metadata_show(cs, args):
"""Show metadata of given share."""
share = _find_share(cs, args.share)
metadata = cs.shares.get_metadata(share)._info
utils.print_dict(metadata, 'Metadata-property')
cliutils.print_dict(metadata, 'Metadata-property')
@cliutils.arg(
@ -447,7 +446,7 @@ def do_metadata_update_all(cs, args):
share = _find_share(cs, args.share)
metadata = _extract_metadata(args)
metadata = share.update_all_metadata(metadata)._info['metadata']
utils.print_dict(metadata, 'Metadata-property')
cliutils.print_dict(metadata, 'Metadata-property')
@cliutils.arg(
@ -522,7 +521,7 @@ def do_access_allow(cs, args):
"""Allow access to the share."""
share = _find_share(cs, args.share)
access = share.allow(args.access_type, args.access_to)
utils.print_dict(access)
cliutils.print_dict(access)
@cliutils.arg(
@ -549,7 +548,8 @@ def do_access_list(cs, args):
"""Show access list for share."""
share = _find_share(cs, args.share)
access_list = share.access_list()
utils.print_list(access_list, ['id', 'access type', 'access to', 'state'])
cliutils.print_list(access_list,
['id', 'access type', 'access to', 'state'])
@cliutils.arg(
@ -704,7 +704,7 @@ def do_list(cs, args):
sort_key=args.sort_key,
sort_dir=args.sort_dir,
)
utils.print_list(shares, list_of_keys)
cliutils.print_list(shares, list_of_keys)
@cliutils.arg(
@ -799,7 +799,7 @@ def do_snapshot_list(cs, args):
sort_key=args.sort_key,
sort_dir=args.sort_dir,
)
utils.print_list(snapshots, list_of_keys)
cliutils.print_list(snapshots, list_of_keys)
@cliutils.arg(
@ -991,7 +991,7 @@ def do_share_network_create(cs, args):
'description': args.description}
share_network = cs.share_networks.create(**values)
info = share_network._info.copy()
utils.print_dict(info)
cliutils.print_dict(info)
@cliutils.arg(
@ -1028,7 +1028,7 @@ def do_share_network_update(cs, args):
share_network = _find_share_network(
cs, args.share_network).update(**values)
info = share_network._info.copy()
utils.print_dict(info)
cliutils.print_dict(info)
@cliutils.arg(
@ -1039,7 +1039,7 @@ def do_share_network_show(cs, args):
"""Get a description for network used by the tenant."""
share_network = _find_share_network(cs, args.share_network)
info = share_network._info.copy()
utils.print_dict(info)
cliutils.print_dict(info)
@cliutils.arg(
@ -1164,7 +1164,7 @@ def do_share_network_list(cs, args):
cs, args.security_service).id
share_networks = cs.share_networks.list(search_opts=search_opts)
fields = ['id', 'name']
utils.print_list(share_networks, fields=fields)
cliutils.print_list(share_networks, fields=fields)
@cliutils.arg(
@ -1209,7 +1209,7 @@ def do_share_network_security_service_list(cs, args):
}
security_services = cs.security_services.list(search_opts=search_opts)
fields = ['id', 'name', 'status', 'type', ]
utils.print_list(security_services, fields=fields)
cliutils.print_list(security_services, fields=fields)
@cliutils.arg(
@ -1273,7 +1273,7 @@ def do_security_service_create(cs, args):
}
security_service = cs.security_services.create(args.type, **values)
info = security_service._info.copy()
utils.print_dict(info)
cliutils.print_dict(info)
@cliutils.arg(
@ -1328,7 +1328,7 @@ def do_security_service_update(cs, args):
}
security_service = _find_security_service(
cs, args.security_service).update(**values)
utils.print_dict(security_service._info)
cliutils.print_dict(security_service._info)
@cliutils.arg(
@ -1339,7 +1339,7 @@ def do_security_service_show(cs, args):
"""Show security service."""
security_service = _find_security_service(cs, args.security_service)
info = security_service._info.copy()
utils.print_dict(info)
cliutils.print_dict(info)
@cliutils.arg(
@ -1437,7 +1437,7 @@ def do_security_service_list(cs, args):
fields = ['id', 'name', 'status', 'type', ]
if args.detailed:
fields.append('share_networks')
utils.print_list(security_services, fields=fields)
cliutils.print_list(security_services, fields=fields)
@cliutils.arg(
@ -1487,7 +1487,7 @@ def do_share_server_list(cs, args):
"Updated_at",
]
share_servers = cs.share_servers.list(search_opts=search_opts)
utils.print_list(share_servers, fields=fields)
cliutils.print_list(share_servers, fields=fields)
@cliutils.arg(
@ -1502,7 +1502,7 @@ def do_share_server_show(cs, args):
# so remove big dict from view.
if "backend_details" in share_server._info:
del share_server._info["backend_details"]
utils.print_dict(share_server._info)
cliutils.print_dict(share_server._info)
@cliutils.arg(
@ -1513,7 +1513,7 @@ def do_share_server_show(cs, args):
def do_share_server_details(cs, args):
"""Show share server details."""
details = cs.share_servers.details(args.id)
utils.print_dict(details._info)
cliutils.print_dict(details._info)
@cliutils.arg(
@ -1562,7 +1562,7 @@ def do_service_list(cs, args):
}
fields = ["Id", "Binary", "Host", "Zone", "Status", "State", "Updated_at"]
services = cs.services.list(search_opts=search_opts)
utils.print_list(services, fields=fields)
cliutils.print_list(services, fields=fields)
def _print_type_extra_specs(vol_type):
@ -1573,12 +1573,12 @@ def _print_type_extra_specs(vol_type):
def _print_volume_type_list(vtypes):
utils.print_list(vtypes, ['ID', 'Name'])
cliutils.print_list(vtypes, ['ID', 'Name'])
def _print_type_and_extra_specs_list(vtypes):
formatters = {'extra_specs': _print_type_extra_specs}
utils.print_list(vtypes, ['ID', 'Name', 'extra_specs'], formatters)
cliutils.print_list(vtypes, ['ID', 'Name', 'extra_specs'], formatters)
def _find_volume_type(cs, vtype):