Skip to content

Commit

Permalink
Add href support to resource_option
Browse files Browse the repository at this point in the history
fixes #315
  • Loading branch information
mdellweg committed Aug 17, 2021
1 parent 2852cf9 commit bd897a6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 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.
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
4 changes: 4 additions & 0 deletions pulpcore/cli/file/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
default_plugin="file",
default_type="file",
context_table={"file:file": PulpFileRemoteContext},
href_pattern=r"^/pulp/api/v3/remotes/(?P<plugin>\w+)/(?P<resource_type>\w+)/",
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 @@ -26,6 +26,9 @@ expect_succ pulp file repository update --name "cli_test_file_repo" --remote "fi
expect_succ pulp file repository update --name "cli_test_file_repo" --remote "file::cli:test:file:remote2"
expect_succ pulp file repository show --name "cli_test_file_repo"
expect_succ 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"
expect_succ 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"
expect_succ test "$(echo "$OUTPUT" | jq -r '.description')" = "null"
Expand All @@ -34,7 +37,7 @@ 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

0 comments on commit bd897a6

Please sign in to comment.