From 421e9ca3376f7fd643ba2af08b38ce4a8357f2dd Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Mon, 19 Sep 2022 14:04:09 +0200 Subject: [PATCH] Add filter parameters to task list fixes #543 --- CHANGES/543.feature | 1 + pulpcore/cli/core/context.py | 1 + pulpcore/cli/core/generic.py | 73 ++++++++++++++++++++++++++++- pulpcore/cli/core/task.py | 4 +- tests/scripts/pulpcore/test_task.sh | 10 ++++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 CHANGES/543.feature diff --git a/CHANGES/543.feature b/CHANGES/543.feature new file mode 100644 index 000000000..539f1d141 --- /dev/null +++ b/CHANGES/543.feature @@ -0,0 +1 @@ +Added task filtering options. diff --git a/pulpcore/cli/core/context.py b/pulpcore/cli/core/context.py index c2928aeed..1c254fc8a 100644 --- a/pulpcore/cli/core/context.py +++ b/pulpcore/cli/core/context.py @@ -455,3 +455,4 @@ class PulpWorkerContext(PulpEntityContext): ENTITIES = _("workers") HREF = "worker_href" ID_PREFIX = "workers" + HREF_PATTERN = r"workers/" diff --git a/pulpcore/cli/core/generic.py b/pulpcore/cli/core/generic.py index 57cc5943c..b31fec0f7 100644 --- a/pulpcore/cli/core/generic.py +++ b/pulpcore/cli/core/generic.py @@ -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 @@ -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] @@ -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), + ), ] diff --git a/pulpcore/cli/core/task.py b/pulpcore/cli/core/task.py index c0f253f5f..090b92aa8 100644 --- a/pulpcore/cli/core/task.py +++ b/pulpcore/cli/core/task.py @@ -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] diff --git a/tests/scripts/pulpcore/test_task.sh b/tests/scripts/pulpcore/test_task.sh index 0c5a48c46..fc2afb105 100755 --- a/tests/scripts/pulpcore/test_task.sh +++ b/tests/scripts/pulpcore/test_task.sh @@ -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" @@ -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