|
11 | 11 | from samtranslator.model.lambda_ import LambdaPermission
|
12 | 12 | from samtranslator.model.events import EventsRule
|
13 | 13 | from samtranslator.model.iot import IotTopicRule
|
| 14 | +from samtranslator.model.cognito import CognitoUserPool |
14 | 15 | from samtranslator.translator.arn_generator import ArnGenerator
|
15 | 16 | from samtranslator.model.exceptions import InvalidEventException, InvalidResourceException
|
16 | 17 | from samtranslator.swagger.swagger import SwaggerEditor
|
@@ -42,14 +43,17 @@ class PushEventSource(ResourceMacro):
|
42 | 43 | """
|
43 | 44 | principal = None
|
44 | 45 |
|
45 |
| - def _construct_permission(self, function, source_arn=None, source_account=None, suffix="", event_source_token=None): |
| 46 | + def _construct_permission( |
| 47 | + self, function, source_arn=None, source_account=None, suffix="", event_source_token=None, prefix=None): |
46 | 48 | """Constructs the Lambda Permission resource allowing the source service to invoke the function this event
|
47 | 49 | source triggers.
|
48 | 50 |
|
49 | 51 | :returns: the permission resource
|
50 | 52 | :rtype: model.lambda_.LambdaPermission
|
51 | 53 | """
|
52 |
| - lambda_permission = LambdaPermission(self.logical_id + 'Permission' + suffix, |
| 54 | + if prefix is None: |
| 55 | + prefix = self.logical_id |
| 56 | + lambda_permission = LambdaPermission(prefix + 'Permission' + suffix, |
53 | 57 | attributes=function.get_passthrough_resource_attributes())
|
54 | 58 |
|
55 | 59 | try:
|
@@ -741,3 +745,76 @@ def _construct_iot_rule(self, function):
|
741 | 745 | rule.set_resource_attribute(CONDITION, function.resource_attributes[CONDITION])
|
742 | 746 |
|
743 | 747 | return rule
|
| 748 | + |
| 749 | + |
| 750 | +class Cognito(PushEventSource): |
| 751 | + resource_type = 'Cognito' |
| 752 | + principal = 'cognito-idp.amazonaws.com' |
| 753 | + |
| 754 | + property_types = { |
| 755 | + 'UserPool': PropertyType(True, is_str()), |
| 756 | + 'Trigger': PropertyType(True, one_of(is_str(), list_of(is_str()))) |
| 757 | + } |
| 758 | + |
| 759 | + def resources_to_link(self, resources): |
| 760 | + if isinstance(self.UserPool, dict) and 'Ref' in self.UserPool: |
| 761 | + userpool_id = self.UserPool['Ref'] |
| 762 | + if userpool_id in resources: |
| 763 | + return { |
| 764 | + 'userpool': resources[userpool_id], |
| 765 | + 'userpool_id': userpool_id |
| 766 | + } |
| 767 | + raise InvalidEventException( |
| 768 | + self.relative_id, |
| 769 | + "Cognito events must reference a Cognito UserPool in the same template.") |
| 770 | + |
| 771 | + def to_cloudformation(self, **kwargs): |
| 772 | + function = kwargs.get('function') |
| 773 | + |
| 774 | + if not function: |
| 775 | + raise TypeError("Missing required keyword argument: function") |
| 776 | + |
| 777 | + if 'userpool' not in kwargs or kwargs['userpool'] is None: |
| 778 | + raise TypeError("Missing required keyword argument: userpool") |
| 779 | + |
| 780 | + if 'userpool_id' not in kwargs or kwargs['userpool_id'] is None: |
| 781 | + raise TypeError("Missing required keyword argument: userpool_id") |
| 782 | + |
| 783 | + userpool = kwargs['userpool'] |
| 784 | + userpool_id = kwargs['userpool_id'] |
| 785 | + |
| 786 | + resources = [] |
| 787 | + resources.append( |
| 788 | + self._construct_permission( |
| 789 | + function, event_source_token=self.UserPool, prefix=function.logical_id + "Cognito")) |
| 790 | + |
| 791 | + self._inject_lambda_config(function, userpool) |
| 792 | + resources.append(CognitoUserPool.from_dict(userpool_id, userpool)) |
| 793 | + return resources |
| 794 | + |
| 795 | + def _inject_lambda_config(self, function, userpool): |
| 796 | + event_triggers = self.Trigger |
| 797 | + if isinstance(self.Trigger, string_types): |
| 798 | + event_triggers = [self.Trigger] |
| 799 | + |
| 800 | + # TODO can these be conditional? |
| 801 | + |
| 802 | + properties = userpool.get('Properties', None) |
| 803 | + if properties is None: |
| 804 | + properties = {} |
| 805 | + userpool['Properties'] = properties |
| 806 | + |
| 807 | + lambda_config = properties.get('LambdaConfig', None) |
| 808 | + if lambda_config is None: |
| 809 | + lambda_config = {} |
| 810 | + properties['LambdaConfig'] = lambda_config |
| 811 | + |
| 812 | + for event_trigger in event_triggers: |
| 813 | + if event_trigger not in lambda_config: |
| 814 | + lambda_config[event_trigger] = function.get_runtime_attr("arn") |
| 815 | + else: |
| 816 | + raise InvalidEventException( |
| 817 | + self.relative_id, |
| 818 | + 'Cognito trigger "{trigger}" defined multiple times.'.format( |
| 819 | + trigger=self.Trigger)) |
| 820 | + return userpool |
0 commit comments