U
    3d(                     @   s\   d Z ddlmZ ddlmZ ddlmZ G dd deZG dd deZ	G d	d
 d
eZ
dS )z0
Represents an EC2 Elastic Block Storage Volume
    )	ResultSet)Tag)TaggedEC2Objectc                       s   e Zd ZdZd fdd	Zdd Z fddZd	d
 Zdd ZdddZ	d ddZ
d!ddZd"ddZd#ddZdd Zdd Zd$ddZ  ZS )%Volumea  
    Represents an EBS volume.

    :ivar id: The unique ID of the volume.
    :ivar create_time: The timestamp of when the volume was created.
    :ivar status: The status of the volume.
    :ivar size: The size (in GB) of the volume.
    :ivar snapshot_id: The ID of the snapshot this volume was created
        from, if applicable.
    :ivar attach_data: An AttachmentSet object.
    :ivar zone: The availability zone this volume is in.
    :ivar type: The type of volume (standard or consistent-iops)
    :ivar iops: If this volume is of type consistent-iops, this is
        the number of IOPS provisioned (10-300).
    :ivar encrypted: True if this volume is encrypted.
    Nc                    sP   t t| | d | _d | _d | _d | _d | _d | _d | _	d | _
d | _d | _d S N)superr   __init__idcreate_timestatussizesnapshot_idattach_datazonetypeiops	encrypted)self
connection	__class__ 3/tmp/pip-unpacked-wheel-d7dsrkjd/boto/ec2/volume.pyr   2   s    zVolume.__init__c                 C   s
   d| j  S )Nz	Volume:%sr	   r   r   r   r   __repr__?   s    zVolume.__repr__c                    s\   t t| |||}|d k	r |S |dkr6t | _| jS |dkrTtdtfg| _| jS d S d S )NZattachmentSetZtagSetitem)r   r   startElementAttachmentSetr   r   r   tags)r   nameattrsr   retvalr   r   r   r   B   s    zVolume.startElementc                 C   s   |dkr|| _ n|dkr || _n|dkr8|dkr|| _n||dkrLt|| _nh|dkr\|| _nX|dkrl|| _nH|dkr||| _n8|d	krt|| _n$|d
kr|	 dk| _
nt| || d S )NvolumeIdZ
createTimer    r   Z
snapshotIdZavailabilityZoneZ
volumeTyper   r   true)r	   r
   r   intr   r   r   r   r   lowerr   setattrr   r    valuer   r   r   r   
endElementO   s(    zVolume.endElementc                 C   s   | j |j  d S r   )__dict__update)r   updatedr   r   r   _updatef   s    zVolume._updateFc                    sZ    j j jg|d} fdd|D }t|dkrB |d  n|rTtd j  jS )a  
        Update the data associated with this volume by querying EC2.

        :type validate: bool
        :param validate: By default, if EC2 returns no data about the
                         volume the update method returns quietly.  If
                         the validate param is True, however, it will
                         raise a ValueError exception if no data is
                         returned from EC2.
        dry_runc                    s   g | ]}|j  j kr|qS r   r   ).0xr   r   r   
<listcomp>y   s      z!Volume.update.<locals>.<listcomp>r   z%s is not a valid Volume ID)r   Zget_all_volumesr	   lenr/   
ValueErrorr   )r   validater1   Zunfiltered_rsrsr   r   r   r-   i   s    zVolume.updatec                 C   s   | j j| j|dS )zc
        Delete this EBS volume.

        :rtype: bool
        :return: True if successful
        r0   )r   Zdelete_volumer	   )r   r1   r   r   r   delete   s    zVolume.deletec                 C   s   | j j| j|||dS )a  
        Attach this EBS volume to an EC2 instance.

        :type instance_id: str
        :param instance_id: The ID of the EC2 instance to which it will
                            be attached.

        :type device: str
        :param device: The device on the instance through which the
                       volume will be exposed (e.g. /dev/sdh)

        :rtype: bool
        :return: True if successful
        r0   )r   Zattach_volumer	   )r   instance_iddevicer1   r   r   r   attach   s    zVolume.attachc                 C   s<   d}| j r| j j}d}| j r$| j j}| jj| j||||dS )a  
        Detach this EBS volume from an EC2 instance.

        :type force: bool
        :param force: Forces detachment if the previous detachment
            attempt did not occur cleanly.  This option can lead to
            data loss or a corrupted file system. Use this option only
            as a last resort to detach a volume from a failed
            instance. The instance will not have an opportunity to
            flush file system caches nor file system meta data. If you
            use this option, you must perform file system check and
            repair procedures.

        :rtype: bool
        :return: True if successful
        Nr0   )r   r:   r;   r   Zdetach_volumer	   )r   forcer1   r:   r;   r   r   r   detach   s    zVolume.detachc                 C   s   | j j| j||dS )a  
        Create a snapshot of this EBS Volume.

        :type description: str
        :param description: A description of the snapshot.
            Limited to 256 characters.

        :rtype: :class:`boto.ec2.snapshot.Snapshot`
        :return: The created Snapshot object
        r0   )r   create_snapshotr	   )r   descriptionr1   r   r   r   r?      s
    zVolume.create_snapshotc                 C   s   | j S )zW
        Returns the state of the volume.  Same value as the status attribute.
        )r   r   r   r   r   volume_state   s    zVolume.volume_statec                 C   s   d}| j r| j j}|S )z+
        Get the attachment state.
        N)r   r   )r   stater   r   r   attachment_state   s    zVolume.attachment_statec                 C   s:   | j j|||d}g }|D ]}|j| jkr|| q|S )a  
        Get all snapshots related to this volume.  Note that this requires
        that all available snapshots for the account be retrieved from EC2
        first and then the list is filtered client-side to contain only
        those for this volume.

        :type owner: str
        :param owner: If present, only the snapshots owned by the
            specified user will be returned.  Valid values are:

            * self
            * amazon
            * AWS Account ID

        :type restorable_by: str
        :param restorable_by: If present, only the snapshots that
            are restorable by the specified account id will be returned.

        :rtype: list of L{boto.ec2.snapshot.Snapshot}
        :return: The requested Snapshot objects

        )ownerrestorable_byr1   )r   Zget_all_snapshotsZ	volume_idr	   append)r   rD   rE   r1   r8   ZmineZsnapr   r   r   	snapshots   s    zVolume.snapshots)N)FF)F)F)FF)NF)NNF)__name__
__module____qualname____doc__r   r   r   r+   r/   r-   r9   r<   r>   r?   rA   rC   rG   __classcell__r   r   r   r   r       s   

	


	r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   a  
    Represents an EBS attachmentset.

    :ivar id: The unique ID of the volume.
    :ivar instance_id: The unique ID of the attached instance
    :ivar status: The status of the attachment
    :ivar attach_time: Attached since
    :ivar device: The device the instance has mapped
    c                 C   s"   d | _ d | _d | _d | _d | _d S r   )r	   r:   r   attach_timer;   r   r   r   r   r     s
    zAttachmentSet.__init__c                 C   s
   d| j  S )NzAttachmentSet:%sr   r   r   r   r   r     s    zAttachmentSet.__repr__c                 C   s   d S r   r   r   r    r!   r   r   r   r   r     s    zAttachmentSet.startElementc                 C   s`   |dkr|| _ nL|dkr || _n<|dkr0|| _n,|dkr@|| _n|dkrP|| _nt| || d S )Nr#   Z
instanceIdr   Z
attachTimer;   )r	   r:   r   rM   r;   r(   r)   r   r   r   r+     s    zAttachmentSet.endElementN)rH   rI   rJ   rK   r   r   r   r+   r   r   r   r   r     s
   	r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	VolumeAttributeNc                 C   s   d | _ d | _i | _d S r   )r	   	_key_namer!   )r   parentr   r   r   r   (  s    zVolumeAttribute.__init__c                 C   s   |dkr|| _ d S )NZautoEnableIO)rP   rN   r   r   r   r   -  s    zVolumeAttribute.startElementc                 C   sP   |dkr0|  dkr"d| j| j< qLd| j| j< n|dkr@|| _nt| || d S )Nr*   r%   TFr#   )r'   r!   rP   r	   r(   r)   r   r   r   r+   2  s    zVolumeAttribute.endElement)N)rH   rI   rJ   r   r   r+   r   r   r   r   rO   '  s   
rO   N)rK   Zboto.resultsetr   Zboto.ec2.tagr   Zboto.ec2.ec2objectr   r   objectr   rO   r   r   r   r   <module>   s    b&