Skip to content

Instantly share code, notes, and snippets.

@RyanKor
Created October 15, 2021 11:12
Show Gist options
  • Save RyanKor/2442f13bfce84e92837ed7ee3f60ec56 to your computer and use it in GitHub Desktop.
Save RyanKor/2442f13bfce84e92837ed7ee3f60ec56 to your computer and use it in GitHub Desktop.
class TaskImage(command.Lister):
_description = _("Retrieve a listing of Task objects.")
def get_parser(self, prog_name):
parser = super(TaskImage, self).get_parser(prog_name)
parser.add_argument(
'--sort-key',
metavar="<key>[:<field>]",
help=_("Sorts the response by one of the following attributes: "
"created_at, expires_at, id, status, type, updated_at. "
"Default is created_at. "
"multiple keys and directions can be "
"specified separated by comma"),
)
parser.add_argument(
'--sort-dir',
metavar="<key>[:<direction>]",
help=_("Sort output by selected keys and directions(asc or desc) "
"(default: name:desc), multiple keys and directions can be "
"specified separated by comma"),
)
parser.add_argument(
"--page-size",
metavar="<size>",
help=argparse.SUPPRESS,
)
parser.add_argument(
'--type',
metavar='<type>',
choices=[
'import'
],
help=_("Filters the response by a task type. "
"A valid value is import. "
),
)
parser.add_argument(
'--status',
metavar='<status>',
choices=[
"pending", "processing", "success", "failure"
],
default=None,
help=_("Filter tasks based on status.")
)
return parser
def take_action(self, parsed_args):
image_client = self.app.client_manager.image
columns = (
"id",
"type",
"status",
"owner_id"
)
column_headers = (
"ID",
"Type",
"Status",
"Owner"
)
kwargs = {}
copy_attrs = ("sort_key", "sort_dir", "page_size", "type", "status")
for attr in copy_attrs:
if attr in parsed_args:
val = getattr(parsed_args, attr, None)
if val is not None:
# Only include a value in kwargs for attributes that are
# actually present on the command line
kwargs[attr] = val
data = image_client.tasks(**kwargs)
return (
column_headers,
(utils.get_item_properties(
s,
columns,
formatters=_formatters,
) for s in data)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment