def test_intrinsics_fn_not(): condition = Condition('cond_name').expression( Intrinsics.fn_equals('value_1', 'value_2')) assert_equal(Intrinsics.fn_not(condition), {'Fn::Not': [{ 'Fn::Equals': ['value_1', 'value_2'] }]})
def test_intrinsics_fn_or__two_conditions(): conditions = [Condition('cond_name').expression(Intrinsics.fn_equals('value_1', 'value_2')) for _ in range(2)] assert_equal( Intrinsics.fn_or(conditions), {'Fn::Or': [ {'Fn::Equals': ['value_1', 'value_2']}, {'Fn::Equals': ['value_1', 'value_2']} ]} )
def test_intrinsics_fn_or__two_conditions(): conditions = [ Condition('cond_name').expression( Intrinsics.fn_equals('value_1', 'value_2')) for _ in range(2) ] assert_equal( Intrinsics.fn_or(conditions), { 'Fn::Or': [{ 'Fn::Equals': ['value_1', 'value_2'] }, { 'Fn::Equals': ['value_1', 'value_2'] }] })
def test_output__standard(): template = {} Output('abcde').description('description').value(Intrinsics.get_att('res_name', 'attr_name')).to_template(template) assert_equal( template, {'abcde': {'Description': 'description', 'Value': {'Fn::GetAtt': ['res_name', 'attr_name']}}} )
def test_condition(): template = {} Condition('abcde').expression(Intrinsics.fn_equals('value_1', 'value_2')).to_template(template) assert_equal( template, {'abcde': {'Fn::Equals': ['value_1', 'value_2']}} )
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_output__standard(): template = {} Output('abcde').description('description').value( Intrinsics.get_att('res_name', 'attr_name')).to_template(template) assert_equal( template, { 'abcde': { 'Description': 'description', 'Value': { 'Fn::GetAtt': ['res_name', 'attr_name'] } } })
def test_intrinsics_sub__with_parameters(): assert_equal( Intrinsics.sub('template', { 'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3' }), { 'Fn::Sub': [ 'template', { 'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3' } ] })
def test_output__with_export(): template = {} Output('abcde') \ .description('description') \ .value(Intrinsics.get_att('res_name', 'attr_name')) \ .export('variable_name') \ .to_template(template) assert_equal( template, { 'abcde': { 'Description': 'description', 'Value': {'Fn::GetAtt': ['res_name', 'attr_name']}, 'Export': {'Name': 'variable_name'} } } )
def test_output__with_export(): template = {} Output('abcde') \ .description('description') \ .value(Intrinsics.get_att('res_name', 'attr_name')) \ .export('variable_name') \ .to_template(template) assert_equal( template, { 'abcde': { 'Description': 'description', 'Value': { 'Fn::GetAtt': ['res_name', 'attr_name'] }, 'Export': { 'Name': 'variable_name' } } })
def test_intrinsics_get_att(): assert_equal(Intrinsics.get_att('res_name', 'attr_name'), {'Fn::GetAtt': ['res_name', 'attr_name']})
def test_intrinsics_get_azs__without_region(): assert_equal(Intrinsics.get_azs(), {'Fn::GetAZs': ''})
def test_intrinsics_base64(): assert_equal(Intrinsics.base64('abcde'), {'Fn::Base64': 'abcde'})
def test_intrinsics_fn_or__only_one_condition(): condition = Condition('cond_name').expression( Intrinsics.fn_equals('value_1', 'value_2')) Intrinsics.fn_or([condition])
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_intrinsics_fn_equals(): assert_equal(Intrinsics.fn_equals('value_1', 'value_2'), {'Fn::Equals': ['value_1', 'value_2']})
def test_intrinsics_join(): assert_equal( Intrinsics.join('delim', ['value_1', 'value_2', 'value_3']), {'Fn::Join': ['delim', ['value_1', 'value_2', 'value_3']]} )
def test_intrinsics_fn_or__only_one_condition(): condition = Condition('cond_name').expression(Intrinsics.fn_equals('value_1', 'value_2')) Intrinsics.fn_or([condition])
def test_intrinsics_get_azs__without_region(): assert_equal( Intrinsics.get_azs(), {'Fn::GetAZs': ''} )
def test_intrinsics_import_value(): assert_equal( Intrinsics.import_value('value'), {'Fn::ImportValue': 'value'} )
def test_intrinsics_get_azs__with_region(): assert_equal( Intrinsics.get_azs('region_name'), {'Fn::GetAZs': 'region_name'} )
def test_intrinsics_get_att(): assert_equal( Intrinsics.get_att('res_name', 'attr_name'), {'Fn::GetAtt': ['res_name', 'attr_name']} )
def test_intrinsics_fn_or__more_than_ten_conditions(): conditions = [Condition('cond_name').expression(Intrinsics.fn_equals('value_1', 'value_2')) for _ in range(11)] Intrinsics.fn_or(conditions)
def test_intrinsics_join(): assert_equal(Intrinsics.join('delim', ['value_1', 'value_2', 'value_3']), {'Fn::Join': ['delim', ['value_1', 'value_2', 'value_3']]})
def test_intrinsics_select(): assert_equal( Intrinsics.select(2, ['value_1', 'value_2', 'value_3']), {'Fn::Select': [2, ['value_1', 'value_2', 'value_3']]} )
def test_intrinsics_fn_if(): assert_equal( Intrinsics.fn_if('cond_name', 'value_1', 'value_2'), {'Fn::If': ['cond_name', 'value_1', 'value_2']} )
def test_intrinsics_sub__with_parameters(): assert_equal( Intrinsics.sub('template', {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}), {'Fn::Sub': ['template', {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}]} )
def test_intrinsics_find_in_map__map_name(): assert_equal(Intrinsics.find_in_map('map_name', 'top_key', 'second_key'), {'Fn::FindInMap': ['map_name', 'top_key', 'second_key']})
def test_intrinsics_sub__without_parameters(): assert_equal( Intrinsics.sub('template'), {'Fn::Sub': 'template'} )
def test_intrinsics_find_in_map__others(): Intrinsics.find_in_map({}, 'top_key', 'second_key')
def test_intrinsics_ref__elem_name(): assert_equal( Intrinsics.ref('elem_name'), {'Ref': 'elem_name'} )
def test_intrinsics_fn_if(): assert_equal(Intrinsics.fn_if('cond_name', 'value_1', 'value_2'), {'Fn::If': ['cond_name', 'value_1', 'value_2']})
def test_intrinsics_ref__element(): element = Element('elem_name') assert_equal( Intrinsics.ref(element), {'Ref': 'elem_name'} )
def test_intrinsics_fn_not(): condition = Condition('cond_name').expression(Intrinsics.fn_equals('value_1', 'value_2')) assert_equal( Intrinsics.fn_not(condition), {'Fn::Not': [{'Fn::Equals': ['value_1', 'value_2']}]} )
def test_intrinsics_ref__others(): Intrinsics.ref({}),
def test_intrinsics_fn_or__more_than_ten_conditions(): conditions = [ Condition('cond_name').expression( Intrinsics.fn_equals('value_1', 'value_2')) for _ in range(11) ] Intrinsics.fn_or(conditions)
def test_intrinsics_find_in_map__map_name(): assert_equal( Intrinsics.find_in_map('map_name', 'top_key', 'second_key'), {'Fn::FindInMap': ['map_name', 'top_key', 'second_key']} )
def test_intrinsics_get_azs__with_region(): assert_equal(Intrinsics.get_azs('region_name'), {'Fn::GetAZs': 'region_name'})
def test_intrinsics_ref__elem_name(): assert_equal(Intrinsics.ref('elem_name'), {'Ref': 'elem_name'})
def test_intrinsics_import_value(): assert_equal(Intrinsics.import_value('value'), {'Fn::ImportValue': 'value'})
def test_intrinsics_select(): assert_equal(Intrinsics.select(2, ['value_1', 'value_2', 'value_3']), {'Fn::Select': [2, ['value_1', 'value_2', 'value_3']]})
def test_intrinsics_fn_equals(): assert_equal( Intrinsics.fn_equals('value_1', 'value_2'), {'Fn::Equals': ['value_1', 'value_2']} )
def test_intrinsics_sub__without_parameters(): assert_equal(Intrinsics.sub('template'), {'Fn::Sub': 'template'})
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_intrinsics_ref__element(): element = Element('elem_name') assert_equal(Intrinsics.ref(element), {'Ref': 'elem_name'})
def test_condition(): template = {} Condition('abcde').expression(Intrinsics.fn_equals( 'value_1', 'value_2')).to_template(template) assert_equal(template, {'abcde': {'Fn::Equals': ['value_1', 'value_2']}})