U
    3d                     @   s8   d Z ddlZddlmZ ddlmZ G dd deZdS )z
Represents an EC2 Keypair
    N)	EC2Object)BotoClientErrorc                       sJ   e Zd Zd fdd	Zdd Zdd Zdd	d
Zdd ZdddZ  Z	S )KeyPairNc                    s&   t t| | d | _d | _d | _d S )N)superr   __init__namefingerprintmaterial)self
connection	__class__ 4/tmp/pip-unpacked-wheel-d7dsrkjd/boto/ec2/keypair.pyr   !   s    zKeyPair.__init__c                 C   s
   d| j  S )Nz
KeyPair:%s)r   )r
   r   r   r   __repr__'   s    zKeyPair.__repr__c                 C   s@   |dkr|| _ n,|dkr || _n|dkr0|| _nt| || d S )NZkeyNameZkeyFingerprintZkeyMaterial)r   r   r	   setattr)r
   r   valuer   r   r   r   
endElement*   s    zKeyPair.endElementFc                 C   s   | j j| j|dS )zq
        Delete the KeyPair.

        :rtype: bool
        :return: True if successful, otherwise False.
        dry_run)r   Zdelete_key_pairr   )r
   r   r   r   r   delete4   s    zKeyPair.deletec                 C   sx   | j rltj|}tj|d| j }tj|r>td| t|d}|	| j  |
  t|d dS tddS )a  
        Save the material (the unencrypted PEM encoded RSA private key)
        of a newly created KeyPair to a local file.

        :type directory_path: string
        :param directory_path: The fully qualified path to the directory
                               in which the keypair will be saved.  The
                               keypair file will be named using the name
                               of the keypair as the base name and .pem
                               for the file extension.  If a file of that
                               name already exists in the directory, an
                               exception will be raised and the old file
                               will not be overwritten.

        :rtype: bool
        :return: True if successful.
        z%s.pemz-%s already exists, it will not be overwrittenwbi  TzKeyPair contains no materialN)r	   ospath
expanduserjoinr   existsr   openwriteclosechmod)r
   Zdirectory_path	file_pathfpr   r   r   save=   s    
zKeyPair.savec                 C   s>   |j | jkrtd| j }|jf |}|j| j |d}|S )a   
        Create a new key pair of the same new in another region.
        Note that the new key pair will use a different ssh
        cert than the this key pair.  After doing the copy,
        you will need to save the material associated with the
        new key pair (use the save method) to a local file.

        :type region: :class:`boto.ec2.regioninfo.RegionInfo`
        :param region: The region to which this security group will be copied.

        :rtype: :class:`boto.ec2.keypair.KeyPair`
        :return: The new key pair
        z!Unable to copy to the same Regionr   )r   regionr   r   
get_paramsconnectZcreate_key_pair)r
   r$   r   Zconn_paramsZrconnZkpr   r   r   copy_to_region\   s    
zKeyPair.copy_to_region)N)F)F)
__name__
__module____qualname__r   r   r   r   r#   r'   __classcell__r   r   r   r   r      s   

	r   )__doc__r   Zboto.ec2.ec2objectr   Zboto.exceptionr   r   r   r   r   r   <module>   s   