Skip to content

Commit

Permalink
refactor: changing find resources to find nodes (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis-Henrique authored Nov 29, 2023
1 parent a6da2d3 commit 2023b58
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class CFLoader(yaml.SafeLoader):
pass


class ResourceType(Enum):
class NodeType(Enum):
API_GATEWAY = "AWS::Serverless::Api"
LAMBDA = "AWS::Serverless::Function"
API = "Api"


def multi_constructor(loader: CFLoader, tag_suffix: str, node: yaml.nodes.Node) -> Dict[str, Any]:
Expand Down Expand Up @@ -77,11 +78,11 @@ def load(template: Optional[str] = None) -> Dict[str, Any]:
return yaml.load(fp, CFLoader)


def find_resources(template: Dict[str, Any], resource_type: ResourceType) -> List[Dict[str, Any]]:
resources = []
def find_nodes(tree: Dict[str, Any], node_type: NodeType) -> List[Dict[str, Any]]:
nodes = []

for id_, properties in template["Resources"].items():
if properties["Type"] == resource_type.value:
resources.append({id_: properties})
for id_, properties in tree.items():
if properties["Type"] == node_type.value:
nodes.append({id_: properties})

return resources
return nodes
10 changes: 5 additions & 5 deletions tests/test_cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def test_load_raises_exception(self):
with self.assertRaisesRegex(cf.CFTemplateNotFound, regex):
cf.load(template)

def test_find_resources(self):
template = cf.load("tests/fixtures/templates/example1.yml")
resources = cf.find_resources(template, cf.ResourceType.LAMBDA)
def test_find_nodes(self):
tree = cf.load("tests/fixtures/templates/example1.yml")
nodes = cf.find_nodes(tree["Resources"], cf.NodeType.LAMBDA)

expected_resources = [
expected_nodes = [
{
"HelloWorldFunction": {
"Type": "AWS::Serverless::Function",
Expand All @@ -110,4 +110,4 @@ def test_find_resources(self):
}
]

self.assertEqual(resources, expected_resources)
self.assertEqual(nodes, expected_nodes)

0 comments on commit 2023b58

Please sign in to comment.