From 020c07de184737bdc2aa359301654d540cfb14e4 Mon Sep 17 00:00:00 2001 From: cmart Date: Tue, 15 Apr 2014 20:27:15 +0000 Subject: [PATCH] Add sort-by option for climateclient list commands Adding a sort-by parameter for the climateclient list command so that when a list is retrieved, it will be order by the column that the user prefered. Default value will be 'name' for leases and 'hypervisor_hostname' for hosts (which are more meaningful order attributes than id). Change-Id: Ie6a4df384770123ec5fc4cf43823ce5ef1a47ca5 Closes-Bug: #1304595 --- climateclient/command.py | 14 +++++++++++++- climateclient/v1/hosts.py | 7 +++++-- climateclient/v1/leases.py | 7 +++++-- climateclient/v1/shell_commands/hosts.py | 9 +++++++++ climateclient/v1/shell_commands/leases.py | 9 +++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/climateclient/command.py b/climateclient/command.py index 11a00c1..8820903 100644 --- a/climateclient/command.py +++ b/climateclient/command.py @@ -23,6 +23,7 @@ from cliff.formatters import table from cliff import lister from cliff import show +from climateclient import exception from climateclient import utils @@ -217,6 +218,16 @@ class ListCommand(ClimateCommand, lister.Lister): list_columns = [] unknown_parts_flag = True + def args2body(self, parsed_args): + params = {} + if parsed_args.sort_by: + if parsed_args.sort_by in self.list_columns: + params['sort_by'] = parsed_args.sort_by + else: + msg = 'Invalid sort option %s' % parsed_args.sort_by + raise exception.ClimateClientException(msg) + return params + def get_parser(self, prog_name): parser = super(ListCommand, self).get_parser(prog_name) return parser @@ -224,8 +235,9 @@ class ListCommand(ClimateCommand, lister.Lister): def retrieve_list(self, parsed_args): """Retrieve a list of resources from Climate server""" climate_client = self.get_client() + body = self.args2body(parsed_args) resource_manager = getattr(climate_client, self.resource) - data = resource_manager.list() + data = resource_manager.list(**body) return data def setup_columns(self, info, parsed_args): diff --git a/climateclient/v1/hosts.py b/climateclient/v1/hosts.py index bf8660d..083cd96 100644 --- a/climateclient/v1/hosts.py +++ b/climateclient/v1/hosts.py @@ -42,6 +42,9 @@ class ComputeHostClientManager(base.BaseClientManager): """Deletes host with specified ID.""" self._delete('/os-hosts/%s' % host_id) - def list(self): + def list(self, sort_by=None): """List all hosts.""" - return self._get('/os-hosts', 'hosts') + hosts = self._get('/os-hosts', 'hosts') + if sort_by: + hosts = sorted(hosts, key=lambda l: l[sort_by]) + return hosts diff --git a/climateclient/v1/leases.py b/climateclient/v1/leases.py index a509a19..5fc17b6 100644 --- a/climateclient/v1/leases.py +++ b/climateclient/v1/leases.py @@ -72,6 +72,9 @@ class LeaseClientManager(base.BaseClientManager): """Deletes lease with specified ID.""" self._delete('/leases/%s' % lease_id) - def list(self): + def list(self, sort_by=None): """List all leases.""" - return self._get('/leases', 'leases') + leases = self._get('/leases', 'leases') + if sort_by: + leases = sorted(leases, key=lambda l: l[sort_by]) + return leases diff --git a/climateclient/v1/shell_commands/hosts.py b/climateclient/v1/shell_commands/hosts.py index bf17b62..22867d8 100644 --- a/climateclient/v1/shell_commands/hosts.py +++ b/climateclient/v1/shell_commands/hosts.py @@ -24,6 +24,15 @@ class ListHosts(command.ListCommand): list_columns = ['id', 'hypervisor_hostname', 'vcpus', 'memory_mb', 'local_gb'] + def get_parser(self, prog_name): + parser = super(ListHosts, self).get_parser(prog_name) + parser.add_argument( + '--sort-by', metavar="", + help='column name used to sort result', + default='hypervisor_hostname' + ) + return parser + class ShowHost(command.ShowCommand): resource = 'host' diff --git a/climateclient/v1/shell_commands/leases.py b/climateclient/v1/shell_commands/leases.py index d82a246..de35c32 100644 --- a/climateclient/v1/shell_commands/leases.py +++ b/climateclient/v1/shell_commands/leases.py @@ -27,6 +27,15 @@ class ListLeases(command.ListCommand): log = logging.getLogger(__name__ + '.ListLeases') list_columns = ['id', 'name', 'start_date', 'end_date'] + def get_parser(self, prog_name): + parser = super(ListLeases, self).get_parser(prog_name) + parser.add_argument( + '--sort-by', metavar="", + help='column name used to sort result', + default='name' + ) + return parser + class ShowLease(command.ShowCommand): resource = 'lease'