Ejemplo n.º 1
0
def test_resource__metadata__config_sets():
    template = {}
    resource = Resource('abcde').type('type')
    resource.metadata(CfnInitMetadata.of([
        CfnInitMetadata.Init([
            CfnInitMetadata.ConfigSet('default', [
                CfnInitMetadata.Config('config').commands('key_1', 'value_1')
            ])
        ])
    ]))
    resource.to_template(template)
    assert_equal(
        template,
        {
            'abcde': {
                'Type': 'type',
                'Metadata': {
                    'AWS::CloudFormation::Init': {
                        'configSets': {'default': ['config']},
                        'config': {'commands': {'key_1': {'command': 'value_1'}}}
                    }
                }
            }
        }
    )
Ejemplo n.º 2
0
def test_resource__depends_on__resource():
    template = {}
    Resource('abcde').type('type').depends_on(
        Resource('res_name')).to_template(template)
    assert_equal(template,
                 {'abcde': {
                     'Type': 'type',
                     'DependsOn': 'res_name'
                 }})
Ejemplo n.º 3
0
def test_resource__add_property():
    template = {}
    resource = Resource('abcde').type('type')
    resource.add_property({'key_1': 'value_1'})
    resource.add_property({'key_2': 'value_2'})
    resource.to_template(template)
    assert_equal(
        template,
        {'abcde': {'Type': 'type', 'Properties': {'key_1': 'value_1', 'key_2': 'value_2'}}}
    )
Ejemplo n.º 4
0
def test_resource__metadata__config_sets():
    template = {}
    resource = Resource('abcde').type('type')
    resource.metadata(
        CfnInitMetadata.of([
            CfnInitMetadata.Init([
                CfnInitMetadata.ConfigSet('default', [
                    CfnInitMetadata.Config('config').commands(
                        'key_1', 'value_1')
                ])
            ])
        ]))
    resource.to_template(template)
    assert_equal(
        template, {
            'abcde': {
                'Type': 'type',
                'Metadata': {
                    'AWS::CloudFormation::Init': {
                        'configSets': {
                            'default': ['config']
                        },
                        'config': {
                            'commands': {
                                'key_1': {
                                    'command': 'value_1'
                                }
                            }
                        }
                    }
                }
            }
        })
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def test_resource__depends_on__named_object():
    class NamedObject(Resource):
        def __init__(self):
            super(NamedObject, self).__init__('res_name')

    template = {}
    Resource('abcde').type('type').depends_on(
        NamedObject()).to_template(template)
    assert_equal(template,
                 {'abcde': {
                     'Type': 'type',
                     'DependsOn': 'res_name'
                 }})
Ejemplo n.º 7
0
def test_resource__properties():
    template = {}
    Resource('abcde').type('type').properties([{
        'key_1': 'value_1'
    }, {
        'key_2': 'value_2'
    }]).to_template(template)
    assert_equal(
        template, {
            'abcde': {
                'Type': 'type',
                'Properties': {
                    'key_1': 'value_1',
                    'key_2': 'value_2'
                }
            }
        })
Ejemplo n.º 8
0
def test_resource__user_data():
    template = {}
    Resource('abcde') \
        .type('type') \
        .properties([UserData.of(['value_1', 'value_2', 'value_3'])]) \
        .to_template(template)
    assert_equal(
        template, {
            'abcde': {
                'Type': 'type',
                'Properties': {
                    'UserData': {
                        'Fn::Base64': {
                            'Fn::Join':
                            ['', ['value_1', 'value_2', 'value_3']]
                        }
                    }
                }
            }
        })
Ejemplo n.º 9
0
def test_resource__add_property():
    template = {}
    resource = Resource('abcde').type('type')
    resource.add_property({'key_1': 'value_1'})
    resource.add_property({'key_2': 'value_2'})
    resource.to_template(template)
    assert_equal(
        template, {
            'abcde': {
                'Type': 'type',
                'Properties': {
                    'key_1': 'value_1',
                    'key_2': 'value_2'
                }
            }
        })
Ejemplo n.º 10
0
def generate():
    t = Template(description='td-agent Template')

    ami_id = t.parameters(
        Parameter('AmiId').description(
            'EC2 machine image ID of the sample server').type(
                'AWS::EC2::Image::Id'))

    instance_type = t.parameters(
        Parameter('InstanceType').description(
            'EC2 instance type of the sample server').type(
                'AWS::EC2::KeyPair::KeyName'))

    security_group_ids = t.parameters(
        Parameter('SecurityGroupIds').description(
            'List of security group IDs of the sample server').type(
                'List<AWS::EC2::SecurityGroup::Id>'))

    key_name = t.parameters(
        Parameter('KeyName').description(
            'Name of an existing EC2 key pair to enable SSH access to the sample server'
        ).type('AWS::EC2::KeyPair::KeyName'))

    subnet_id = t.parameters(
        Parameter('SubnetId').description(
            'Subnet ID which the sample server runs on').type(
                'AWS::EC2::Subnet::Id'))

    sample_server = t.resources(
        Resource('MongoDBServer').type('AWS::EC2::Instance').properties([
            Attributes.of('ImageId', ami_id),
            Attributes.of('InstanceType', instance_type),
            Attributes.of('SecurityGroupIds', security_group_ids),
            Attributes.of('KeyName', key_name),
            Attributes.of('SubnetId', subnet_id)
        ]))

    sample_server.add_property(
        UserData.from_files(
            [('files/x-shellscript', 'x-shellscript'),
             ('files/cloud-config', 'cloud-config')], {
                 'stack_id': Pseudos.stack_id(),
                 'resource_name': sample_server.name,
                 'region': Pseudos.region()
             }))

    sample_server.metadata(
        CfnInitMetadata.of([
            CfnInitMetadata.Init([
                CfnInitMetadata.ConfigSet('default', [
                    CfnInitMetadata.Config('SetupRepos').commands(
                        'import_td-agent_GPG-KEY',
                        'rpm --import https://packages.treasuredata.com/GPG-KEY-td-agent'
                    ),
                    CfnInitMetadata.Config('Install').packages('yum', 'dstat').
                    packages('yum', 'td-agent').commands(
                        'install_td-agent_plugin',
                        'td-agnet-gem install fluent-plugin-dstat fluent-plugin-map fluent-plugin-forest'
                    ),
                    CfnInitMetadata.Config('Configure').files(
                        '/etc/td-agent/td-agent.conf',
                        local_file_path='files/td-agent.conf',
                        mode='000644',
                        owner='root',
                        group='root'),
                    CfnInitMetadata.Config('Start').services(
                        'sysvinit',
                        'td-agent',
                        enabled=True,
                        ensure_running=True)
                ])
            ])
        ]))

    return t
Ejemplo n.º 11
0
def generate():
    t = Template(description='MongoDB Template')

    ami_id = t.parameters(
        Parameter('AmiId').description(
            'EC2 machine image ID of the MongoDB server').type(
                'AWS::EC2::Image::Id'))

    instance_type = t.parameters(
        Parameter('InstanceType').description(
            'EC2 instance type of the MongoDB server').type(
                'AWS::EC2::KeyPair::KeyName'))

    security_group_ids = t.parameters(
        Parameter('SecurityGroupIds').description(
            'List of security group IDs of the MongoDB server').type(
                'List<AWS::EC2::SecurityGroup::Id>'))

    key_name = t.parameters(
        Parameter('KeyName').description(
            'Name of an existing EC2 key pair to enable SSH access to the MongoDB server'
        ).type('AWS::EC2::KeyPair::KeyName'))

    subnet_id = t.parameters(
        Parameter('SubnetId').description(
            'Subnet ID which the MongoDB server runs on').type(
                'AWS::EC2::Subnet::Id'))

    mongodb_server = t.resources(
        Resource('MongoDBServer').type('AWS::EC2::Instance').properties([
            Attributes.of('ImageId', ami_id),
            Attributes.of('InstanceType', instance_type),
            Attributes.of('SecurityGroupIds', security_group_ids),
            Attributes.of('KeyName', key_name),
            Attributes.of('SubnetId', subnet_id)
        ]))

    mongodb_server.add_property(
        UserData.from_files(
            [('files/x-shellscript', 'x-shellscript'),
             ('files/cloud-config', 'cloud-config')], {
                 'stack_id': Pseudos.stack_id(),
                 'resource_name': mongodb_server.name,
                 'region': Pseudos.region()
             }))

    mongodb_server.metadata(
        CfnInitMetadata.of([
            CfnInitMetadata.Init([
                CfnInitMetadata.ConfigSet('default', [
                    CfnInitMetadata.Config('SetupRepos').files(
                        '/etc/yum.repos.d/mongodb-org.3.2.repo',
                        local_file_path='files/mongodb-org-3.2.repo',
                        mode='00644',
                        owner='root',
                        group='root').
                    commands(
                        'import_mongodb_public_key',
                        'rpm --import https://www.mongodb.org/static/pgp/server-3.2.asc'
                    ),
                    CfnInitMetadata.Config('DownloadFromS3').files(
                        '/path/to',
                        source='https://s3.amazonaws.com/bucket/object',
                        mode='000644',
                        owner='root',
                        group='root',
                        authentication='s3credentials'),
                    CfnInitMetadata.Config('Install').packages(
                        'yum', 'mongodb-org-server').packages(
                            'yum', 'mongodb-org-shell').packages(
                                'yum', 'mongodb-org-tools'),
                    CfnInitMetadata.Config('Configure').files(
                        '/etc/mongod.conf',
                        local_file_path='files/mongod.conf',
                        mode='000644',
                        owner='root',
                        group='root').commands(
                            'make_data_directory',
                            'mkdir -p /data/db; chmod 777 /data/db'),
                    CfnInitMetadata.Config('Start').services(
                        'sysvinit',
                        'mongod',
                        enabled=True,
                        ensure_running=True)
                ])
            ]),
            CfnInitMetadata.Authentication('s3credentials',
                                           'S3').role_name('some-role')
        ]))

    return t
Ejemplo n.º 12
0
def test_resource__depends_on__other():
    Resource('abcde').type('type').depends_on({})