Skip to content

Commit

Permalink
Add support for the "tasks purge" command.
Browse files Browse the repository at this point in the history
closes pulp#430
Required PR: pulp/pulpcore#1721
  • Loading branch information
ggainey committed Dec 9, 2021
1 parent b0c5ef9 commit f322a53
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/430.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for "tasks purge".
12 changes: 12 additions & 0 deletions pulpcore/cli/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down
34 changes: 33 additions & 1 deletion pulpcore/cli/core/task.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
11 changes: 11 additions & 0 deletions tests/scripts/pulpcore/test_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f322a53

Please sign in to comment.