def test_custom_resource_type(self): """ Ensures that a custom resource type is implicitly defined. """ template = Template() template.add_resource(MyCustomResource("foo", Foo="bar", ServiceToken="baz")) generated = TemplateGenerator(json.loads(template.to_json())) # validated that the templates are equal to each other self.assertDictEqual(template.to_dict(), generated.to_dict()) foo = generated.resources["foo"] self.assertFalse(isinstance(foo, MyCustomResource))
def test_macro_resource(self): """ Ensures that a custom member can be defined. """ template = Template() template.add_resource(MyMacroResource("foo", Foo="bar")) generated = TemplateGenerator(json.loads(template.to_json()), CustomMembers=[MyMacroResource]) # validated that the templates are equal to each other self.assertDictEqual(template.to_dict(), generated.to_dict()) foo = generated.resources["foo"] self.assertTrue(isinstance(foo, MyMacroResource)) self.assertEqual("bar", foo.Foo)
def test_macro_resource(self): """ Ensures that a custom member can be defined. """ template = Template() template.add_resource(MyMacroResource("foo", Foo="bar")) generated = TemplateGenerator(json.loads(template.to_json()), CustomMembers=[MyMacroResource]) # validated that the templates are equal to each other self.assertDictEqual(template.to_dict(), generated.to_dict()) foo = generated.resources["foo"] self.assertTrue(isinstance(foo, MyMacroResource)) self.assertEquals("bar", foo.Foo)
def test_no_nested_name(self): """ Prevent regression for ensuring no nested Name (Issue #977) """ template_json = json.loads( """ { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Description", "Outputs": { "TestOutput": { "Description": "ARN for TestData", "Export": { "Name": {"Fn::Sub": "${AWS::StackName}-TestOutput"} }, "Value": {"Ref": "TestPolicy"} } } } """ ) d = TemplateGenerator(template_json).to_dict() name = d["Outputs"]["TestOutput"]["Export"]["Name"] self.assertIn("Fn::Sub", name)
def test_template_is_same_as_sample(self): work_sample_name_path = abspath("tests/worksample.cfn.template") g = Generator() generated = g.to_dict() json_sample = json.loads( self.__open_and_read_file(work_sample_name_path)) actual = TemplateGenerator(json_sample).to_dict() self.assertDictEqual(generated, actual)
def test_resource_type_not_defined(self): template_json = json.loads(""" { "Resources": { "Foo": { } } } """) with self.assertRaises(ResourceTypeNotDefined) as context: TemplateGenerator(template_json) self.assertEqual("ResourceType not defined for Foo", str(context.exception)) self.assertEqual("Foo", context.exception.resource)
def test_template_generator(self): """ Ensures that all example outputs can be loaded into the template generator and back to JSON with no difference. """ # we first get both outputs as JSON strings template = self.expected_output generated = TemplateGenerator(json.loads(template)).to_json() # then we make them into a dict for comparison template = json.loads(template) generated = json.loads(generated) self.assertDictEqual(template, generated)
def test_unknown_resource_type(self): template_json = json.loads(""" { "Resources": { "Foo": { "Type": "Some::Unknown::Type" } } } """) with self.assertRaises(ResourceTypeNotFound) as context: TemplateGenerator(template_json) self.assertEqual( "ResourceType not found for Some::Unknown::Type - Foo", str(context.exception)) self.assertEqual("Foo", context.exception.resource) self.assertEqual("Some::Unknown::Type", context.exception.resource_type)
def from_dict(cls, dct): """ Factory method to construct a Troposphere template from dictionary data. :type dct: dict :return: Template .. note:: troposphere provides a factory class TemplateGenerator to deserialize the dict data to troposphere.Template object. troposphere_mate.Template should be able to do the same things. To reuse ``TemplateGenerator`` code, we convert ``troposphere.Template`` to ``troposphere_mate.Template`` afterwards. """ tropo_tpl = TemplateGenerator(dct) tropo_mate_tpl = cls() tropo_mate_tpl.description = tropo_tpl.description tropo_mate_tpl.metadata = tropo_tpl.metadata tropo_mate_tpl.conditions = tropo_tpl.conditions tropo_mate_tpl.mappings = tropo_tpl.mappings tropo_mate_tpl.outputs = { k: convert_to_mate_output(v, tropo_mate_tpl) for k, v in tropo_tpl.outputs.items() } tropo_mate_tpl.parameters = tropo_tpl.parameters tropo_mate_tpl.resources = { k: convert_to_mate_resource(v, tropo_mate_tpl) for k, v in tropo_tpl.resources.items() } tropo_mate_tpl.version = tropo_tpl.version tropo_mate_tpl.transform = tropo_tpl.transform return tropo_mate_tpl
def load_vector_as_template(vector_name: str) -> TemplateGenerator: vector_dict = load_vector(vector_name) return TemplateGenerator(vector_dict)
def _load_template(template_path): raw_json = json.loads(open(template_path, 'r').read()) template = TemplateGenerator(raw_json) return template