U
    3d;?                     @   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)AWSQueryConnection)
RegionInfo)JSONResponseError)
exceptionsc                       s   e Zd ZdZdZdZdZdZdZe	Z
ejejejdZ fdd	Zd
d Zdd Zd1ddZd2ddZdd Zdd Zdd Zdd Zd3ddZd4ddZdd  Zd!d" Zd5d#d$Zd6d%d&Zd7d'd(Zd8d)d*Z d9d+d,Z!d-d. Z"d/d0 Z#  Z$S ):CloudHSMConnectionz
    AWS CloudHSM Service
    z
2014-05-30z	us-east-1z cloudhsm.us-east-1.amazonaws.comZCloudHSMZCloudHsmFrontendService)InvalidRequestExceptionCloudHsmServiceExceptionCloudHsmInternalExceptionc                    sZ   | dd }|s t| | j| j}d|ks4|d d kr>|j|d< tt| jf | || _d S )Nregionhost)	popr   DefaultRegionNameDefaultRegionEndpointendpointsuperr   __init__r   )selfkwargsr   	__class__ 8/tmp/pip-unpacked-wheel-d7dsrkjd/boto/cloudhsm/layer1.pyr   1   s    
zCloudHSMConnection.__init__c                 C   s   dgS )Nzhmac-v4r   )r   r   r   r   _required_auth_capability=   s    z,CloudHSMConnection._required_auth_capabilityc                 C   s   d|i}| j dt|dS )a  
        Creates a high-availability partition group. A high-
        availability partition group is a group of partitions that
        spans multiple physical HSMs.

        :type label: string
        :param label: The label of the new high-availability partition group.

        LabelZ
CreateHapgactionbodymake_requestr   dumps)r   labelparamsr   r   r   create_hapg@   s    
zCloudHSMConnection.create_hapgNc	           
      C   sb   ||||d}	|dk	r||	d< |dk	r.||	d< |dk	r>||	d< |dk	rN||	d< | j dt|	dS )	a  
        Creates an uninitialized HSM instance. Running this command
        provisions an HSM appliance and will result in charges to your
        AWS account for the HSM.

        :type subnet_id: string
        :param subnet_id: The identifier of the subnet in your VPC in which to
            place the HSM.

        :type ssh_key: string
        :param ssh_key: The SSH public key to install on the HSM.

        :type eni_ip: string
        :param eni_ip: The IP address to assign to the HSM's ENI.

        :type iam_role_arn: string
        :param iam_role_arn: The ARN of an IAM role to enable the AWS CloudHSM
            service to allocate an ENI on your behalf.

        :type external_id: string
        :param external_id: The external ID from **IamRoleArn**, if present.

        :type subscription_type: string
        :param subscription_type: The subscription type.

        :type client_token: string
        :param client_token: A user-defined token to ensure idempotence.
            Subsequent calls to this action with the same token will be
            ignored.

        :type syslog_ip: string
        :param syslog_ip: The IP address for the syslog monitoring server.

        )SubnetIdZSshKey
IamRoleArnZSubscriptionTypeNEniIp
ExternalIdZClientTokenSyslogIpZ	CreateHsmr   r   )
r   	subnet_idZssh_keyiam_role_arnZsubscription_typeeni_ipexternal_idZclient_token	syslog_ipr"   r   r   r   
create_hsmN   s     &zCloudHSMConnection.create_hsmc                 C   s,   d|i}|dk	r||d< | j dt|dS )a(  
        Creates an HSM client.

        :type label: string
        :param label: The label for the client.

        :type certificate: string
        :param certificate: The contents of a Base64-Encoded X.509 v3
            certificate to be installed on the HSMs used by this client.

        CertificateNr   ZCreateLunaClientr   r   )r   certificater!   r"   r   r   r   create_luna_client   s    z%CloudHSMConnection.create_luna_clientc                 C   s   d|i}| j dt|dS )z
        Deletes a high-availability partition group.

        :type hapg_arn: string
        :param hapg_arn: The ARN of the high-availability partition group to
            delete.

        HapgArnZ
DeleteHapgr   r   r   hapg_arnr"   r   r   r   delete_hapg   s    	zCloudHSMConnection.delete_hapgc                 C   s   d|i}| j dt|dS )z
        Deletes an HSM. Once complete, this operation cannot be undone
        and your key material cannot be recovered.

        :type hsm_arn: string
        :param hsm_arn: The ARN of the HSM to delete.

        HsmArnZ	DeleteHsmr   r   )r   hsm_arnr"   r   r   r   
delete_hsm   s    	zCloudHSMConnection.delete_hsmc                 C   s   d|i}| j dt|dS )z
        Deletes a client.

        :type client_arn: string
        :param client_arn: The ARN of the client to delete.

        	ClientArnZDeleteLunaClientr   r   )r   
client_arnr"   r   r   r   delete_luna_client   s    z%CloudHSMConnection.delete_luna_clientc                 C   s   d|i}| j dt|dS )z
        Retrieves information about a high-availability partition
        group.

        :type hapg_arn: string
        :param hapg_arn: The ARN of the high-availability partition group to
            describe.

        r2   ZDescribeHapgr   r   r3   r   r   r   describe_hapg   s    
z CloudHSMConnection.describe_hapgc                 C   s8   i }|dk	r||d< |dk	r$||d< | j dt|dS )a  
        Retrieves information about an HSM. You can identify the HSM
        by its ARN or its serial number.

        :type hsm_arn: string
        :param hsm_arn: The ARN of the HSM. Either the HsmArn or the
            SerialNumber parameter must be specified.

        :type hsm_serial_number: string
        :param hsm_serial_number: The serial number of the HSM. Either the
            HsmArn or the HsmSerialNumber parameter must be specified.

        Nr6   ZHsmSerialNumberZDescribeHsmr   r   )r   r7   Zhsm_serial_numberr"   r   r   r   describe_hsm   s    zCloudHSMConnection.describe_hsmc                 C   s8   i }|dk	r||d< |dk	r$||d< | j dt|dS )a  
        Retrieves information about an HSM client.

        :type client_arn: string
        :param client_arn: The ARN of the client.

        :type certificate_fingerprint: string
        :param certificate_fingerprint: The certificate fingerprint.

        Nr9   ZCertificateFingerprintZDescribeLunaClientr   r   )r   r:   Zcertificate_fingerprintr"   r   r   r   describe_luna_client   s    z'CloudHSMConnection.describe_luna_clientc                 C   s    |||d}| j dt|dS )a  
        Gets the configuration files necessary to connect to all high
        availability partition groups the client is associated with.

        :type client_arn: string
        :param client_arn: The ARN of the client.

        :type client_version: string
        :param client_version: The client version.

        :type hapg_list: list
        :param hapg_list: A list of ARNs that identify the high-availability
            partition groups that are associated with the client.

        )r9   ZClientVersionZHapgListZ	GetConfigr   r   )r   r:   client_versionZ	hapg_listr"   r   r   r   
get_config   s    zCloudHSMConnection.get_configc                 C   s   i }| j dt|dS )zk
        Lists the Availability Zones that have available AWS CloudHSM
        capacity.

        
        ZListAvailableZonesr   r   )r   r"   r   r   r   list_available_zones  s    z'CloudHSMConnection.list_available_zonesc                 C   s(   i }|dk	r||d< | j dt|dS )a  
        Lists the high-availability partition groups for the account.

        This operation supports pagination with the use of the
        NextToken member. If more results are available, the NextToken
        member of the response contains a token that you pass in the
        next call to ListHapgs to retrieve the next set of items.

        :type next_token: string
        :param next_token: The NextToken value from a previous call to
            ListHapgs. Pass null if this is the first call.

        N	NextTokenZ	ListHapgsr   r   r   Z
next_tokenr"   r   r   r   
list_hapgs  s    zCloudHSMConnection.list_hapgsc                 C   s(   i }|dk	r||d< | j dt|dS )a  
        Retrieves the identifiers of all of the HSMs provisioned for
        the current customer.

        This operation supports pagination with the use of the
        NextToken member. If more results are available, the NextToken
        member of the response contains a token that you pass in the
        next call to ListHsms to retrieve the next set of items.

        :type next_token: string
        :param next_token: The NextToken value from a previous call to
            ListHsms. Pass null if this is the first call.

        NrB   ZListHsmsr   r   rC   r   r   r   	list_hsms+  s    zCloudHSMConnection.list_hsmsc                 C   s(   i }|dk	r||d< | j dt|dS )a  
        Lists all of the clients.

        This operation supports pagination with the use of the
        NextToken member. If more results are available, the NextToken
        member of the response contains a token that you pass in the
        next call to ListLunaClients to retrieve the next set of
        items.

        :type next_token: string
        :param next_token: The NextToken value from a previous call to
            ListLunaClients. Pass null if this is the first call.

        NrB   ZListLunaClientsr   r   rC   r   r   r   list_luna_clients@  s    z$CloudHSMConnection.list_luna_clientsc                 C   s<   d|i}|dk	r||d< |dk	r(||d< | j dt|dS )a  
        Modifies an existing high-availability partition group.

        :type hapg_arn: string
        :param hapg_arn: The ARN of the high-availability partition group to
            modify.

        :type label: string
        :param label: The new label for the high-availability partition group.

        :type partition_serial_list: list
        :param partition_serial_list: The list of partition serial numbers to
            make members of the high-availability partition group.

        r2   Nr   ZPartitionSerialListZ
ModifyHapgr   r   )r   r4   r!   Zpartition_serial_listr"   r   r   r   modify_hapgU  s    zCloudHSMConnection.modify_hapgc                 C   sl   d|i}|dk	r||d< |dk	r(||d< |dk	r8||d< |dk	rH||d< |dk	rX||d< | j dt|d	S )
a  
        Modifies an HSM.

        :type hsm_arn: string
        :param hsm_arn: The ARN of the HSM to modify.

        :type subnet_id: string
        :param subnet_id: The new identifier of the subnet that the HSM is in.

        :type eni_ip: string
        :param eni_ip: The new IP address for the elastic network interface
            attached to the HSM.

        :type iam_role_arn: string
        :param iam_role_arn: The new IAM role ARN.

        :type external_id: string
        :param external_id: The new external ID.

        :type syslog_ip: string
        :param syslog_ip: The new IP address for the syslog monitoring server.

        r6   Nr$   r&   r%   r'   r(   Z	ModifyHsmr   r   )r   r7   r)   r+   r*   r,   r-   r"   r   r   r   
modify_hsmm  s    zCloudHSMConnection.modify_hsmc                 C   s   ||d}| j dt|dS )ai  
        Modifies the certificate used by the client.

        This action can potentially start a workflow to install the
        new certificate on the client's HSMs.

        :type client_arn: string
        :param client_arn: The ARN of the client.

        :type certificate: string
        :param certificate: The new certificate for the client.

        )r9   r/   ZModifyLunaClientr   r   )r   r:   r0   r"   r   r   r   modify_luna_client  s    z%CloudHSMConnection.modify_luna_clientc           
      C   s   d| j |f | jjdtt|d}| jdddi ||d}| j|d dd}| d	}t	j
| |jd
kr~|rt|S n8t|}|dd }| j|| j}	|	|j|j|dd S )Nz%s.%szapplication/x-amz-json-1.1)zX-Amz-TargetHostzContent-TypezContent-LengthPOST/)methodpathZ	auth_pathr"   headersdata
   )ZsenderZoverride_num_retrieszutf-8   Z__type)r   )TargetPrefixr   r   strlenZbuild_base_http_requestZ_mexereaddecodebotologdebugstatusr   loadsget_faultsResponseErrorreason)
r   r   r   rO   http_requestresponseZresponse_bodyZ	json_bodyZ
fault_nameZexception_classr   r   r   r     s6    
    


zCloudHSMConnection.make_request)NNNN)N)NN)NN)N)N)N)NN)NNNNN)%__name__
__module____qualname____doc__Z
APIVersionr   r   ZServiceNamerS   r   r_   r   r   r	   r
   r^   r   r   r#   r.   r1   r5   r8   r;   r<   r=   r>   r@   rA   rD   rE   rF   rG   rH   rI   r   __classcell__r   r   r   r   r      sR         
6

  




      
'r   )rX   Zboto.compatr   Zboto.connectionr   Zboto.regioninfor   Zboto.exceptionr   Zboto.cloudhsmr   r   r   r   r   r   <module>   s   