Skip to content

Commit

Permalink
Add validation to file content json parameters
Browse files Browse the repository at this point in the history
fixes pulp#255
  • Loading branch information
mdellweg committed Jul 16, 2021
1 parent 7dd0877 commit 5cd7fff
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES/255.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added validation to some json input parameters.
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ ignore_missing_imports = True

[mypy-click_shell.*]
ignore_missing_imports = True

[mypy-schema.*]
ignore_missing_imports = True
30 changes: 22 additions & 8 deletions pulpcore/cli/file/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, Dict, List, Optional

import click
import schema as s

from pulpcore.cli.common.context import (
EntityFieldDefinition,
Expand Down Expand Up @@ -59,6 +60,19 @@ def _content_callback(ctx: click.Context, param: click.Parameter, value: Any) ->
return value


CONTENT_LIST_SCHEMA = s.Schema([{"sha256": str, "relative_path": str}])


def _content_list_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any:
result = load_json_callback(ctx, param, value)
if result is None:
return None
try:
return CONTENT_LIST_SCHEMA.validate(result)
except s.SchemaError as e:
raise click.ClickException("Validation of '{}' failed: {}".format(param.name, str(e)))


@click.group()
@click.option(
"-t",
Expand Down Expand Up @@ -104,19 +118,19 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
modify_options = [
click.option(
"--add-content",
callback=content_json_callback,
callback=_content_list_callback,
help=_(
"""JSON string with a list of objects to add to the repository.
Each object should consist of the following keys: "sha256", "relative_path"..
Each object must contain the following keys: "sha256", "relative_path".
The argument prefixed with the '@' can be the path to a JSON file with a list of objects."""
),
),
click.option(
"--remove-content",
callback=content_json_callback,
callback=_content_list_callback,
help=_(
"""JSON string with a list of objects to remove from the repository.
Each object should consist of the following keys: "sha256", "relative_path"..
Each object must contain the following keys: "sha256", "relative_path".
The argument prefixed with the '@' can be the path to a JSON file with a list of objects."""
),
),
Expand Down Expand Up @@ -247,22 +261,22 @@ def remove(
@click.option(
"--add-content",
default="[]",
callback=load_json_callback,
callback=_content_list_callback,
expose_value=True,
help=_(
"""JSON string with a list of objects to add to the repository.
Each object should consist of the following keys: "sha256", "relative_path"..
Each object must contain the following keys: "sha256", "relative_path".
The argument prefixed with the '@' can be the path to a JSON file with a list of objects."""
),
)
@click.option(
"--remove-content",
default="[]",
callback=load_json_callback,
callback=_content_list_callback,
expose_value=True,
help=_(
"""JSON string with a list of objects to remove from the repository.
Each object should consist of the following keys: "sha256", "relative_path"..
Each object must contain the following keys: "sha256", "relative_path".
The argument prefixed with the '@' can be the path to a JSON file with a list of objects."""
),
)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"click>=7.1.2,<9.0.0",
"packaging",
"PyYAML~=5.3",
"schema==0.7.4",
"requests~=2.24",
"toml==0.10.2",
],
Expand Down

0 comments on commit 5cd7fff

Please sign in to comment.