Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support --order and --order_by for list operations #506

Merged
21 changes: 20 additions & 1 deletion linodecli/api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ def _build_filter_header(
if p.name in parsed_args_dict:
del parsed_args_dict[p.name]

# check for order_by and order
order_by, order = None, None
if parsed_args_dict["order_by"] is not None:
order_by = parsed_args_dict["order_by"]
order = (
"asc"
if parsed_args_dict["order"] is None
else parsed_args_dict["order"]
)

del parsed_args_dict["order_by"]
del parsed_args_dict["order"]

amisiorek-akamai marked this conversation as resolved.
Show resolved Hide resolved
# The "+and" list to be used in the filter header
filter_list = []

Expand All @@ -162,7 +175,13 @@ def _build_filter_header(
filter_list.extend(new_filters)

if len(filter_list) > 0:
return json.dumps({"+and": filter_list})
if order_by is None:
return json.dumps({"+and": filter_list})
return json.dumps(
{"+and": filter_list, "+order_by": order_by, "+order": order}
)
if order_by is not None:
return json.dumps({"+order_by": order_by, "+order": order})

return None
amisiorek-akamai marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
18 changes: 18 additions & 0 deletions linodecli/baked/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import platform
import re
import sys
from getpass import getpass
from os import environ, path

Expand Down Expand Up @@ -315,8 +316,10 @@ def parse_args(

if self.method == "get":
# build args for filtering
filterable_args = []
for attr in self.response_model.attrs:
if attr.filterable:
filterable_args.append(attr.name)
expected_type = TYPES[attr.datatype]
if expected_type == list:
parser.add_argument(
Expand All @@ -333,6 +336,21 @@ def parse_args(
metavar=attr.name,
)

# Add --order_by and --order argument
parser.add_argument(
"--order_by",
choices=filterable_args,
help="Attribute to order the results by - must be filterable.",
required="--order" in sys.argv,
)

parser.add_argument(
"--order",
choices=["asc", "desc"],
default="asc",
help="Either “asc” or “desc”. Defaults to “asc”. Requires +order_by",
)

elif self.method in ("post", "put"):
# build args for body JSON
for arg in self.args:
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def test_build_filter_header(self, list_operation):
SimpleNamespace(
filterable_result="bar",
filterable_list_result=["foo", "bar"],
order_by=None,
order=None,
amisiorek-akamai marked this conversation as resolved.
Show resolved Hide resolved
),
)

Expand Down