def prepare_plugins(plugins, parameters=None):
    """
    Creates & returns a plugins object with the given list of plugins installed. In addition to the given plugins,
    we will also install a few "required" plugins that are necessary to provide complete support for SAM template spec.

    :param plugins: list of samtranslator.plugins.BasePlugin plugins: List of plugins to install
    :param parameters: Dictionary of parameter values
    :return samtranslator.plugins.SamPlugins: Instance of `SamPlugins`
    """

    if parameters is None:
        parameters = {}
    required_plugins = [
        DefaultDefinitionBodyPlugin(),
        make_implicit_rest_api_plugin(),
        make_implicit_http_api_plugin(),
        GlobalsPlugin(),
        make_policy_template_for_function_plugin(),
    ]

    plugins = [] if not plugins else plugins

    # If a ServerlessAppPlugin does not yet exist, create one and add to the beginning of the required plugins list.
    if not any(isinstance(plugin, ServerlessAppPlugin) for plugin in plugins):
        required_plugins.insert(0, ServerlessAppPlugin(parameters=parameters))

    # Execute customer's plugins first before running SAM plugins. It is very important to retain this order because
    # other plugins will be dependent on this ordering.
    return SamPlugins(plugins + required_plugins)
Example #2
0
class TestDefaultDefinitionBodyPlugin_on_before_transform_template(TestCase):
    def setUp(self):
        self.plugin = DefaultDefinitionBodyPlugin()

    @patch("samtranslator.plugins.api.default_definition_body_plugin.SamTemplate")
    def test_must_process_functions(self, SamTemplateMock):

        template_dict = {"a": "b"}
        api_resources = [("id1", ApiResource()), ("id2", ApiResource()), ("id3", ApiResource())]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = api_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_any_call({"AWS::Serverless::Api"})
        sam_template.iterate.assert_any_call({"AWS::Serverless::HttpApi"})
class TestDefaultDefinitionBodyPlugin_on_before_transform_template(TestCase):

    def setUp(self):
        self.plugin = DefaultDefinitionBodyPlugin()

    @patch("samtranslator.plugins.api.default_definition_body_plugin.SamTemplate")
    def test_must_process_functions(self, SamTemplateMock):

        template_dict = {"a": "b"}
        api_resources = [("id1", ApiResource()), ("id2", ApiResource()), ("id3", ApiResource())]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = api_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Api")
Example #4
0
def prepare_plugins(plugins):
    """
    Creates & returns a plugins object with the given list of plugins installed. In addition to the given plugins,
    we will also install a few "required" plugins that are necessary to provide complete support for SAM template spec.

    :param list of samtranslator.plugins.BasePlugin plugins: List of plugins to install
    :return samtranslator.plugins.SamPlugins: Instance of `SamPlugins`
    """

    required_plugins = [
        DefaultDefinitionBodyPlugin(),
        make_implicit_api_plugin(),
        GlobalsPlugin(),
        make_policy_template_for_function_plugin(),
    ]

    plugins = [] if not plugins else plugins

    # Execute customer's plugins first before running SAM plugins. It is very important to retain this order because
    # other plugins will be dependent on this ordering.
    return SamPlugins(plugins + required_plugins)
 def setUp(self):
     self.plugin = DefaultDefinitionBodyPlugin()
 def setUp(self):
     self.plugin = DefaultDefinitionBodyPlugin()