Ejemplo n.º 1
0
 def wrapper_inner(**kwargs):
     '''Inner, worker function'''
     ctx = kwargs['ctx']
     # Add new operation arguments
     kwargs['resource_type'] = resource_type
     kwargs['iface'] = class_decl(
         ctx.source.node,
         logger=ctx.logger,
         resource_id=utils.get_resource_id(
             node=ctx.source.node,
             instance=ctx.source.instance,
             raise_on_missing=True)) if class_decl else None
     kwargs['resource_config'] = kwargs.get('resource_config') or dict()
     # Check if using external
     if ctx.source.node.properties.get('use_external_resource', False):
         resource_id = utils.get_resource_id(
             node=ctx.source.node, instance=ctx.source.instance)
         ctx.logger.info('%s ID# "%s" is user-provided.' %
                         (resource_type, resource_id))
         if not kwargs.get('force_operation', False):
             return
         ctx.logger.warn('%s ID# "%s" has force_operation set.' %
                         (resource_type, resource_id))
     # Execute the function
     ret = function(**kwargs)
     # When modifying nested runtime properties, the internal
     # "dirty checking" mechanism will not know of our changes.
     # This forces the internal tracking to mark the properties as
     # dirty and will be refreshed on next query.
     # pylint: disable=W0212
     ctx.source.instance.runtime_properties._set_changed()
     ctx.target.instance.runtime_properties._set_changed()
     return ret
Ejemplo n.º 2
0
def detach_from(ctx, iface, resource_config, **_):
    '''Detaches an IAM User from something else'''
    if utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.Group'):
        resource_config['UserName'] = iface.resource_id
        IAMGroup(ctx.target.node,
                 logger=ctx.logger,
                 resource_id=utils.get_resource_id(
                     node=ctx.target.node,
                     instance=ctx.target.instance,
                     raise_on_missing=True)).detach_user(resource_config)
    elif utils.is_node_type(ctx.target.node,
                            'cloudify.nodes.aws.iam.LoginProfile'):
        iface.delete_login_profile(resource_config)
    elif utils.is_node_type(ctx.target.node,
                            'cloudify.nodes.aws.iam.AccessKey'):
        resource_config['AccessKeyId'] = utils.get_resource_id(
            node=ctx.target.node,
            instance=ctx.target.instance,
            raise_on_missing=True)
        iface.delete_access_key(resource_config)
    elif utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.Policy'):
        resource_config['PolicyArn'] = utils.get_resource_arn(
            node=ctx.target.node,
            instance=ctx.target.instance,
            raise_on_missing=True)
        iface.detach_policy(resource_config)
Ejemplo n.º 3
0
        def wrapper_inner(**kwargs):
            '''Inner, worker function'''
            ctx = kwargs['ctx']
            props = ctx.node.properties
            runtime_instance_properties = ctx.instance.runtime_properties
            # Override the resource ID if needed
            resource_id = kwargs.get(EXT_RES_ID)
            if resource_id and not \
                    ctx.instance.runtime_properties.get(EXT_RES_ID):
                ctx.instance.runtime_properties[EXT_RES_ID] = resource_id
            if resource_id and not \
                    ctx.instance.runtime_properties.get(EXT_RES_ARN):
                ctx.instance.runtime_properties[EXT_RES_ARN] = resource_id
            # Override any runtime properties if needed
            runtime_properties = kwargs.get('runtime_properties') or dict()
            for key, val in runtime_properties.iteritems():
                ctx.instance.runtime_properties[key] = val
            # Add new operation arguments
            kwargs['resource_type'] = resource_type
            kwargs['iface'] = class_decl(
                ctx.node,
                logger=ctx.logger,
                resource_id=utils.get_resource_id(
                    node=ctx.node,
                    instance=ctx.instance)) if class_decl else None
            if not ignore_properties:
                # Normalize resource_config property
                resource_config = props.get('resource_config') or dict()
                resource_config_kwargs = \
                    resource_config.get('kwargs') or dict()
                if 'kwargs' in resource_config:
                    del resource_config['kwargs']
                resource_config.update(resource_config_kwargs)
                # Update the argument
                kwargs['resource_config'] = kwargs.get('resource_config') or \
                    resource_config or dict()

                # ``resource_config`` could be part of the runtime instance
                # properties, If ``resource_config`` is empty then check if it
                # exists on runtime instance properties
                if not resource_config and runtime_instance_properties \
                        and runtime_instance_properties.get('resource_config'):
                    kwargs['resource_config'] =\
                        runtime_instance_properties['resource_config']

            # Check if using external
            if ctx.node.properties.get('use_external_resource', False):
                resource_id = utils.get_resource_id(node=ctx.node,
                                                    instance=ctx.instance)
                ctx.logger.info('%s ID# "%s" is user-provided.' %
                                (resource_type, resource_id))
                if not kwargs.get('force_operation', False):
                    return
                ctx.logger.warn('%s ID# "%s" has force_operation set.' %
                                (resource_type, resource_id))
            return function(**kwargs)
Ejemplo n.º 4
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS Lambda Function'''
    # Build API params
    params = resource_config
    params.update(dict(FunctionName=iface.resource_id))
    vpc_config = params.get('VpcConfig', dict())
    # Attach a Subnet Group if it exists
    subnet_ids = vpc_config.get('SubnetIds', list())
    for rel in utils.find_rels_by_node_type(ctx.instance,
                                            'cloudify.aws.nodes.Subnet'):
        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())
    for rel in utils.find_rels_by_node_type(
            ctx.instance, 'cloudify.aws.nodes.SecurityGroup'):
        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'])
Ejemplo n.º 5
0
def detach_from(ctx, resource_config, **_):
    '''Detaches an IAM Access Key from something else'''
    if utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.User'):
        resource_config['AccessKeyId'] = utils.get_resource_id(
            node=ctx.source.node,
            instance=ctx.source.instance,
            raise_on_missing=True)
        IAMUser(ctx.target.node,
                logger=ctx.logger,
                resource_id=utils.get_resource_id(
                    node=ctx.target.node,
                    instance=ctx.target.instance,
                    raise_on_missing=True)).delete_access_key(resource_config)
Ejemplo n.º 6
0
    def test_get_resource_id(self):
        _test_name = 'test_get_resource_id'
        _test_node_properties = {'use_external_resource': False}
        _test_runtime_properties = {'resource_config': {}}
        _ctx = self.get_mock_ctx(
            _test_name,
            test_properties=_test_node_properties,
            test_runtime_properties=_test_runtime_properties,
            type_hierarchy=['cloudify.nodes.Root'])
        current_ctx.set(_ctx)
        self.assertEqual(utils.get_resource_id(), None)

        with self.assertRaises(NonRecoverableError):
            utils.get_resource_id(raise_on_missing=True)
Ejemplo n.º 7
0
def attach_to(ctx, iface, resource_config, **_):
    '''Attaches an IAM User to something else'''
    if utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.Group'):
        resource_config['UserName'] = iface.resource_id
        IAMGroup(ctx.target.node,
                 logger=ctx.logger,
                 resource_id=utils.get_resource_id(
                     node=ctx.target.node,
                     instance=ctx.target.instance,
                     raise_on_missing=True)).attach_user(resource_config)
    elif utils.is_node_type(ctx.target.node,
                            'cloudify.nodes.aws.iam.LoginProfile'):
        iface.create_login_profile(
            resource_config
            or ctx.target.instance.runtime_properties.get('resource_config'))
    elif utils.is_node_type(ctx.target.node,
                            'cloudify.nodes.aws.iam.AccessKey'):
        resp = iface.create_access_key(
            resource_config
            or ctx.target.instance.runtime_properties.get('resource_config'))
        utils.update_resource_id(ctx.target.instance, resp['AccessKeyId'])
        ctx.target.instance.runtime_properties['SecretAccessKey'] = \
            resp['SecretAccessKey']
    elif utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.Policy'):
        resource_config['PolicyArn'] = utils.get_resource_arn(
            node=ctx.target.node,
            instance=ctx.target.instance,
            raise_on_missing=True)
        iface.attach_policy(resource_config)
Ejemplo n.º 8
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS KMS Key Grant"""
    # Create a copy of the resource config for clean manipulation.
    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
        )
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    key_id = params.get(KEY_ID)
    if not key_id:
        target_key = \
            utils.find_rel_by_node_type(
                ctx.instance,
                KEY_TYPE)
        key_id = \
            target_key.target.instance.runtime_properties[EXTERNAL_RESOURCE_ID]
        params[KEY_ID] = key_id
        ctx.instance.runtime_properties[KEY_ID] = key_id

    # Actually create the resource
    output = iface.create(params)
    ctx.instance.runtime_properties[GRANT_TOKEN] = \
        output.get(GRANT_TOKEN)
    utils.update_resource_id(ctx.instance, output.get(GRANT_ID))
Ejemplo n.º 9
0
def detach_from(ctx, iface, resource_config, **_):
    '''Detaches an RDS OptionGroup from something else'''
    if utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.rds.Option'):
        iface.remove_option(
            utils.get_resource_id(node=ctx.target.node,
                                  instance=ctx.target.instance,
                                  raise_on_missing=True))
Ejemplo n.º 10
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS ELB classic policy"""

    # Create a copy of the resource config for clean manipulation.
    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)
Ejemplo n.º 11
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM Role'''
    # Build API params
    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'])
Ejemplo n.º 12
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS KMS Key Alias"""
    # Create a copy of the resource config for clean manipulation.
    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
        )
    params[RESOURCE_NAME] = resource_id
    utils.update_resource_id(ctx.instance, resource_id)

    target_key_id = params.get(TARGET_KEY_ID)
    if not target_key_id:
        target_key = \
            utils.find_rel_by_node_type(
                ctx.instance,
                KEY_TYPE)
        target_key_id = \
            target_key.target.instance.runtime_properties[EXTERNAL_RESOURCE_ID]
        params[TARGET_KEY_ID] = target_key_id
    # Actually create the resource
    iface.create(params)
Ejemplo n.º 13
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM Profile'''
    # Build API params
    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)

    role_name = params.pop('RoleName', None)

    res_id, res_arn = iface.create(params)
    utils.update_resource_id(ctx.instance, res_id)
    utils.update_resource_arn(ctx.instance, res_arn)

    role_name = role_name or \
        utils.find_resource_id_by_type(ctx.instance,
                                       IAM_ROLE_TYPE)
    if role_name:
        add_role_params = {
            RESOURCE_NAME: iface.resource_id,
            'RoleName': role_name
        }
        iface.add_role_to_instance_profile(add_role_params)
        ctx.instance.runtime_properties['RoleName'] = role_name
def configure(ctx, resource_config, **_):
    '''Configures an AWS RDS Parameter'''
    # Save the parameters
    if resource_config.get('ParameterName') and not utils.get_resource_id():
        utils.update_resource_id(ctx.instance,
                                 resource_config['ParameterName'])
    ctx.instance.runtime_properties['resource_config'] = resource_config
Ejemplo n.º 15
0
def create(ctx, resource_config, **_):
    '''Creates an AWS Route53 Resource Record Set'''
    Route53HostedZone(ctx.node,
                      resource_id=utils.get_resource_id(raise_on_missing=True),
                      logger=ctx.logger).change_resource_record_sets(
                          ctx.instance.runtime_properties['resource_config']
                          or dict())
Ejemplo n.º 16
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS Autoscaling Lifecycle Hook"""
    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)

    # 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)
Ejemplo n.º 17
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS EC2 Vpc Peering"""
    params = dict() if not resource_config else resource_config.copy()

    # Accepter and Requester options are not part of create api, so we
    # Should check them if they are exists and then remove them
    accepter_vpc_options = params.get(ACCEPTER_VPC_PEERING_CONNECTION)
    requester_vpc_options = params.get(REQUESTER_VPC_PEERING_CONNECTION)

    if accepter_vpc_options:
        del params[ACCEPTER_VPC_PEERING_CONNECTION]

    if requester_vpc_options:
        del params[REQUESTER_VPC_PEERING_CONNECTION]

    # Actually create the resource
    create_response = iface.create(params)[VPC_PEERING_CONNECTION]
    ctx.instance.runtime_properties['create_response'] = \
        utils.JsonCleanuper(create_response).to_dict()
    if create_response:
        resource_id = \
            utils.get_resource_id(
                ctx.node,
                ctx.instance,
                create_response.get(VPC_PEERING_CONNECTION_ID),
                use_instance_id=True
            )

        utils.update_resource_id(ctx.instance, resource_id)
        prepare_describe_vpc_peering_filter(resource_config.copy(), iface)
Ejemplo n.º 18
0
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS IAM Role Policy'''
    # Build API params
    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)
def attach_to(ctx, resource_config, **_):
    '''Attaches an RDS Parameter to something else'''
    rtprops = ctx.source.instance.runtime_properties
    params = resource_config or rtprops.get('resource_config') or dict()
    if utils.is_node_type(ctx.target.node,
                          'cloudify.nodes.aws.rds.ParameterGroup'):
        params['ParameterName'] = utils.get_resource_id(
            node=ctx.target.node,
            instance=ctx.target.instance,
            raise_on_missing=True)
        ParameterGroup(ctx.target.node,
                       logger=ctx.logger,
                       resource_id=utils.get_resource_id(
                           node=ctx.target.node,
                           instance=ctx.target.instance,
                           raise_on_missing=True)).update_parameter(params)
Ejemplo n.º 20
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS Autoscaling Group"""
    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)

    # 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 = \
        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:
        params[SUBNET_LIST] = ', '.join(subnet_list)

    # Actually create the resource
    resource_id, resource_arn = iface.create(params)
    iface.update_resource_id(resource_id)
    utils.update_resource_id(ctx.instance, resource_id)
    utils.update_resource_arn(ctx.instance, resource_arn)
Ejemplo n.º 21
0
def create(ctx, iface, resource_config, **_):
    """Creates an AWS Autoscaling Autoscaling Launch Configuration"""
    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)

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

    image_id = params.get(IMAGEID)

    # Add Instance and Instance Type
    instance_id = params.get(INSTANCEID)
    instance_type = params.get(INSTANCE_TYPE_PROPERTY)
    if not image_id and not instance_id:
        instance_id = utils.find_resource_id_by_type(
            ctx.instance,
            INSTANCE_TYPE_NEW) or \
            utils.find_resource_id_by_type(
                ctx.instance,
                INSTANCE_TYPE)
        params.update({INSTANCEID: instance_id})
    if instance_id and not instance_type:
        targ = utils.find_rel_by_node_type(
            ctx.instance,
            INSTANCE_TYPE_NEW) or \
            utils.find_rel_by_node_type(
                ctx.instance,
                INSTANCE_TYPE)
        if targ:
            instance_type = \
                targ.target.instance.runtime_properties.get(
                    'resource_config', {}).get(
                        INSTANCE_TYPE_PROPERTY) or \
                targ.target.node.properties.get(
                    INSTANCE_TYPE_PROPERTY_DEPRECATED)
        params.update({INSTANCE_TYPE_PROPERTY: instance_type})

    utils.update_resource_id(ctx.instance, params.get(RESOURCE_NAME))
    iface.update_resource_id(params.get(RESOURCE_NAME))
    # Actually create the resource
    if not iface.resource_id:
        setattr(iface, 'resource_id', params.get(RESOURCE_NAME))
    iface.create(params)
    resource_arn = iface.properties[LC_ARN]
    utils.update_resource_arn(ctx.instance, resource_arn)
Ejemplo n.º 22
0
def prepare_assoc(ctx, iface, resource_config, **inputs):
    '''Prepares to associate an RDS Instance to something else'''
    if utils.is_node_type(ctx.target.node,
                          'cloudify.nodes.aws.rds.SubnetGroup'):
        ctx.source.instance.runtime_properties['resource_config'][
            'DBSubnetGroupName'] = utils.get_resource_id(
                node=ctx.target.node,
                instance=ctx.target.instance,
                raise_on_missing=True)
    elif utils.is_node_type(ctx.target.node,
                            'cloudify.nodes.aws.rds.OptionGroup'):
        ctx.source.instance.runtime_properties['resource_config'][
            'OptionGroupName'] = utils.get_resource_id(
                node=ctx.target.node,
                instance=ctx.target.instance,
                raise_on_missing=True)
    elif utils.is_node_type(ctx.target.node,
                            'cloudify.nodes.aws.rds.ParameterGroup'):
        ctx.source.instance.runtime_properties['resource_config'][
            'DBParameterGroupName'] = utils.get_resource_id(
                node=ctx.target.node,
                instance=ctx.target.instance,
                raise_on_missing=True)
    elif utils.is_node_type(ctx.target.node,
                            'cloudify.aws.nodes.SecurityGroup'):
        security_groups = ctx.source.instance.runtime_properties[
            'resource_config'].get('VpcSecurityGroupIds', list())
        security_groups.append(
            utils.get_resource_id(node=ctx.target.node,
                                  instance=ctx.target.instance,
                                  raise_on_missing=True))
        ctx.source.instance.runtime_properties['resource_config'][
            'VpcSecurityGroupIds'] = security_groups
    elif utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.Role'):
        if not inputs.get('iam_role_type_key') or \
                not inputs.get('iam_role_id_key'):
            raise NonRecoverableError(
                'Missing required relationship inputs "iam_role_type_key" '
                'and/or "iam_role_id_key".')
        ctx.source.instance.runtime_properties[
            'resource_config'][inputs['iam_role_type_key']] = \
            utils.get_resource_string(
                node=ctx.target.node,
                instance=ctx.target.instance,
                attribute_key=inputs['iam_role_id_key'])
Ejemplo n.º 23
0
def prepare(ctx, resource_config, **_):
    '''Prepares an AWS Lambda Permission'''
    # Save the parameters
    if not utils.get_resource_id():
        if resource_config.get('StatementId'):
            utils.update_resource_id(ctx.instance,
                                     resource_config['StatementId'])
        else:
            utils.update_resource_id(ctx.instance, str(uuid4()))
    ctx.instance.runtime_properties['resource_config'] = resource_config
Ejemplo n.º 24
0
def prepare_assoc(ctx, **_):
    '''Prepares to associate an Route53 Resource Record Set to something'''
    if utils.is_node_type(ctx.target.node,
                          'cloudify.nodes.aws.route53.HostedZone'):
        zone_id = utils.get_resource_id(node=ctx.target.node,
                                        instance=ctx.target.instance,
                                        raise_on_missing=True)
        utils.update_resource_id(ctx.source.instance, zone_id)
        ctx.source.instance.runtime_properties['resource_config'].update(
            dict(HostedZoneId=zone_id))
Ejemplo n.º 25
0
def attach_to(ctx, iface, resource_config, **_):
    '''Attaches an RDS OptionGroup to something else'''
    rtprops = ctx.target.instance.runtime_properties
    params = resource_config or rtprops.get('resource_config') or dict()
    if utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.rds.Option'):
        params['OptionName'] = utils.get_resource_id(
            node=ctx.target.node,
            instance=ctx.target.instance,
            raise_on_missing=True)
        iface.include_option(params)
def detach_from(ctx, resource_config, **_):
    '''Detaches an IAM Login Profile from something else'''
    if utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.User'):
        IAMUser(
            ctx.target.node,
            logger=ctx.logger,
            resource_id=utils.get_resource_id(
                node=ctx.target.node,
                instance=ctx.target.instance,
                raise_on_missing=True)).delete_login_profile(resource_config)
def create(ctx, iface, resource_config, **_):
    '''Creates an AWS ELB load balancer'''
    # Build API params
    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('Name'),
            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)
    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))
def attach_to(ctx, resource_config, **_):
    '''Attaches an IAM Login Profile to something else'''
    rtprops = ctx.source.instance.runtime_properties
    params = resource_config or rtprops.get('resource_config') or dict()
    if utils.is_node_type(ctx.target.node, 'cloudify.nodes.aws.iam.User'):
        IAMUser(ctx.target.node,
                logger=ctx.logger,
                resource_id=utils.get_resource_id(
                    node=ctx.target.node,
                    instance=ctx.target.instance,
                    raise_on_missing=True)).create_login_profile(params)
Ejemplo n.º 29
0
 def wrapper(**kwargs):
     ctx = kwargs.get('ctx')
     iface = kwargs.get('iface')
     resource_id = utils.get_resource_id(node=ctx.node,
                                         instance=ctx.instance)
     tags = utils.get_tags_list(ctx.node.properties.get('Tags'),
                                ctx.instance.runtime_properties.get('Tags'),
                                kwargs.get('Tags'))
     if iface and tags and resource_id:
         iface.untag({'Tags': tags, 'Resources': [resource_id]})
     return fn(**kwargs)
def prepare_assoc(ctx, iface, resource_config, **_):
    '''Prepares to associate an RDS SubnetGroup to something else'''
    if utils.is_node_type(ctx.target.node, 'cloudify.aws.nodes.Subnet'):
        subnet_ids = ctx.source.instance.runtime_properties[
            'resource_config'].get('SubnetIds', list())
        subnet_ids.append(
            utils.get_resource_id(node=ctx.target.node,
                                  instance=ctx.target.instance,
                                  raise_on_missing=True))
        ctx.source.instance.runtime_properties['resource_config'][
            'SubnetIds'] = subnet_ids