Skip to content

Commit

Permalink
Add container namespace command group
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
mdellweg committed Mar 15, 2021
1 parent bcdb435 commit dfb36ca
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/176.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the container namespace command group.
2 changes: 2 additions & 0 deletions pulpcore/cli/container/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pulpcore.cli.common import main
from pulpcore.cli.common.context import PulpContext, pass_pulp_context
from pulpcore.cli.container.distribution import distribution
from pulpcore.cli.container.namespace import namespace
from pulpcore.cli.container.remote import remote
from pulpcore.cli.container.repository import repository

Expand All @@ -17,4 +18,5 @@ def container(pulp_ctx: PulpContext) -> None:

container.add_command(repository)
container.add_command(remote)
container.add_command(namespace)
container.add_command(distribution)
26 changes: 26 additions & 0 deletions pulpcore/cli/container/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import gettext
import sys
from typing import Any

import click

from pulpcore.cli.common.context import (
EntityDefinition,
Expand All @@ -10,6 +14,28 @@
_ = gettext.gettext


class PulpContainerNamespaceContext(PulpEntityContext):
ENTITY = "container namespace"
ENTITIES = "container namespaces"
HREF = "container_container_namespace_href"
LIST_ID = "pulp_container_namespaces_list"
READ_ID = "pulp_container_namespaces_read"
CREATE_ID = "pulp_container_namespaces_create"
DELETE_ID = "pulp_container_namespaces_delete"

def find(self, **kwargs: Any) -> Any:
"""Workaroud for the missing ability to filter"""
if self.pulp_ctx.has_plugin("container", min_version="2.3"):
# Workaround not needed anymore
return super().find(**kwargs)
search_result = self.list(limit=sys.maxsize, offset=0, parameters={})
for key, value in kwargs.items():
search_result = [res for res in search_result if res[key] == value]
if len(search_result) != 1:
raise click.ClickException(f"Could not find {self.ENTITY} with {kwargs}.")
return search_result[0]


class PulpContainerDistributionContext(PulpEntityContext):
ENTITY = "container distribution"
ENTITIES = "container distributions"
Expand Down
44 changes: 44 additions & 0 deletions pulpcore/cli/container/namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import gettext

import click

from pulpcore.cli.common.context import PulpContext, pass_pulp_context
from pulpcore.cli.common.generic import (
create_command,
destroy_command,
href_option,
list_command,
name_option,
show_command,
)
from pulpcore.cli.container.context import PulpContainerNamespaceContext

_ = gettext.gettext


@click.group()
@click.option(
"-t",
"--type",
"namespace_type",
type=click.Choice(["container"], case_sensitive=False),
default="container",
)
@pass_pulp_context
@click.pass_context
def namespace(ctx: click.Context, pulp_ctx: PulpContext, namespace_type: str) -> None:
if namespace_type == "container":
ctx.obj = PulpContainerNamespaceContext(pulp_ctx)
else:
raise NotImplementedError()


lookup_options = [href_option, name_option]
create_options = [
click.option("--name", required=True),
]

namespace.add_command(list_command())
namespace.add_command(show_command(decorators=lookup_options))
namespace.add_command(create_command(decorators=create_options))
namespace.add_command(destroy_command(decorators=lookup_options))
19 changes: 19 additions & 0 deletions tests/scripts/pulp_container/test_namespace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source

pulp debug has-plugin --name "container" || exit 3

cleanup() {
pulp container namespace destroy --name "cli_test_container_namespace" || true
}
trap cleanup EXIT

cleanup

expect_succ pulp container namespace list

expect_succ pulp container namespace create --name "cli_test_container_namespace"
expect_succ pulp container namespace list
expect_succ pulp container namespace destroy --name "cli_test_container_namespace"

0 comments on commit dfb36ca

Please sign in to comment.