def add_lambda_integration(self,
                               path,
                               method,
                               integration_uri,
                               method_auth_config=None,
                               api_auth_config=None,
                               condition=None):
        """
        Adds aws_proxy APIGW integration to the given path+method.

        :param string path: Path name
        :param string method: HTTP Method
        :param string integration_uri: URI for the integration.
        """

        method = self._normalize_method_name(method)
        if self.has_integration(path, method):
            raise ValueError(
                "Lambda integration already exists on Path={}, Method={}".
                format(path, method))

        self.add_path(path, method)

        # Wrap the integration_uri in a Condition if one exists on that function
        # This is necessary so CFN doesn't try to resolve the integration reference.
        if condition:
            integration_uri = make_conditional(condition, integration_uri)

        path_dict = self.get_path(path)
        path_dict[method][self._X_APIGW_INTEGRATION] = {
            'type': 'aws_proxy',
            'httpMethod': 'POST',
            'uri': integration_uri
        }

        method_auth_config = method_auth_config or {}
        api_auth_config = api_auth_config or {}
        if method_auth_config.get('Authorizer') == 'AWS_IAM' \
           or api_auth_config.get('DefaultAuthorizer') == 'AWS_IAM' and not method_auth_config:
            method_invoke_role = method_auth_config.get('InvokeRole')
            if not method_invoke_role and 'InvokeRole' in method_auth_config:
                method_invoke_role = 'NONE'
            api_invoke_role = api_auth_config.get('InvokeRole')
            if not api_invoke_role and 'InvokeRole' in api_auth_config:
                api_invoke_role = 'NONE'
            credentials = self._generate_integration_credentials(
                method_invoke_role=method_invoke_role,
                api_invoke_role=api_invoke_role)
            if credentials and credentials != 'NONE':
                self.paths[path][method][
                    self._X_APIGW_INTEGRATION]['credentials'] = credentials

        # If 'responses' key is *not* present, add it with an empty dict as value
        path_dict[method].setdefault('responses', {})

        # If a condition is present, wrap all method contents up into the condition
        if condition:
            path_dict[method] = make_conditional(condition, path_dict[method])
Esempio n. 2
0
 def make_path_conditional(self, path, condition):
     """
     Wrap entire API path definition in a CloudFormation if condition.
     :param path: path name
     :param condition: condition name
     """
     self.paths[path] = make_conditional(condition, self.paths[path])
    def _add_route_settings_to_api(self, event_id, event_properties, template,
                                   condition):
        """
        Adds the RouteSettings for this path/method from the given event to the RouteSettings configuration
        on the AWS::Serverless::HttpApi that this refers to.

        :param string event_id: LogicalId of the event
        :param dict event_properties: Properties of the event
        :param SamTemplate template: SAM Template to search for Serverless::HttpApi resources
        :param string condition: Condition on this HttpApi event (if any)
        """

        api_id = self._get_api_id(event_properties)
        resource = template.get(api_id)

        path = event_properties["Path"]
        method = event_properties["Method"]

        # Route should be in format "METHOD /path" or just "/path" if the ANY method is used
        route = "{} {}".format(method.upper(), path)
        if method == OpenApiEditor._X_ANY_METHOD:
            route = path

        # Handle Resource-level conditions if necessary
        api_route_settings = resource.properties.get("RouteSettings", {})
        event_route_settings = event_properties.get("RouteSettings", {})
        if condition:
            event_route_settings = make_conditional(
                condition, event_properties.get("RouteSettings", {}))

        # Merge event-level and api-level RouteSettings properties
        api_route_settings.setdefault(route, {})
        api_route_settings[route].update(event_route_settings)
        resource.properties["RouteSettings"] = api_route_settings
        template.set(api_id, resource)
Esempio n. 4
0
    def add_lambda_integration(self,
                               path,
                               method,
                               integration_uri,
                               method_auth_config=None,
                               api_auth_config=None,
                               condition=None):
        """
        Adds aws_proxy APIGW integration to the given path+method.

        :param string path: Path name
        :param string method: HTTP Method
        :param string integration_uri: URI for the integration.
        """

        method = self._normalize_method_name(method)
        if self.has_integration(path, method):
            # Not throwing an error- we will add lambda integrations to existing swagger if not present
            return

        self.add_path(path, method)

        # Wrap the integration_uri in a Condition if one exists on that function
        # This is necessary so CFN doesn't try to resolve the integration reference.
        if condition:
            integration_uri = make_conditional(condition, integration_uri)

        path_dict = self.get_path(path)
        path_dict[method][self._X_APIGW_INTEGRATION] = {
            "type": "aws_proxy",
            "httpMethod": "POST",
            "payloadFormatVersion": "2.0",
            "uri": integration_uri,
        }

        if path == self._DEFAULT_PATH and method == self._X_ANY_METHOD:
            path_dict[method]["isDefaultRoute"] = True

        # If 'responses' key is *not* present, add it with an empty dict as value
        path_dict[method].setdefault("responses", {})

        # If a condition is present, wrap all method contents up into the condition
        if condition:
            path_dict[method] = make_conditional(condition, path_dict[method])
    def add_lambda_integration(self, path, method, integration_uri,
                               method_auth_config=None, api_auth_config=None, condition=None):
        """
        Adds aws_proxy APIGW integration to the given path+method.

        :param string path: Path name
        :param string method: HTTP Method
        :param string integration_uri: URI for the integration.
        """

        method = self._normalize_method_name(method)
        if self.has_integration(path, method):
            raise ValueError("Lambda integration already exists on Path={}, Method={}".format(path, method))

        self.add_path(path, method)

        # Wrap the integration_uri in a Condition if one exists on that function
        # This is necessary so CFN doesn't try to resolve the integration reference.
        if condition:
            integration_uri = make_conditional(condition, integration_uri)

        path_dict = self.get_path(path)
        path_dict[method][self._X_APIGW_INTEGRATION] = {
                'type': 'aws_proxy',
                'httpMethod': 'POST',
                'uri': integration_uri
        }

        method_auth_config = method_auth_config or {}
        api_auth_config = api_auth_config or {}
        if method_auth_config.get('Authorizer') == 'AWS_IAM' \
           or api_auth_config.get('DefaultAuthorizer') == 'AWS_IAM' and not method_auth_config:
            self.paths[path][method][self._X_APIGW_INTEGRATION]['credentials'] = self._generate_integration_credentials(
                method_invoke_role=method_auth_config.get('InvokeRole'),
                api_invoke_role=api_auth_config.get('InvokeRole')
            )

        # If 'responses' key is *not* present, add it with an empty dict as value
        path_dict[method].setdefault('responses', {})

        # If a condition is present, wrap all method contents up into the condition
        if condition:
            path_dict[method] = make_conditional(condition, path_dict[method])
Esempio n. 6
0
    def add_lambda_integration(self,
                               path,
                               method,
                               integration_uri,
                               condition=None):
        """
        Adds aws_proxy APIGW integration to the given path+method.

        :param string path: Path name
        :param string method: HTTP Method
        :param string integration_uri: URI for the integration.
        """

        method = self._normalize_method_name(method)
        if self.has_integration(path, method):
            raise ValueError(
                "Lambda integration already exists on Path={}, Method={}".
                format(path, method))

        self.add_path(path, method)

        # Wrap the integration_uri in a Condition if one exists on that function
        # This is necessary so CFN doesn't try to resolve the integration reference.
        if condition:
            integration_uri = make_conditional(condition, integration_uri)

        path_dict = self.get_path(path)
        path_dict[method][self._X_APIGW_INTEGRATION] = {
            'type': 'aws_proxy',
            'httpMethod': 'POST',
            'uri': integration_uri
        }

        # If 'responses' key is *not* present, add it with an empty dict as value
        path_dict[method].setdefault('responses', {})

        # If a condition is present, wrap all method contents up into the condition
        if condition:
            path_dict[method] = make_conditional(condition, path_dict[method])
Esempio n. 7
0
    def _inject_notification_configuration(self, function, bucket):
        base_event_mapping = {
            'Function': function.get_runtime_attr("arn")
        }

        if self.Filter is not None:
            base_event_mapping['Filter'] = self.Filter

        event_types = self.Events
        if isinstance(self.Events, string_types):
            event_types = [self.Events]

        event_mappings = []
        for event_type in event_types:

            lambda_event = copy.deepcopy(base_event_mapping)
            lambda_event['Event'] = event_type
            if CONDITION in function.resource_attributes:
                lambda_event = make_conditional(function.resource_attributes[CONDITION], lambda_event)
            event_mappings.append(lambda_event)

        properties = bucket.get('Properties', None)
        if properties is None:
            properties = {}
            bucket['Properties'] = properties

        notification_config = properties.get('NotificationConfiguration', None)
        if notification_config is None:
            notification_config = {}
            properties['NotificationConfiguration'] = notification_config

        lambda_notifications = notification_config.get('LambdaConfigurations', None)
        if lambda_notifications is None:
            lambda_notifications = []
            notification_config['LambdaConfigurations'] = lambda_notifications

        for event_mapping in event_mappings:
            if event_mapping not in lambda_notifications:
                lambda_notifications.append(event_mapping)
        return bucket
    def _inject_notification_configuration(self, function, bucket):
        base_event_mapping = {
            'Function': function.get_runtime_attr("arn")
        }

        if self.Filter is not None:
            base_event_mapping['Filter'] = self.Filter

        event_types = self.Events
        if isinstance(self.Events, string_types):
            event_types = [self.Events]

        event_mappings = []
        for event_type in event_types:

            lambda_event = copy.deepcopy(base_event_mapping)
            lambda_event['Event'] = event_type
            if CONDITION in function.resource_attributes:
                lambda_event = make_conditional(function.resource_attributes[CONDITION], lambda_event)
            event_mappings.append(lambda_event)

        properties = bucket.get('Properties', None)
        if properties is None:
            properties = {}
            bucket['Properties'] = properties

        notification_config = properties.get('NotificationConfiguration', None)
        if notification_config is None:
            notification_config = {}
            properties['NotificationConfiguration'] = notification_config

        lambda_notifications = notification_config.get('LambdaConfigurations', None)
        if lambda_notifications is None:
            lambda_notifications = []
            notification_config['LambdaConfigurations'] = lambda_notifications

        for event_mapping in event_mappings:
            if event_mapping not in lambda_notifications:
                lambda_notifications.append(event_mapping)
        return bucket
 def make_path_conditional(self, path, condition):
     """
     Wrap entire API path definition in a CloudFormation if condition.
     """
     self.paths[path] = make_conditional(condition, self.paths[path])
 def make_path_conditional(self, path, condition):
     """
     Wrap entire API path definition in a CloudFormation if condition.
     """
     self.paths[path] = make_conditional(condition, self.paths[path])