From 054f3571b35fc190b19a22aa959fc998ec072ce0 Mon Sep 17 00:00:00 2001 From: Grant Gainey Date: Sat, 13 Nov 2021 16:57:10 -0500 Subject: [PATCH] Add support for the "tasks purge" command. closes #430 Required PR: https://github.com/pulp/pulpcore/pull/1721 --- CHANGES/430.feature | 1 + pulpcore/cli/core/context.py | 12 ++++++++++ pulpcore/cli/core/task.py | 34 ++++++++++++++++++++++++++++- tests/scripts/pulpcore/test_task.sh | 11 ++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 CHANGES/430.feature diff --git a/CHANGES/430.feature b/CHANGES/430.feature new file mode 100644 index 000000000..05c943667 --- /dev/null +++ b/CHANGES/430.feature @@ -0,0 +1 @@ +Added support for "tasks purge". diff --git a/pulpcore/cli/core/context.py b/pulpcore/cli/core/context.py index 2d79521f8..ae6abdf12 100644 --- a/pulpcore/cli/core/context.py +++ b/pulpcore/cli/core/context.py @@ -278,6 +278,7 @@ class PulpTaskContext(PulpEntityContext): READ_ID = "tasks_read" DELETE_ID = "tasks_delete" CANCEL_ID: ClassVar[str] = "tasks_cancel" + PURGE_ID = "tasks_purge" resource_context: Optional[PulpEntityContext] = None @@ -295,6 +296,17 @@ def scope(self) -> Dict[str, Any]: else: return {} + def purge(self, finished_before: Optional[str], states: Optional[List[str]]) -> Any: + body: Dict[str, Any] = {} + if finished_before: + body["finished_before"] = finished_before + if states: + body["states"] = states + return self.pulp_ctx.call( + self.PURGE_ID, + body=body, + ) + class PulpTaskGroupContext(PulpEntityContext): ENTITY = _("task group") diff --git a/pulpcore/cli/core/task.py b/pulpcore/cli/core/task.py index 1ec15aec8..f03a8c347 100644 --- a/pulpcore/cli/core/task.py +++ b/pulpcore/cli/core/task.py @@ -1,11 +1,13 @@ import gettext import re from contextlib import suppress -from typing import Optional +from datetime import datetime +from typing import Optional, Tuple import click from pulpcore.cli.common.context import ( + PluginRequirement, PulpContext, PulpEntityContext, pass_entity_context, @@ -112,3 +114,33 @@ def cancel( if not re.match("Task /pulp/api/v3/tasks/[-0-9a-f]*/ canceled", str(e)): raise e click.echo(_("Done."), err=True) + + +DATETIME_FORMATS = ["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S"] + + +@task.command() +@click.option( + "--finished-before", + "finished", + help=_("Purge task-records whose 'finished' time is **before** the time specified."), + type=click.DateTime(formats=DATETIME_FORMATS), +) +@click.option( + "--state", + help=_("Only purge tasks in the specified state(s). Can be specified multiple times."), + type=click.Choice(["skipped", "completed", "failed", "canceled"]), + multiple=True, +) +@pass_entity_context +@pass_pulp_context +def purge( + pulp_ctx: PulpContext, + task_ctx: PulpTaskContext, + finished: Optional[datetime], + state: Optional[Tuple[str]], +) -> None: + pulp_ctx.needs_plugin(PluginRequirement("core", "3.17.0.dev")) + state_list = list(state) if state else None + finished_str = finished.strftime(DATETIME_FORMATS[1]) if finished else None + task_ctx.purge(finished_str, state_list) diff --git a/tests/scripts/pulpcore/test_task.sh b/tests/scripts/pulpcore/test_task.sh index a812f172b..8821fcfd8 100755 --- a/tests/scripts/pulpcore/test_task.sh +++ b/tests/scripts/pulpcore/test_task.sh @@ -51,3 +51,14 @@ expect_succ test "$(echo "$OUTPUT" | jq -r '.[0].state')" = "completed" expect_succ pulp task list --limit 1 expect_succ test "$(echo "$OUTPUT" | jq -r length)" -eq 1 + +# Test purging tasks +if pulp debug has-plugin --name core --min-version 3.17.0.dev +then + expect_succ pulp task purge + expect_succ pulp task purge --finished-before "2021-12-01" --state "failed" + expect_succ pulp task purge --finished-before "2021-12-01T12:00:00" --state "completed" --state "failed" + expect_fail pulp task purge --finished-before "NOT A DATE" + expect_fail pulp task purge --finished-before "2021-12-01T12:00:00" --state "NOT A STATE" + expect_fail pulp task purge --finished-before "2021-12-01T12:00:00" --state "running" +fi