U
    W+d*T                     @   sX   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 G dd deZdS )	    N)json)JSONResponseError)AWSAuthConnection)
RegionInfo)
exceptionsc                       s   e Zd ZdZdZdZdZeZe	j
e	je	je	jdZ fddZdd	 Zd$ddZdd Zdd Zdd Zdd Zdd Zd%ddZd&ddZdd Zd'ddZd(dd Zd)d"d#Z  ZS )*AWSLambdaConnectionaM  
    AWS Lambda
    **Overview**

    This is the AWS Lambda API Reference. The AWS Lambda Developer
    Guide provides additional information. For the service overview,
    go to `What is AWS Lambda`_, and for information about how the
    service works, go to `AWS LambdaL How it Works`_ in the AWS Lambda
    Developer Guide.
    z
2014-11-11z	us-east-1zlambda.us-east-1.amazonaws.com)InvalidRequestContentExceptionResourceNotFoundExceptionInvalidParameterValueExceptionServiceExceptionc                    sL   | d}|s t| | j| j}n|d= |j|d< tt| jf | || _d S )Nregionhost)	getr   DefaultRegionNameDefaultRegionEndpointZendpointsuperr   __init__r   )selfkwargsr   	__class__ 9/tmp/pip-unpacked-wheel-dlxw5sjy/boto/awslambda/layer1.pyr   7   s    

zAWSLambdaConnection.__init__c                 C   s   dgS )Nzhmac-v4r   )r   r   r   r   _required_auth_capabilityB   s    z-AWSLambdaConnection._required_auth_capabilityNc           
      C   sT   d}|||d}i }i }	|dk	r(||d< |dk	r8||d< | j d|dt|||	dS )	a  
        Identifies an Amazon Kinesis stream as the event source for an
        AWS Lambda function. AWS Lambda invokes the specified function
        when records are posted to the stream.

        This is the pull model, where AWS Lambda invokes the function.
        For more information, go to `AWS LambdaL How it Works`_ in the
        AWS Lambda Developer Guide.

        This association between an Amazon Kinesis stream and an AWS
        Lambda function is called the event source mapping. You
        provide the configuration information (for example, which
        stream to read from and which AWS Lambda function to invoke)
        for the event source mapping in the request body.

        This operation requires permission for the `iam:PassRole`
        action for the IAM role. It also requires permission for the
        `lambda:AddEventSource` action.

        :type event_source: string
        :param event_source: The Amazon Resource Name (ARN) of the Amazon
            Kinesis stream that is the event source. Any record added to this
            stream causes AWS Lambda to invoke your Lambda function. AWS Lambda
            POSTs the Amazon Kinesis event, containing records, to your Lambda
            function as JSON.

        :type function_name: string
        :param function_name: The Lambda function to invoke when AWS Lambda
            detects an event on the stream.

        :type role: string
        :param role: The ARN of the IAM role (invocation role) that AWS Lambda
            can assume to read from the stream and invoke the function.

        :type batch_size: integer
        :param batch_size: The largest number of records that AWS Lambda will
            give to your function in a single event. The default is 100
            records.

        :type parameters: map
        :param parameters: A map (key-value pairs) defining the configuration
            for AWS Lambda to use when reading the event source. Currently, AWS
            Lambda supports only the `InitialPositionInStream` key. The valid
            values are: "TRIM_HORIZON" and "LATEST". The default value is
            "TRIM_HORIZON". For more information, go to `ShardIteratorType`_ in
            the Amazon Kinesis Service API Reference.

        "/2014-11-13/event-source-mappings/)EventSourceFunctionNameRoleNZ	BatchSizeZ
ParametersPOST   expected_statusdataheadersparamsmake_requestr   dumps)
r   Zevent_sourcefunction_nameroleZ
batch_size
parametersurir$   r#   query_paramsr   r   r   add_event_sourceE   s     3
 z$AWSLambdaConnection.add_event_sourcec                 C   s   d |}| jd|ddS )a  
        Deletes the specified Lambda function code and configuration.

        This operation requires permission for the
        `lambda:DeleteFunction` action.

        :type function_name: string
        :param function_name: The Lambda function to delete.

        /2014-11-13/functions/{0}DELETE   r!   formatr&   r   r(   r+   r   r   r   delete_function   s    
z#AWSLambdaConnection.delete_functionc                 C   s   d |}| jd|ddS )a>  
        Returns configuration information for the specified event
        source mapping (see AddEventSource).

        This operation requires permission for the
        `lambda:GetEventSource` action.

        :type uuid: string
        :param uuid: The AWS Lambda assigned ID of the event source mapping.

        %/2014-11-13/event-source-mappings/{0}GETr   r1   r2   r   uuidr+   r   r   r   get_event_source   s    
z$AWSLambdaConnection.get_event_sourcec                 C   s   d |}| jd|ddS )a:  
        Returns the configuration information of the Lambda function
        and a presigned URL link to the .zip file you uploaded with
        UploadFunction so you can download the .zip file. Note that
        the URL is valid for up to 10 minutes. The configuration
        information is the same information you provided as parameters
        when uploading the function.

        This operation requires permission for the
        `lambda:GetFunction` action.

        :type function_name: string
        :param function_name: The Lambda function name.

        r.   r7   r   r1   r2   r4   r   r   r   get_function   s    
z AWSLambdaConnection.get_functionc                 C   s   d |}| jd|ddS )a  
        Returns the configuration information of the Lambda function.
        This the same information you provided as parameters when
        uploading the function by using UploadFunction.

        This operation requires permission for the
        `lambda:GetFunctionConfiguration` operation.

        :type function_name: string
        :param function_name: The name of the Lambda function for which you
            want to retrieve the configuration information.

        '/2014-11-13/functions/{0}/configurationr7   r   r1   r2   r4   r   r   r   get_function_configuration   s    
z.AWSLambdaConnection.get_function_configurationc                 C   s   d |}i }i }ztt|}W n\ ttfk
r~   z|  W n" tttfk
rd   tdY nX tt	|
 j}Y nX ||d< | jd|d|||dS )aF  
        Submits an invocation request to AWS Lambda. Upon receiving
        the request, Lambda executes the specified function
        asynchronously. To see the logs generated by the Lambda
        function execution, see the CloudWatch logs console.

        This operation requires permission for the
        `lambda:InvokeAsync` action.

        :type function_name: string
        :param function_name: The Lambda function name.

        :type invoke_args: blob
        :param invoke_args: JSON that you want to provide to your Lambda
            function as input.

        z'/2014-11-13/functions/{0}/invoke-async/zFFile-like object passed to parameter ``invoke_args`` must be seekable.Content-Lengthr      r    r3   strlen	TypeErrorAttributeErrortellOSErrorIOErrorosfstatfilenost_sizer&   )r   r(   Zinvoke_argsr+   r#   r,   content_lengthr   r   r   invoke_async   s&    


 z AWSLambdaConnection.invoke_asyncc           	      C   sl   d}i }i }i }|dk	r ||d< |dk	r0||d< |dk	r@||d< |dk	rP||d< | j d|dt|||d	S )
a  
        Returns a list of event source mappings. For each mapping, the
        API returns configuration information (see AddEventSource).
        You can optionally specify filters to retrieve specific event
        source mappings.

        This operation requires permission for the
        `lambda:ListEventSources` action.

        :type event_source_arn: string
        :param event_source_arn: The Amazon Resource Name (ARN) of the Amazon
            Kinesis stream.

        :type function_name: string
        :param function_name: The name of the AWS Lambda function.

        :type marker: string
        :param marker: Optional string. An opaque pagination token returned
            from a previous `ListEventSources` operation. If present, specifies
            to continue the list from where the returning call left off.

        :type max_items: integer
        :param max_items: Optional integer. Specifies the maximum number of
            event sources to return in response. This value must be greater
            than 0.

        r   Nr   r   MarkerMaxItemsr7   r   r    r%   )	r   Zevent_source_arnr(   marker	max_itemsr+   r$   r#   r,   r   r   r   list_event_sources   s"    
 z&AWSLambdaConnection.list_event_sourcesc                 C   sL   d}i }i }i }|dk	r ||d< |dk	r0||d< | j d|dt|||dS )a  
        Returns a list of your Lambda functions. For each function,
        the response includes the function configuration information.
        You must use GetFunction to retrieve the code for your
        function.

        This operation requires permission for the
        `lambda:ListFunctions` action.

        :type marker: string
        :param marker: Optional string. An opaque pagination token returned
            from a previous `ListFunctions` operation. If present, indicates
            where to continue the listing.

        :type max_items: integer
        :param max_items: Optional integer. Specifies the maximum number of AWS
            Lambda functions to return in response. This parameter value must
            be greater than 0.

        z/2014-11-13/functions/NrN   rO   r7   r   r    r%   )r   rP   rQ   r+   r$   r#   r,   r   r   r   list_functions"  s    
 z"AWSLambdaConnection.list_functionsc                 C   s   d |}| jd|ddS )aN  
        Removes an event source mapping. This means AWS Lambda will no
        longer invoke the function for events in the associated
        source.

        This operation requires permission for the
        `lambda:RemoveEventSource` action.

        :type uuid: string
        :param uuid: The event source mapping ID.

        r6   r/   r0   r1   r2   r8   r   r   r   remove_event_sourceD  s    
z'AWSLambdaConnection.remove_event_sourcec                 C   s   d |}i }i }	i }
|dk	r&||
d< |dk	r6||
d< |dk	rF||
d< |dk	rV||
d< |dk	rf||
d< | jd|d	t||	|
d
S )a  
        Updates the configuration parameters for the specified Lambda
        function by using the values provided in the request. You
        provide only the parameters you want to change. This operation
        must only be used on an existing Lambda function and cannot be
        used to update the function's code.

        This operation requires permission for the
        `lambda:UpdateFunctionConfiguration` action.

        :type function_name: string
        :param function_name: The name of the Lambda function.

        :type role: string
        :param role: The Amazon Resource Name (ARN) of the IAM role that Lambda
            will assume when it executes your function.

        :type handler: string
        :param handler: The function that Lambda calls to begin executing your
            function. For Node.js, it is the module-name.export value in your
            function.

        :type description: string
        :param description: A short user-defined function description. Lambda
            does not use this value. Assign a meaningful description as you see
            fit.

        :type timeout: integer
        :param timeout: The function execution time at which Lambda should
            terminate the function. Because the execution time has cost
            implications, we recommend you set this value based on your
            expected execution time. The default is 3 seconds.

        :type memory_size: integer
        :param memory_size: The amount of memory, in MB, your Lambda function
            is given. Lambda uses this memory size to infer the amount of CPU
            allocated to your function. Your function use-case determines your
            CPU and memory requirements. For example, a database operation
            might need less memory compared to an image processing function.
            The default value is 128 MB. The value must be a multiple of 64 MB.

        r<   Nr   HandlerDescriptionTimeout
MemorySizePUTr   r    )r3   r&   r   r'   )r   r(   r)   handlerdescriptiontimeoutmemory_sizer+   r$   r#   r,   r   r   r   update_function_configurationU  s&    .

 z1AWSLambdaConnection.update_function_configurationc
                 C   s  d |}
i }i }|dk	r"||d< |dk	r2||d< |dk	rB||d< |dk	rR||d< |dk	rb||d< |dk	rr||d< |	dk	r|	|d	< ztt|}W n\ ttfk
r   z|  W n" tttfk
r   td
Y nX tt	|
 j}Y nX ||d< | jd|
d|||dS )a  
        Creates a new Lambda function or updates an existing function.
        The function metadata is created from the request parameters,
        and the code for the function is provided by a .zip file in
        the request body. If the function name already exists, the
        existing Lambda function is updated with the new code and
        metadata.

        This operation requires permission for the
        `lambda:UploadFunction` action.

        :type function_name: string
        :param function_name: The name you want to assign to the function you
            are uploading. The function names appear in the console and are
            returned in the ListFunctions API. Function names are used to
            specify functions to other AWS Lambda APIs, such as InvokeAsync.

        :type function_zip: blob
        :param function_zip: A .zip file containing your packaged source code.
            For more information about creating a .zip file, go to `AWS LambdaL
            How it Works`_ in the AWS Lambda Developer Guide.

        :type runtime: string
        :param runtime: The runtime environment for the Lambda function you are
            uploading. Currently, Lambda supports only "nodejs" as the runtime.

        :type role: string
        :param role: The Amazon Resource Name (ARN) of the IAM role that Lambda
            assumes when it executes your function to access any other Amazon
            Web Services (AWS) resources.

        :type handler: string
        :param handler: The function that Lambda calls to begin execution. For
            Node.js, it is the module-name . export value in your function.

        :type mode: string
        :param mode: How the Lambda function will be invoked. Lambda supports
            only the "event" mode.

        :type description: string
        :param description: A short, user-defined function description. Lambda
            does not use this value. Assign a meaningful description as you see
            fit.

        :type timeout: integer
        :param timeout: The function execution time at which Lambda should
            terminate the function. Because the execution time has cost
            implications, we recommend you set this value based on your
            expected execution time. The default is 3 seconds.

        :type memory_size: integer
        :param memory_size: The amount of memory, in MB, your Lambda function
            is given. Lambda uses this memory size to infer the amount of CPU
            allocated to your function. Your function use-case determines your
            CPU and memory requirements. For example, database operation might
            need less memory compared to image processing function. The default
            value is 128 MB. The value must be a multiple of 64 MB.

        r.   NZRuntimer   rU   ZModerV   rW   rX   zGFile-like object passed to parameter ``function_zip`` must be seekable.r>   rY      r    r@   )r   r(   Zfunction_zipZruntimer)   rZ   moder[   r\   r]   r+   r#   r,   rL   r   r   r   upload_function  sB    >


 z#AWSLambdaConnection.upload_function c                 C   s   |d kri }t j| |||||d}| d}|r>t|}|j|krL|S |ddd }	| j	
|	| j}
|
|j|j|d S )N)r#   r"   r$   zutf-8zx-amzn-ErrorType:r   )r   r&   readdecoder   loadsstatus	getheadersplit_faultsr   ResponseErrorreason)r   Zverbresourcer#   r"   r!   r$   responsebodyZ
error_typeZerror_classr   r   r   r&     s$         

z AWSLambdaConnection.make_request)NN)NNNN)NN)NNNNN)NNN)Nrb   NN)__name__
__module____qualname____doc__Z
APIVersionr   r   r   rk   r   r   r	   r
   r   rj   r   r   r-   r5   r:   r;   r=   rM   rR   rS   rT   r^   ra   r&   __classcell__r   r   r   r   r      sP   
   
C'    
.
"       
A    
b    r   )rH   Zboto.compatr   Zboto.exceptionr   Zboto.connectionr   Zboto.regioninfor   Zboto.awslambdar   r   r   r   r   r   <module>   s   