o
    ȷe_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__ M/var/www/ideatree/venv/lib/python3.10/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                    s@   t t| |||}|d ur|S |dkr| jS |dkr| jS d S )NipPermissionsipPermissionsEgress)r   r   startElementr   r   )r   r
   attrsr   retvalr   r   r   r   .   s   zSecurityGroup.startElementc                 C   s   |dkr	|| _ d S |dkr|| _d S |dkr|| _d S |dkr$|| _d S |dkr-|| _d S |dkr3d S |dkrR|dkr@d	| _d S |d
krId| _d S td|| jf t| || d S )NownerIdgroupId	groupNamevpcIdgroupDescription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 )N)group_iddry_runr-   )r   r   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   r2   r3   r4   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 ]F}
|
j|krE|
j|krE|
j|krE|
}	d}|
jD ]}|j|ks0|j|kr<|j|kr<|j	|kr<|}q$|rE|
j
| t|
jdkrR| 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   
ValueErrorr2   r3   r4   grantsr
   r,   r	   r9   removelen)r   r2   r3   r4   r7   r8   r9   r:   r-   target_ruler;   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}|r0d}|j}
| j s$|j}	nt|dr-|j}n|j}| jj||	|
|||||||d
}|r\t|t	sJ|g}|D ]}| j
||||	|
|||d qL|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   authorize_security_group
isinstancelistr<   )r   r2   r3   r4   r9   	src_groupr-   
group_namer,   r7   r8   r:   r&   single_cidr_ipr   r   r   	authorize   sJ   


zSecurityGroup.authorizec                 C   s   d }| j s| j}d }| j r| j}d }	d }
d }|r0d }|j}
| j s$|j}	nt|dr-|j}n|j}| jj||	|
|||||||d
}|rO| j||||	|
|||d |S )Nr,   r.   )	r   r
   r   r	   rE   r,   r   revoke_security_grouprD   )r   r2   r3   r4   r9   rI   r-   rJ   r,   r7   r8   r:   r&   r   r   r   revoke   sD   

zSecurityGroup.revokec              
   C   s   |j | jkr
td| j }|jdi |}|j|p| j | j|d}g }| jD ]3}|j	D ]-}	|	j p4|	j
}
|
rL|
|vrK||
 |jdddd|	|d q-|j|j|j|j|	j|d q-q(|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.   Nr   )r
   regionr   r   
get_paramsconnectcreate_security_groupr   r   r>   r,   r5   rL   r2   r3   r4   r9   )r   rO   r
   r-   conn_paramsrconnsgsource_groupsr;   rC   	grant_nomr   r   r   copy_to_region   s2   




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   get_all_reservationsr   )r   r-   rsrZ   r   r   r   rZ   "  s   


zSecurityGroup.instances)NNNNN)F)NNNNNF)NF)__name__
__module____qualname__r   r   r   r+   r0   r<   rD   rL   rN   rX   rZ   __classcell__r   r   r   r   r      s(    



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)r5   r1   r   r
   r   r   r   r   r   r   ;  s   zIPPermissionsList.startElementc                 C      d S r   r   r)   r   r   r   r+   A     zIPPermissionsList.endElementN)rb   rc   rd   r   r+   r   r   r   r   r   9  s    r   c                   @   s<   e Zd ZdddZdd Zdd Zdd	 Z		
dddZdS )r1   Nc                 C   s"   || _ d | _d | _d | _g | _d S r   )parentr2   r3   r4   r>   r   rl   r   r   r   r   G  s
   
zIPPermissions.__init__c                 C   s   d| j | j| jf S )NzIPPermissions:%s(%s-%s))r2   r3   r4   r   r   r   r   r   N  s   zIPPermissions.__repr__c                 C   s&   |dkr| j t|  | j d S d S rf   )r>   r5   GroupOrCIDRri   r   r   r   r   R  s   
zIPPermissions.startElementc                 C   sF   |dkr	|| _ d S |dkr|| _d S |dkr|| _d S t| || d S )N
ipProtocolfromPorttoPort)r2   r3   r4   r(   r)   r   r   r   r+   X  s   


zIPPermissions.endElementFc                 C   s0   t | }||_||_||_||_| j| |S r   )rn   r	   r,   r
   r9   r>   r5   )r   r
   r	   r9   r,   r-   rC   r   r   r   r6   b  s   zIPPermissions.add_grantr   )NNNNF)rb   rc   rd   r   r   r   r+   r6   r   r   r   r   r1   E  s    

r1   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )rn   Nc                 C   s   d | _ d | _d | _d | _d S r   )r	   r,   r
   r9   rm   r   r   r   r   o  s   
zGroupOrCIDR.__init__c                 C   s&   | j rd| j  S d| jp| j| jf S )Nz%sz%s-%s)r9   r
   r,   r	   r   r   r   r   r   u  s   
zGroupOrCIDR.__repr__c                 C   rj   r   r   ri   r   r   r   r   {  rk   zGroupOrCIDR.startElementc                 C   sP   |dkr|| _ n|dkr|| _n|dkr|| _|dkr || _d S t| || d S )NuserIdr   r   cidrIp)r	   r,   r
   r9   r(   r)   r   r   r   r+   ~  s   
zGroupOrCIDR.endElementr   )rb   rc   rd   r   r   r   r+   r   r   r   r   rn   m  s
    
rn   N)__doc__boto.ec2.ec2objectr   boto.exceptionr   r   rH   r   objectr1   rn   r   r   r   r   <module>   s     (