Skip to content

Commit

Permalink
Add needs_capability to EntityContext
Browse files Browse the repository at this point in the history
This also makes use of it for tagging and untagging in container
repositories as well as for all roles related subcommands.

fixes pulp#465
  • Loading branch information
mdellweg committed Feb 4, 2022
1 parent 43f3d6f commit 4740fbd
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
12 changes: 11 additions & 1 deletion .ci/scripts/validate_commit_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@

KEYWORDS = ["fixes", "closes"]
NO_ISSUE = "[noissue]"
CHANGELOG_EXTS = [".feature", ".bugfix", ".doc", ".removal", ".misc", ".deprecation"]
# TODO (On a rainy afternoon) Fetch the extensions from pyproject.toml
CHANGELOG_EXTS = [
".feature",
".bugfix",
".doc",
".removal",
".misc",
".deprecation",
".translation",
".devel",
]

sha = sys.argv[1]
project = "pulp-cli"
Expand Down
1 change: 1 addition & 0 deletions CHANGES/465.devel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `needs_capability` to `EntityContext` so context member function can require capabilities.
15 changes: 15 additions & 0 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,19 +551,23 @@ def show_label(self, href: str, key: str) -> Any:
raise click.ClickException(_("Could not find label with key '{key}'.").format(key=key))

def my_permissions(self) -> Any:
self.needs_capability("roles")
return self.call("my_permissions", parameters={self.HREF: self.pulp_href})

def list_roles(self) -> Any:
self.needs_capability("roles")
return self.call("list_roles", parameters={self.HREF: self.pulp_href})

def add_role(self, role: str, users: List[str], groups: List[str]) -> Any:
self.needs_capability("roles")
return self.call(
"add_role",
parameters={self.HREF: self.pulp_href},
body={"role": role, "users": users, "groups": groups},
)

def remove_role(self, role: str, users: List[str], groups: List[str]) -> Any:
self.needs_capability("roles")
return self.call(
"remove_role",
parameters={self.HREF: self.pulp_href},
Expand All @@ -575,6 +579,17 @@ def capable(self, capability: str) -> bool:
(self.pulp_ctx.has_plugin(pr) for pr in self.CAPABILITIES[capability])
)

def needs_capability(self, capability: str) -> None:
if capability in self.CAPABILITIES:
for pr in self.CAPABILITIES[capability]:
self.pulp_ctx.needs_plugin(pr)
else:
raise click.ClickException(
_("Capability '{capability}' needed on '{entity}' for this command.").format(
capability=capability, entity=self.ENTITY
)
)


class PulpRemoteContext(PulpEntityContext):
"""
Expand Down
24 changes: 22 additions & 2 deletions pulpcore/cli/container/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from pulpcore.cli.common.context import (
EntityDefinition,
PluginRequirement,
Expand Down Expand Up @@ -76,7 +78,25 @@ class PulpContainerPushRepositoryVersionContext(PulpRepositoryVersionContext):
ID_PREFIX = "repositories_container_container_push_versions"


class PulpContainerRepositoryContext(PulpRepositoryContext):
class PulpContainerBaseRepositoryContext(PulpRepositoryContext):
def tag(self, tag: str, digest: str) -> Any:
self.needs_capability("tag")
return self.call(
"tag",
parameters={self.HREF: self.pulp_href},
body={"tag": tag, "digest": digest},
)

def untag(self, tag: str) -> Any:
self.needs_capability("tag")
return self.call(
"untag",
parameters={self.HREF: self.pulp_href},
body={"tag": tag},
)


class PulpContainerRepositoryContext(PulpContainerBaseRepositoryContext):
HREF = "container_container_repository_href"
ID_PREFIX = "repositories_container_container"
VERSION_CONTEXT = PulpContainerRepositoryVersionContext
Expand All @@ -87,7 +107,7 @@ class PulpContainerRepositoryContext(PulpRepositoryContext):
}


class PulpContainerPushRepositoryContext(PulpRepositoryContext):
class PulpContainerPushRepositoryContext(PulpContainerBaseRepositoryContext):
HREF = "container_container_push_repository_href"
ID_PREFIX = "repositories_container_container_push"
VERSION_CONTEXT = PulpContainerPushRepositoryVersionContext
Expand Down
23 changes: 5 additions & 18 deletions pulpcore/cli/container/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
from pulpcore.cli.common.i18n import get_translation
from pulpcore.cli.container.context import (
PulpContainerBaseRepositoryContext,
PulpContainerPushRepositoryContext,
PulpContainerRemoteContext,
PulpContainerRepositoryContext,
Expand Down Expand Up @@ -142,37 +143,23 @@ def sync(
@click.option("--digest", help=_("SHA256 digest of the Manifest file"), required=True)
@pass_repository_context
def add_tag(
repository_ctx: PulpRepositoryContext,
repository_ctx: PulpContainerBaseRepositoryContext,
digest: str,
tag: str,
) -> None:
if not repository_ctx.capable("tag"):
raise click.ClickException(_("pulp_container 2.3.0 is required to tag images"))

digest = digest.strip()
if not digest.startswith("sha256:"):
digest = f"sha256:{digest}"
if len(digest) != 71: # len("sha256:") + 64
raise click.ClickException("Improper SHA256, please provide a valid 64 digit digest.")

repository_ctx.call(
"tag",
parameters={repository_ctx.HREF: repository_ctx.pulp_href},
body={"tag": tag, "digest": digest},
)
repository_ctx.tag(tag, digest)


@repository.command(name="untag")
@name_option
@href_option
@click.option("--tag", help=_("Name of tag to remove"), required=True, callback=_tag_callback)
@pass_repository_context
def remove_tag(repository_ctx: PulpRepositoryContext, tag: str) -> None:
if not repository_ctx.capable("tag"):
raise click.ClickException(_("pulp_container 2.3.0 is required to untag images"))

repository_ctx.call(
"untag",
parameters={repository_ctx.HREF: repository_ctx.pulp_href},
body={"tag": tag},
)
def remove_tag(repository_ctx: PulpContainerBaseRepositoryContext, tag: str) -> None:
repository_ctx.untag(tag)
4 changes: 1 addition & 3 deletions pulpcore/cli/core/content_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def rbac(ctx: click.Context, pulp_ctx: PulpContext) -> None:
rbac.add_command(show_command(decorators=lookup_options))
rbac.add_command(update_command(decorators=lookup_options))
rbac.add_command(destroy_command(decorators=lookup_options))
rbac.add_command(
role_command(decorators=lookup_options, needs_plugins=[PluginRequirement("core", min="3.17")])
)
rbac.add_command(role_command(decorators=lookup_options))


@rbac.command()
Expand Down
3 changes: 3 additions & 0 deletions pulpcore/cli/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class PulpGroupContext(PulpEntityContext):
# Handled by a workaround
# HREF = "group_href"
ID_PREFIX = "groups"
CAPABILITIES = {"roles": [PluginRequirement("core", "3.17.0")]}

@property
def HREF(self) -> str: # type:ignore
Expand Down Expand Up @@ -245,6 +246,7 @@ class PulpRbacContentGuardContext(PulpContentGuardContext):
HREF = "r_b_a_c_content_guard_href"
ID_PREFIX = "contentguards_core_rbac"
DOWNLOAD_ROLE: ClassVar[str] = "core.rbaccontentguard_downloader"
CAPABILITIES = {"roles": [PluginRequirement("core", "3.17.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.dev")):
Expand Down Expand Up @@ -285,6 +287,7 @@ class PulpTaskContext(PulpEntityContext):
ENTITIES = _("tasks")
HREF = "task_href"
ID_PREFIX = "tasks"
CAPABILITIES = {"roles": [PluginRequirement("core", "3.17.0")]}

resource_context: Optional[PulpEntityContext] = None

Expand Down

0 comments on commit 4740fbd

Please sign in to comment.