Skip to content

Commit

Permalink
Cast all parameters to strings
Browse files Browse the repository at this point in the history
boto3 only supports strings for parameters, even if the parameter itself is non a string type
On branch support-non-string-type-parameters - Wed 13 Jun 2018 13:25:46 PDT by Geoff Lee <geofflee25@gmail.com>
  • Loading branch information
matrinox committed Jul 11, 2019
1 parent 2b4b78e commit 8bc6895
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
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
39 changes: 33 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.stack._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.stack._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.stack._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 Down

0 comments on commit 8bc6895

Please sign in to comment.