def on_before_transform_template(self, template_dict): """ Hook method that runs before a template gets transformed. In this method, we parse and process Globals section from the template (if present). :param dict template_dict: SAM template as a dictionary """ try: global_section = Globals(template_dict) except InvalidGlobalsSectionException as ex: raise InvalidDocumentException([ex]) # For each resource in template, try and merge with Globals if necessary template = SamTemplate(template_dict) for logicalId, resource in template.iterate(): resource.properties = global_section.merge(resource.type, resource.properties) template.set(logicalId, resource) # Remove the Globals section from template if necessary Globals.del_section(template_dict) # If there was a global openApiVersion flag, check and convert swagger # to the right version Globals.fix_openapi_definitions(template_dict)
def test_merge_end_to_end_unknown_type(self): type = "some unknown type" properties = {"a": "b", "key": [1, 2, 3]} # Output equals input expected = {"a": "b", "key": [1, 2, 3]} globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_must_skip_with_no_types(self, parse_mock): parse_mock.return_value = {} local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result)
def test_merge_must_skip_with_no_types(self, parse_mock): parse_mock.return_value = { } local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result)
def test_merge_must_skip_unsupported_types(self, parse_mock): type1_mock = Mock() parse_mock.return_value = {"type1": type1_mock} local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result) type1_mock.merge.assert_not_called()
def test_merge_end_to_end_on_known_type2(self): type = "prefix_type2" properties = {"a": "b", "key": [1, 2, 3]} expected = { "otherprop1": "value1", # inherited from global "otherprop2": "value2", # inherited from global "a": "b", "key": [1, 2, 3] } globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_end_to_end_on_known_type1(self): type = "prefix_type1" properties = {"prop1": "overridden value", "a": "b", "key": [1, 2, 3]} expected = { "prop1": "overridden value", "prop2": "value2", "a": "b", "key": [1, 2, 3] } # inherited from global globals = Globals(self.template) result = globals.merge(type, properties) self.assertEqual(expected, result)
def test_merge_must_skip_unsupported_types(self, parse_mock): type1_mock = Mock() parse_mock.return_value = { "type1": type1_mock } local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result) type1_mock.merge.assert_not_called()
def test_merge_must_actually_do_merge(self, parse_mock): type1_mock = Mock() type2_mock = Mock() parse_mock.return_value = {"type1": type1_mock, "type2": type2_mock} local_properties = {"a": "b"} expected = "some merged value" type1_mock.merge.return_value = expected # Try to merge for type1 globals = Globals(self.template) result = globals.merge("type1", local_properties) self.assertEqual(expected, result) type1_mock.merge.assert_called_with(local_properties) type2_mock.merge.assert_not_called()
def test_merge_end_to_end_unknown_type(self): type = "some unknown type" properties = { "a": "b", "key": [1,2,3] } # Output equals input expected = { "a": "b", "key": [1,2,3] } globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_end_to_end_on_known_type2(self): type = "prefix_type2" properties = { "a": "b", "key": [1,2,3] } expected = { "otherprop1": "value1", # inherited from global "otherprop2": "value2", # inherited from global "a": "b", "key": [1,2,3] } globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_must_actually_do_merge(self, parse_mock): type1_mock = Mock() type2_mock = Mock() parse_mock.return_value = { "type1": type1_mock, "type2": type2_mock, } local_properties = {"a": "b"} expected = "some merged value" type1_mock.merge.return_value = expected # Try to merge for type1 globals = Globals(self.template) result = globals.merge("type1", local_properties) self.assertEquals(expected, result) type1_mock.merge.assert_called_with(local_properties) type2_mock.merge.assert_not_called()
def on_before_transform_template(self, template_dict): """ Hook method that runs before a template gets transformed. In this method, we parse and process Globals section from the template (if present). :param dict template_dict: SAM template as a dictionary """ try: global_section = Globals(template_dict) except InvalidGlobalsSectionException as ex: raise InvalidDocumentException([ex]) # For each resource in template, try and merge with Globals if necessary template = SamTemplate(template_dict) for logicalId, resource in template.iterate(): resource.properties = global_section.merge(resource.type, resource.properties) template.set(logicalId, resource) # Remove the Globals section from template if necessary Globals.del_section(template_dict)