Skip to content

Commit

Permalink
Adding support for default values in FindInMap
Browse files Browse the repository at this point in the history
  • Loading branch information
the-jules authored and markpeek committed Oct 21, 2023
1 parent 7090af9 commit 7533fba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
37 changes: 37 additions & 0 deletions tests/test_findinmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest

from troposphere import BaseAWSObject, FindInMap

expected_find_in_map = {"Fn::FindInMap": ["m", "t", "s"]}

expected_find_in_map_with_default = {
"Fn::FindInMap": ["m", "t", "s", {"DefaultValue": "d"}]
}

map_object = BaseAWSObject(title="m")


class TestFindInMap(unittest.TestCase):
def test_find_in_map(self):
find_in_map = FindInMap(mapname="m", toplevelkey="t", secondlevelkey="s")
self.assertEqual(find_in_map.to_dict(), expected_find_in_map)

def test_find_in_map_with_object(self):
find_in_map = FindInMap(mapname=map_object, toplevelkey="t", secondlevelkey="s")
self.assertEqual(find_in_map.to_dict(), expected_find_in_map)

def test_find_in_map_with_default(self):
find_in_map = FindInMap(
mapname="m", toplevelkey="t", secondlevelkey="s", defaultvalue="d"
)
self.assertEqual(find_in_map.to_dict(), expected_find_in_map_with_default)

def test_find_in_map_with_object_and_default(self):
find_in_map = FindInMap(
mapname=map_object, toplevelkey="t", secondlevelkey="s", defaultvalue="d"
)
self.assertEqual(find_in_map.to_dict(), expected_find_in_map_with_default)


if __name__ == "__main__":
unittest.main()
15 changes: 11 additions & 4 deletions troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,18 @@ def __init__(self, data: Any) -> None:

class FindInMap(AWSHelperFn):
def __init__(
self, mapname: object, toplevelkey: object, secondlevelkey: object
self,
mapname: object,
toplevelkey: object,
secondlevelkey: object,
defaultvalue: Optional[object] = None,
) -> None:
self.data = {
"Fn::FindInMap": [self.getdata(mapname), toplevelkey, secondlevelkey]
}
find_in_map_details = [self.getdata(mapname), toplevelkey, secondlevelkey]

if defaultvalue is not None:
find_in_map_details.append({"DefaultValue": defaultvalue})

self.data = {"Fn::FindInMap": find_in_map_details}


class GetAtt(AWSHelperFn):
Expand Down

0 comments on commit 7533fba

Please sign in to comment.