U
    W+d_9                     @   s`   d 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
dS )z"
Represents an EC2 Security Group
    )TaggedEC2Object)BotoClientErrorc                       s   e Zd Zd fdd	Zdd Z f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dZd ddZ  ZS )!SecurityGroupNc                    sB   t t| | || _|| _|| _|| _d | _t | _	t | _
d S N)superr   __init__idowner_idnamedescriptionvpc_idIPPermissionsListrulesrules_egress)self
connectionr	   r
   r   r   	__class__ :/tmp/pip-unpacked-wheel-dlxw5sjy/boto/ec2/securitygroup.pyr       s    zSecurityGroup.__init__c                 C   s
   d| j  S )NzSecurityGroup:%s)r
   r   r   r   r   __repr__+   s    zSecurityGroup.__repr__c                    sD   t t| |||}|d k	r |S |dkr.| jS |dkr<| jS d S d S )NZipPermissionsZipPermissionsEgress)r   r   startElementr   r   )r   r
   attrsr   retvalr   r   r   r   .   s    zSecurityGroup.startElementc                 C   s   |dkr|| _ n|dkr || _n|dkr0|| _nr|dkr@|| _nb|dkrP|| _nR|dkrZnH|dkr|dkrrd	| _q|d
krd| _qtd|| jf nt| || d S )NZownerIdgroupId	groupNameZvpcIdZgroupDescriptionZipRangesreturnfalseFtrueTz*Unexpected value of status %s for group %s)r	   r   r
   r   r   status	Exceptionsetattrr   r
   valuer   r   r   r   
endElement9   s0    zSecurityGroup.endElementFc                 C   s.   | j r| jj| j|dS | jj| j|dS d S )N)group_iddry_runr'   )r   r   Zdelete_security_groupr   r
   )r   r'   r   r   r   deleteU   s    zSecurityGroup.deletec	           
      C   s>   t | }	||	_||	_||	_| j|	 |	j|||||d dS )z
        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)IPPermissionsip_protocol	from_portto_portr   append	add_grant)
r   r+   r,   r-   src_group_namesrc_group_owner_idcidr_ipsrc_group_group_idr'   ruler   r   r   add_rulea   s    zSecurityGroup.add_rulec	                 C   s   | j stdd}	| j D ]}
|
j|kr|
j|kr|
j|kr|
}	d}|
jD ]0}|j|ks`|j|krH|j|krH|j	|krH|}qH|r|
j
| t|
jdkr| j 
|	 qdS )z
        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.
        zThe security group has no rulesNr   )r   
ValueErrorr+   r,   r-   grantsr
   r&   r	   r2   removelen)r   r+   r,   r-   r0   r1   r2   r3   r'   Ztarget_ruler4   Ztarget_grantgrantr   r   r   remove_rulev   s$    






zSecurityGroup.remove_rulec                 C   s   d}| j s| j}d}| j r | j}d}	d}
d}|r`d}|j}
| j sH|j}	nt|drZ|j}n|j}| jj||	|
|||||||d
}|rt|t	s|g}|D ]}| j
||||	|
|||d q|S )a  
        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.
        Nr&   r(   )r   r
   r   r	   hasattrr&   r   Zauthorize_security_group
isinstancelistr5   )r   r+   r,   r-   r2   	src_groupr'   
group_namer&   r0   r1   r3   r    Zsingle_cidr_ipr   r   r   	authorize   sN    


  zSecurityGroup.authorizec                 C   s   d }| j s| j}d }| j r | j}d }	d }
d }|r`d }|j}
| j sH|j}	nt|drZ|j}n|j}| jj||	|
|||||||d
}|r| j||||	|
|||d |S )Nr&   r(   )	r   r
   r   r	   r<   r&   r   Zrevoke_security_groupr;   )r   r+   r,   r-   r2   r?   r'   r@   r&   r0   r1   r3   r    r   r   r   revoke   sH    

  zSecurityGroup.revokec              
   C   s   |j | jkrtd| j }|jf |}|j|p6| j | j|d}g }| jD ]f}|j	D ]Z}	|	j pd|	j
}
|
r|
|kr||
 |jdddd|	|d qV|j|j|j|j|	j|d qVqL|S )a{  
        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.
        z!Unable to copy to the same Regionr(   N)r
   regionr   r   
get_paramsconnectZcreate_security_groupr   r   r7   r&   r.   rA   r+   r,   r-   r2   )r   rC   r
   r'   Zconn_paramsZrconnZsgZsource_groupsr4   r:   Z	grant_nomr   r   r   copy_to_region   s0    



 
zSecurityGroup.copy_to_regionc                 C   sV   g }| j r(|| jjd| ji|d n|| jjd| ji|d dd |D }|S )z
        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
        zinstance.group-id)filtersr'   zgroup-idc                 S   s   g | ]}|j D ]}|qqS r   )	instances).0rir   r   r   
<listcomp>5  s       z+SecurityGroup.instances.<locals>.<listcomp>)r   extendr   Zget_all_reservationsr   )r   r'   rsrH   r   r   r   rH   "  s    


zSecurityGroup.instances)NNNNN)F)F)F)NNNNNF)NNNNNF)NF)F)__name__
__module____qualname__r   r   r   r%   r)   r5   r;   rA   rB   rF   rH   __classcell__r   r   r   r   r      s2         
 
 
      
C      
%
(r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s"   |dkr|  t|  | d S d S Nitem)r.   r*   r   r
   r   r   r   r   r   r   ;  s    zIPPermissionsList.startElementc                 C   s   d S r   r   r#   r   r   r   r%   A  s    zIPPermissionsList.endElementN)rO   rP   rQ   r   r%   r   r   r   r   r   9  s   r   c                   @   s8   e Zd ZdddZdd Zdd Zdd	 ZdddZdS )r*   Nc                 C   s"   || _ d | _d | _d | _g | _d S r   )parentr+   r,   r-   r7   r   rW   r   r   r   r   G  s
    zIPPermissions.__init__c                 C   s   d| j | j| jf S )NzIPPermissions:%s(%s-%s))r+   r,   r-   r   r   r   r   r   N  s     zIPPermissions.__repr__c                 C   s&   |dkr"| j t|  | j d S d S rS   )r7   r.   GroupOrCIDRrV   r   r   r   r   R  s    
zIPPermissions.startElementc                 C   s@   |dkr|| _ n,|dkr || _n|dkr0|| _nt| || d S )NZ
ipProtocolZfromPortZtoPort)r+   r,   r-   r"   r#   r   r   r   r%   X  s    zIPPermissions.endElementFc                 C   s0   t | }||_||_||_||_| j| |S r   )rY   r	   r&   r
   r2   r7   r.   )r   r
   r	   r2   r&   r'   r:   r   r   r   r/   b  s    zIPPermissions.add_grant)N)NNNNF)rO   rP   rQ   r   r   r   r%   r/   r   r   r   r   r*   E  s   

  r*   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )rY   Nc                 C   s   d | _ d | _d | _d | _d S r   )r	   r&   r
   r2   rX   r   r   r   r   o  s    zGroupOrCIDR.__init__c                 C   s*   | j rd| j  S d| jp| j| jf S d S )Nz%sz%s-%s)r2   r
   r&   r	   r   r   r   r   r   u  s    
zGroupOrCIDR.__repr__c                 C   s   d S r   r   rV   r   r   r   r   {  s    zGroupOrCIDR.startElementc                 C   sN   |dkr|| _ n|dkr || _n|dkr.|| _|dkr>|| _nt| || d S )NZuserIdr   r   ZcidrIp)r	   r&   r
   r2   r"   r#   r   r   r   r%   ~  s    zGroupOrCIDR.endElement)N)rO   rP   rQ   r   r   r   r%   r   r   r   r   rY   m  s   
rY   N)__doc__Zboto.ec2.ec2objectr   Zboto.exceptionr   r   r>   r   objectr*   rY   r   r   r   r   <module>   s     (