Beispiel #1
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS Autoscaling Lifecycle Hook"""
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = params.get(RESOURCE_NAME)
    if not resource_id:
        resource_id = \
            iface.resource_id or \
            utils.get_resource_id(
                ctx.node,
                ctx.instance,
                use_instance_id=True)
        params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    # Ensure the $GROUP_NAME parameter is populated.
    autoscaling_group = params.get(GROUP_NAME)
    if not autoscaling_group:
        autoscaling_group = \
            utils.find_resource_id_by_type(
                ctx.instance, GROUP_TYPE)
        params[GROUP_NAME] = autoscaling_group
    ctx.instance.runtime_properties[GROUP_NAME] = \
        autoscaling_group
    if not iface.resource_id:
        setattr(iface, 'resource_id', params.get(RESOURCE_NAME))

    # Actually create the resource
    iface.create(params)
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM Policy'''
    # Build API params
    params = \
        utils.clean_params(
            dict() if not resource_config else resource_config.copy())
    resource_id = \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True
        ) or iface.resource_id
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    if 'PolicyDocument' in params and \
            isinstance(params['PolicyDocument'], dict):
        params['PolicyDocument'] = json_dumps(params['PolicyDocument'])
    # Actually create the resource
    create_response = iface.create(params)
    resource_id = create_response['Policy']['PolicyName']
    iface.update_resource_id(resource_id)
    utils.update_resource_id(ctx.instance, resource_id)
    utils.update_resource_arn(ctx.instance, create_response['Policy']['Arn'])
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS EC2 Security Group'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    vpc_id = params.get(VPC_ID)

    # Try to get the group_name and if it does not exits then try to
    # generate new one based on instance_id
    group_name = params.get(GROUP_NAME)
    params[GROUP_NAME] = utils.get_ec2_vpc_resource_name(group_name)

    if not vpc_id:
        vpc = \
            utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE) or utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE_DEPRECATED)

    if vpc_id or vpc:
        params[VPC_ID] = \
            vpc_id or \
            vpc.target.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ID)

    # Actually create the resource
    create_response = iface.create(params)
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    group_id = create_response.get(GROUPID, '')
    iface.update_resource_id(group_id)
    utils.update_resource_id(ctx.instance, group_id)
Beispiel #4
0
def delete(ctx, iface, resource_config, **_):
    '''Deletes an AWS ELB load balancer'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    if LB_ARN not in params.keys():
        params.update({LB_ARN: iface.properties.get(LB_ARN)})
    iface.delete(params)
Beispiel #5
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS ELB classic policy"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = \
        iface.resource_id or \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True)
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)
    ctx.instance.runtime_properties[RESOURCE_NAME] = \
        resource_id

    lb_name = params.get(LB_NAME)
    if not lb_name:
        targs = \
            utils.find_rels_by_node_type(
                ctx.instance,
                LB_TYPE)
        lb_name = \
            targs[0].target.instance.runtime_properties[
                EXTERNAL_RESOURCE_ID]
        params.update({LB_NAME: lb_name})

    ctx.instance.runtime_properties[LB_NAME] = \
        lb_name

    # Actually create the resource
    iface.create(params)
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS Lambda Permission'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    function_rels = \
        utils.find_rels_by_node_type(
            ctx.instance, FUNCTION_TYPE)

    lambda_function = None if len(function_rels) != 1 else function_rels[0]
    if lambda_function:
        params[FUNCTION_NAME] = utils.get_resource_id(
            node=lambda_function.target.node,
            instance=lambda_function.target.instance,
            raise_on_missing=False)

    if STATEMENT_ID not in params and iface.resource_id:
        params.update({'StatementId': iface.resource_id})

    create_response = iface.create(params)

    statement = create_response.get('Statement')
    # The actual value for key "statement" is not a python dict type,
    # so it is required to check if it is "unicode" and then convert it back
    # as python dict type

    if statement:
        if isinstance(statement, unicode):
            statement = json.loads(statement)

        resource_id = statement['Sid'] if statement.get('Sid') else None
        iface.update_resource_id(resource_id)
        utils.update_resource_id(ctx.instance, resource_id)
        utils.update_resource_arn(ctx.instance, resource_id)
def create(ctx, iface, resource_config, **_):
    """Creates an AWS S3 Bucket Policy"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Get the bucket name from either params or a relationship.
    bucket_name = params.get(BUCKET)
    if not bucket_name:
        targ = utils.find_rel_by_node_type(ctx.instance, BUCKET_TYPE)
        bucket_name = \
            targ.target.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ID
            )
        params[BUCKET] = bucket_name
    ctx.instance.runtime_properties[BUCKET] = bucket_name
    utils.update_resource_id(ctx.instance, bucket_name)

    # Get the policy name from either params or a relationship.
    bucket_policy = params.get(POLICY)
    if not isinstance(bucket_policy, basestring):
        bucket_policy = json.dumps(bucket_policy)
        params[POLICY] = bucket_policy
    ctx.instance.runtime_properties[POLICY] = bucket_policy

    # Actually create the resource
    iface.create(params)
def create(ctx, iface, resource_config, **_):
    """
    Creates an AWS EC2 EBS Volume
    :param ctx:
    :param iface:
    :param resource_config:
    :param _:
    :return:
    """
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Create ebs resource
    create_response = iface.create(params)
    # Check if the resource created
    if create_response:
        ctx.instance.runtime_properties['eps_create'] =\
            utils.JsonCleanuper(create_response).to_dict()

        # Update the esp_id (volume_id)
        esp_id = create_response.get(VOLUME_ID, '')
        utils.update_resource_id(ctx.instance, esp_id)
        iface.update_resource_id(esp_id)

    else:
        raise NonRecoverableError(
            '{0} ID# "{1}" reported an empty response'
            .format(RESOURCE_TYPE_VOLUME, iface.resource_id))
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS Lambda Permission'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    function_rels = \
        utils.find_rels_by_node_type(
            ctx.instance, FUNCTION_TYPE)

    lambda_function = None if len(function_rels) != 1 else function_rels[0]
    if lambda_function:
        params[FUNCTION_NAME] = utils.get_resource_id(
            node=lambda_function.target.node,
            instance=lambda_function.target.instance,
            raise_on_missing=False)

    if STATEMENT_ID not in params and iface.resource_id:
        params.update({'StatementId': iface.resource_id})

    create_response = iface.create(params)

    statement = create_response.get('Statement')
    # The actual value for key "statement" is not a python dict type,
    # so it is required to check if it is "unicode" and then convert it back
    # as python dict type

    if statement:
        if isinstance(statement, text_type):
            statement = json.loads(statement)

        resource_id = statement['Sid'] if statement.get('Sid') else None
        iface.update_resource_id(resource_id)
        utils.update_resource_id(ctx.instance, resource_id)
        utils.update_resource_arn(ctx.instance, resource_id)
def delete(ctx, iface, resource_config, **_):
    '''Deletes an AWS ELB load balancer'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    if LB_ARN not in params.keys():
        params.update({LB_ARN: iface.properties.get(LB_ARN)})
    iface.delete(params)
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS ELB target group'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    # TG attributes are only applied in modify operation.
    params.pop(GRP_ATTR, {})
    if VPC_ID not in params.keys():
        targs = \
            utils.find_rels_by_node_type(
                ctx.instance,
                VPC_TYPE) or utils.find_rels_by_node_name(
                ctx.instance,
                VPC_TYPE_DEPRECATED)
        tg_attr = targs[0].target.instance.runtime_properties
        params[VPC_ID] = \
            tg_attr.get(EXTERNAL_RESOURCE_ID)
        del targs

    # Actually create the resource
    create_response = iface.create(params)
    iface.update_resource_id(
        create_response['TargetGroups'][0][TARGETGROUP_ARN])
    utils.update_resource_id(
        ctx.instance, create_response['TargetGroups'][0][TARGETGROUP_ARN])
    utils.update_resource_arn(
        ctx.instance, create_response['TargetGroups'][0][TARGETGROUP_ARN])
        def wrapper_inner(*argc, **kwargs):
            ctx = kwargs.get('ctx')
            iface = kwargs.get('iface')
            resource_config = kwargs.get('resource_config')

            # Create a copy of the resource config for clean manipulation.
            params = utils.clean_params(
                dict() if not resource_config else resource_config.copy())
            if params_priority:
                # params value will overwrite other resources id
                resource_id = params.get(resource_name)
                if not resource_id:
                    resource_id = \
                        iface.resource_id or \
                        utils.get_resource_id(
                            ctx.node,
                            ctx.instance,
                            use_instance_id=True)
                    params[resource_name] = resource_id
            else:
                # resource id from runtime has priority over params
                resource_id = \
                    iface.resource_id or \
                    utils.get_resource_id(
                        ctx.node,
                        ctx.instance,
                        params.get(resource_name),
                        use_instance_id=True)
                params[resource_name] = resource_id
                ctx.instance.runtime_properties[resource_name] = \
                    resource_id

            utils.update_resource_id(ctx.instance, resource_id)
            kwargs['params'] = params
            return function(*argc, **kwargs)
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS EC2 Security Group'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    vpc_id = params.get(VPC_ID)

    # Try to get the group_name and if it does not exits then try to
    # generate new one based on instance_id
    group_name = params.get(GROUP_NAME)
    params[GROUP_NAME] = utils.get_ec2_vpc_resource_name(group_name)

    if not vpc_id:
        vpc = \
            utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE) or utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE_DEPRECATED)

    if vpc_id or vpc:
        params[VPC_ID] = \
            vpc_id or \
            vpc.target.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ID)

    # Actually create the resource
    create_response = iface.create(params)
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    group_id = create_response.get(GROUPID, '')
    iface.update_resource_id(group_id)
    utils.update_resource_id(
        ctx.instance, group_id)
def create(ctx, iface, resource_config, **_):
    """Creates an AWS S3 Bucket Policy"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Get the bucket name from either params or a relationship.
    bucket_name = params.get(BUCKET)
    if not bucket_name:
        targ = utils.find_rel_by_node_type(
            ctx.instance,
            BUCKET_TYPE
        )
        bucket_name = \
            targ.target.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ID
            )
        params[BUCKET] = bucket_name
    ctx.instance.runtime_properties[BUCKET] = bucket_name
    utils.update_resource_id(ctx.instance, bucket_name)

    # Get the policy name from either params or a relationship.
    bucket_policy = params.get(POLICY)
    if not isinstance(bucket_policy, basestring):
        bucket_policy = json.dumps(bucket_policy)
        params[POLICY] = bucket_policy
    ctx.instance.runtime_properties[POLICY] = bucket_policy

    # Actually create the resource
    iface.create(params)
Beispiel #15
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM Role Policy'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True
        ) or iface.resource_id
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    # Add RoleName
    role_name = params.get(ROLE_NAME, '')
    if not role_name:
        params[ROLE_NAME] = \
            utils.find_resource_id_by_type(
                ctx.instance,
                ROLE_TYPE)
    if 'PolicyDocument' in params and \
            isinstance(params['PolicyDocument'], dict):
        params['PolicyDocument'] = json_dumps(params['PolicyDocument'])

    # Actually create the resource
    iface.create(params)
Beispiel #16
0
def create(ctx, iface, resource_config, **_):
    """
    Creates an AWS EC2 EBS Volume
    :param ctx:
    :param iface:
    :param resource_config:
    :param _:
    :return:
    """
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Create ebs resource
    create_response = iface.create(params)
    # Check if the resource created
    if create_response:
        ctx.instance.runtime_properties['eps_create'] =\
            utils.JsonCleanuper(create_response).to_dict()

        # Update the esp_id (volume_id)
        esp_id = create_response.get(VOLUME_ID, '')
        utils.update_resource_id(ctx.instance, esp_id)
        iface.update_resource_id(esp_id)

    else:
        raise NonRecoverableError(
            '{0} ID# "{1}" reported an empty response'.format(
                RESOURCE_TYPE_VOLUME, iface.resource_id))
Beispiel #17
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS ELB load balancer'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = \
        params.get('Name') or \
        iface.resource_id or \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            use_instance_id=True)
    params['Name'] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    # LB attributes are only applied in modify operation.
    params.pop(LB_ATTR, {})
    # Add Subnets
    subnets_from_params = params.get(SUBNETS, [])
    subnets = \
        utils.find_rels_by_node_type(
            ctx.instance,
            SUBNET_TYPE) or utils.find_rels_by_node_name(
            ctx.instance,
            SUBNET_TYPE_DEPRECATED)
    for subnet in subnets:
        subnet_id = \
            subnet.target.instance.runtime_properties[EXTERNAL_RESOURCE_ID]
        subnets_from_params.append(subnet_id)
    params[SUBNETS] = subnets_from_params
    # Add Security Groups
    secgroups_from_params = params.get(SECGROUPS, [])
    secgroups = \
        utils.find_rels_by_node_type(
            ctx.instance,
            SECGROUP_TYPE) or \
        utils.find_rels_by_node_type(
            ctx.instance,
            SECGROUP_TYPE_DEPRECATED)

    for secgroup in secgroups:
        secgroup_id = \
            secgroup.target.instance.runtime_properties[EXTERNAL_RESOURCE_ID]
        secgroups_from_params.append(secgroup_id)
    params[SECGROUPS] = secgroups_from_params

    # Actually create the resource
    output = iface.create(params)
    lb_id = output['LoadBalancers'][0][RESOURCE_NAME]
    iface.resource_id = lb_id
    try:
        utils.update_resource_id(ctx.instance, lb_id)
        utils.update_resource_arn(ctx.instance,
                                  output['LoadBalancers'][0][LB_ARN])
    except (IndexError, KeyError) as e:
        raise NonRecoverableError(
            '{0}: {1} or {2} not located in response: {3}'.format(
                str(e), RESOURCE_NAME, LB_ARN, output))
Beispiel #18
0
def create(ctx, iface, resource_config, **_):
    """
    Creates an AWS EC2 EBS Volume
    :param ctx:
    :param iface:
    :param resource_config:
    :param _:
    :return:
    """
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Actually create ebs resource
    region_name = ctx.node.properties['client_config']['region_name']
    use_available_zones = ctx.node.properties.get('use_available_zones', False)
    try:
        create_response = iface.create(params)
    except CapacityNotAvailableError:
        if use_available_zones:
            ctx.logger.warn(
                "The Availability Zone chosen {0} "
                "is not available".format(params['AvailabilityZone']))
            valid_zone = \
                iface.get_available_zone({
                    'Filters': [
                        {'Name': 'region-name', 'Values': [region_name]}
                    ]
                })
            if valid_zone:
                ctx.logger.info(
                    "using {0} Availability Zone instead".format(valid_zone))
                params['AvailabilityZone'] = valid_zone
                create_response = iface.create(params)
            else:
                raise NonRecoverableError(
                    "no available Availability Zones "
                    "in region {0}".format(region_name))
        else:
            raise NonRecoverableError(
                "The Availability Zone chosen "
                "{0} is not available".format(params['AvailabilityZone']))
    # Check if the resource created
    if create_response:
        ctx.instance.runtime_properties['eps_create'] =\
            utils.JsonCleanuper(create_response).to_dict()

        # Update the esp_id (volume_id)
        esp_id = create_response.get(VOLUME_ID, '')
        utils.update_resource_id(ctx.instance, esp_id)
        iface.update_resource_id(esp_id)

    else:
        raise NonRecoverableError(
            '{0} ID# "{1}" reported an empty response'
            .format(RESOURCE_TYPE_VOLUME, iface.resource_id))
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS EC2 Subnet'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    vpc_id = params.get(VPC_ID)
    cidr_block = params.get(CIDR_BLOCK)
    ipv6_cidr_block = params.get(IPV6_CIDR_BLOCK)

    # If either of these values is missing,
    # they must be filled from a connected VPC.
    if not vpc_id or not cidr_block:
        targ = \
            utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE) or utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE_DEPRECATED)

        # Attempt to use the VPC ID from parameters.
        # Fallback to connected VPC.
        params[VPC_ID] = \
            vpc_id or \
            targ.target.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ID)
        # Attempt to use the CIDR Block from parameters.
        # Fallback to connected VPC.
        params[CIDR_BLOCK] = \
            cidr_block or \
            targ.instance.runtime_properties.get(
                'resource_config', {}).get(CIDR_BLOCK)

    # If ipv6 cidr block is provided by user, then we need to make sure that
    # The subnet size must use a /64 prefix length
    if ipv6_cidr_block:
        ipv6_cidr_block = ipv6_cidr_block[:-2] + '64'
        params[IPV6_CIDR_BLOCK] = ipv6_cidr_block

    # Actually create the resource
    create_response = iface.create(params)[SUBNET]
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    subnet_id = create_response.get(SUBNET_ID)
    iface.update_resource_id(subnet_id)
    utils.update_resource_id(ctx.instance, subnet_id)

    modify_subnet_attribute_args = \
        _.get('modify_subnet_attribute_args')
    if modify_subnet_attribute_args:
        modify_subnet_attribute_args[SUBNET_ID] = \
            subnet_id
        iface.modify_subnet_attribute(
            modify_subnet_attribute_args)
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS EC2 Subnet'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    vpc_id = params.get(VPC_ID)
    cidr_block = params.get(CIDR_BLOCK)
    ipv6_cidr_block = params.get(IPV6_CIDR_BLOCK)

    # If either of these values is missing,
    # they must be filled from a connected VPC.
    if not vpc_id or not cidr_block:
        targ = \
            utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE) or utils.find_rel_by_node_type(
                ctx.instance,
                VPC_TYPE_DEPRECATED)

        # Attempt to use the VPC ID from parameters.
        # Fallback to connected VPC.
        params[VPC_ID] = \
            vpc_id or \
            targ.target.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ID)
        # Attempt to use the CIDR Block from parameters.
        # Fallback to connected VPC.
        params[CIDR_BLOCK] = \
            cidr_block or \
            targ.instance.runtime_properties.get(
                'resource_config', {}).get(CIDR_BLOCK)

    # If ipv6 cidr block is provided by user, then we need to make sure that
    # The subnet size must use a /64 prefix length
    if ipv6_cidr_block:
        ipv6_cidr_block = ipv6_cidr_block[:-2] + '64'
        params[IPV6_CIDR_BLOCK] = ipv6_cidr_block

    # Actually create the resource
    create_response = iface.create(params)[SUBNET]
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    subnet_id = create_response.get(SUBNET_ID)
    iface.update_resource_id(subnet_id)
    utils.update_resource_id(ctx.instance, subnet_id)

    modify_subnet_attribute_args = \
        _.get('modify_subnet_attribute_args')
    if modify_subnet_attribute_args:
        modify_subnet_attribute_args[SUBNET_ID] = \
            subnet_id
        iface.modify_subnet_attribute(modify_subnet_attribute_args)
Beispiel #21
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS Lambda Function'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    if RESOURCE_ID not in params:
        params[RESOURCE_ID] = iface.resource_id
    vpc_config = params.get('VpcConfig', dict())

    # Attach a Subnet Group if it exists
    subnet_ids = _get_subnets_to_attach(ctx, vpc_config)
    if subnet_ids:
        vpc_config['SubnetIds'] = subnet_ids

    # Attach any security groups if they exist
    security_groups = _get_security_groups_to_attach(ctx, vpc_config)
    if security_groups:
        vpc_config['SecurityGroupIds'] = security_groups

    params['VpcConfig'] = vpc_config
    # Attach an IAM Role if it exists
    iam_role = _get_iam_role_to_attach(ctx)
    if iam_role:
        params['Role'] = iam_role

    # Handle user-profided code ZIP file
    if params.get('Code', dict()).get('ZipFile'):
        codezip = params['Code']['ZipFile']
        ctx.logger.debug('ZipFile: "%s" (%s)' % (codezip, type(codezip)))
        if not path_exists(codezip):
            codezip = ctx.download_resource(codezip)
            ctx.logger.debug('Downloaded resource: "%s"' % codezip)
            with open(codezip, mode='rb') as _file:
                params['Code']['ZipFile'] = _file.read()
            ctx.logger.debug('Deleting resource: "%s"' % codezip)
            os_remove(codezip)
        else:
            with open(codezip, mode='rb') as _file:
                params['Code']['ZipFile'] = _file.read()
    # Actually create the resource
    create_response = iface.create(params)
    resource_id = create_response['FunctionName']
    utils.update_resource_id(ctx.instance, resource_id)
    utils.update_resource_arn(ctx.instance, create_response['FunctionArn'])

    # Save vpc_config to be used later on when remove eni created by invoke
    # function
    if vpc_config and create_response.get('VpcConfig'):
        ctx.instance.runtime_properties['vpc_config'] =\
            create_response['VpcConfig']
Beispiel #22
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS ELB classic load balancer"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = \
        iface.resource_id or \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True)
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)
    ctx.instance.runtime_properties[RESOURCE_NAME] = \
        resource_id

    # Add Subnets
    subnets_list = params.get(SUBNETS, [])
    params[SUBNETS] = \
        utils.add_resources_from_rels(
            ctx.instance,
            SUBNET_TYPE,
            subnets_list) or utils.add_resources_from_rels(
            ctx.instance,
            SUBNET_TYPE_DEPRECATED,
            subnets_list)

    # Add Security Groups
    secgroups_list = params.get(SECGROUPS, [])
    params[SECGROUPS] = \
        utils.add_resources_from_rels(
            ctx.instance,
            SECGROUP_TYPE,
            secgroups_list) or \
        utils.add_resources_from_rels(
            ctx.instance,
            SECGROUP_TYPE_DEPRECATED,
            secgroups_list)

    create_response = iface.create(params)

    # Actually create the resource
    ctx.instance.runtime_properties['DNSName'] = \
        create_response['DNSName']
    ctx.instance.runtime_properties['create_response'] = \
        create_response
def create(ctx, iface, resource_config, **_):
    """Creates an AWS EC2 ElasticIP"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Actually create the resource
    create_response = iface.create(params)
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    elasticip_id = create_response.get(ELASTICIP_ID, '')
    iface.update_resource_id(elasticip_id)
    utils.update_resource_id(ctx.instance, elasticip_id)
    ctx.instance.runtime_properties['allocation_id'] = \
        create_response.get(ALLOCATION_ID)
def create(ctx, iface, resource_config, **_):
    """Creates an AWS EC2 ElasticIP"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Actually create the resource
    create_response = iface.create(params)
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    elasticip_id = create_response.get(ELASTICIP_ID, '')
    iface.update_resource_id(elasticip_id)
    utils.update_resource_id(ctx.instance, elasticip_id)
    ctx.instance.runtime_properties['allocation_id'] = \
        create_response.get(ALLOCATION_ID)
def modify(ctx, iface, resource_config, **_):
    '''modify an AWS ELB load balancer attributes'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    if LB_ARN not in params.keys():
        params.update(
            {LB_ARN: ctx.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ARN)})
    modify_params_attributes = params.pop(LB_ATTR, {})
    if modify_params_attributes:
        # Add the LB ARN
        modify_params = {}
        modify_params[LB_ARN] = params.get(LB_ARN)
        modify_params[LB_ATTR] = modify_params_attributes
        # Actually modify the resource
        attributes = iface.modify_attribute(modify_params)
        ctx.instance.runtime_properties['resource_config'][LB_ATTR] = \
            attributes
Beispiel #26
0
def modify(ctx, iface, resource_config, **_):
    '''modify an AWS ELB load balancer attributes'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    if LB_ARN not in params:
        params.update(
            {LB_ARN: ctx.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ARN)})
    modify_params_attributes = params.pop(LB_ATTR, {})
    if modify_params_attributes:
        # Add the LB ARN
        modify_params = {}
        modify_params[LB_ARN] = params.get(LB_ARN)
        modify_params[LB_ATTR] = modify_params_attributes
        # Actually modify the resource
        attributes = iface.modify_attribute(modify_params)
        ctx.instance.runtime_properties['resource_config'][LB_ATTR] = \
            attributes
Beispiel #27
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM Group'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = \
        iface.resource_id or \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True)
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)
    # Actually create the resource
    res_id, res_arn = iface.create(params)
    utils.update_resource_id(ctx.instance, res_id)
    utils.update_resource_arn(ctx.instance, res_arn)
Beispiel #28
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM Role'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True
        ) or iface.resource_id
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    if 'AssumeRolePolicyDocument' in params and \
            isinstance(params['AssumeRolePolicyDocument'], dict):
        params['AssumeRolePolicyDocument'] = \
            json_dumps(params['AssumeRolePolicyDocument'])

    # Actually create the resource
    create_response = iface.create(params)
    resource_id = create_response['Role']['RoleName']
    iface.update_resource_id(resource_id)
    utils.update_resource_id(ctx.instance, resource_id)
    utils.update_resource_arn(
        ctx.instance, create_response['Role']['Arn'])

    # attach policy role
    policies_arn = []
    policies = _.get('modify_role_attribute_args', [])
    for policy in policies:
        payload = dict()
        payload['RoleName'] = resource_id
        payload['PolicyArn'] = policy['PolicyArn']
        policies_arn.append(payload['PolicyArn'])
        iface.attach_policy(payload)

    # If there are policies added attached to role, then we need to make
    # sure that when uninstall triggers, all the attached policies arn are
    # available to detach
    if policies_arn:
        ctx.instance.runtime_properties['policies'] = policies_arn
Beispiel #29
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS EC2 Vpc'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Actually create the resource
    create_response = iface.create(params)[VPC]
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()

    vpc_id = create_response.get(VPC_ID, '')
    iface.update_resource_id(vpc_id)
    utils.update_resource_id(ctx.instance, vpc_id)

    modify_vpc_attribute_args = \
        _.get('modify_vpc_attribute_args')
    if modify_vpc_attribute_args:
        modify_vpc_attribute_args[VPC_ID] = \
            vpc_id
        iface.modify_vpc_attribute(modify_vpc_attribute_args)
Beispiel #30
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS S3 Bucket"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = params.get(RESOURCE_NAME)
    if not resource_id:
        resource_id = \
            iface.resource_id or \
            utils.get_resource_id(
                ctx.node,
                ctx.instance,
                use_instance_id=True)
        params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    # Actually create the resource
    bucket = iface.create(resource_config)
    ctx.instance.runtime_properties[LOCATION] = \
        bucket.get(LOCATION)
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS ELB rule'''
    # Build API params
    resource_config = \
        resource_config or ctx.instance.runtime_properties['resource_config']
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    if LISTENER_ARN not in params:
        targs = \
            utils.find_rels_by_node_type(
                ctx.instance,
                LISTENER_TYPE)
        listener_arn = \
            targs[0].target.instance.runtime_properties[EXTERNAL_RESOURCE_ARN]
        params.update({LISTENER_ARN: listener_arn})
        del targs

    for action in params.get('Actions', []):
        target_grp = action.get(TARGET_ARN)
        if not ARN_MATCHER.match(action.get(target_grp, '')):
            targs = \
                utils.find_rels_by_node_type(
                    ctx.instance,
                    TARGET_TYPE)
            for targ in targs:
                target_group_arn = \
                    targ.target.instance.runtime_properties[
                        EXTERNAL_RESOURCE_ARN]
                if targ.target.node.name == target_grp:
                    action.update({TARGET_ARN: target_group_arn})

    # Actually create the resource
    create_response = iface.create(params)
    iface.update_resource_id(
        create_response['Rules'][0][RULE_ARN])
    utils.update_resource_id(
        ctx.instance, create_response['Rules'][0][RULE_ARN])
    utils.update_resource_arn(
        ctx.instance, create_response['Rules'][0][RULE_ARN])
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS EC2 Vpc'''
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Actually create the resource
    create_response = iface.create(params)[VPC]
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()

    vpc_id = create_response.get(VPC_ID, '')
    iface.update_resource_id(vpc_id)
    utils.update_resource_id(
        ctx.instance, vpc_id)

    modify_vpc_attribute_args = \
        _.get('modify_vpc_attribute_args')
    if modify_vpc_attribute_args:
        modify_vpc_attribute_args[VPC_ID] = \
            vpc_id
        iface.modify_vpc_attribute(
            modify_vpc_attribute_args)
def create(ctx, iface, resource_config, **_):
    """Creates an AWS ELB classic listener"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    lb_name = params.get(LB_NAME)
    if not lb_name:
        targs = \
            utils.find_rels_by_node_type(
                ctx.instance,
                LB_TYPE)
        lb_name = \
            targs[0].target.instance.runtime_properties[
                EXTERNAL_RESOURCE_ID]
        params.update({LB_NAME: lb_name})

    ctx.instance.runtime_properties[LB_NAME] = lb_name

    # Actually create the resource
    iface.create(params)
Beispiel #34
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM User'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = \
        iface.resource_id or \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True)
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    # Actually create the resource
    create_response = iface.create(params)
    resource_id = create_response['User']['UserName']
    iface.update_resource_id(resource_id)
    utils.update_resource_id(ctx.instance, resource_id)
    utils.update_resource_arn(
        ctx.instance, create_response['User']['Arn'])
Beispiel #35
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS ELB rule'''
    # Build API params
    resource_config = \
        resource_config or ctx.instance.runtime_properties['resource_config']
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    if LISTENER_ARN not in params:
        targs = \
            utils.find_rels_by_node_type(
                ctx.instance,
                LISTENER_TYPE)
        listener_arn = \
            targs[0].target.instance.runtime_properties[EXTERNAL_RESOURCE_ARN]
        params.update({LISTENER_ARN: listener_arn})
        del targs

    for action in params.get('Actions', []):
        target_grp = action.get(TARGET_ARN)
        if not ARN_MATCHER.match(action.get(target_grp, '')):
            targs = \
                utils.find_rels_by_node_type(
                    ctx.instance,
                    TARGET_TYPE)
            for targ in targs:
                target_group_arn = \
                    targ.target.instance.runtime_properties[
                        EXTERNAL_RESOURCE_ARN]
                if targ.target.node.name == target_grp:
                    action.update({TARGET_ARN: target_group_arn})

    # Actually create the resource
    create_response = iface.create(params)
    iface.update_resource_id(create_response['Rules'][0][RULE_ARN])
    utils.update_resource_id(ctx.instance,
                             create_response['Rules'][0][RULE_ARN])
    utils.update_resource_arn(ctx.instance,
                              create_response['Rules'][0][RULE_ARN])
def create(ctx, iface, resource_config, **_):
    """Creates an AWS ELB classic listener"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    lb_name = params.get(LB_NAME)
    if not lb_name:
        targs = \
            utils.find_rels_by_node_type(
                ctx.instance,
                LB_TYPE)
        lb_name = \
            targs[0].target.instance.runtime_properties[
                EXTERNAL_RESOURCE_ID]
        params.update({LB_NAME: lb_name})

    ctx.instance.runtime_properties[LB_NAME] = lb_name

    # Actually create the resource
    iface.create(params)
Beispiel #37
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS DynamoDB Table"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    resource_id = \
        iface.resource_id or \
        utils.get_resource_id(
            ctx.node,
            ctx.instance,
            params.get(RESOURCE_NAME),
            use_instance_id=True)
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    # Actually create the resource
    create_respose = iface.create(params)
    resource_id = create_respose['TableDescription']['TableName']
    iface.update_resource_id(resource_id)
    utils.update_resource_id(ctx.instance, resource_id)
    utils.update_resource_arn(ctx.instance,
                              create_respose['TableDescription']['TableArn'])
def create(ctx, iface, resource_config, **_):
    """Creates an AWS EC2 ElasticIP"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Actually create the resource
    create_response = None
    if ctx.node.properties.get('use_unassociated_addresses', False):
        create_response = get_already_allocated_ip(iface.list())
    if not create_response:
        create_response = iface.create(params)
    else:
        ctx.instance.runtime_properties['unassociated_address'] = \
            create_response.get(ELASTICIP_ID)
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    elasticip_id = create_response.get(ELASTICIP_ID, '')
    iface.update_resource_id(elasticip_id)
    utils.update_resource_id(ctx.instance, elasticip_id)
    allocation_id = create_response.get(ALLOCATION_ID)
    iface.update_allocation_id(allocation_id)
    ctx.instance.runtime_properties['allocation_id'] = allocation_id
def create(ctx, iface, resource_config, **_):
    """Creates an AWS Autoscaling Group"""
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    resource_id = params.get(RESOURCE_NAME)
    if not resource_id:
        resource_id = \
            iface.resource_id or \
            utils.get_resource_id(
                ctx.node,
                ctx.instance,
                use_instance_id=True)
        params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    # Try to populate the Launch Configuration field
    # with a relationship
    lc_name = params.get(LC_NAME)
    instance_id = params.get(INSTANCE_ID)

    if not lc_name and not instance_id:
        lc_name = \
            utils.find_resource_id_by_type(
                ctx.instance,
                LC_TYPE)
        if lc_name:
            params.update({LC_NAME: lc_name})

    # If no LC_NAME, try to populate the
    # InstanceId field with a relationship.
    if not lc_name:
        instance_id = \
            utils.find_resource_id_by_type(
                ctx.instance,
                INSTANCE_TYPE)
        params[INSTANCE_ID] = instance_id

    subnet_list_string = params.get(SUBNET_LIST)
    subnet_list = \
        sub("[^\w]", " ", subnet_list_string).split() if \
        subnet_list_string else []

    subnet_list = \
        utils.add_resources_from_rels(
            ctx.instance,
            SUBNET_TYPE,
            subnet_list)
    subnet_list = \
        utils.add_resources_from_rels(
            ctx.instance,
            SUBNET_TYPE_DEPRECATED,
            subnet_list)

    if subnet_list:
        # Remove any duplicate items from subnet list
        subnet_list = list(set(subnet_list))
        params[SUBNET_LIST] = ', '.join(subnet_list)

    # Actually create the resource
    if not iface.resource_id:
        setattr(iface, 'resource_id', params.get(RESOURCE_NAME))
    iface.create(params)
    iface.update_resource_id(iface.properties.get(RESOURCE_NAME))
    utils.update_resource_id(ctx.instance, iface.properties.get(RESOURCE_NAME))
    utils.update_resource_arn(ctx.instance, iface.properties.get(GROUP_ARN))
Beispiel #40
0
def configure(ctx, resource_config, **_):
    '''Configures an AWS IAM Login Profile'''
    # Save the parameters
    ctx.instance.runtime_properties['resource_config'] = \
        utils.clean_params(resource_config)
Beispiel #41
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS Lambda Function'''
    # Build API params
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())
    if RESOURCE_ID not in params:
        params[RESOURCE_ID] = iface.resource_id
    vpc_config = params.get('VpcConfig', dict())
    # Attach a Subnet Group if it exists
    subnet_ids = vpc_config.get('SubnetIds', list())

    subnet_rels = \
        utils.find_rels_by_node_type(
            ctx.instance, SUBNET_TYPE) or \
        utils.find_rels_by_node_type(
            ctx.instance, SUBNET_TYPE)

    for rel in subnet_rels:
        subnet_ids.append(utils.get_resource_id(
            node=rel.target.node,
            instance=rel.target.instance,
            raise_on_missing=True))
    vpc_config['SubnetIds'] = subnet_ids
    # Attach any security groups if they exist
    security_groups = vpc_config.get('SecurityGroupIds', list())

    sg_rels = \
        utils.find_rels_by_node_type(
            ctx.instance, SECGROUP_TYPE) or \
        utils.find_rels_by_node_type(
            ctx.instance, SECGROUP_TYPE_DEPRECATED)

    for rel in sg_rels:
        security_groups.append(
            utils.get_resource_id(
                node=rel.target.node,
                instance=rel.target.instance,
                raise_on_missing=True))
    vpc_config['SecurityGroupIds'] = security_groups
    params['VpcConfig'] = vpc_config
    # Attach an IAM Role if it exists
    iam_role = utils.find_rel_by_node_type(
        ctx.instance, 'cloudify.nodes.aws.iam.Role')
    if iam_role:
        params['Role'] = utils.get_resource_arn(
            node=iam_role.target.node,
            instance=iam_role.target.instance,
            raise_on_missing=True)
    # Handle user-profided code ZIP file
    if params.get('Code', dict()).get('ZipFile'):
        codezip = params['Code']['ZipFile']
        ctx.logger.debug('ZipFile: "%s" (%s)' % (codezip, type(codezip)))
        if not path_exists(codezip):
            codezip = ctx.download_resource(codezip)
            ctx.logger.debug('Downloaded resource: "%s"' % codezip)
            with open(codezip, mode='rb') as _file:
                params['Code']['ZipFile'] = _file.read()
            ctx.logger.debug('Deleting resource: "%s"' % codezip)
            os_remove(codezip)
        else:
            with open(codezip, mode='rb') as _file:
                params['Code']['ZipFile'] = _file.read()
    # Actually create the resource
    create_response = iface.create(params)
    resource_id = create_response['FunctionName']
    utils.update_resource_id(ctx.instance, resource_id)
    utils.update_resource_arn(
        ctx.instance, create_response['FunctionArn'])

    # Save vpc_config to be used later on when remove eni created by invoke
    # function
    if vpc_config and create_response.get('VpcConfig'):
        ctx.instance.runtime_properties['vpc_config'] =\
            create_response['VpcConfig']
def create(ctx, iface, resource_config, **_):
    """Creates an AWS S3 Bucket Object"""

    # Create a copy of the resource config for clean manipulation.
    params = utils.clean_params(
        dict() if not resource_config else resource_config.copy())

    # Get the bucket object key from params
    object_key = params.get(OBJECT_KEY)
    if not object_key:
        raise NonRecoverableError('{0} param is required'.format(OBJECT_KEY))

    utils.update_resource_id(ctx.instance, object_key)
    source_type = ctx.node.properties.get(OBJECT_SOURCE_TYPE)

    cloudify_path = None
    object_body = None

    # If "source_type" is either local or remote then we need to download
    # the file from remote or local directory and then parse the data as
    # bytes and prepared it to be sent on the "Body" param for "put_object"
    # method
    if source_type in [OBJECT_LOCAL_SOURCE, OBJECT_REMOTE_SOURCE]:
        path = ctx.node.properties.get(OBJECT_PATH)
        if not path:
            raise NonRecoverableError(
                'path param must be provided when '
                'source_type is selected as remote or local')

        if source_type == OBJECT_LOCAL_SOURCE:
            cloudify_path = _download_local_file(path)

        elif source_type == OBJECT_REMOTE_SOURCE:
            cloudify_path = _download_remote_file(path)

        try:
            object_body = open(cloudify_path, 'rb')
        except IOError as error:
            _, _, tb = sys.exc_info()
            raise NonRecoverableError(
                'Failed to open file {0},'
                ' with error message {1}'
                ''.format(path,
                          error.strerror,
                          causes=[exception_to_error_cause(error, tb)]))

        # Set the updated path url so that it can
        # be uploaded to the AWS S3 bucket
        params[BUCKET_OBJECT_BODY] = object_body

    # If the "source_type" is "bytes" then the body should provided from the
    #  blueprint and follow the boto3 API documents
    elif source_type == OBJECT_BYTES_SOURCE:
        if not params.get(BUCKET_OBJECT_BODY):
            raise NonRecoverableError('Body param must be provided when '
                                      'source_type is selected as bytes')

    # Get the bucket name from either params or a relationship.
    bucket_name = params.get(BUCKET)
    if not bucket_name:
        targ = utils.find_rel_by_node_type(ctx.instance, BUCKET_TYPE)
        bucket_name = \
            targ.target.instance.runtime_properties.get(
                EXTERNAL_RESOURCE_ID
            )
        params[BUCKET] = bucket_name

    iface.bucket_name = bucket_name
    ctx.instance.runtime_properties[BUCKET] = bucket_name

    # Actually create the resource
    iface.create(params)
def configure(ctx, resource_config, **_):
    '''Configures an AWS IAM Access Key'''
    # Save the parameters
    ctx.instance.runtime_properties['resource_config'] = \
        utils.clean_params(resource_config)