Commit 020c07de authored by cmart's avatar cmart

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
parent fb02a3a3
......@@ -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):
......
......@@ -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
......@@ -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
......@@ -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="<host_column>",
help='column name used to sort result',
default='hypervisor_hostname'
)
return parser
class ShowHost(command.ShowCommand):
resource = 'host'
......
......@@ -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="<lease_column>",
help='column name used to sort result',
default='name'
)
return parser
class ShowLease(command.ShowCommand):
resource = 'lease'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment