Skip to content

Commit

Permalink
Move validation steps into context
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
mdellweg committed Sep 12, 2022
1 parent 43076a8 commit d68c6db
Show file tree
Hide file tree
Showing 24 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pulpcore/cli/ansible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@pulp_group()
@pass_pulp_context
def ansible(pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("ansible", min="0.7"))
pulp_ctx.needs_plugin(PluginRequirement("ansible", min="0.7.0"))


def mount(main: click.Group, **kwargs: Any) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _debug_callback(level: int, x: str) -> None:
background_tasks=background,
timeout=timeout,
)
ctx.obj.needs_plugin(PluginRequirement("core", min="3.11"))
ctx.obj.needs_plugin(PluginRequirement("core", min="3.11.0"))


main.add_command(config)
Expand Down
11 changes: 9 additions & 2 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,13 @@ class PulpEntityContext:
ID_PREFIX: ClassVar[str]
# Set of fields that can be cleared by sending 'null'
NULLABLES: ClassVar[Set[str]] = set()
# List of necessary plugin to operate such an entity in the server
NEEDS_PLUGINS: ClassVar[List[PluginRequirement]] = []
# Subclasses can specify version dependent capabilities here
# e.g. `CAPABILITIES = {
# "feature1": [
# PluginRequirement("file"),
# PluginRequirement("core", min_version="3.7")
# PluginRequirement("core", min_version="3.7.0")
# ]
# }
CAPABILITIES: ClassVar[Dict[str, List[PluginRequirement]]] = {}
Expand Down Expand Up @@ -442,6 +444,11 @@ def __init__(

self.meta: Dict[str, str] = {}
self.pulp_ctx: PulpContext = pulp_ctx

# Add requirements to the lazy evaluated list
for req in self.NEEDS_PLUGINS:
self.pulp_ctx.needs_plugin(req)

self._entity = None
if pulp_href is None:
self._entity_lookup = entity or {}
Expand Down Expand Up @@ -705,7 +712,7 @@ def get_version_context(self) -> PulpRepositoryVersionContext:

def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> EntityDefinition:
body = super().preprocess_entity(body, partial=partial)
if self.pulp_ctx.has_plugin(PluginRequirement("core", "3.13", "3.15")):
if self.pulp_ctx.has_plugin(PluginRequirement("core", "3.13.0", "3.15.0")):
# "retain_repo_versions" has been named "retained_versions" until pulpcore 3.15
# https://github.com/pulp/pulpcore/pull/1472
if "retain_repo_versions" in body:
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/container/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@pulp_group()
@pass_pulp_context
def container(pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("container", min="2.3"))
pulp_ctx.needs_plugin(PluginRequirement("container", min="2.3.0"))


def mount(main: click.Group, **kwargs: Any) -> None:
Expand Down
2 changes: 0 additions & 2 deletions pulpcore/cli/core/access_policy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpEntityContext,
pass_entity_context,
Expand Down Expand Up @@ -58,6 +57,5 @@ def access_policy(ctx: click.Context, pulp_ctx: PulpContext) -> None:
@pass_entity_context
@pass_pulp_context
def reset(pulp_ctx: PulpContext, access_policy_ctx: PulpAccessPolicyContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("core", min="3.17"))
result = access_policy_ctx.reset()
pulp_ctx.output_result(result)
9 changes: 1 addition & 8 deletions pulpcore/cli/core/content_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
pass_entity_context,
pass_pulp_context,
)
from pulpcore.cli.common.context import PulpContext, pass_entity_context, pass_pulp_context
from pulpcore.cli.common.generic import (
create_command,
destroy_command,
Expand Down Expand Up @@ -48,7 +43,6 @@ def content_guard(ctx: click.Context, pulp_ctx: PulpContext) -> None:
@pass_pulp_context
@click.pass_context
def rbac(ctx: click.Context, pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("core", "3.15.0"))
ctx.obj = PulpRbacContentGuardContext(pulp_ctx)


Expand Down Expand Up @@ -120,7 +114,6 @@ def remove(
@pass_pulp_context
@click.pass_context
def redirect(ctx: click.Context, pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("core", "3.18.0"))
ctx.obj = PulpContentRedirectContentGuardContext(pulp_ctx)


Expand Down
18 changes: 11 additions & 7 deletions pulpcore/cli/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ class PulpAccessPolicyContext(PulpEntityContext):
ID_PREFIX = "access_policies"

def reset(self) -> Any:
self.pulp_ctx.needs_plugin(PluginRequirement("core", min="3.17.0"))
return self.call("reset", parameters={self.HREF: self.pulp_href})

def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> EntityDefinition:
body = super().preprocess_entity(body, partial=partial)
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17")):
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17.0")):
if "creation_hooks" in body:
body["permissions_assignment"] = body.pop("creation_hooks")
return body
Expand Down Expand Up @@ -108,18 +109,18 @@ class PulpGroupContext(PulpEntityContext):

@property
def HREF(self) -> str: # type:ignore
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17")):
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17.0")):
return "auth_group_href"
return "group_href"


class PulpGroupPermissionContext(PulpEntityContext):
ENTITY = _("group permission")
ENTITIES = _("group permissions")
NEEDS_PLUGINS = [PluginRequirement("core", max="3.20.0", feature=_("group permissions"))]
group_ctx: PulpGroupContext

def __init__(self, pulp_ctx: PulpContext, group_ctx: PulpGroupContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("core", max="3.20", feature=_("group permissions")))
super().__init__(pulp_ctx)
self.group_ctx = group_ctx

Expand Down Expand Up @@ -178,7 +179,7 @@ class PulpGroupModelPermissionContext(PulpGroupPermissionContext):

@property
def HREF(self) -> str: # type:ignore
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17")):
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17.0")):
return "auth_groups_model_permission_href"
return "groups_model_permission_href"

Expand All @@ -192,7 +193,7 @@ class PulpGroupObjectPermissionContext(PulpGroupPermissionContext):

@property
def HREF(self) -> str: # type:ignore
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17")):
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17.0")):
return "auth_groups_object_permission_href"
return "groups_object_permission_href"

Expand Down Expand Up @@ -224,7 +225,7 @@ class PulpGroupUserContext(PulpEntityContext):

@property
def HREF(self) -> str: # type:ignore
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17")):
if not self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17.0")):
return "auth_groups_user_href"
return "groups_user_href"

Expand All @@ -249,6 +250,7 @@ class PulpContentRedirectContentGuardContext(PulpEntityContext):
ENTITIES = "content redirect content guards"
HREF = "content_redirect_content_guard_href"
ID_PREFIX = "contentguards_core_content_redirect"
NEEDS_PLUGINS = [PluginRequirement("core", "3.18.0")]


class PulpDistributionContext(PulpEntityContext):
Expand Down Expand Up @@ -279,6 +281,7 @@ class PulpRbacContentGuardContext(PulpContentGuardContext):
ID_PREFIX = "contentguards_core_rbac"
DOWNLOAD_ROLE: ClassVar[str] = "core.rbaccontentguard_downloader"
CAPABILITIES = {"roles": [PluginRequirement("core", "3.17.0")]}
NEEDS_PLUGINS = [PluginRequirement("core", "3.15.0")]

def assign(self, href: str, users: Optional[List[str]], groups: Optional[List[str]]) -> Any:
if self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17.0")):
Expand All @@ -305,6 +308,7 @@ class PulpRoleContext(PulpEntityContext):
HREF = "role_href"
ID_PREFIX = "roles"
NULLABLES = {"description"}
NEEDS_PLUGINS = [PluginRequirement("core", "3.17.0")]


class PulpSigningServiceContext(PulpEntityContext):
Expand Down Expand Up @@ -350,7 +354,7 @@ def purge(self, finished_before: Optional[str], states: Optional[List[str]]) ->

def summary(self) -> Dict[str, int]:
task_states = ["waiting", "skipped", "running", "completed", "failed", "canceled"]
if self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.14")):
if self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.14.0")):
task_states.append("canceling")
result = {}
for state in task_states:
Expand Down
5 changes: 2 additions & 3 deletions pulpcore/cli/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def group(ctx: click.Context, pulp_ctx: PulpContext) -> None:
group.add_command(destroy_command(decorators=lookup_options))
group.add_command(create_command(decorators=create_options))
group.add_command(
role_command(decorators=lookup_options, needs_plugins=[PluginRequirement("core", min="3.17")])
role_command(decorators=lookup_options, needs_plugins=[PluginRequirement("core", min="3.17.0")])
)


@group.group(needs_plugins=[PluginRequirement("core", max="3.20-dev")])
@group.group(needs_plugins=[PluginRequirement("core", max="3.20.0")])
@click.option(
"-t",
"--type",
Expand Down Expand Up @@ -168,7 +168,6 @@ def remove_user(pulp_ctx: PulpContext, entity_ctx: PulpGroupUserContext, usernam
@pass_pulp_context
@click.pass_context
def role(ctx: click.Context, pulp_ctx: PulpContext, group_ctx: PulpGroupContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("core", min="3.17"))
ctx.obj = PulpGroupRoleContext(pulp_ctx, group_ctx)


Expand Down
6 changes: 3 additions & 3 deletions pulpcore/cli/core/orphan.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def orphan() -> None:
"--content-hrefs",
help=_("List of specific Contents to delete if they are orphans"),
callback=load_json_callback,
needs_plugins=[PluginRequirement("core", "3.14")],
needs_plugins=[PluginRequirement("core", "3.14.0")],
)
@pulp_option(
"--protection-time",
Expand All @@ -31,15 +31,15 @@ def orphan() -> None:
"How long in minutes Pulp should hold orphan Content and Artifacts before becoming"
" candidates for cleanup task"
),
needs_plugins=[PluginRequirement("core", "3.15")],
needs_plugins=[PluginRequirement("core", "3.15.0")],
)
@pass_pulp_context
def cleanup(pulp_ctx: PulpContext, **kwargs: Any) -> None:
"""
Cleanup orphaned content.
"""
body = {k: v for k, v in kwargs.items() if v is not None}
if pulp_ctx.has_plugin(PluginRequirement("core", "3.14")):
if pulp_ctx.has_plugin(PluginRequirement("core", "3.14.0")):
result = pulp_ctx.call("orphans_cleanup_cleanup", body=body)
else:
result = pulp_ctx.call("orphans_delete")
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/core/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _permission_callback(
@pass_pulp_context
@click.pass_context
def role(ctx: click.Context, pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("core", min="3.17"))
pulp_ctx.needs_plugin(PluginRequirement("core", min="3.17.0"))
ctx.obj = PulpRoleContext(pulp_ctx)


Expand Down
3 changes: 2 additions & 1 deletion pulpcore/cli/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def task(ctx: click.Context, pulp_ctx: PulpContext) -> None:
task.add_command(destroy_command(decorators=[href_option, uuid_option]))
task.add_command(
role_command(
decorators=[href_option, uuid_option], needs_plugins=[PluginRequirement("core", min="3.17")]
decorators=[href_option, uuid_option],
needs_plugins=[PluginRequirement("core", min="3.17.0")],
)
)

Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
_ = translation.gettext


req_core_3_17 = PluginRequirement("core", min="3.17")
req_core_3_17 = PluginRequirement("core", min="3.17.0")

username_option = pulp_option(
"--username",
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/file/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@pulp_group(name="file")
@pass_pulp_context
def file_group(pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("file", min="1.6"))
pulp_ctx.needs_plugin(PluginRequirement("file", min="1.6.0"))


def mount(main: click.Group, **kwargs: Any) -> None:
Expand Down
2 changes: 0 additions & 2 deletions pulpcore/cli/file/acs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpRemoteContext,
pass_entity_context,
Expand Down Expand Up @@ -44,7 +43,6 @@
@pass_pulp_context
@click.pass_context
def acs(ctx: click.Context, pulp_ctx: PulpContext, acs_type: str) -> None:
pulp_ctx.needs_plugin(PluginRequirement("file", "1.9.0"))
if acs_type == "file":
ctx.obj = PulpFileACSContext(pulp_ctx)
else:
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/file/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def upload(
uploads: Dict[str, IO[bytes]] = {}
if chunk_size > size:
uploads["file"] = file
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20")):
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20.0")):
upload_href = PulpUploadContext(pulp_ctx).upload_file(file, chunk_size)
body["upload"] = upload_href
else:
Expand Down
1 change: 1 addition & 0 deletions pulpcore/cli/file/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PulpFileACSContext(PulpEntityContext):
ENTITIES = _("file ACSes")
HREF = "file_file_alternate_content_source_href"
ID_PREFIX = "acs_file_file"
NEEDS_PLUGINS = [PluginRequirement("file", "1.9.0")]
CAPABILITIES = {"roles": [PluginRequirement("file", min="1.11.0")]}

def refresh(self, href: str) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@pulp_group(name="python")
@pass_pulp_context
def python_group(pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("python", min="3.1"))
pulp_ctx.needs_plugin(PluginRequirement("python", min="3.1.0"))


def mount(main: click.Group, **kwargs: Any) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/python/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def upload(
uploads: Dict[str, IO[bytes]] = {}
if chunk_size > size:
uploads["file"] = file
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20")):
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20.0")):
upload_href = PulpUploadContext(pulp_ctx).upload_file(file, chunk_size)
body["upload"] = upload_href
else:
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/rpm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@pulp_group()
@pass_pulp_context
def rpm(pulp_ctx: PulpContext) -> None:
pulp_ctx.needs_plugin(PluginRequirement("rpm", min="3.9"))
pulp_ctx.needs_plugin(PluginRequirement("rpm", min="3.9.0"))


def mount(main: click.Group, **kwargs: Any) -> None:
Expand Down
2 changes: 0 additions & 2 deletions pulpcore/cli/rpm/acs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpRemoteContext,
pass_entity_context,
Expand Down Expand Up @@ -43,7 +42,6 @@
@pass_pulp_context
@click.pass_context
def acs(ctx: click.Context, pulp_ctx: PulpContext, acs_type: str) -> None:
pulp_ctx.needs_plugin(PluginRequirement("rpm", "3.18.0"))
if acs_type == "rpm":
ctx.obj = PulpRpmACSContext(pulp_ctx)
else:
Expand Down
2 changes: 0 additions & 2 deletions pulpcore/cli/rpm/comps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from pulpcore.cli.common.context import (
EntityFieldDefinition,
PluginRequirement,
PulpContext,
PulpEntityContext,
pass_pulp_context,
Expand Down Expand Up @@ -37,7 +36,6 @@ def comps_upload(
replace: Optional[bool],
) -> None:
"""Create comps.xml content-units by uploading a comps.xml-formatted file"""
pulp_ctx.needs_plugin(PluginRequirement("rpm", min="3.17.0dev"))
entity_ctx = PulpRpmCompsXmlContext(pulp_ctx)
href = None
if isinstance(repository, PulpEntityContext):
Expand Down
6 changes: 1 addition & 5 deletions pulpcore/cli/rpm/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,10 @@ def upload(
body: Dict[str, Any] = {}
uploads: Dict[str, IO[bytes]] = {}
if isinstance(entity_ctx, PulpRpmPackageContext):
if kwargs.get("relative_path") is None:
pulp_ctx.needs_plugin(PluginRequirement("rpm", min="3.18"))
else:
ValueError("--relative-path must be provided")
size = os.path.getsize(file.name)
if chunk_size > size:
uploads["file"] = file
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20")):
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20.0")):
upload_href = PulpUploadContext(pulp_ctx).upload_file(file, chunk_size)
body["upload"] = upload_href
else:
Expand Down
Loading

0 comments on commit d68c6db

Please sign in to comment.