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

[Resolve #390] Cast all parameters to strings #401

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 16 additions & 3 deletions sceptre/plan/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,15 +639,28 @@ def _format_parameters(self, parameters):
for name, value in parameters.items():
if value is None:
continue
if isinstance(value, list):
value = ",".join(value)
formatted_parameters.append({
"ParameterKey": name,
"ParameterValue": value
"ParameterValue": self._format_value(value)
})

return formatted_parameters

def _format_value(self, value):
"""
Converts CloudFormation value to the format used by Boto3.
"""
if isinstance(value, list):
# recurse
new_list = [self._format_value(item) for item in value]
return ",".join(new_list)
if value is True:
return "true"
elif value is False:
return "false"
else:
return str(value)

def _get_role_arn(self):
"""
Returns the Role ARN assumed by CloudFormation when building a Stack.
Expand Down
56 changes: 50 additions & 6 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,7 @@ def test_format_parameters_with_none_values(self):
"key2": None,
"key3": None
}
formatted_parameters = self.actions._format_parameters(parameters)
sorted_formatted_parameters = sorted(
formatted_parameters,
key=lambda x: x["ParameterKey"]
)
assert sorted_formatted_parameters == []
assert self.actions._format_parameters(parameters) == []

def test_format_parameters_with_none_and_string_values(self):
parameters = {
Expand All @@ -749,6 +744,38 @@ def test_format_parameters_with_none_and_string_values(self):
{"ParameterKey": "key3", "ParameterValue": "value3"}
]

def test_format_parameters_with_number_values(self):
parameters = {
"key1": 0.0,
"key2": 2.349520,
"key3": 2173540618439890,
}
formatted_parameters = self.actions._format_parameters(parameters)
sorted_formatted_parameters = sorted(
formatted_parameters,
key=lambda x: x["ParameterKey"]
)
assert sorted_formatted_parameters == [
{"ParameterKey": "key1", "ParameterValue": "0.0"},
{"ParameterKey": "key2", "ParameterValue": "2.34952"},
{"ParameterKey": "key3", "ParameterValue": "2173540618439890"}
]

def test_format_parameters_with_boolean_values(self):
parameters = {
"key1": True,
"key2": False
}
formatted_parameters = self.actions._format_parameters(parameters)
sorted_formatted_parameters = sorted(
formatted_parameters,
key=lambda x: x["ParameterKey"]
)
assert sorted_formatted_parameters == [
{"ParameterKey": "key1", "ParameterValue": "true"},
{"ParameterKey": "key2", "ParameterValue": "false"}
]

def test_format_parameters_with_list_values(self):
parameters = {
"key1": ["value1", "value2", "value3"],
Expand All @@ -766,6 +793,23 @@ def test_format_parameters_with_list_values(self):
{"ParameterKey": "key3", "ParameterValue": "value7,value8,value9"}
]

def test_format_parameters_with_list_values_containing_mixed_values(self):
parameters = {
"key1": ["value1", 2.3, True],
"key2": ["value4", 5123.58, False],
"key3": ["value7", 0, "value9"]
}
formatted_parameters = self.actions._format_parameters(parameters)
sorted_formatted_parameters = sorted(
formatted_parameters,
key=lambda x: x["ParameterKey"]
)
assert sorted_formatted_parameters == [
{"ParameterKey": "key1", "ParameterValue": "value1,2.3,true"},
{"ParameterKey": "key2", "ParameterValue": "value4,5123.58,false"},
{"ParameterKey": "key3", "ParameterValue": "value7,0,value9"}
]

def test_format_parameters_with_none_and_list_values(self):
parameters = {
"key1": ["value1", "value2", "value3"],
Expand Down