Skip to content

Commit 29dbddd

Browse files
Add sort key and direction parameters
1 parent 961691e commit 29dbddd

4 files changed

Lines changed: 50 additions & 7 deletions

File tree

coriolisclient/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def __init__(self, client):
167167

168168
@wrap_unauthorized_exception
169169
def _list(self, url, response_key=None, obj_class=None, json=None,
170-
values_key='values', query: dict | None=None):
170+
values_key='values', query: dict | list | None=None):
171171
"""List the collection.
172172
:param url: a partial URL, e.g., '/servers'
173173
:param response_key: the key to be looked up in response dictionary,
@@ -177,7 +177,8 @@ def _list(self, url, response_key=None, obj_class=None, json=None,
177177
(self.resource_class will be used by default)
178178
:param json: data that will be encoded as JSON and passed in POST
179179
request (GET will be sent by default)
180-
:param query: an optional dict containing query filters
180+
:param query: an optional dict or list of (key, value) tuples
181+
containing query filters
181182
"""
182183

183184
if query:

coriolisclient/cli/transfer_executions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from cliff import show
2323

2424
from coriolisclient.cli import formatter
25+
from coriolisclient.cli import utils as cli_utils
2526

2627

2728
class TransferExecutionFormatter(formatter.EntityFormatter):
@@ -186,9 +187,19 @@ def get_parser(self, prog_name):
186187
parser.add_argument(
187188
'--limit', type=int,
188189
help='Maximum number of executions to retrieve.')
190+
parser.add_argument(
191+
'--sort',
192+
help='Comma-separated list of sort keys and directions in the form '
193+
'of <key>[:<asc|desc>]. The direction defaults to descending if '
194+
'not specified.')
189195
return parser
190196

191197
def take_action(self, args):
198+
sort_keys, sort_dirs = cli_utils.parse_sort_args(arg.sort)
192199
obj_list = self.app.client_manager.coriolis.transfer_executions.list(
193-
args.transfer)
200+
args.transfer,
201+
marker=args.marker,
202+
limit=args.limit,
203+
sort_keys=sort_keys,
204+
sort_dirs=sort_dirs)
194205
return TransferExecutionFormatter().list_objects(obj_list)

coriolisclient/cli/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import uuid
2121

2222
from coriolisclient import constants
23+
from coriolisclient import exceptions
2324

2425

2526
def add_storage_mappings_arguments_to_parser(parser):
@@ -264,3 +265,25 @@ def _split_pool_mapping_arg(arg):
264265
'same OS type and which are compatible with OSMorphing '
265266
'the guest OS of each afferent instance. The mappings must '
266267
'be of the form "INSTANCE_IDENTIFIER=MINION_POOL_ID".')
268+
269+
270+
def parse_sort_arg(sort: str) -> tuple[list, list]:
271+
"""Parse sort CLI argument.
272+
273+
:param sort: Comma-separated list of sort keys and directions in the form
274+
of <key>[:<asc|desc>]. The direction defaults to descending if
275+
not specified.
276+
:returns: (sort_keys, sort_dirs)
277+
"""
278+
sort_keys = []
279+
sort_dirs = []
280+
for sort_entry in sort.split(','):
281+
sort_key, _sep, sort_dir = sort_entry.partition(':')
282+
if not sort_dir:
283+
sort_dir = 'desc'
284+
elif sort_dir not in ('asc', 'desc'):
285+
raise exceptions.CoriolisException(
286+
'Unknown sort direction: %s' % sort_dir)
287+
sort_keys.append(sort_key)
288+
sort_dirs.append(sort_dir)
289+
return sort_keys, sort_dirs

coriolisclient/v1/transfer_executions.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,20 @@ class TransferExecutionManager(base.BaseManager):
3434
def __init__(self, api):
3535
super(TransferExecutionManager, self).__init__(api)
3636

37-
def list(self, transfer, marker=None, limit=None):
38-
query = {}
37+
def list(self, transfer, marker=None, limit=None,
38+
sort_keys=None, sort_dirs=None):
39+
# List of key-value tuples.
40+
query = []
3941
if marker is not None:
40-
query['marker'] = marker
42+
query.append(("marker", marker))
4143
if limit is not None:
42-
query['limit'] = limit
44+
query.append(("limit", limit))
45+
if sort_keys is not None:
46+
query.extend(('sort_key', sort_key)
47+
for sort_key in sort_keys)
48+
if sort_dirs is not None:
49+
query.extend(('sort_dir', sort_dir)
50+
for sort_dir in sort_dirs)
4351

4452
return self._list(
4553
'/transfers/%s/executions' % base.getid(transfer), 'executions',

0 commit comments

Comments
 (0)