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 32ab3fda6..4814081d7 100644 --- a/pulpcore/cli/core/context.py +++ b/pulpcore/cli/core/context.py @@ -2,7 +2,7 @@ import hashlib import os import sys -from typing import IO, Any, ClassVar, Dict, List, Optional +from typing import IO, Any, ClassVar, Dict, List, Optional, Union import click @@ -245,6 +245,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 @@ -262,6 +263,17 @@ def scope(self) -> Dict[str, Any]: else: return {} + def purge(self, finished_before: Optional[str], states: Optional[List[str]]) -> Any: + body: Dict[str, Union[str, List[str]]] = {} + 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..63dccc989 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 List, Optional 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( + "--states", + help=_("Only purge tasks in the specified state(s)."), + 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], + states: Optional[List[str]], +) -> None: + pulp_ctx.needs_plugin(PluginRequirement("core", "3.17.0.dev")) + state_list = list(states) if states 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..3ea0d950c 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" --states "failed" + expect_succ pulp task purge --finished-before "2021-12-01T12:00:00" --states "completed" --states "failed" + expect_fail pulp task purge --finished-before "NOT A DATE" + expect_fail pulp task purge --finished-before "2021-12-01T12:00:00" --states "NOT A STATE" + expect_fail pulp task purge --finished-before "2021-12-01T12:00:00" --states "running" +fi