Commit 2cad39a4 authored by Jenkins's avatar Jenkins Committed by Gerrit Code Review

Merge "Add sort-by option for climateclient list commands"

parents 6f7f443a 020c07de
......@@ -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
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',
def get_parser(self, prog_name):
parser = super(ListHosts, self).get_parser(prog_name)
'--sort-by', metavar="<host_column>",
help='column name used to sort result',
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)
'--sort-by', metavar="<lease_column>",
help='column name used to sort result',
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