B
    ?F[_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__ 5lib/python3.7/site-packages/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   Zretval)r   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returnZ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   s,    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}	x| j D ]}
|
j|kr|
j|kr|
j|kr|
}	d}x:|
jD ]0}|j|ksd|j|krL|j|krL|j	|krL|}qLW |r|
j
| t|
jdkr| j 
|	 qW 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   r+   removelen)r   r$   r%   r&   r)   r*   r+   r,   r!   Ztarget_ruler-   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}x&|D ]}| j
||||	|
|||d qW |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listr.   )r   r$   r%   r&   r+   	src_groupr!   
group_namer    r)   r*   r,   r   Zsingle_cidr_ipr   r   r   	authorize   sF    


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   r5   r    r   Zrevoke_security_groupr4   )r   r$   r%   r&   r+   r8   r!   r9   r    r)   r*   r,   r   r   r   r   revoke   s@    
zSecurityGroup.revokec          
   C   s   |j | jkrtd| j }|jf |}|j|p6| j | j|d}g }xt| jD ]j}xd|j	D ]Z}	|	j ph|	j
}
|
r|
|kr||
 |jdddd|	|d qZ|j|j|j|j|	j|d qZW qNW |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 Region)r!   N)r	   regionr   r   Z
get_paramsZconnectZcreate_security_groupr
   r   r0   r    r'   r:   r$   r%   r&   r+   )r   r<   r	   r!   Zconn_paramsZrconnZsgZsource_groupsr-   r3   Z	grant_nomr   r   r   copy_to_region   s(    


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!   Zrsr?   r   r   r   r?   "  s    


zSecurityGroup.instances)NNNNN)F)F)F)NNNNNF)NNNNNF)NF)F)__name__
__module____qualname__r   r   r   r   r"   r.   r4   r:   r;   r=   r?   __classcell__r   r   )r   r   r      s    



 
B 
$
(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 )Nr   )r   r	   r   r   r   r   r   r   A  s    zIPPermissionsList.endElementN)rE   rF   rG   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 )N)parentr$   r%   r&   r0   )r   rK   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 )NrI   rJ   )r0   r'   GroupOrCIDR)r   r	   r   r   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   r   r   r   X  s    zIPPermissions.endElementFc             C   s0   t | }||_||_||_||_| j| |S )N)rL   r   r    r	   r+   r0   r'   )r   r	   r   r+   r    r!   r3   r   r   r   r(   b  s    zIPPermissions.add_grant)N)NNNNF)rE   rF   rG   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 )rL   Nc             C   s   d | _ d | _d | _d | _d S )N)r   r    r	   r+   )r   rK   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)r+   r	   r    r   )r   r   r   r   r   u  s    
zGroupOrCIDR.__repr__c             C   s   d S )Nr   )r   r	   r   r   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	   r+   r   )r   r	   r   r   r   r   r   r   ~  s    zGroupOrCIDR.endElement)N)rE   rF   rG   r   r   r   r   r   r   r   r   rL   m  s   
rL   N)__doc__Zboto.ec2.ec2objectr   Zboto.exceptionr   r   r7   r   objectr#   rL   r   r   r   r   <module>   s     (