Skip to content

Commit

Permalink
Add retained_versions option to repository commands.
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
David Davis committed Apr 26, 2021
1 parent 0158b85 commit e2060bc
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGES/210.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added retained_versions option to repository commands.
11 changes: 3 additions & 8 deletions pulpcore/cli/ansible/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
PulpAnsibleDistributionContext,
PulpAnsibleRepositoryContext,
)
from pulpcore.cli.common.context import (
EntityDefinition,
EntityFieldDefinition,
PulpContext,
pass_entity_context,
pass_pulp_context,
)
from pulpcore.cli.common.generic import PluginRequiredVersion as PRV
from pulpcore.cli.common.context import EntityDefinition, EntityFieldDefinition
from pulpcore.cli.common.context import PluginRequiredVersion as PRV
from pulpcore.cli.common.context import PulpContext, pass_entity_context, pass_pulp_context
from pulpcore.cli.common.generic import (
base_path_contains_option,
base_path_option,
Expand Down
3 changes: 3 additions & 0 deletions pulpcore/cli/ansible/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
label_select_option,
list_command,
name_option,
pulp_option,
show_command,
update_command,
version_command,
Expand Down Expand Up @@ -65,10 +66,12 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
click.option("--name", required=True),
click.option("--description"),
click.option("--remote", callback=_remote_callback, help=_("an optional remote")),
pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"),
]
update_options = [
click.option("--description"),
click.option("--remote", callback=_remote_callback, help=_("new optional remote")),
pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"),
]

repository.add_command(show_command(decorators=lookup_options))
Expand Down
32 changes: 23 additions & 9 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import sys
import time
from typing import IO, Any, ClassVar, Dict, List, Optional, Set, Tuple, Type, Union
from typing import IO, Any, ClassVar, Dict, List, NamedTuple, Optional, Set, Tuple, Type, Union

import click
import yaml
Expand Down Expand Up @@ -33,6 +33,14 @@
RepositoryDefinition = Tuple[str, str] # name, pulp_type
RepositoryVersionDefinition = Tuple[str, str, int] # name, pulp_type, version


class PluginRequiredVersion(NamedTuple):
name: str
min: Optional[str] = None
max: Optional[str] = None
feature: Optional[str] = _("this command")


new_component_names_to_pre_3_11_names: Dict[str, str] = dict(
ansible="pulp_ansible",
container="pulp_container",
Expand Down Expand Up @@ -74,7 +82,7 @@ class PulpContext:
def __init__(self, api_kwargs: Dict[str, Any], format: str, background_tasks: bool) -> None:
self._api: Optional[OpenAPI] = None
self._api_kwargs = api_kwargs
self._needed_plugins: List[Tuple[str, Optional[str], Optional[str]]] = []
self._needed_plugins: List[PluginRequiredVersion] = []
self.isatty: bool = sys.stdout.isatty()

self.format: str = format
Expand All @@ -90,8 +98,8 @@ def api(self) -> OpenAPI:
except OpenAPIError as e:
raise click.ClickException(str(e))
# Rerun scheduled version checks
for name, min_version, max_version in self._needed_plugins:
self.needs_plugin(name, min_version, max_version)
for plugin in self._needed_plugins:
self.needs_plugin(plugin.name, plugin.min, plugin.max, plugin.feature)
return self._api

@property
Expand Down Expand Up @@ -194,7 +202,11 @@ def has_plugin(
return True

def needs_plugin(
self, name: str, min_version: Optional[str] = None, max_version: Optional[str] = None
self,
name: str,
min_version: Optional[str] = None,
max_version: Optional[str] = None,
feature: Optional[str] = _("this command"),
) -> None:
if self._api is not None:
if not self.has_plugin(name, min_version, max_version):
Expand All @@ -208,12 +220,14 @@ def needs_plugin(
raise click.ClickException(
_(
"The server does not have '{specifier}' installed,"
" which is needed to run this command."
).format(specifier=specifier)
" which is needed to use {feature}."
).format(specifier=specifier, feature=feature)
)
else:
# Schedule for later checking
self._needed_plugins.append((name, min_version, max_version))
self._needed_plugins.append(
PluginRequiredVersion(name, min_version, max_version, feature)
)


class PulpEntityContext:
Expand Down Expand Up @@ -542,7 +556,7 @@ class PulpRepositoryContext(PulpEntityContext):
SYNC_ID: ClassVar[str]
MODIFY_ID: ClassVar[str]
VERSION_CONTEXT: ClassVar[Type[PulpRepositoryVersionContext]]
NULLABLES = {"description"}
NULLABLES = {"description", "retained_versions"}

def get_version_context(self) -> PulpRepositoryVersionContext:
return self.VERSION_CONTEXT(self.pulp_ctx, self)
Expand Down
62 changes: 41 additions & 21 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import gettext
import json
from typing import Any, NamedTuple, Optional, Tuple, Union
from typing import Any, Callable, Optional, Tuple, TypeVar, Union

import click

from pulpcore.cli.common.context import (
DEFAULT_LIMIT,
EntityDefinition,
PluginRequiredVersion,
PulpContext,
PulpEntityContext,
PulpRepositoryContext,
Expand All @@ -18,12 +19,7 @@
)

_ = gettext.gettext


class PluginRequiredVersion(NamedTuple):
name: str
min: Optional[str] = None
max: Optional[str] = None
_F = TypeVar("_F")


class PulpCommand(click.Command):
Expand All @@ -40,6 +36,32 @@ def format_help_text(


class PulpOption(click.Option):
def __init__(
self,
*args: Any,
needs_plugin: Optional[str] = None,
min_version: Optional[str] = None,
max_version: Optional[str] = None,
**kwargs: Any,
):
if min_version or max_version:
assert needs_plugin, "Must supply required_version if min or max_version supplied."
self.needs_plugin = needs_plugin
self.min_version = min_version
self.max_version = max_version
super().__init__(*args, **kwargs)

def process_value(self, ctx: click.Context, value: Any) -> Any:
if value is not None and self.needs_plugin:
pulp_ctx = ctx.find_object(PulpContext)
pulp_ctx.needs_plugin(
self.needs_plugin,
self.min_version,
self.max_version,
_("the {name} option").format(name=self.name),
)
return super().process_value(ctx, value)

def get_help_record(self, ctx: click.Context) -> Tuple[str, str]:
synopsis, help_text = super().get_help_record(ctx)
entity_ctx: PulpEntityContext = ctx.find_object(PulpEntityContext)
Expand Down Expand Up @@ -133,35 +155,36 @@ def load_json_callback(
# Decorator common options


limit_option = click.option(
def pulp_option(*args: Any, **kwargs: Any) -> Callable[[_F], _F]:
kwargs["cls"] = PulpOption
return click.option(*args, **kwargs)


limit_option = pulp_option(
"--limit",
default=DEFAULT_LIMIT,
type=int,
help=_("Limit the number of {entities} to show."),
cls=PulpOption,
)
offset_option = click.option(
offset_option = pulp_option(
"--offset",
default=0,
type=int,
help=_("Skip a number of {entities} to show."),
cls=PulpOption,
)

href_option = click.option(
href_option = pulp_option(
"--href",
help=_("HREF of the {entity}"),
callback=_href_callback,
expose_value=False,
cls=PulpOption,
)

name_option = click.option(
name_option = pulp_option(
"--name",
help=_("Name of the {entity}"),
callback=_name_callback,
expose_value=False,
cls=PulpOption,
)

repository_href_option = click.option(
Expand All @@ -186,27 +209,24 @@ def load_json_callback(
expose_value=False,
)

label_select_option = click.option(
label_select_option = pulp_option(
"--label-select",
"pulp_label_select",
help=_("Filter {entities} by a label search query."),
type=str,
cls=PulpOption,
)

base_path_option = click.option(
base_path_option = pulp_option(
"--base-path",
help=_("Base-path of the {entity}"),
type=str,
cls=PulpOption,
)

base_path_contains_option = click.option(
base_path_contains_option = pulp_option(
"--base-path-contains",
"base_path__icontains",
help=_("{entity} base-path contains search"),
type=str,
cls=PulpOption,
)

common_remote_create_options = [
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/core/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def list(
exporter: str,
limit: int,
offset: int,
**kwargs: Any
**kwargs: Any,
) -> None:
params = {k: v for k, v in kwargs.items() if v is not None}
exporter_ctx = PulpExporterContext(pulp_ctx)
Expand Down
6 changes: 5 additions & 1 deletion pulpcore/cli/file/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
list_command,
load_json_callback,
name_option,
pulp_option,
repository_href_option,
repository_option,
show_command,
Expand Down Expand Up @@ -68,7 +69,10 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
click.option("--description"),
click.option("--remote", callback=_remote_callback),
click.option("--manifest"),
click.option("--autopublish/--no-autopublish", default=None),
pulp_option(
"--autopublish/--no-autopublish", needs_plugin="file", min_version="1.7.0", default=None
),
pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"),
]
create_options = update_options + [
click.option("--name", required=True),
Expand Down
3 changes: 3 additions & 0 deletions pulpcore/cli/python/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
label_select_option,
list_command,
name_option,
pulp_option,
show_command,
update_command,
version_command,
Expand Down Expand Up @@ -60,10 +61,12 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
click.option("--name", required=True),
click.option("--description"),
click.option("--remote", callback=_remote_callback),
pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"),
]
update_options = [
click.option("--description"),
click.option("--remote", callback=_remote_callback),
pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"),
]

repository.add_command(list_command(decorators=[label_select_option]))
Expand Down
6 changes: 5 additions & 1 deletion pulpcore/cli/rpm/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
label_select_option,
list_command,
name_option,
pulp_option,
show_command,
update_command,
version_command,
Expand Down Expand Up @@ -69,7 +70,10 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
click.option("--gpgcheck", type=click.Choice(("0", "1"))),
click.option("--repo-gpgcheck", type=click.Choice(("0", "1"))),
click.option("--sqlite-metadata/--no-sqlite-metadata", default=None),
click.option("--autopublish/--no-autopublish", default=None),
pulp_option(
"--autopublish/--no-autopublish", needs_plugin="rpm", min_version="3.11.0.dev", default=None
),
pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"),
]
create_options = update_options + [click.option("--name", required=True)]

Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/pulp_file/test_repository.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test "$(echo "$OUTPUT" | jq -r '.|length')" != "0"
expect_succ pulp file repository task list --repository "cli_test_file_repo"
test "$(echo "$OUTPUT" | jq -r '.|length')" = "3"

if [ "$(pulp debug has-plugin --name "file" --min-version "1.7.0.dev")" = "true" ]
if [ "$(pulp debug has-plugin --name "file" --min-version "1.7.0")" = "true" ]
then
expect_succ pulp file repository update --name "cli_test_file_repo" --manifest "manifest.csv"
fi
Expand Down
10 changes: 10 additions & 0 deletions tests/scripts/pulp_file/test_sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
pulp debug has-plugin --name "file" || exit 3

autopublish_repo="cli_test_file_repository_autopublish"
one_version_repo="cli_test_one_version_repo"

cleanup() {
pulp file remote destroy --name "cli_test_file_remote" || true
Expand Down Expand Up @@ -55,3 +56,12 @@ then
created_resources=$(pulp show --href "$task" | jq -r ".created_resources")
echo "$created_resources" | grep -q '/pulp/api/v3/publications/file/file/'
fi

# Test retained versions
if [ "$(pulp debug has-plugin --name "core" --min-version "3.13.0.dev")" = "true" ]
then
expect_succ pulp file repository create --name "$one_version_repo" --remote "cli_test_file_remote" --retained-versions 1
expect_succ pulp file repository sync --name "$one_version_repo"
expect_succ pulp file repository version list --repository "$one_version_repo"
test "$(echo "$OUTPUT" | jq -r length)" -eq 1
fi

0 comments on commit e2060bc

Please sign in to comment.