def resolve_parameter_refs()

in samtranslator/intrinsics/actions.py [0:0]


    def resolve_parameter_refs(self, input_dict: Optional[Any], parameters: Dict[str, Any]) -> Optional[Any]:
        """
        Recursively resolves "Fn::FindInMap"references that are present in the mappings and returns the value.
        If it is not in mappings, this method simply returns the input unchanged.

        :param input_dict: Dictionary representing the FindInMap function. Must contain only one key and it
                           should be "Fn::FindInMap".

        :param parameters: Dictionary of mappings from the SAM template
        """
        if input_dict is None or not self.can_handle(input_dict):
            return input_dict

        value = input_dict[self.intrinsic_name]

        # FindInMap expects an array with 3 values
        if not isinstance(value, list) or len(value) != self._NUM_ARGUMENTS:
            raise InvalidDocumentException(
                [
                    InvalidTemplateException(
                        f"Invalid FindInMap value {value}. FindInMap expects an array with {self._NUM_ARGUMENTS} values."
                    )
                ]
            )

        map_name = self.resolve_parameter_refs(value[0], parameters)
        top_level_key = self.resolve_parameter_refs(value[1], parameters)
        second_level_key = self.resolve_parameter_refs(value[2], parameters)

        if not all(isinstance(key, str) for key in [map_name, top_level_key, second_level_key]):
            return input_dict

        invalid_2_level_map_exception = InvalidDocumentException(
            [
                InvalidTemplateException(
                    f"Cannot use {self.intrinsic_name} on Mapping '{map_name}' which is not a a two-level map."
                )
            ]
        )

        # We should be able to use dict_deep_get() if
        # the behavior of missing key is return None instead of input_dict.
        if map_name not in parameters:
            return input_dict

        if not isinstance(parameters[map_name], dict):
            raise invalid_2_level_map_exception
        if top_level_key not in parameters[map_name]:
            return input_dict

        if not isinstance(parameters[map_name][top_level_key], dict):
            raise invalid_2_level_map_exception
        if second_level_key not in parameters[map_name][top_level_key]:
            return input_dict

        return parameters[map_name][top_level_key][second_level_key]