
?F[c           @   s   d  Z  d d l m Z d d l m Z d e f d     YZ d e f d     YZ d e f d	     YZ	 d
 e f d     YZ
 d S(   s"   
Represents an EC2 Security Group
i(   t   TaggedEC2Object(   t   BotoClientErrort   SecurityGroupc           B   s   e  Z d d d d d d   Z d   Z d   Z d   Z e d  Z e d  Z	 e d  Z
 d d d d d e d  Z d d d d d e d  Z d e d	  Z e d
  Z RS(   c         C   s_   t  t |   j |  | |  _ | |  _ | |  _ | |  _ d  |  _ t	   |  _
 t	   |  _ d  S(   N(   t   superR   t   __init__t   idt   owner_idt   namet   descriptiont   Nonet   vpc_idt   IPPermissionsListt   rulest   rules_egress(   t   selft
   connectionR   R   R   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR       s    					c         C   s   d |  j  S(   Ns   SecurityGroup:%s(   R   (   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt   __repr__+   s    c         C   s\   t  t |   j | | |  } | d  k	 r. | S| d k rA |  j S| d k rT |  j Sd  Sd  S(   Nt   ipPermissionst   ipPermissionsEgress(   R   R   t   startElementR	   R   R   (   R   R   t   attrsR   t   retval(    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   .   s    c         C   s   | d k r | |  _  n | d k r0 | |  _ n | d k rH | |  _ n | d k r` | |  _ n | d k rx | |  _ nw | d k r nh | d k r | d k r t |  _ q | d	 k r t |  _ q t d
 | |  j f   n t	 |  | |  d  S(   Nt   ownerIdt   groupIdt	   groupNamet   vpcIdt   groupDescriptiont   ipRangest   returnt   falset   trues*   Unexpected value of status %s for group %s(
   R   R   R   R
   R   t   Falset   statust   Truet	   Exceptiont   setattr(   R   R   t   valueR   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt
   endElement9   s,    c         C   sB   |  j  r% |  j j d |  j d |  S|  j j |  j d | Sd  S(   Nt   group_idt   dry_run(   R
   R   t   delete_security_groupR   R   (   R   R'   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt   deleteU   s    				c	   
      C   sW   t  |   }	 | |	 _ | |	 _ | |	 _ |  j j |	  |	 j | | | | d | d S(   s   
        Add a rule to the SecurityGroup object.  Note that this method
        only changes the local version of the object.  No information
        is sent to EC2.
        R'   N(   t   IPPermissionst   ip_protocolt	   from_portt   to_portR   t   appendt	   add_grant(
   R   R+   R,   R-   t   src_group_namet   src_group_owner_idt   cidr_ipt   src_group_group_idR'   t   rule(    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt   add_rulea   s    			c	         C   s  |  j  s t d   n  d }	 x |  j  D] }
 |
 j | k r |
 j | k r |
 j | k r |
 }	 d } x\ |
 j D]Q } | j | k s | j | k rq | j	 | k r | j
 | k r | } q q qq qq W| r |
 j j |  q q q n  t |
 j  d k r( |  j  j |	  q( q( Wd S(   s   
        Remove a rule to the SecurityGroup object.  Note that this method
        only changes the local version of the object.  No information
        is sent to EC2.
        s   The security group has no rulesi    N(   R   t
   ValueErrorR	   R+   R,   R-   t   grantsR   R&   R   R2   t   removet   len(   R   R+   R,   R-   R0   R1   R2   R3   R'   t   target_ruleR4   t   target_grantt   grant(    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt   remove_rulev   s$    	c         C   s'  d } |  j s |  j } n  d } |  j r6 |  j } n  d }	 d }
 d } | r d } | j }
 |  j sr | j }	 q t | d  r | j } q | j } n  |  j j | |	 |
 | | | | | | d | 	} | r#t	 | t
  s | g } n  x6 | D]+ } |  j | | | |	 |
 | | d | q Wn  | S(   s  
        Add a new rule to this security group.
        You need to pass in either src_group_name
        OR ip_protocol, from_port, to_port,
        and cidr_ip.  In other words, either you are authorizing another
        group or you are authorizing some ip-based rule.

        :type ip_protocol: string
        :param ip_protocol: Either tcp | udp | icmp

        :type from_port: int
        :param from_port: The beginning port number you are enabling

        :type to_port: int
        :param to_port: The ending port number you are enabling

        :type cidr_ip: string or list of strings
        :param cidr_ip: The CIDR block you are providing access to.
                        See http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

        :type src_group: :class:`boto.ec2.securitygroup.SecurityGroup` or
                         :class:`boto.ec2.securitygroup.GroupOrCIDR`
        :param src_group: The Security Group you are granting access to.

        :rtype: bool
        :return: True if successful.
        R&   R'   N(   R	   R
   R   R   R   t   hasattrR&   R   t   authorize_security_groupt
   isinstancet   listR5   (   R   R+   R,   R-   R2   t	   src_groupR'   t
   group_nameR&   R0   R1   R3   R    t   single_cidr_ip(    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt	   authorize   sF    					c         C   s   d  } |  j s |  j } n  d  } |  j r6 |  j } n  d  }	 d  }
 d  } | r d  } | j }
 |  j sr | j }	 q t | d  r | j } q | j } n  |  j j | |	 |
 | | | | | | d | 	} | r |  j	 | | | |	 |
 | | d | n  | S(   NR&   R'   (
   R	   R
   R   R   R   R>   R&   R   t   revoke_security_groupR=   (   R   R+   R,   R-   R2   RB   R'   RC   R&   R0   R1   R3   R    (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt   revoke   s@    					
c      
   C   s  | j  |  j k r! t d   n  |  j j   } | j |   } | j | pQ |  j  |  j d | } g  } x |  j D] } x | j	 D] }	 |	 j  p |	 j
 }
 |
 r |
 | k r| j |
  | j d d d d |	 d | qq | j | j | j | j |	 j d | q Wqs W| S(   s{  
        Create a copy of this security group in another region.
        Note that the new security group will be a separate entity
        and will not stay in sync automatically after the copy
        operation.

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

        :type name: string
        :param name: The name of the copy.  If not supplied, the copy
                     will have the same name as this security group.

        :rtype: :class:`boto.ec2.securitygroup.SecurityGroup`
        :return: The new security group.
        s!   Unable to copy to the same RegionR'   N(   R   t   regionR   R   t
   get_paramst   connectt   create_security_groupR   R   R7   R&   R.   RE   R	   R+   R,   R-   R2   (   R   RH   R   R'   t   conn_paramst   rconnt   sgt   source_groupsR4   R<   t	   grant_nom(    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt   copy_to_region   s(    		c         C   s   g  } |  j  r> | j |  j j d i |  j d 6d |   n, | j |  j j d i |  j d 6d |   g  | D] } | j D] } | ^ q~ qq } | S(   s   
        Find all of the current instances that are running within this
        security group.

        :rtype: list of :class:`boto.ec2.instance.Instance`
        :return: A list of Instance objects
        t   filterss   instance.group-idR'   s   group-id(   R
   t   extendR   t   get_all_reservationsR   t	   instances(   R   R'   t   rst   rt   iRU   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyRU   "  s    	
)N(   t   __name__t
   __module__R	   R   R   R   R%   R   R)   R5   R=   RE   RG   RQ   RU   (    (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR      s   
				B	$(R   c           B   s   e  Z d    Z d   Z RS(   c         C   s+   | d k r' |  j  t |    |  d Sd  S(   Nt   itemi(   R.   R*   R	   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   ;  s    c         C   s   d  S(   N(    (   R   R   R$   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR%   A  s    (   RY   RZ   R   R%   (    (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   9  s   	R*   c           B   sG   e  Z d d   Z d   Z d   Z d   Z d d d d e d  Z RS(   c         C   s1   | |  _  d  |  _ d  |  _ d  |  _ g  |  _ d  S(   N(   t   parentR	   R+   R,   R-   R7   (   R   R\   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   G  s
    				c         C   s   d |  j  |  j |  j f S(   Ns   IPPermissions:%s(%s-%s)(   R+   R,   R-   (   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   N  s    	c         C   s1   | d k r- |  j  j t |    |  j  d Sd  S(   NR[   i(   R7   R.   t   GroupOrCIDRR	   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   R  s    c         C   s\   | d k r | |  _  n@ | d k r0 | |  _ n( | d k rH | |  _ n t |  | |  d  S(   Nt
   ipProtocolt   fromPortt   toPort(   R+   R,   R-   R#   (   R   R   R$   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR%   X  s    c         C   sD   t  |   } | | _ | | _ | | _ | | _ |  j j |  | S(   N(   R]   R   R&   R   R2   R7   R.   (   R   R   R   R2   R&   R'   R<   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR/   b  s    				N(	   RY   RZ   R	   R   R   R   R%   R   R/   (    (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR*   E  s   			
R]   c           B   s/   e  Z d d   Z d   Z d   Z d   Z RS(   c         C   s(   d  |  _ d  |  _ d  |  _ d  |  _ d  S(   N(   R	   R   R&   R   R2   (   R   R\   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   o  s    			c         C   s5   |  j  r d |  j  Sd |  j p& |  j |  j f Sd  S(   Ns   %ss   %s-%s(   R2   R   R&   R   (   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   u  s    	c         C   s   d  S(   N(   R	   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR   {  s    c         C   st   | d k r | |  _  n0 | d k r0 | |  _ n | d k rH | |  _ n  | d k r` | |  _ n t |  | |  d  S(   Nt   userIdR   R   t   cidrIp(   R   R&   R   R2   R#   (   R   R   R$   R   (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR%   ~  s    N(   RY   RZ   R	   R   R   R   R%   (    (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyR]   m  s   		N(   t   __doc__t   boto.ec2.ec2objectR    t   boto.exceptionR   R   RA   R   t   objectR*   R]   (    (    (    s5   lib/python2.7/site-packages/boto/ec2/securitygroup.pyt   <module>   s    (