Skip to content

Commit

Permalink
Add href support to resource_option
Browse files Browse the repository at this point in the history
fixes pulp#315
  • Loading branch information
mdellweg committed Aug 20, 2021
1 parent e259fe9 commit 8182d69
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGES/315.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the ability to pass an href to a resource option.
5 changes: 5 additions & 0 deletions pulpcore/cli/ansible/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
EntityFieldDefinition,
PulpContext,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
pass_pulp_context,
pass_repository_context,
Expand Down Expand Up @@ -41,6 +42,10 @@
"ansible:collection": PulpAnsibleCollectionRemoteContext,
"ansible:role": PulpAnsibleRoleRemoteContext,
},
href_pattern=PulpRemoteContext.HREF_PATTERN,
help=_(
"Remote used for synching in the form '[[<plugin>:]<resource_type>:]<name>' or by href."
),
)


Expand Down
2 changes: 2 additions & 0 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ class PulpRemoteContext(PulpEntityContext):

ENTITY = "remote"
ENTITIES = "remotes"
HREF_PATTERN = r"^/pulp/api/v3/remotes/(?P<plugin>\w+)/(?P<resource_type>\w+)/"
NULLABLES = {
"ca_cert",
"client_cert",
Expand Down Expand Up @@ -525,6 +526,7 @@ class PulpRepositoryContext(PulpEntityContext):

ENTITY = "repository"
ENTITIES = "repositories"
HREF_PATTERN = r"^/pulp/api/v3/repositories/(?P<plugin>\w+)/(?P<resource_type>\w+)/"
LIST_ID = "repositories_list"
SYNC_ID: ClassVar[str]
MODIFY_ID: ClassVar[str]
Expand Down
37 changes: 32 additions & 5 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gettext
import json
import re
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -248,6 +249,7 @@ def resource_option(*args: Any, **kwargs: Any) -> Callable[[_FC], _FC]:
lookup_key: str = kwargs.pop("lookup_key", "name")
context_table: Dict[str, Type[PulpEntityContext]] = kwargs.pop("context_table")
capabilities: Optional[List[str]] = kwargs.pop("capabilities", None)
href_pattern: Optional[str] = kwargs.pop("href_pattern", None)

def _option_callback(
ctx: click.Context, param: click.Parameter, value: Optional[str]
Expand All @@ -256,10 +258,35 @@ def _option_callback(
if not value:
return value

split_value = value.split(":", maxsplit=2)
while len(split_value) < 3:
split_value.insert(0, "")
plugin, resource_type, identifier = split_value
pulp_href: Optional[str] = None
entity: Optional[EntityDefinition] = None

if value.startswith("/"):
# An href was passed
if href_pattern is None:
raise click.ClickException(
_("The option {option_name} does not support href.").format(
option_name=param.name
)
)
match = re.match(href_pattern, value)
if match is None:
raise click.ClickException(
_("'{value}' is not a valid href for {option_name}.").format(
value=value, option_name=param.name
)
)
match_groups = match.groupdict(default="")
plugin = match_groups.get("plugin", "")
resource_type = match_groups.get("resource_type", "")
pulp_href = value
else:
# A natural key identifier was passed
split_value = value.split(":", maxsplit=2)
while len(split_value) < 3:
split_value.insert(0, "")
plugin, resource_type, identifier = split_value
entity = {lookup_key: identifier}

if resource_type == "":
if default_type is None:
Expand Down Expand Up @@ -288,7 +315,7 @@ def _option_callback(
)
pulp_ctx = ctx.find_object(PulpContext)
assert pulp_ctx is not None
entity_ctx: PulpEntityContext = context_class(pulp_ctx, entity={lookup_key: identifier})
entity_ctx: PulpEntityContext = context_class(pulp_ctx, pulp_href=pulp_href, entity=entity)

if capabilities is not None:
for capability in capabilities:
Expand Down
5 changes: 5 additions & 0 deletions pulpcore/cli/container/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
EntityFieldDefinition,
PulpContext,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
pass_pulp_context,
pass_repository_context,
Expand Down Expand Up @@ -42,6 +43,10 @@
default_plugin="container",
default_type="container",
context_table={"container:container": PulpContainerRemoteContext},
href_pattern=PulpRemoteContext.HREF_PATTERN,
help=_(
"Remote used for synching in the form '[[<plugin>:]<resource_type>:]<name>' or by href."
),
)


Expand Down
31 changes: 26 additions & 5 deletions pulpcore/cli/core/export.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import gettext
from typing import Any, Dict, Iterable, Tuple
import re
from typing import Any, Dict, Iterable, Optional, Tuple

import click

from pulpcore.cli.common.context import (
DEFAULT_LIMIT,
EntityDefinition,
PulpContext,
PulpRepositoryContext,
PulpRepositoryVersionContext,
Expand All @@ -23,9 +25,26 @@ def _version_list_callback(
) -> Iterable[PulpRepositoryVersionContext]:
result = []
for item in value:
plugin, resource_type, identifier = item[0].split(":", maxsplit=2)
if not identifier:
raise click.ClickException(_("Repositories must be specified with plugin and type"))
pulp_href: Optional[str] = None
entity: Optional[EntityDefinition] = None

if item[0].startswith("/"):
match = re.match(PulpRepositoryContext.HREF_PATTERN, item[0])
if match is None:
raise click.ClickException(
_("'{value}' is not a valid href for {option_name}.").format(
value=value, option_name=param.name
)
)
match_groups = match.groupdict(default="")
plugin = match_groups.get("plugin", "")
resource_type = match_groups.get("resource_type", "")
pulp_href = item[0]
else:
plugin, resource_type, identifier = item[0].split(":", maxsplit=2)
if not identifier:
raise click.ClickException(_("Repositories must be specified with plugin and type"))
entity = {"name": identifier}
context_class = registered_repository_contexts.get(plugin + ":" + resource_type)
if context_class is None:
raise click.ClickException(
Expand All @@ -36,7 +55,9 @@ def _version_list_callback(
)
pulp_ctx = ctx.find_object(PulpContext)
assert pulp_ctx is not None
repository_ctx: PulpRepositoryContext = context_class(pulp_ctx, entity={"name": identifier})
repository_ctx: PulpRepositoryContext = context_class(
pulp_ctx, pulp_href=pulp_href, entity=entity
)

if not repository_ctx.capable("pulpexport"):
raise click.ClickException(
Expand Down
8 changes: 7 additions & 1 deletion pulpcore/cli/core/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
EntityFieldDefinition,
PulpContext,
PulpEntityContext,
PulpRepositoryContext,
pass_entity_context,
pass_pulp_context,
registered_repository_contexts,
Expand All @@ -29,6 +30,11 @@
context_table=registered_repository_contexts,
capabilities=["pulpexport"],
multiple=True,
href_pattern=PulpRepositoryContext.HREF_PATTERN,
help=_(
"Repository to export from in the form '[[<plugin>:]<resource_type>:]<name>' or by href."
" Can be called multiple times."
),
)


Expand Down Expand Up @@ -83,7 +89,7 @@ def create(
@href_option
@click.option("--path")
@multi_repository_option
@click.option("--repository-href", multiple=True)
@click.option("--repository-href", multiple=True) # This should be deprecated
@pass_entity_context
@pass_pulp_context
def update(
Expand Down
5 changes: 5 additions & 0 deletions pulpcore/cli/file/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PluginRequirement,
PulpContext,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
pass_pulp_context,
pass_repository_context,
Expand Down Expand Up @@ -49,6 +50,10 @@
default_plugin="file",
default_type="file",
context_table={"file:file": PulpFileRemoteContext},
href_pattern=PulpRemoteContext.HREF_PATTERN,
help=_(
"Remote used for synching in the form '[[<plugin>:]<resource_type>:]<name>' or by href."
),
)


Expand Down
5 changes: 5 additions & 0 deletions pulpcore/cli/python/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PluginRequirement,
PulpContext,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
pass_pulp_context,
pass_repository_context,
Expand Down Expand Up @@ -46,6 +47,10 @@
default_plugin="python",
default_type="python",
context_table={"python:python": PulpPythonRemoteContext},
href_pattern=PulpRemoteContext.HREF_PATTERN,
help=_(
"Remote used for synching in the form '[[<plugin>:]<resource_type>:]<name>' or by href."
),
)


Expand Down
5 changes: 5 additions & 0 deletions pulpcore/cli/rpm/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PluginRequirement,
PulpContext,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
pass_pulp_context,
pass_repository_context,
Expand Down Expand Up @@ -42,6 +43,10 @@
default_plugin="rpm",
default_type="rpm",
context_table={"rpm:rpm": PulpRpmRemoteContext},
href_pattern=PulpRemoteContext.HREF_PATTERN,
help=_(
"Remote used for synching in the form '[[<plugin>:]<resource_type>:]<name>' or by href."
),
)


Expand Down
5 changes: 4 additions & 1 deletion tests/scripts/pulp_file/test_repository.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ expect_succ pulp file repository update --name "cli_test_file_repo" --remote "fi
expect_succ pulp file repository show --name "cli_test_file_repo"
test "$(echo "$OUTPUT" | jq '.description')" = '"Test\nrepository"'
test "$(echo "$OUTPUT" | jq -r '.remote')" = "$REMOTE2_HREF"
expect_succ pulp file repository update --name "cli_test_file_repo" --remote "$REMOTE1_HREF"
expect_succ pulp file repository show --name "cli_test_file_repo"
test "$(echo "$OUTPUT" | jq -r '.remote')" = "$REMOTE1_HREF"
expect_succ pulp file repository update --name "cli_test_file_repo" --remote ""
expect_succ pulp file repository show --name "cli_test_file_repo"
test "$(echo "$OUTPUT" | jq -r '.remote')" = "null"
expect_succ pulp file repository list
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')" = "5"
test "$(echo "$OUTPUT" | jq -r '.|length')" = "6"

if [ "$(pulp debug has-plugin --name "file" --min-version "1.7.0")" = "true" ]
then
Expand Down
9 changes: 5 additions & 4 deletions tests/scripts/pulpcore/test_pulpexporter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ expect_succ pulp file remote create --name $RMOTE --url "$FILE_REMOTE_URL"
expect_succ pulp file repository create --name $REPO1
expect_succ pulp file repository sync --name $REPO1 --remote $RMOTE
expect_succ pulp file repository create --name $REPO2
REPO1_HREF="$(pulp file repository show --name $REPO1 | jq -r '.pulp_href')"

# Test create
expect_succ pulp exporter pulp create --name $NAME1 --path $PATH1 --repository file:file:$REPO1 --repository file:file:$REPO2
expect_succ pulp exporter pulp create --name $NAME2 --path $PATH1 --repository file:file:$REPO1
expect_succ pulp exporter pulp create --name $NAME2 --path $PATH1 --repository "$REPO1_HREF"

# Test read
expect_succ pulp exporter pulp show --name $NAME1
Expand All @@ -52,14 +53,14 @@ expect_succ pulp export pulp run --exporter $NAME1 --full False
expect_succ pulp export pulp run --exporter $NAME1 --full True --chunk-size 5KB
expect_succ pulp export pulp run --exporter $NAME2 --full False --start-versions file:file:${REPO1} 0
expect_succ pulp export pulp run --exporter $NAME2 --full False --versions file:file:${REPO1} 0
expect_succ pulp export pulp run --exporter $NAME2 --full False --start-versions file:file:${REPO1} 0 --versions file:file:${REPO1} 1
expect_succ pulp export pulp run --exporter $NAME2 --full False --start-versions file:file:${REPO1} 0 --versions file:file:${REPO1} 1 --chunk-size 5KB
expect_succ pulp export pulp run --exporter $NAME2 --full False --start-versions file:file:${REPO1} 0 --versions "${REPO1_HREF}" 1
expect_succ pulp export pulp run --exporter $NAME2 --full False --start-versions "${REPO1_HREF}" 0 --versions file:file:${REPO1} 1 --chunk-size 5KB

# Test list exports
expect_succ pulp export pulp list --exporter $NAME1

# Test delete export
HREF=$(pulp export pulp list --exporter $NAME1 | jq -r '.[0].pulp_href')
HREF=$(pulp export pulp list --exporter $NAME1 | jq -r '.[0].pulp_href')
expect_succ pulp export pulp destroy --href "$HREF"

# Test delete exporter
Expand Down

0 comments on commit 8182d69

Please sign in to comment.