Skip to content

Commit

Permalink
Add validation for parameter types
Browse files Browse the repository at this point in the history
[noissue]

Co-authored-by: Grant Gainey <[email protected]>
  • Loading branch information
mdellweg and ggainey committed Oct 4, 2022
1 parent 95588de commit 29fb074
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 55 deletions.
11 changes: 8 additions & 3 deletions pulpcore/cli/ansible/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
pulp_group,
pulp_option,
repository_content_command,
repository_href_option,
repository_option,
resource_option,
retained_versions_option,
show_command,
Expand All @@ -43,6 +45,7 @@
)
from pulpcore.cli.common.i18n import get_translation
from pulpcore.cli.core.context import PulpSigningServiceContext
from pulpcore.cli.core.generic import task_command

translation = get_translation(__name__)
_ = translation.gettext
Expand Down Expand Up @@ -100,6 +103,7 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non


lookup_options = [href_option, name_option]
nested_lookup_options = [repository_href_option, repository_option]
create_options = [
click.option("--name", required=True),
click.option("--description"),
Expand Down Expand Up @@ -168,12 +172,13 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
]


repository.add_command(show_command(decorators=lookup_options))
repository.add_command(list_command(decorators=[label_select_option]))
repository.add_command(destroy_command(decorators=lookup_options))
repository.add_command(version_command())
repository.add_command(show_command(decorators=lookup_options))
repository.add_command(create_command(decorators=create_options))
repository.add_command(update_command(decorators=lookup_options + update_options))
repository.add_command(destroy_command(decorators=lookup_options))
repository.add_command(task_command(decorators=nested_lookup_options))
repository.add_command(version_command())
repository.add_command(label_command())
repository.add_command(
repository_content_command(
Expand Down
12 changes: 10 additions & 2 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,19 @@ def _type_callback(ctx: click.Context, param: click.Parameter, value: Optional[s

ordering_option = pulp_option(
"--ordering",
multiple=True,
required=False,
help=_("A field that will be used to order the results."),
help=_("A field that will be used to order the results. Can be specified multiple times."),
)

field_option = pulp_option(
"--field",
"fields",
multiple=True,
required=False,
help=_("A field that is to be selected from a result. Can be specified multiple times."),
)

exclude_field_option = pulp_option(
"--exclude-field",
"exclude_fields",
Expand Down Expand Up @@ -587,7 +590,8 @@ def _type_callback(ctx: click.Context, param: click.Parameter, value: Optional[s
name_in_option = pulp_option(
"--name-in",
"name__in",
help=_("Filter {entity} results where name is in comma separated list of values"),
multiple=True,
help=_("Filter {entity} by name. Can be specified multiple times"),
)

repository_href_option = click.option(
Expand Down Expand Up @@ -821,6 +825,10 @@ def callback(
"""
Show the list of optionally filtered {entities}.
"""
if "ordering" in kwargs:
# Workaround for missing ordering filter
if not kwargs["ordering"]:
kwargs["ordering"] = None
result = entity_ctx.list(limit=limit, offset=offset, parameters=kwargs)
pulp_ctx.output_result(result)

Expand Down
29 changes: 22 additions & 7 deletions pulpcore/cli/common/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import json
import os
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
from urllib.parse import urljoin

import requests
Expand Down Expand Up @@ -128,12 +128,12 @@ def extract_params(
method_spec: Dict[str, Any],
params: Dict[str, Any],
) -> Dict[str, Any]:
param_spec = {
param_specs = {
entry["name"]: entry
for entry in path_spec.get("parameters", [])
if entry["in"] == param_type
}
param_spec.update(
param_specs.update(
{
entry["name"]: entry
for entry in method_spec.get("parameters", [])
Expand All @@ -142,11 +142,26 @@ def extract_params(
)
result: Dict[str, Any] = {}
for name in list(params.keys()):
if name in param_spec:
param_spec.pop(name)
result[name] = params.pop(name)
if name in param_specs:
param = params.pop(name)
param_spec = param_specs.pop(name)
style = param_spec.get(
"style", "form" if param_type in ("query", "cookie") else "simple"
)
param_schema = param_spec.get("schema")
if param_schema:
param_type = param_schema.get("type", "string")
if param_type == "array":
if not param:
continue
assert isinstance(param, Iterable) and not isinstance(param, str)
if not param_spec.get("explode", style == "form"):
param = ",".join(param)
elif param_type == "integer":
assert isinstance(param, int)
result[name] = param
remaining_required = [
item["name"] for item in param_spec.values() if item.get("required", False)
item["name"] for item in param_specs.values() if item.get("required", False)
]
if any(remaining_required):
raise RuntimeError(
Expand Down
5 changes: 4 additions & 1 deletion pulpcore/cli/container/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ def content(ctx: click.Context, pulp_ctx: PulpContext, content_type: str) -> Non

list_options = [
pulp_option("--media-type"),
pulp_option("--names", "name__in", allowed_with_contexts=(PulpContainerTagContext,)),
pulp_option("--name", "name", allowed_with_contexts=(PulpContainerTagContext,)),
pulp_option(
"--name-in", "name__in", multiple=True, allowed_with_contexts=(PulpContainerTagContext,)
),
]

show_options = [
Expand Down
5 changes: 5 additions & 0 deletions pulpcore/cli/container/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class PulpContainerTagContext(PulpContentContext):
HREF = "container_tag_href"
ID_PREFIX = "content_container_tags"

def find(self, **kwargs: Any) -> Any:
if "digest" in kwargs and isinstance(kwargs["digest"], str):
kwargs["digest"] = [kwargs["digest"]]
return super().find(**kwargs)


class PulpContainerNamespaceContext(PulpEntityContext):
ENTITY = _("container namespace")
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def cancel(self, task_href: str) -> Any:
@property
def scope(self) -> Dict[str, Any]:
if self.resource_context:
return {"reserved_resources_record": self.resource_context.pulp_href}
return {"reserved_resources_record": [self.resource_context.pulp_href]}
else:
return {}

Expand Down
2 changes: 1 addition & 1 deletion pulpcore/cli/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def role(ctx: click.Context, pulp_ctx: PulpContext, group_ctx: PulpGroupContext)
decorators=[
group_option,
click.option("--role"),
click.option("--role-in", "role__in"),
click.option("--role-in", "role__in", multiple=True),
click.option("--role-contains", "role__contains"),
click.option("--role-icontains", "role__icontains"),
click.option("--role-startswith", "role__startswith"),
Expand Down
18 changes: 2 additions & 16 deletions pulpcore/cli/core/repository.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

from pulpcore.cli.common.context import PulpContext, PulpRepositoryContext, pass_pulp_context
from pulpcore.cli.common.generic import list_command, pulp_group
from pulpcore.cli.common.generic import list_command, name_filter_options, pulp_group
from pulpcore.cli.common.i18n import get_translation

translation = get_translation(__name__)
Expand All @@ -21,20 +21,6 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext) -> None:
ctx.obj = PulpRepositoryContext(pulp_ctx)


filter_options = [
click.option("--name"),
click.option(
"--name-contains",
"name__contains",
),
click.option(
"--name-icontains",
"name__icontains",
),
click.option(
"--name-in",
"name__in",
),
]
filter_options = name_filter_options

repository.add_command(list_command(decorators=filter_options))
7 changes: 2 additions & 5 deletions pulpcore/cli/core/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
destroy_command,
href_option,
list_command,
name_filter_options,
name_option,
pulp_group,
show_command,
Expand All @@ -35,12 +36,8 @@ def _permission_callback(
return value or None


filters = [
filters = name_filter_options + [
click.option("--locked/--unlocked", default=None),
click.option("--name"),
click.option("--name-in", "name__in"),
click.option("--name-contains", "name__contains"),
click.option("--name-icontains", "name__icontains"),
click.option("--name-startswith", "name__startswith"),
]
lookup_options = [href_option, name_option]
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 @@ -85,7 +85,7 @@ def role(ctx: click.Context, pulp_ctx: PulpContext, user_ctx: PulpUserContext) -
decorators=[
username_option,
click.option("--role"),
click.option("--role-in", "role__in"),
click.option("--role-in", "role__in", multiple=True),
click.option("--role-contains", "role__contains"),
click.option("--role-icontains", "role__icontains"),
click.option("--role-startswith", "role__startswith"),
Expand Down
47 changes: 35 additions & 12 deletions pulpcore/cli/rpm/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,31 @@ def content() -> None:
pulp_option("--fields"),
pulp_option("--repository-version"),
pulp_option("--arch", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--arch-in", "arch__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option(
"--arch-in", "arch__in", multiple=True, allowed_with_contexts=(PulpRpmPackageContext,)
),
pulp_option("--arch-ne", "arch__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--epoch", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--epoch-in", "epoch__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option(
"--epoch-in", "epoch__in", multiple=True, allowed_with_contexts=(PulpRpmPackageContext,)
),
pulp_option("--epoch-ne", "epoch__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--id", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmAdvisoryContext)),
pulp_option("--id", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--id-in", "id__in", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmAdvisoryContext)
"--id-in", "id__in", multiple=True, allowed_with_contexts=(PulpRpmAdvisoryContext,)
),
pulp_option("--module", allowed_with_contexts=(PulpRpmModulemdDefaultsContext,)),
pulp_option(
"--module-in", "module__in", allowed_with_contexts=(PulpRpmModulemdDefaultsContext,)
"--module-in",
"module__in",
multiple=True,
allowed_with_contexts=(PulpRpmModulemdDefaultsContext,),
),
pulp_option("--name", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext)),
pulp_option(
"--name-in",
"name__in",
multiple=True,
allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext),
),
pulp_option(
Expand All @@ -136,17 +144,25 @@ def content() -> None:
allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext),
),
pulp_option("--package-href", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--pkgId", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext)),
pulp_option("--pkgId", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option(
"--pkgId-in",
"pkgId__in",
allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext),
multiple=True,
allowed_with_contexts=(PulpRpmPackageContext,),
),
pulp_option("--release", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--release-in", "release__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option(
"--release-in", "release__in", multiple=True, allowed_with_contexts=(PulpRpmPackageContext,)
),
pulp_option("--release-ne", "release__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--severity", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--severity-in", "severity__in", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--severity-in",
"severity__in",
multiple=True,
allowed_with_contexts=(PulpRpmAdvisoryContext,),
),
pulp_option("--severity-ne", "severity__ne", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--sha256",
Expand All @@ -157,21 +173,28 @@ def content() -> None:
),
),
pulp_option("--status", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--status-in", "status__in", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--status-in", "status__in", multiple=True, allowed_with_contexts=(PulpRpmAdvisoryContext,)
),
pulp_option("--status-ne", "status__ne", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--stream", allowed_with_contexts=(PulpRpmModulemdDefaultsContext, PulpRpmModulemdContext)
),
pulp_option(
"--stream-in",
"stream__in",
multiple=True,
allowed_with_contexts=(PulpRpmModulemdDefaultsContext, PulpRpmModulemdContext),
),
pulp_option("--type", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--type-in", "type__in", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--type-in", "type__in", multiple=True, allowed_with_contexts=(PulpRpmAdvisoryContext,)
),
pulp_option("--type-ne", "type__ne", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--version", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--version-in", "version__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option(
"--version-in", "version__in", multiple=True, allowed_with_contexts=(PulpRpmPackageContext,)
),
pulp_option("--version-ne", "version__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
]
lookup_options = [
Expand Down
18 changes: 12 additions & 6 deletions tests/scripts/pulp_rpm/test_content.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
pulp debug has-plugin --name "rpm" || exit 3

TEST_ADVISORY="$(dirname "$(realpath "$0")")"/test_advisory.json
RPM_NAME="lemon-0-1.noarch.rpm"
RPM2_NAME="icecubes-2-3.noarch.rpm"
RPM_FILENAME="lemon-0-1.noarch.rpm"
RPM2_FILENAME="icecubes-2-3.noarch.rpm"
RPM_NAME="lemon"
RPM2_NAME="icecubes"
REPO1_NAME="cli_test_rpm"
REPO2_NAME="cli_test_modular"
PACKAGE_HREF=
Expand All @@ -24,8 +26,8 @@ trap cleanup EXIT
cleanup

# Test rpm package upload
wget --no-check-certificate "${RPM_WEAK_DEPS_URL}/${RPM_NAME}"
expect_succ pulp rpm content upload --file "${RPM_NAME}" --relative-path "${RPM_NAME}"
wget --no-check-certificate "${RPM_WEAK_DEPS_URL}/${RPM_FILENAME}"
expect_succ pulp rpm content upload --file "${RPM_FILENAME}" --relative-path "${RPM_FILENAME}"
PACKAGE_HREF=$(echo "${OUTPUT}" | jq -r .pulp_href)
expect_succ pulp rpm content show --href "${PACKAGE_HREF}"

Expand Down Expand Up @@ -62,8 +64,8 @@ expect_succ pulp rpm repository content modify \
--repository "${REPO1_NAME}" \
--remove-content "[{\"pulp_href\": \"${PACKAGE_HREF}\"}]"

wget --no-check-certificate "${RPM_WEAK_DEPS_URL}/${RPM2_NAME}"
expect_succ pulp rpm content upload --file "${RPM2_NAME}" --relative-path "${RPM2_NAME}" --repository "${REPO1_NAME}"
wget --no-check-certificate "${RPM_WEAK_DEPS_URL}/${RPM2_FILENAME}"
expect_succ pulp rpm content upload --file "${RPM2_FILENAME}" --relative-path "${RPM2_FILENAME}" --repository "${REPO1_NAME}"
expect_succ pulp rpm repository content list --repository "${REPO1_NAME}"
test "$(echo "${OUTPUT}" | jq -r length)" -eq "1"

Expand Down Expand Up @@ -120,6 +122,10 @@ do
fi
done

expect_succ pulp rpm content list --name-in "${RPM_NAME}" --name-in "${RPM2_NAME}"
pulp rpm content list
expect_succ test "$(echo "${OUTPUT}" | jq -r 'length')" -eq 2

# test upload for advisory, package-upload is tested at the start
expect_succ pulp rpm content -t advisory upload --file "${TEST_ADVISORY}"
ADVISORY_HREF=$(echo "${OUTPUT}" | jq -r .pulp_href)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source

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

cleanup() {
pulp file repository destroy --name aaaaaaaaaa || true
pulp file repository destroy --name bbbbbbbbbb || true
Expand Down
Loading

0 comments on commit 29fb074

Please sign in to comment.