def test_throws_when_resources_not_all_dicts(self):
        template = {"Resources": {"notadict": None, "MyResource": {}}}

        with self.assertRaises(InvalidDocumentException):
            sam_parser = Parser()
            translator = Translator({}, sam_parser)
            translator.translate(template, {})
    def test_throws_when_resource_not_found(self):
        template = {"foo": "bar"}

        with self.assertRaises(InvalidDocumentException):
            sam_parser = Parser()
            translator = Translator({}, sam_parser)
            translator.translate(template, {})
    def test_throws_when_resource_is_not_dict(self):
        template = {"Resources": [1, 2, 3]}

        with self.assertRaises(InvalidDocumentException):
            sam_parser = Parser()
            translator = Translator({}, sam_parser)
            translator.translate(template, {})
    def __init__(self, sam_template, managed_policy_loader):
        """
        Construct a SamTemplateValidator

        Design Details:

        managed_policy_loader is injected into the `__init__` to allow future expansion
        and overriding capabilities. A typically pattern is to pass the name of the class into
        the `__init__` as keyword args. As long as the class 'conforms' to the same 'interface'.
        This allows the class to be changed by the client and allowing customization of the class being
        initialized. Something I had in mind would be allowing a template to be run and checked
        'offline' (not needing aws creds). To make this an easier transition in the future, we ingest
        the ManagedPolicyLoader class.

        Parameters
        ----------
        sam_template dict
            Dictionary representing a SAM Template
        managed_policy_loader ManagedPolicyLoader
            Sam ManagedPolicyLoader
        """
        self.sam_template = sam_template
        self.managed_policy_loader = managed_policy_loader

        self.sam_parser = Parser()
    def test_transform_method_must_inject_plugins_when_creating_resources(
            self, prepare_plugins_mock, sam_plugins_class_mock,
            resource_from_dict_mock):
        manifest = {
            "Resources": {
                "MyTable": {
                    "Type": "AWS::Serverless::SimpleTable",
                    "Properties": {}
                }
            }
        }

        sam_plugins_object_mock = Mock()
        sam_plugins_class_mock.return_value = sam_plugins_object_mock
        prepare_plugins_mock.return_value = sam_plugins_object_mock
        resource_from_dict_mock.return_value = SamSimpleTable("MyFunction")

        initial_plugins = [1, 2, 3]
        sam_parser = Parser()
        translator = Translator({}, sam_parser, plugins=initial_plugins)
        translator.translate(manifest, {})

        resource_from_dict_mock.assert_called_with(
            "MyTable",
            manifest["Resources"]["MyTable"],
            sam_plugins=sam_plugins_object_mock)
        prepare_plugins_mock.assert_called_once_with(initial_plugins, {
            "AWS::Region": "ap-southeast-1",
            "AWS::Partition": "aws"
        })
def transform(input_fragment, parameter_values, managed_policy_loader):
    """Translates the SAM manifest provided in the and returns the translation to CloudFormation.

    :param dict input_fragment: the SAM template to transform
    :param dict parameter_values: Parameter values provided by the user
    :returns: the transformed CloudFormation template
    :rtype: dict
    """

    sam_parser = Parser()
    translator = Translator(managed_policy_loader.load(), sam_parser)
    return translator.translate(input_fragment, parameter_values=parameter_values)
Beispiel #7
0
    def test_add_default_parameter_values_must_ignore_invalid_template_parameters(
            self, template_parameters):
        parameter_values = {"Param1": "value1"}

        expected = {"Param1": "value1"}

        sam_template = {"Parameters": template_parameters}

        sam_parser = Parser()
        translator = Translator({}, sam_parser)
        result = translator._add_default_parameter_values(
            sam_template, parameter_values)
        self.assertEquals(expected, result)
Beispiel #8
0
    def __translate(self, parameter_values):
        """
        This method is unused and a Work In Progress
        """

        template_copy = self.template

        sam_parser = Parser()
        sam_translator = Translator(managed_policy_map=self.__managed_policy_map(),
                                    sam_parser=sam_parser,
                                    # Default plugins are already initialized within the Translator
                                    plugins=self.extra_plugins)

        return sam_translator.translate(sam_template=template_copy,
                                        parameter_values=parameter_values)
Beispiel #9
0
    def test_add_default_parameter_values_must_merge(self):
        parameter_values = {"Param1": "value1"}

        sam_template = {
            "Parameters": {
                "Param2": {
                    "Type": "String",
                    "Default": "template default"
                }
            }
        }

        expected = {"Param1": "value1", "Param2": "template default"}

        sam_parser = Parser()
        translator = Translator({}, sam_parser)
        result = translator._add_default_parameter_values(
            sam_template, parameter_values)
        self.assertEquals(expected, result)
    def test_add_default_parameter_values_must_skip_params_without_defaults(
            self):
        parameter_values = {"Param1": "value1"}

        sam_template = {
            "Parameters": {
                "Param1": {
                    "Type": "String"
                },
                "Param2": {
                    "Type": "String"
                }
            }
        }

        expected = {"Param1": "value1"}

        sam_parser = Parser()
        translator = Translator({}, sam_parser)
        result = translator._add_default_parameter_values(
            sam_template, parameter_values)
        self.assertEqual(expected, result)