44
55import logging
66from pathlib import Path
7- from typing import Any , Dict , Iterator , List , Optional , cast
7+ from typing import Any , Dict , Iterator , List , Optional , Tuple , cast
88
99from samtranslator .policy_template_processor .exceptions import TemplateNotFoundException
1010
@@ -45,6 +45,7 @@ def __init__(
4545 use_raw_codeuri : bool = False ,
4646 ignore_code_extraction_warnings : bool = False ,
4747 locate_layer_nested : bool = False ,
48+ function_logical_ids : Optional [Tuple [str , ...]] = None ,
4849 ) -> None :
4950 """
5051 Initialize the class with SAM template data. The SAM template passed to this provider is assumed
@@ -61,16 +62,18 @@ def __init__(
6162 Note(xinhol): use_raw_codeuri is temporary to fix a bug, and will be removed for a permanent solution.
6263 :param bool ignore_code_extraction_warnings: Ignores Log warnings
6364 :param bool locate_layer_nested: resolved nested layer reference to their actual location in the nested stack
65+ :param tuple function_logical_ids: Optional tuple of function logical IDs to filter by
6466 """
6567
6668 self ._stacks = stacks
69+ self ._function_logical_ids = function_logical_ids
6770
6871 for stack in stacks :
6972 LOG .debug ("%d resources found in the stack %s" , len (stack .resources ), stack .stack_path )
7073
7174 # Store a map of function full_path to function information for quick reference
7275 self .functions = SamFunctionProvider ._extract_functions (
73- self ._stacks , use_raw_codeuri , ignore_code_extraction_warnings , locate_layer_nested
76+ self ._stacks , use_raw_codeuri , ignore_code_extraction_warnings , locate_layer_nested , function_logical_ids
7477 )
7578
7679 self ._colored = Colored ()
@@ -90,6 +93,7 @@ def update(
9093 use_raw_codeuri : bool = False ,
9194 ignore_code_extraction_warnings : bool = False ,
9295 locate_layer_nested : bool = False ,
96+ function_logical_ids : Optional [Tuple [str , ...]] = None ,
9397 ) -> None :
9498 """
9599 Hydrate the function provider with updated stacks
@@ -98,10 +102,11 @@ def update(
98102 Note(xinhol): use_raw_codeuri is temporary to fix a bug, and will be removed for a permanent solution.
99103 :param bool ignore_code_extraction_warnings: Ignores Log warnings
100104 :param bool locate_layer_nested: resolved nested layer reference to their actual location in the nested stack
105+ :param tuple function_logical_ids: Optional tuple of function logical IDs to filter by
101106 """
102107 self ._stacks = stacks
103108 self .functions = SamFunctionProvider ._extract_functions (
104- self ._stacks , use_raw_codeuri , ignore_code_extraction_warnings , locate_layer_nested
109+ self ._stacks , use_raw_codeuri , ignore_code_extraction_warnings , locate_layer_nested , function_logical_ids
105110 )
106111
107112 def get (self , name : str ) -> Optional [Function ]:
@@ -180,12 +185,29 @@ def get_all(self) -> Iterator[Function]:
180185 for _ , function in self .functions .items ():
181186 yield function
182187
188+ @staticmethod
189+ def _should_include_function (function : Function , function_logical_ids : set ) -> bool :
190+ """
191+ Determines if a function should be included based on the filter.
192+ Matches against function_id, name, and functionname.
193+
194+ :param function: Function object to check
195+ :param function_logical_ids: Set of function logical IDs to match against
196+ :return bool: True if function should be included, False otherwise
197+ """
198+ return bool (
199+ function .function_id in function_logical_ids
200+ or function .name in function_logical_ids
201+ or (function .functionname and function .functionname in function_logical_ids )
202+ )
203+
183204 @staticmethod
184205 def _extract_functions (
185206 stacks : List [Stack ],
186207 use_raw_codeuri : bool = False ,
187208 ignore_code_extraction_warnings : bool = False ,
188209 locate_layer_nested : bool = False ,
210+ function_logical_ids : Optional [Tuple [str , ...]] = None ,
189211 ) -> Dict [str , Function ]:
190212 """
191213 Extracts and returns function information from the given dictionary of SAM/CloudFormation resources. This
@@ -195,6 +217,7 @@ def _extract_functions(
195217 :param bool use_raw_codeuri: Do not resolve adjust core_uri based on the template path, use the raw uri.
196218 :param bool ignore_code_extraction_warnings: suppress log statements on code extraction from resources.
197219 :param bool locate_layer_nested: resolved nested layer reference to their actual location in the nested stack
220+ :param tuple function_logical_ids: Optional tuple of function logical IDs to filter by
198221 :return dict(string : samcli.commands.local.lib.provider.Function): Dictionary of function full_path to the
199222 Function configuration object
200223 """
@@ -271,6 +294,17 @@ def _extract_functions(
271294
272295 # We don't care about other resource types. Just ignore them
273296
297+ # Apply filtering if function_logical_ids provided
298+ if function_logical_ids :
299+ function_logical_ids_set = set (function_logical_ids )
300+ filtered_functions = {}
301+
302+ for full_path , function in result .items ():
303+ if SamFunctionProvider ._should_include_function (function , function_logical_ids_set ):
304+ filtered_functions [full_path ] = function
305+
306+ return filtered_functions
307+
274308 return result
275309
276310 @staticmethod
@@ -800,6 +834,7 @@ def __init__(
800834 global_parameter_overrides : Optional [Dict ] = None ,
801835 use_raw_codeuri : bool = False ,
802836 ignore_code_extraction_warnings : bool = False ,
837+ function_logical_ids : Optional [Tuple [str , ...]] = None ,
803838 ) -> None :
804839 """
805840 Initialize the class with SAM template data. The SAM template passed to this provider is assumed
@@ -815,9 +850,15 @@ def __init__(
815850 :param bool use_raw_codeuri: Do not resolve adjust core_uri based on the template path, use the raw uri.
816851 Note(xinhol): use_raw_codeuri is temporary to fix a bug, and will be removed for a permanent solution.
817852 :param bool ignore_code_extraction_warnings: Ignores Log warnings
853+ :param tuple function_logical_ids: Optional tuple of function logical IDs to filter by
818854 """
819855
820- super ().__init__ (stacks , use_raw_codeuri , ignore_code_extraction_warnings )
856+ # Store function_logical_ids before calling super().__init__
857+ self ._function_logical_ids = function_logical_ids
858+
859+ super ().__init__ (
860+ stacks , use_raw_codeuri , ignore_code_extraction_warnings , function_logical_ids = function_logical_ids
861+ )
821862
822863 # initialize root templates. Usually it will be one template, as sam commands only support processing
823864 # one template. These templates will be fixed.
@@ -909,6 +950,7 @@ def _watch_stack_templates(self, stacks: List[Stack]) -> None:
909950 def _refresh_loaded_functions (self ) -> None :
910951 """
911952 Reload the stacks, and lambda functions from template files.
953+ Applies the same function filter during refresh.
912954 """
913955 LOG .debug ("A change got detected in one of the stack templates. Reload the lambda function resources" )
914956 self ._stacks = []
@@ -925,8 +967,12 @@ def _refresh_loaded_functions(self) -> None:
925967 raise ex
926968
927969 self .is_changed = False
970+ # Pass function_logical_ids to maintain filter during refresh
928971 self .functions = self ._extract_functions (
929- self ._stacks , self ._use_raw_codeuri , self ._ignore_code_extraction_warnings
972+ self ._stacks ,
973+ self ._use_raw_codeuri ,
974+ self ._ignore_code_extraction_warnings ,
975+ function_logical_ids = self ._function_logical_ids ,
930976 )
931977 self ._watch_stack_templates (self ._stacks )
932978
0 commit comments