Skip to content

Commit d262e03

Browse files
authored
Verify profile variables have been replaced (#264)
1 parent 6603b30 commit d262e03

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

samtranslator/model/connector_profiles/profile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ def get_profile(source_type: str, dest_type: str):
1313
return PROFILE["Permissions"].get(source_type, {}).get(dest_type)
1414

1515

16+
def verify_profile_variables_replaced(obj: Any) -> None:
17+
"""
18+
Verifies all profile variables have been replaced; throws ValueError if not.
19+
"""
20+
s = json.dumps(obj)
21+
matches = re.findall(r"%{[\w\.]+}", s)
22+
if matches:
23+
raise ValueError(f"The following variables have not been replaced: {matches}")
24+
25+
1626
def profile_replace(obj: Any, replacements: Dict[str, Any]):
1727
"""
1828
This function is used to recursively replace all keys in 'replacements' found

samtranslator/model/sam_resources.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ConnectorProfile,
1414
profile_replace,
1515
get_profile,
16+
verify_profile_variables_replaced,
1617
)
1718

1819
import samtranslator.model.eventsources
@@ -1694,6 +1695,8 @@ def to_cloudformation(self, **kwargs) -> List:
16941695
except ValueError as e:
16951696
raise InvalidResourceException(self.logical_id, str(e))
16961697

1698+
verify_profile_variables_replaced(profile_properties)
1699+
16971700
generated_resources: List[Resource] = []
16981701
if profile_type == "AWS_IAM_ROLE_MANAGED_POLICY":
16991702
generated_resources.append(

tests/model/connector_profiles/test_profile.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from unittest import TestCase
22

3-
from samtranslator.model.connector_profiles.profile import profile_replace
3+
from parameterized import parameterized
4+
5+
from samtranslator.model.connector_profiles.profile import profile_replace, verify_profile_variables_replaced
46

57

68
class TestProfile(TestCase):
@@ -215,3 +217,21 @@ def test_profile_replace_dict_input(self):
215217
}
216218
},
217219
)
220+
221+
def test_verify_replaced(self):
222+
verify_profile_variables_replaced({"Foo": {"Bar": "${AllGood}something What"}})
223+
verify_profile_variables_replaced({"Foo": {"Bar": "${All.Good}something %{What"}})
224+
verify_profile_variables_replaced({"Foo": {"${AllGood}": "something %{What"}})
225+
226+
@parameterized.expand(
227+
[
228+
({"Foo": {"Bar": "%{NotGood}something What"}}, "%{NotGood}"),
229+
({"Foo": {"Bar": "%{Not.Good}something What"}}, "%{Not.Good}"),
230+
({"Foo": {"%{NotGood}": "something What"}}, "%{NotGood}"),
231+
({"Foo": {"%{NotGood}": "something %{What.No}"}}, "['%{NotGood}', '%{What.No}']"),
232+
]
233+
)
234+
def test_verify_not_replaced(self, profile, error_includes):
235+
with self.assertRaises(ValueError) as ctx:
236+
verify_profile_variables_replaced(profile)
237+
self.assertIn(error_includes, str(ctx.exception))

0 commit comments

Comments
 (0)