U
    W+d[                     @   s   d Z ddlZddlmZm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 dd	lmZ ddl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G dd deZG dd deZdS )z
Represents an EC2 Instance
    N)	EC2ObjectTaggedEC2Object)	ResultSet)Address)BlockDeviceMapping)ProductCodes)NetworkInterface)Groupc                   @   s2   e Zd ZdZdddZdd Zdd	 Zd
d ZdS )InstanceStatea  
    The state of the instance.

    :ivar code: The low byte represents the state. The high byte is an
        opaque internal value and should be ignored.  Valid values:

        * 0 (pending)
        * 16 (running)
        * 32 (shutting-down)
        * 48 (terminated)
        * 64 (stopping)
        * 80 (stopped)

    :ivar name: The name of the state of the instance.  Valid values:

        * "pending"
        * "running"
        * "shutting-down"
        * "terminated"
        * "stopping"
        * "stopped"
    r   Nc                 C   s   || _ || _d S N)codename)selfr   r    r   5/tmp/pip-unpacked-wheel-dlxw5sjy/boto/ec2/instance.py__init__=   s    zInstanceState.__init__c                 C   s   d| j | jf S )Nz%s(%d))r   r   r   r   r   r   __repr__A   s    zInstanceState.__repr__c                 C   s   d S r   r   r   r   attrs
connectionr   r   r   startElementD   s    zInstanceState.startElementc                 C   s4   |dkrt || _n|dkr$|| _nt| || d S )Nr   r   )intr   r   setattrr   r   valuer   r   r   r   
endElementG   s
    zInstanceState.endElement)r   N__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r
   &   s
   
r
   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )InstancePlacementa  
    The location where the instance launched.

    :ivar zone: The Availability Zone of the instance.
    :ivar group_name: The name of the placement group the instance is
        in (for cluster compute instances).
    :ivar tenancy: The tenancy of the instance (if the instance is
        running within a VPC). An instance with a tenancy of dedicated
        runs on single-tenant hardware.
    Nc                 C   s   || _ || _|| _d S r   )zone
group_nametenancy)r   r#   r$   r%   r   r   r   r   [   s    zInstancePlacement.__init__c                 C   s   | j S r   )r#   r   r   r   r   r   `   s    zInstancePlacement.__repr__c                 C   s   d S r   r   r   r   r   r   r   c   s    zInstancePlacement.startElementc                 C   s@   |dkr|| _ n,|dkr || _n|dkr0|| _nt| || d S )NZavailabilityZone	groupNamer%   )r#   r$   r%   r   r   r   r   r   r   f   s    zInstancePlacement.endElement)NNNr   r   r   r   r   r"   P   s
   

r"   c                       sD   e Zd ZdZd fdd	Zdd Zdd Zd	d
 ZdddZ  Z	S )Reservationa  
    Represents a Reservation response object.

    :ivar id: The unique ID of the Reservation.
    :ivar owner_id: The unique ID of the owner of the Reservation.
    :ivar groups: A list of Group objects representing the security
                  groups associated with launched instances.
    :ivar instances: A list of Instance objects launched in this
                     Reservation.
    Nc                    s,   t t| | d | _d | _g | _g | _d S r   )superr'   r   idowner_idgroups	instancesr   r   	__class__r   r   r   |   s
    zReservation.__init__c                 C   s
   d| j  S )NzReservation:%sr)   r   r   r   r   r      s    zReservation.__repr__c                 C   sD   |dkrt dtfg| _| jS |dkr<t dtfg| _| jS d S d S )NZinstancesSetitemgroupSet)r   Instancer,   r	   r+   r   r   r   r   r      s    zReservation.startElementc                 C   s0   |dkr|| _ n|dkr || _nt| || d S )NZreservationIdZownerId)r)   r*   r   r   r   r   r   r      s
    zReservation.endElementFc                 C   s   | j D ]}|j|d qd S Ndry_run)r,   stop)r   r6   instancer   r   r   stop_all   s    
zReservation.stop_all)N)F)
r   r   r    r!   r   r   r   r   r9   __classcell__r   r   r.   r   r'   q   s   

r'   c                       s&  e Zd ZdZd8 fdd	Zdd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Z fddZdd Zdd Zd9ddZd: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*d+ZdAd,d-ZdBd.d/ZdCd0d1ZdDd2d3ZdEd4d5ZdFd6d7Z  ZS )Gr3   a  
    Represents an instance.

    :ivar id: The unique ID of the Instance.
    :ivar groups: A list of Group objects representing the security
                  groups associated with the instance.
    :ivar public_dns_name: The public dns name of the instance.
    :ivar private_dns_name: The private dns name of the instance.
    :ivar state: The string representation of the instance's current state.
    :ivar state_code: An integer representation of the instance's
        current state.
    :ivar previous_state: The string representation of the instance's
        previous state.
    :ivar previous_state_code: An integer representation of the
        instance's current state.
    :ivar key_name: The name of the SSH key associated with the instance.
    :ivar instance_type: The type of instance (e.g. m1.small).
    :ivar launch_time: The time the instance was launched.
    :ivar image_id: The ID of the AMI used to launch this instance.
    :ivar placement: The availability zone in which the instance is running.
    :ivar placement_group: The name of the placement group the instance
        is in (for cluster compute instances).
    :ivar placement_tenancy: The tenancy of the instance, if the instance
        is running within a VPC.  An instance with a tenancy of dedicated
        runs on a single-tenant hardware.
    :ivar kernel: The kernel associated with the instance.
    :ivar ramdisk: The ramdisk associated with the instance.
    :ivar architecture: The architecture of the image (i386|x86_64).
    :ivar hypervisor: The hypervisor used.
    :ivar virtualization_type: The type of virtualization used.
    :ivar product_codes: A list of product codes associated with this instance.
    :ivar ami_launch_index: This instances position within it's launch group.
    :ivar monitored: A boolean indicating whether monitoring is enabled or not.
    :ivar monitoring_state: A string value that contains the actual value
        of the monitoring element returned by EC2.
    :ivar spot_instance_request_id: The ID of the spot instance request
        if this is a spot instance.
    :ivar subnet_id: The VPC Subnet ID, if running in VPC.
    :ivar vpc_id: The VPC ID, if running in VPC.
    :ivar private_ip_address: The private IP address of the instance.
    :ivar ip_address: The public IP address of the instance.
    :ivar platform: Platform of the instance (e.g. Windows)
    :ivar root_device_name: The name of the root device.
    :ivar root_device_type: The root device type (ebs|instance-store).
    :ivar block_device_mapping: The Block Device Mapping for the instance.
    :ivar state_reason: The reason for the most recent state transition.
    :ivar interfaces: List of Elastic Network Interfaces associated with
        this instance.
    :ivar ebs_optimized: Whether instance is using optimized EBS volumes
        or not.
    :ivar instance_profile: A Python dict containing the instance
        profile id and arn associated with this instance.
    Nc                    s  t t| | d | _d | _d | _d | _d | _d | _d | _	d | _
d | _d | _t | _d | _d| _d | _d | _d | _d | _d | _d | _d | _d| _d| _d | _d | _d | _d | _d | _d | _d | _ g | _!d | _"g | _#d | _$d | _%d | _&d | _'d | _(t) | _*t+ | _,d S )NF)-r(   r3   r   r)   dns_namepublic_dns_nameprivate_dns_namekey_nameinstance_typelaunch_timeimage_idkernelramdiskr   product_codesami_launch_index	monitoredmonitoring_statespot_instance_request_id	subnet_idvpc_idprivate_ip_address
ip_addressrequester_id_in_monitoring_element
persistentroot_device_nameroot_device_typeblock_device_mappingstate_reasonr$   client_token	eventsSetr+   platform
interfaces
hypervisorvirtualization_typearchitectureinstance_profile_previous_stater
   _stater"   
_placementr-   r.   r   r   r      sP    zInstance.__init__c                 C   s
   d| j  S )NzInstance:%sr0   r   r   r   r   r      s    zInstance.__repr__c                 C   s   | j jS r   )r]   r   r   r   r   r   state  s    zInstance.statec                 C   s   | j jS r   )r]   r   r   r   r   r   
state_code  s    zInstance.state_codec                 C   s   | j r| j jS d S r   )r\   r   r   r   r   r   previous_state	  s    zInstance.previous_statec                 C   s   | j r| j jS dS )Nr   )r\   r   r   r   r   r   previous_state_code  s    zInstance.previous_state_codec                 C   s   | j jS r   )r^   r#   r   r   r   r   	placement  s    zInstance.placementc                 C   s   | j jS r   )r^   r$   r   r   r   r   placement_group  s    zInstance.placement_groupc                 C   s   | j jS r   )r^   r%   r   r   r   r   placement_tenancy  s    zInstance.placement_tenancyc                    s$  t t| |||}|d k	r |S |dkr2d| _ n|dkrHt | _| jS |dkrV| jS |dkrntd| _| jS |dkrt	dt
fg| _| jS |dkrtd| _| jS |d	krt	dtfg| _| jS |d
krtd
| _| jS |dkr| jS |dkr t | _| jS |dkr| jS |dkr | jS d S )NZ
monitoringTblockDeviceMappingZproductCodesZstateReasonr2   r1   rU   ZnetworkInterfaceSetZiamInstanceProfileZcurrentStatepreviousStateZinstanceStaterc   )r(   r3   r   rN   r   rR   rD   SubParserS   r   r	   r+   rU   r   rW   r[   r]   r
   r\   r^   )r   r   r   r   retvalr.   r   r   r   !  sF    






zInstance.startElementc                 C   sl  |dkr|| _ nV|dkr$|| _nD|dks4|dkrD|| _|| _n$|dkrV|| _n|dkrh|| _n |dkrz|| _n|dkr|| _n|d	kr|| _n|d
kr|| _	n|dkr|| _
n|dkr|| _n|dkr|| _n|dkr|| _np|dkr|| _n\|dkr>| jrh|| _|dkr4d| _d| _n*|dkrR|| _n|dkrf|| _n|dkrx|| _n|dkr|| _n|dkr|| _n|dkr|| _n|dkr|dkrd| _nd| _n|dkr| jrh|| _n||dkr|| _nj|dkr|| _nX|dkr"|| _nF|d kr4|| _n4|d!krF|| _n"|d"kr\|dk| _nt | || d S )#N
instanceIdZimageIdZdnsNameZpublicDnsNameZprivateDnsNameZkeyNameZamiLaunchIndexrg   instanceTyperootDeviceNameZrootDeviceTypeZ
launchTimerV   ZkernelIdZ	ramdiskIdr_   ZenabledTFZspotInstanceRequestIdZsubnetIdZvpcIdZprivateIpAddressZ	ipAddressZrequesterIdrO   truer&   ZclientTokenrU   rX   ZvirtualizationTyperZ   ZebsOptimized)!r)   rA   r;   r<   r=   r>   rE   ra   r?   rP   rQ   r@   rV   rB   rC   rN   rG   rF   rH   rI   rJ   rK   rL   rM   rO   r$   rT   eventsrX   rY   rZ   Zebs_optimizedr   r   r   r   r   r   F  s    


































zInstance.endElementc                 C   s   | j |j  d S r   )__dict__update)r   updatedr   r   r   _update  s    zInstance._updateFc                 C   sd   | j j| jg|d}t|dkrL|d }|jD ]}|j| jkr.| | q.n|r^td| j | jS )a  
        Update the instance's state information by making a call to fetch
        the current instance attributes from the service.

        :type validate: bool
        :param validate: By default, if EC2 returns no data about the
                         instance 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.
        r5   r   z%s is not a valid Instance ID)r   Zget_all_reservationsr)   lenr,   rr   
ValueErrorr_   )r   validater6   rsrir   r   r   rp     s    
zInstance.updatec                 C   s2   | j j| jg|d}t|dkr.| |d  dS )z(
        Terminate the instance
        r5   r   N)r   Zterminate_instancesr)   rs   rr   r   r6   rv   r   r   r   	terminate  s    zInstance.terminatec                 C   s4   | j j| jg||d}t|dkr0| |d  dS )z
        Stop the instance

        :type force: bool
        :param force: Forces the instance to stop

        :rtype: list
        :return: A list of the instances stopped
        r5   r   N)r   Zstop_instancesr)   rs   rr   )r   forcer6   rv   r   r   r   r7     s    
zInstance.stopc                 C   s2   | j j| jg|d}t|dkr.| |d  dS )z%
        Start the instance.
        r5   r   N)r   Zstart_instancesr)   rs   rr   ry   r   r   r   start  s    zInstance.startc                 C   s   | j j| jg|dS r4   )r   Zreboot_instancesr)   r   r6   r   r   r   reboot  s    zInstance.rebootc                 C   s   | j j| j|dS )z
        Retrieves the console output for the instance.

        :rtype: :class:`boto.ec2.instance.ConsoleOutput`
        :return: The console output as a ConsoleOutput object
        r5   )r   get_console_outputr)   r}   r   r   r   r     s    zInstance.get_console_outputc                 C   s   | j j| j||dS r4   )r   Zconfirm_product_instancer)   )r   Zproduct_coder6   r   r   r   confirm_product  s
    zInstance.confirm_productc                 C   s$   t |tr|j}| jj| j||dS )a7  
        Associates an Elastic IP to the instance.

        :type ip_address: Either an instance of
            :class:`boto.ec2.address.Address` or a string.
        :param ip_address: The IP address to associate
            with the instance.

        :rtype: bool
        :return: True if successful
        r5   )
isinstancer   Z	public_ipr   Zassociate_addressr)   )r   rL   r6   r   r   r   use_ip  s    
zInstance.use_ipc                 C   s   | j j| j|dS r4   )r   Zmonitor_instancer)   r}   r   r   r   monitor  s    zInstance.monitorc                 C   s   | j j| j|dS r4   )r   Zunmonitor_instancer)   r}   r   r   r   	unmonitor  s    zInstance.unmonitorc                 C   s   | j j| j||dS )a  
        Gets an attribute from this instance.

        :type attribute: string
        :param attribute: The attribute you need information about
            Valid choices are:

            * instanceType
            * kernel
            * ramdisk
            * userData
            * disableApiTermination
            * instanceInitiatedShutdownBehavior
            * rootDeviceName
            * blockDeviceMapping
            * productCodes
            * sourceDestCheck
            * groupSet
            * ebsOptimized

        :rtype: :class:`boto.ec2.image.InstanceAttribute`
        :return: An InstanceAttribute object representing the value of the
                 attribute requested
        r5   )r   Zget_instance_attributer)   r   	attributer6   r   r   r   get_attribute  s
    zInstance.get_attributec                 C   s   | j j| j|||dS )a  
        Changes an attribute of this instance

        :type attribute: string
        :param attribute: The attribute you wish to change.

            * instanceType - A valid instance type (m1.small)
            * kernel - Kernel ID (None)
            * ramdisk - Ramdisk ID (None)
            * userData - Base64 encoded String (None)
            * disableApiTermination - Boolean (true)
            * instanceInitiatedShutdownBehavior - stop|terminate
            * sourceDestCheck - Boolean (true)
            * groupSet - Set of Security Groups or IDs
            * ebsOptimized - Boolean (false)

        :type value: string
        :param value: The new value for the attribute

        :rtype: bool
        :return: Whether the operation succeeded or not
        r5   )r   Zmodify_instance_attributer)   )r   r   r   r6   r   r   r   modify_attribute  s    zInstance.modify_attributec                 C   s   | j j| j||dS )a(  
        Resets an attribute of this instance to its default value.

        :type attribute: string
        :param attribute: The attribute to reset. Valid values are:
                          kernel|ramdisk

        :rtype: bool
        :return: Whether the operation succeeded or not
        r5   )r   Zreset_instance_attributer)   r   r   r   r   reset_attribute/  s
    zInstance.reset_attributec                 C   s   | j j| j||||dS )a  
        Will create an AMI from the instance in the running or stopped
        state.

        :type name: string
        :param name: The name of the new image

        :type description: string
        :param description: An optional human-readable string describing
                            the contents and purpose of the AMI.

        :type no_reboot: bool
        :param no_reboot: An optional flag indicating that the bundling process
                          should not attempt to shutdown the instance before
                          bundling.  If this flag is True, the responsibility
                          of maintaining file system integrity is left to the
                          owner of the instance.

        :rtype: string
        :return: The new image id
        r5   )r   create_imager)   )r   r   descriptionZ	no_rebootr6   r   r   r   r   @  s    zInstance.create_image)N)FF)F)FF)F)F)F)F)F)F)F)F)F)F)NFF) r   r   r    r!   r   r   propertyr_   r`   ra   rb   rc   rd   re   r   r   rr   rp   rz   r7   r|   r~   r   r   r   r   r   r   r   r   r   r:   r   r   r.   r   r3      sH   6*






%G





	






  r3   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	ConsoleOutputNc                 C   s   || _ d | _d | _d | _d S r   )parentinstance_id	timestampoutputr   r   r   r   r   r   a  s    zConsoleOutput.__init__c                 C   s   d S r   r   r   r   r   r   r   g  s    zConsoleOutput.startElementc                 C   sF   |dkr|| _ n2|dkr || _n"|dkr6t|| _nt| || d S )Nrj   r   r   )r   r   base64	b64decoder   r   r   r   r   r   r   j  s    zConsoleOutput.endElement)Nr   r   r    r   r   r   r   r   r   r   r   `  s   
r   c                
   @   s>   e Zd Zddddddddd	d
g
ZdddZdd Zdd ZdS )InstanceAttributerk   rB   rC   ZuserDataZdisableApiTerminationZ!instanceInitiatedShutdownBehaviorrl   rf   ZsourceDestCheckr2   Nc                 C   s    t |  d | _d | _d | _d S r   )dictr   r   
request_id_current_valuer   r   r   r   r   |  s    
zInstanceAttribute.__init__c                 C   sD   |dkrt  | |< | | S |dkr<tdtfg| |< | | S d S d S )Nrf   r2   r1   )r   r   r	   r   r   r   r   r     s    
zInstanceAttribute.startElementc                 C   sb   |dkr|| _ nN|dkr || _n>|dkrJ|dkr6d}n|dkrBd}|| _n|| jkr^| j| |< d S )Nrj   Z	requestIdr   rm   TfalseF)r   r   r   ValidValuesr   r   r   r   r     s    
zInstanceAttribute.endElement)N)r   r   r    r   r   r   r   r   r   r   r   r   u  s     

r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	rh   Nc                 C   s   t |  || _d S r   )r   r   section)r   r   r   r   r   r   r     s    
zSubParse.__init__c                 C   s   d S r   r   r   r   r   r   r     s    zSubParse.startElementc                 C   s   || j kr|| |< d S r   )r   r   r   r   r   r     s    
zSubParse.endElement)Nr   r   r   r   r   rh     s   
rh   )r!   ZbotoZboto.ec2.ec2objectr   r   Zboto.resultsetr   Zboto.ec2.addressr   Zboto.ec2.blockdevicemappingr   Zboto.ec2.imager   Zboto.ec2.networkinterfacer   Zboto.ec2.groupr	   r   objectr
   r"   r'   r3   r   r   r   rh   r   r   r   r   <module>   s&   *!,   F&