Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand parameter overrides #7876

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix json schema and add tests
  • Loading branch information
sigJoe committed Feb 19, 2025
commit af3c3e446f9ced7a3925104c78134471f6574e42
3 changes: 2 additions & 1 deletion samcli/cli/types.py
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ class CfnParameterOverridesType(click.ParamType):

ordered_pattern_match = [_pattern_1, _pattern_2]

name = "string,list"
name = "list,object,string"

def convert(self, values, param, ctx):
"""
@@ -178,6 +178,7 @@ def convert(self, values, param, ctx):
LOG.debug("Output parameters: %s", result)
return result


class CfnMetadataType(click.ParamType):
"""
Custom Click options type to accept values for metadata parameters.
39 changes: 36 additions & 3 deletions schema/make_schema.py
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ class SamCliParameterSchema:
type: Union[str, List[str]]
description: str = ""
default: Optional[Any] = None
items: Optional[str] = None
items: Optional[Union[str, Dict[str, str]]] = None
choices: Optional[Any] = None

def to_schema(self) -> Dict[str, Any]:
@@ -55,7 +55,11 @@ def to_schema(self) -> Dict[str, Any]:
if self.default:
param.update({"default": self.default})
if self.items:
param.update({"items": {"type": self.items}})
if isinstance(self.items, dict):
param.update({"items": self.items})
else:
param.update({"items": {"type": self.items}})

if self.choices:
if isinstance(self.choices, list):
self.choices.sort()
@@ -143,11 +147,15 @@ def format_param(param: click.core.Option) -> SamCliParameterSchema:
formatted_param_types.append(param_name or "string")
formatted_param_types = sorted(list(set(formatted_param_types))) # deduplicate

# Allow nested arrays of objects and strings
parameter_overrides_ref = { "$ref": "#/$defs/parameter_overrides_items" }

formatted_param: SamCliParameterSchema = SamCliParameterSchema(
param.name or "",
formatted_param_types if len(formatted_param_types) > 1 else formatted_param_types[0],
clean_text(param.help or ""),
items="string" if "array" in formatted_param_types else None,
items=parameter_overrides_ref if param.name == "parameter_overrides"
else "string" if "array" in formatted_param_types else None,
)

if param.default and param.name not in PARAMS_TO_OMIT_DEFAULT_FIELD:
@@ -231,6 +239,31 @@ def generate_schema() -> dict:
}
schema["required"] = ["version"]
schema["additionalProperties"] = False
# Allows objects and strings beneath variably nested arrays
schema["$defs"]= {
"parameter_overrides_items": {
"anyOf": [
{
"type": "array",
"items": { "$ref": "#/$defs/parameter_overrides_items" }
},
{
"type": "object",
"additionalProperties": {
"anyOf": [
{ "type": "string" },
{ "type": "integer" }, # Allow cooler types if it's a dict
{ "type": "boolean" },
]
},
},
{
"type": "string"
},
]
}
}

# Iterate through packages for command and parameter information
for package_name in _SAM_CLI_COMMAND_PACKAGES:
commands.extend(retrieve_command_structure(package_name))
55 changes: 47 additions & 8 deletions schema/samcli.json
Original file line number Diff line number Diff line change
@@ -13,6 +13,37 @@
"version"
],
"additionalProperties": false,
"$defs": {
"parameter_overrides_items": {
"anyOf": [
{
"type": "array",
"items": {
"$ref": "#/$defs/parameter_overrides_items"
}
},
{
"type": "object",
"additionalProperties": {
"anyOf": [
{
"type": "string"
},
{
"type": "integer"
},
{
"type": "boolean"
}
]
}
},
{
"type": "string"
}
]
}
},
"patternProperties": {
"^.+$": {
"title": "Environment",
@@ -346,11 +377,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"skip_pull_image": {
@@ -445,11 +477,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"debug_port": {
@@ -636,11 +669,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"debug_port": {
@@ -835,11 +869,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"debug_port": {
@@ -1225,11 +1260,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"signing_profiles": {
@@ -1699,11 +1735,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"beta_features": {
@@ -1982,11 +2019,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"stack_name": {
@@ -2111,11 +2149,12 @@
"title": "parameter_overrides",
"type": [
"array",
"object",
"string"
],
"description": "String that contains AWS CloudFormation parameter overrides encoded as key=value pairs.",
"items": {
"type": "string"
"$ref": "#/$defs/parameter_overrides_items"
}
},
"stack_name": {
1 change: 0 additions & 1 deletion tests/unit/cli/test_types.py
Original file line number Diff line number Diff line change
@@ -195,7 +195,6 @@ def mock_is_file(file_path):
self.assertEqual(result, expected, msg="Failed with Input = " + str(inputs))



class TestCfnMetadataType(TestCase):

def setUp(self):
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version = 0.1

[default.build.parameters]
parameter_overrides = [ 10 ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 0.1

default:
build:
parameters:
parameter_overrides:
- 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 0.1
prod-a:
deploy:
parameters:
stack_name: sam-app
region: us-east-1
parameter_overrides: &prod_a_overrides
- Name: prod-a
- ParamA: 1
- ParamB: True

prod-b:
deploy:
parameters:
stack_name: sam-app-2
region: us-east-1
parameter_overrides:
- *prod_a_overrides
- Name: prod-b
4 changes: 4 additions & 0 deletions tests/unit/schema/testdata/passing_tests/sample.toml
Original file line number Diff line number Diff line change
@@ -14,6 +14,10 @@ capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true

[default.deploy.parameters.parameter_overrides]
A = "foo"
B = "bar"

[default.sync.parameters]
watch = true