U
    W+d5                     @   s   d 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
G dd	 d	eZG d
d de	ZG dd deZG dd de	ZdS )z-
Represents an EC2 Elastic Network Interface
    )BotoClientError)TaggedEC2Object)	ResultSet)Groupc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
Attachmenta`  
    :ivar id: The ID of the attachment.
    :ivar instance_id: The ID of the instance.
    :ivar device_index: The index of this device.
    :ivar status: The status of the device.
    :ivar attach_time: The time the device was attached.
    :ivar delete_on_termination: Whether the device will be deleted
        when the instance is terminated.
    c                 C   s.   d | _ d | _d | _d| _d | _d | _d| _d S )Nr   F)idinstance_idinstance_owner_iddevice_indexstatusattach_timedelete_on_terminationself r   =/tmp/pip-unpacked-wheel-dlxw5sjy/boto/ec2/networkinterface.py__init__+   s    zAttachment.__init__c                 C   s
   d| j  S )NzAttachment:%sr   r   r   r   r   __repr__4   s    zAttachment.__repr__c                 C   s   d S Nr   r   nameattrs
connectionr   r   r   startElement7   s    zAttachment.startElementc                 C   s   |dkr|| _ n|dkr || _nt|dkr4t|| _n`|dkrD|| _nP|dkrT|| _n@|dkrd|| _n0|dkr| dkrd	| _qd
| _nt	| || d S )NZattachmentIdZ
instanceIdZdeviceIndexZinstanceOwnerIdr   Z
attachTimeZdeleteOnTerminationtrueTF)
r   r   intr
   r	   r   r   lowerr   setattrr   r   valuer   r   r   r   
endElement:   s"    zAttachment.endElementN)__name__
__module____qualname____doc__r   r   r   r!   r   r   r   r   r       s
   
	r   c                       sn   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  ZS )NetworkInterfacea  
    An Elastic Network Interface.

    :ivar id: The ID of the ENI.
    :ivar subnet_id: The ID of the VPC subnet.
    :ivar vpc_id: The ID of the VPC.
    :ivar description: The description.
    :ivar owner_id: The ID of the owner of the ENI.
    :ivar requester_managed:
    :ivar status: The interface's status (available|in-use).
    :ivar mac_address: The MAC address of the interface.
    :ivar private_ip_address: The IP address of the interface within
        the subnet.
    :ivar source_dest_check: Flag to indicate whether to validate
        network traffic to or from this network interface.
    :ivar groups: List of security groups associated with the interface.
    :ivar attachment: The attachment object.
    :ivar private_ip_addresses: A list of PrivateIPAddress objects.
    Nc                    sh   t t| | d | _d | _d | _d | _d | _d | _d| _	d | _
d | _d | _d | _g | _d | _g | _d S )NF)superr&   r   r   	subnet_idvpc_idavailability_zonedescriptionowner_idrequester_managedr   mac_addressprivate_ip_addresssource_dest_checkgroups
attachmentprivate_ip_addresses)r   r   	__class__r   r   r   e   s    zNetworkInterface.__init__c                 C   s
   d| j  S )NzNetworkInterface:%sr   r   r   r   r   r   v   s    zNetworkInterface.__repr__c                    sz   t t| |||}|d k	r |S |dkr>tdtfg| _| jS |dkrTt | _| jS |dkrrtdtfg| _	| j	S d S d S )NZgroupSetitemr2   ZprivateIpAddressesSet)
r'   r&   r   r   r   r1   r   r2   PrivateIPAddressr3   )r   r   r   r   retvalr4   r   r   r   y   s    zNetworkInterface.startElementc                 C   s   |dkr|| _ n|dkr || _n|dkr0|| _n|dkr@|| _n|dkrP|| _n|dkr`|| _n|dkr| dkr|d	| _qd
| _n`|dkr|| _nP|dkr|| _	n@|dkr|| _
n0|dkr| dkrd	| _qd
| _nt| || d S )NZnetworkInterfaceIdZsubnetIdZvpcIdZavailabilityZoner+   ZownerIdZrequesterManagedr   TFr   Z
macAddressprivateIpAddressZsourceDestCheck)r   r(   r)   r*   r+   r,   r   r-   r   r.   r/   r0   r   r   r   r   r   r!      s6    zNetworkInterface.endElementc                 C   s   | j |j  d S r   )__dict__update)r   updatedr   r   r   _update   s    zNetworkInterface._updateFc                 C   sH   | j j| jg|d}t|dkr0| |d  n|rBtd| j | jS )a  
        Update the data associated with this ENI by querying EC2.

        :type validate: bool
        :param validate: By default, if EC2 returns no data about the
                         ENI 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_runr   z%s is not a valid ENI ID)r   Zget_all_network_interfacesr   lenr=   
ValueErrorr   )r   validater?   rsr   r   r   r;      s    zNetworkInterface.updatec                 C   s   | j j| j|||dS )ar  
        Attach this ENI 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_index: int
        :param device_index: The interface nunber, N, on the instance (eg. ethN)

        :rtype: bool
        :return: True if successful
        r>   )r   Zattach_network_interfacer   )r   r   r
   r?   r   r   r   attach   s    zNetworkInterface.attachc                 C   s    t | jdd}| jj|||dS )a  
        Detach this ENI from an EC2 instance.

        :type force: bool
        :param force: Forces detachment if the previous detachment
                      attempt did not occur cleanly.

        :rtype: bool
        :return: True if successful
        r   Nr>   )getattrr2   r   Zdetach_network_interface)r   forcer?   Zattachment_idr   r   r   detach   s    zNetworkInterface.detachc                 C   s   | j j| j|dS )Nr>   )r   Zdelete_network_interfacer   )r   r?   r   r   r   delete   s    zNetworkInterface.delete)N)FF)F)FF)F)r"   r#   r$   r%   r   r   r   r!   r=   r;   rD   rG   rH   __classcell__r   r   r4   r   r&   P   s    


r&   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )r7   Nc                 C   s   || _ || _|| _d S r   )r   r/   primary)r   r   r/   rJ   r   r   r   r      s    zPrivateIPAddress.__init__c                 C   s   d S r   r   r   r   r   r   r      s    zPrivateIPAddress.startElementc                 C   s2   |dkr|| _ n|dkr.| dkr(dnd| _d S )Nr9   rJ   r   TF)r/   r   rJ   r   r   r   r   r!      s    zPrivateIPAddress.endElementc                 C   s   d| j | jf S )Nz PrivateIPAddress(%s, primary=%s))r/   rJ   r   r   r   r   r      s    zPrivateIPAddress.__repr__)NNN)r"   r#   r$   r   r   r!   r   r   r   r   r   r7      s     
r7   c                   @   s   e Zd Zdd ZdddZdS )NetworkInterfaceCollectionc                 G   s   |  | d S r   )extend)r   Z
interfacesr   r   r   r     s    z#NetworkInterfaceCollection.__init__ c                 C   s  t | D ]\}}d||f }|jd k	r:t|j||d < |jd k	rXt|j||d < nd||d < |jd k	rt|j||d < |jd k	rt|j||d < |jd k	r|jrdnd||d	 < |jd k	rt|j||d
 < |jd k	rt|j||d < |j	d k	r.t |j	D ]"\}}d||f }t|||< q
|j
d k	rt |j
D ]L\}	}
d||	f }t|
j||d < |
jd k	rD|
jrdnd||d < qD|jd k	r||d  dkrtdt| dkrtd|d }|jrd||< qd||< qd S )Nz%sNetworkInterface.%s.ZNetworkInterfaceIdZDeviceIndexr   ZSubnetIdDescriptionr   falseZDeleteOnTerminationZSecondaryPrivateIpAddressCountZPrivateIpAddressz%sSecurityGroupId.%sz%sPrivateIpAddresses.%sz.PrivateIpAddressz.Primary)r   0zcOnly the interface with device index of 0 can be provided when using 'associate_public_ip_address'.   zLOnly one interface can be provided when using 'associate_public_ip_address'.ZAssociatePublicIpAddress)	enumeratenetwork_interface_idstrr
   r(   r+   r   "secondary_private_ip_address_countr/   r1   r3   rJ   associate_public_ip_addressr   r@   )r   paramsprefixispecZfull_prefixjZgroup_idZquery_param_keykZip_addrZquery_param_key_prefixkeyr   r   r   build_list_params	  s`    














z,NetworkInterfaceCollection.build_list_paramsN)rM   )r"   r#   r$   r   r^   r   r   r   r   rK     s   rK   c                
   @   s   e Zd ZdddZdS )NetworkInterfaceSpecificationNc                 C   s@   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	d S r   )
rS   r
   r(   r+   r/   r1   r   r3   rU   rV   )r   rS   r
   r(   r+   r/   r1   r   r3   rU   rV   r   r   r   r   O  s    z&NetworkInterfaceSpecification.__init__)
NNNNNNNNNN)r"   r#   r$   r   r   r   r   r   r_   N  s               r_   N)r%   Zboto.exceptionr   Zboto.ec2.ec2objectr   Zboto.resultsetr   Zboto.ec2.groupr   objectr   r&   r7   listrK   r_   r   r   r   r   <module>   s   0 !I