Example #1
0
    def get_template(template_dict, parameter_overrides=None):
        """
        Given a SAM template dictionary, return a cleaned copy of the template where SAM plugins have been run
        and parameter values have been substituted.

        Parameters
        ----------
        template_dict : dict
            unprocessed SAM template dictionary

        parameter_overrides: dict
            Optional dictionary of values for template parameters

        Returns
        -------
        dict
            Processed SAM template
        """
        template_dict = template_dict or {}
        if template_dict:
            template_dict = SamTranslatorWrapper(template_dict).run_plugins()
        ResourceMetadataNormalizer.normalize(template_dict)
        logical_id_translator = SamBaseProvider._get_parameter_values(
            template_dict, parameter_overrides)

        resolver = IntrinsicResolver(
            template=template_dict,
            symbol_resolver=IntrinsicsSymbolTable(
                logical_id_translator=logical_id_translator,
                template=template_dict),
        )
        template_dict = resolver.resolve_template(ignore_errors=True)
        return template_dict
    def get_template(template_dict, parameter_overrides=None):
        """
        Given a SAM template dictionary, return a cleaned copy of the template where SAM plugins have been run
        and parameter values have been substituted.

        Parameters
        ----------
        template_dict : dict
            unprocessed SAM template dictionary

        parameter_overrides: dict
            Optional dictionary of values for template parameters

        Returns
        -------
        dict
            Processed SAM template
        """

        template_dict = template_dict or {}
        if template_dict:
            template_dict = SamTranslatorWrapper(template_dict).run_plugins()

        template_dict = SamBaseProvider._resolve_parameters(template_dict, parameter_overrides)
        ResourceMetadataNormalizer.normalize(template_dict)
        return template_dict
    def test_replace_all_resources_that_contain_metadata(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {
                        "Code": "some value"
                    },
                    "Metadata": {
                        "aws:asset:path": "new path",
                        "aws:asset:property": "Code"
                    }
                },
                "Resource2": {
                    "Properties": {
                        "SomeRandomProperty": "some value"
                    },
                    "Metadata": {
                        "aws:asset:path": "super cool path",
                        "aws:asset:property": "SomeRandomProperty"
                    }
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("new path", template_data['Resources']['Function1']['Properties']['Code'])
        self.assertEqual("super cool path", template_data['Resources']['Resource2']['Properties']['SomeRandomProperty'])
    def test_replace_all_resources_that_contain_metadata(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {
                        "Code": "some value"
                    },
                    "Metadata": {
                        "aws:asset:path": "new path",
                        "aws:asset:property": "Code"
                    }
                },
                "Resource2": {
                    "Properties": {
                        "SomeRandomProperty": "some value"
                    },
                    "Metadata": {
                        "aws:asset:path": "super cool path",
                        "aws:asset:property": "SomeRandomProperty"
                    }
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual(
            "new path",
            template_data['Resources']['Function1']['Properties']['Code'])
        self.assertEqual(
            "super cool path", template_data['Resources']['Resource2']
            ['Properties']['SomeRandomProperty'])
    def test_tempalte_without_asset_path(self):
        template_data = {
            "Resources": {
                "Function1": {"Properties": {"Code": "some value"}, "Metadata": {"aws:asset:property": "Code"}}
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("some value", template_data["Resources"]["Function1"]["Properties"]["Code"])
    def test_replace_of_property_that_does_not_exist(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {},
                    "Metadata": {"aws:asset:path": "new path", "aws:asset:property": "Code"},
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("new path", template_data["Resources"]["Function1"]["Properties"]["Code"])
    def test_replace_property_with_path(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {"Code": "some value"},
                    "Metadata": {"aws:asset:path": "new path", "aws:asset:property": "Code"},
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("new path", template_data["Resources"]["Function1"]["Properties"]["Code"])
    def test_tempate_without_metadata(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {
                        "Code": "some value"
                    }
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("some value", template_data['Resources']['Function1']['Properties']['Code'])
    def test_tempate_without_metadata(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {
                        "Code": "some value"
                    }
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual(
            "some value",
            template_data['Resources']['Function1']['Properties']['Code'])
    def test_replace_of_property_that_does_not_exist(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {},
                    "Metadata": {
                        "aws:asset:path": "new path",
                        "aws:asset:property": "Code"
                    }
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("new path", template_data['Resources']['Function1']['Properties']['Code'])
    def test_template_without_asset_property(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {
                        "Code": "some value"
                    },
                    "Metadata": {
                        "aws:asset:path": "new path",
                    }
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("some value", template_data['Resources']['Function1']['Properties']['Code'])
    def test_template_without_asset_property(self):
        template_data = {
            "Resources": {
                "Function1": {
                    "Properties": {
                        "Code": "some value"
                    },
                    "Metadata": {
                        "aws:asset:path": "new path",
                    }
                }
            }
        }

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual(
            "some value",
            template_data['Resources']['Function1']['Properties']['Code'])
    def test_template_with_empty_metadata(self):
        template_data = {"Resources": {"Function1": {"Properties": {"Code": "some value"}, "Metadata": {}}}}

        ResourceMetadataNormalizer.normalize(template_data)

        self.assertEqual("some value", template_data["Resources"]["Function1"]["Properties"]["Code"])