def test_mapping__one_category(): template = {} Mapping('abcde') \ .add_category('category_1').add_item('key_1', 'value_1').add_item('key_2', 'value_2') \ .to_template(template) assert_equal( template, {'abcde': { 'category_1': { 'key_1': 'value_1', 'key_2': 'value_2' } }})
def test_mapping__two_categories(): template = {} mapping = Mapping('abcde') mapping.add_category('category_1').add_item('key_1', 'value_1').add_item( 'key_2', 'value_2') mapping.add_category('category_2').add_item('key_3', 'value_3').add_item( 'key_4', 'value_4') mapping.to_template(template) assert_equal( template, { 'abcde': { 'category_1': { 'key_1': 'value_1', 'key_2': 'value_2' }, 'category_2': { 'key_3': 'value_3', 'key_4': 'value_4' } } })
def generate(): t = Template(description='see. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html') meta = t.metadata(Metadatum('MetadataKey').attributes('name', 'value')) m = t.mappings(Mapping('RegionMap') .add_category('us-east-1').add_item('AMI', 'ami-7f418316').add_item('TestAZ', 'us-east-1a') .add_category('us-west-1').add_item('AMI', 'ami-951945d0').add_item('TestAZ', 'us-west-1a') .add_category('us-west-2').add_item('AMI', 'ami-16fd7026').add_item('TestAZ', 'us-west-2a') ) p = t.parameters(Parameter('EnvType') .description('Environment type.') .type('String') .default('test') .allowed_values(['prod', 'test']) .constraint_description('must specify prod or test.') ) c = t.conditions(Condition('CreateProdResources').expression(Intrinsics.fn_equals(Intrinsics.ref('EnvType'), 'prod'))) r_ec2instance = t.resources(Resource('EC2Instance').type('AWS::EC2::Instance').properties([ Attributes.of('ImageId', m.find_in_map(Pseudos.region(), 'AMI')) ])) r_new_volume = t.resources(Resource('NewVolume').type('AWS:EC2::Volume').properties([ Attributes.of('Size', '100'), Attributes.of('AvailabilityZone', Intrinsics.get_att(r_ec2instance.name, 'AvailabilityZone')) ])).attributes('Condition', c.name) r_mount_point = t.resources(Resource('MountPoint').type('AWS::EC2::VolumeAttachment').properties([ Attributes.of('InstanceId', Intrinsics.ref(r_ec2instance)), Attributes.of('VolumeId', Intrinsics.ref('NewVolume')), Attributes.of('Device', '/dev/sdh') ])).attributes('Condition', c.name) o = t.outputs(Output('VolumeId').value(Intrinsics.ref('NewVolume')).attributes('Condition', c.name)) return t
def test_intrinsics_find_in_map__mapping(): mapping = Mapping('map_name').add_category('top_key').add_item( 'second_key', 'map_value') assert_equal(Intrinsics.find_in_map(mapping, 'top_key', 'second_key'), {'Fn::FindInMap': ['map_name', 'top_key', 'second_key']})
def test_mapping__find_in_map__missing_second_level_key(): mapping = Mapping('abcde') mapping.add_category('category_1').add_item('key_1', 'value_1').add_item( 'key_2', 'value_2') mapping.find_in_map('category_1', 'key_X'),
def test_mapping__find_in_map__ok(): mapping = Mapping('abcde') mapping.add_category('category_1').add_item('key_1', 'value_1').add_item( 'key_2', 'value_2') assert_equal(mapping.find_in_map('category_1', 'key_1'), {'Fn::FindInMap': ['abcde', 'category_1', 'key_1']})