Skip to content

Commit

Permalink
Add filter parameters to task list
Browse files Browse the repository at this point in the history
fixes pulp#543
  • Loading branch information
mdellweg committed Sep 19, 2022
1 parent 8aaa087 commit 421e9ca
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/543.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added task filtering options.
1 change: 1 addition & 0 deletions pulpcore/cli/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,4 @@ class PulpWorkerContext(PulpEntityContext):
ENTITIES = _("workers")
HREF = "worker_href"
ID_PREFIX = "workers"
HREF_PATTERN = r"workers/"
73 changes: 71 additions & 2 deletions pulpcore/cli/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
pass_entity_context,
pass_pulp_context,
)
from pulpcore.cli.common.generic import list_command, pulp_group, pulp_option
from pulpcore.cli.common.generic import list_command, pulp_group, pulp_option, resource_option
from pulpcore.cli.common.i18n import get_translation
from pulpcore.cli.core.context import PulpTaskContext
from pulpcore.cli.core.context import PulpTaskContext, PulpWorkerContext

translation = get_translation(__name__)
_ = translation.gettext
Expand All @@ -21,6 +21,29 @@
##############################################################################
# Generic reusable commands

DATETIME_FORMATS = ["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S"]


def _task_filter_callback(
ctx: click.Context, param: click.Parameter, value: Optional[str]
) -> Optional[str]:
if value is not None:
pulp_ctx = ctx.find_object(PulpContext)
assert pulp_ctx is not None

# Example: "/pulp/api/v3/tasks/a69230d2-506e-44c7-9c46-e64a905f85e7/"
match = re.match(
rf"({pulp_ctx.api_path}tasks/)?"
r"([0-9a-f]{8})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{12})/?",
value,
)
if match:
value = "{}tasks/{}-{}-{}-{}-{}/".format(pulp_ctx.api_path, *match.group(2, 3, 4, 5, 6))
else:
raise click.ClickException(_("Either an href or a UUID must be provided."))

return value


def _task_group_filter_callback(
ctx: click.Context, param: click.Parameter, value: Optional[str]
Expand Down Expand Up @@ -71,6 +94,52 @@ def _task_group_filter_callback(
help=_("List only tasks in this task group. Provide pulp_href or UUID."),
callback=_task_group_filter_callback,
),
click.option(
"--parent-task",
help=_("Parent task by uuid or href."),
callback=_task_filter_callback,
),
resource_option(
"--worker",
default_plugin="core",
default_type="none",
context_table={"core:none": PulpWorkerContext},
href_pattern=PulpWorkerContext.HREF_PATTERN,
help=_("Worker used to execute the task by name or href."),
),
click.option(
"--created-resource",
"created_resources",
help=_("Href of a resource created in the task."),
),
click.option(
"--reserved-resources-record",
help=_("Reserved resource to execute the task."),
),
click.option(
"--started-after",
"started_at__gte",
help=_("Filter for tasks started at or after this ISO 8601 date"),
type=click.DateTime(formats=DATETIME_FORMATS),
),
click.option(
"--started-before",
"started_at__lte",
help=_("Filter for tasks started at or before this ISO 8601 date"),
type=click.DateTime(formats=DATETIME_FORMATS),
),
click.option(
"--finished-after",
"finished_at__gte",
help=_("Filter for tasks finished at or after this ISO 8601 date"),
type=click.DateTime(formats=DATETIME_FORMATS),
),
click.option(
"--finished-before",
"finished_at__lte",
help=_("Filter for tasks finished at or before this ISO 8601 date"),
type=click.DateTime(formats=DATETIME_FORMATS),
),
]


Expand Down
4 changes: 1 addition & 3 deletions pulpcore/cli/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
)
from pulpcore.cli.common.i18n import get_translation
from pulpcore.cli.core.context import PulpTaskContext
from pulpcore.cli.core.generic import task_filter
from pulpcore.cli.core.generic import DATETIME_FORMATS, task_filter

translation = get_translation(__name__)
_ = translation.gettext

DATETIME_FORMATS = ["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S"]


def _uuid_callback(
ctx: click.Context, param: click.Parameter, value: Optional[str]
Expand Down
10 changes: 10 additions & 0 deletions tests/scripts/pulpcore/test_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ trap cleanup EXIT
sync_task="pulp_file.app.tasks.synchronizing.synchronize"
expect_succ pulp task list --name $sync_task --state canceled
count="$(echo "$OUTPUT" | jq -r length)"
expect_succ pulp worker list --limit 1
worker="$(echo "$OUTPUT" | jq -r '.[0].pulp_href')"
worker_name="$(echo "$OUTPUT" | jq -r '.[0].name')"

expect_succ pulp file remote create --name "cli_test_file_remote" \
--url "$FILE_REMOTE_URL"
Expand All @@ -41,9 +44,16 @@ task=$(echo "$ERROUTPUT" | grep -E -o "${PULP_API_ROOT}api/v3/tasks/[-[:xdigit:]
task_uuid="${task%/}"
task_uuid="${task_uuid##*/}"
expect_succ pulp task show --wait --uuid "$task_uuid"
reserved_resource="$(echo "$OUTPUT" | jq -r '.reserved_resources_record[0]')"
created_resource="$(echo "$OUTPUT" | jq -r '.created_resources[0]')"
expect_succ test "$(echo "$OUTPUT" | jq -r '.state')" = "completed"

expect_succ pulp task list --name-contains file
expect_succ pulp task list --parent-task "$task" --worker "$worker"
expect_succ pulp task list --parent-task "$task_uuid" --worker "$worker_name"
expect_succ pulp task list --started-before "2021-12-01" --started-after "2022-06-01"
expect_succ pulp task list --finished-before "2021-12-01" --finished-after "2022-06-01"
expect_fail pulp task list --reserved-resource "$reserved_resource" --created-resource "$created_resource"

expect_fail pulp task list --state=cannotwork
expect_succ pulp task list --state=COmPLetED
Expand Down

0 comments on commit 421e9ca

Please sign in to comment.