Skip to content

Commit 725e1ba

Browse files
Merge pull request #1459 from caberos/issue1453
Add an --orderBy parameters to call-api
2 parents 0a84e45 + d53dc43 commit 725e1ba

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

SoftLayer/CLI/call_api.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,20 @@ def _validate_parameters(ctx, param, value): # pylint: disable=unused-argument
112112
@click.option('--mask', help="String-based object mask")
113113
@click.option('--limit', type=click.INT, help="Result limit")
114114
@click.option('--offset', type=click.INT, help="Result offset")
115+
@click.option('--orderBy', type=click.STRING,
116+
help="To set the sort direction, ASC or DESC can be provided."
117+
"This should be of the form: '--orderBy nested.property' default DESC or "
118+
"'--orderBy nested.property=ASC', e.g. "
119+
" --orderBy subnets.id default DESC"
120+
" --orderBy subnets.id=ASC")
115121
@click.option('--output-python / --no-output-python',
116122
help="Show python example code instead of executing the call")
117123
@click.option('--json-filter', callback=_validate_filter,
118124
help="A JSON string to be passed in as the object filter to the API call. "
119125
"Remember to use double quotes (\") for variable names. Can NOT be used with --filter. "
120126
"Dont use whitespace outside of strings, or the slcli might have trouble parsing it.")
121127
@environment.pass_env
122-
def cli(env, service, method, parameters, _id, _filters, mask, limit, offset,
128+
def cli(env, service, method, parameters, _id, _filters, mask, limit, offset, orderby=None,
123129
output_python=False, json_filter=None):
124130
"""Call arbitrary API endpoints with the given SERVICE and METHOD.
125131
@@ -141,14 +147,19 @@ def cli(env, service, method, parameters, _id, _filters, mask, limit, offset,
141147
--json-filter '{"virtualGuests":{"hostname":{"operation":"^= test"}}}' --limit=10
142148
slcli -v call-api SoftLayer_User_Customer addBulkPortalPermission --id=1234567 \\
143149
'[{"keyName": "NETWORK_MESSAGE_DELIVERY_MANAGE"}]'
150+
slcli call-api Account getVirtualGuests \\
151+
--orderBy virttualguests.id=ASC
144152
"""
145153

146154
if _filters and json_filter:
147155
raise exceptions.CLIAbort("--filter and --json-filter cannot be used together.")
148156

149157
object_filter = _build_filters(_filters)
158+
if orderby:
159+
orderby = utils.build_filter_orderby(orderby)
160+
object_filter = utils.dict_merge(object_filter, orderby)
150161
if json_filter:
151-
object_filter.update(json_filter)
162+
object_filter = utils.dict_merge(json_filter, object_filter)
152163

153164
args = [service, method] + list(parameters)
154165
kwargs = {

SoftLayer/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ def event_log_filter_less_than_date(date, utc):
218218
}
219219

220220

221+
def build_filter_orderby(orderby):
222+
"""Builds filters using the filter options passed into the CLI.
223+
224+
It only supports the orderBy option, the default value is DESC.
225+
"""
226+
_filters = {}
227+
reverse_filter = list(reversed(orderby.split('.')))
228+
for keyword in reverse_filter:
229+
_aux_filter = {}
230+
if '=' in keyword:
231+
_aux_filter[str(keyword).split('=')[0]] = query_filter_orderby(str(keyword).split('=')[1])
232+
_filters = _aux_filter
233+
elif keyword == list(reverse_filter)[0]:
234+
_aux_filter[keyword] = query_filter_orderby('DESC')
235+
else:
236+
_aux_filter[keyword] = _filters
237+
_filters = _aux_filter
238+
return _filters
239+
240+
221241
class IdentifierMixin(object):
222242
"""Mixin used to resolve ids from other names of objects.
223243

tests/CLI/modules/call_api_tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,21 @@ def test_json_filter(self):
298298
result = self.run_command(['call-api', 'Account', 'getObject', '--json-filter={"test":"something"}'])
299299
self.assert_no_fail(result)
300300
self.assert_called_with('SoftLayer_Account', 'getObject', filter={"test": "something"})
301+
302+
def test_call_api_orderBy(self):
303+
result = self.run_command(['call-api', 'Account', 'getVirtualGuests',
304+
'--orderBy', 'virtualGuests.id=DESC',
305+
'--mask=virtualGuests.typeId,maxCpu',
306+
'-f', 'virtualGuests.typeId=1'])
307+
self.assert_no_fail(result)
308+
self.assert_called_with('SoftLayer_Account', 'getVirtualGuests',
309+
filter={
310+
'virtualGuests':
311+
{'id':
312+
{
313+
'operation': 'orderBy',
314+
'options': [{
315+
'name': 'sort',
316+
'value': ['DESC']}]},
317+
'typeId': {'operation': 1}}
318+
})

0 commit comments

Comments
 (0)