o
    rMf                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ dd	lZdd
lmZ e di Zdd Zdd ZG dd deZd	S )zg
This module provides an interface to the Elastic Compute Cloud (EC2)
load balancing service from AWS.
    )AWSQueryConnection)InstanceInfo)LoadBalancerLoadBalancerZones)InstanceState)HealthCheck)
RegionInfoget_regionsload_regions)connectN)sixelasticloadbalancingc                   C   s   t dtdS )z
    Get all available regions for the ELB service.

    :rtype: list
    :return: A list of :class:`boto.RegionInfo` instances
    r   )connection_cls)r	   ELBConnection r   r   W/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/boto/ec2/elb/__init__.pyregions)   s   r   c                 K   s   t d| fdti|S )a4  
    Given a valid region name, return a
    :class:`boto.ec2.elb.ELBConnection`.

    :param str region_name: The name of the region to connect to.

    :rtype: :class:`boto.ec2.ELBConnection` or ``None``
    :return: A connection to the given region, or None if an invalid region
        name is given
    r   r   )r   r   )region_name	kw_paramsr   r   r   connect_to_region3   s
   
r   c                       s:  e Zd ZejdddZejdddZejdddZ			
		dC fdd	Z	dd Z
dd ZdDddZ			dEddZ		dDddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* ZdFd+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#  Z$S )Gr   Botoelb_versionz
2012-06-01elb_region_namez	us-east-1elb_region_endpointz,elasticloadbalancing.us-east-1.amazonaws.comNTr   /c                    sP   |s
t | | j| j}|| _tt| j||||||||| jj|	|
||||d dS )z
        Init method to create a new connection to EC2 Load Balancing Service.

        .. note:: The region argument is overridden by the region specified in
            the boto configuration file.
        )validate_certsprofile_nameN)r   DefaultRegionNameDefaultRegionEndpointregionsuperr   __init__endpoint)selfaws_access_key_idaws_secret_access_key	is_secureportproxy
proxy_port
proxy_user
proxy_passdebughttps_connection_factoryr   pathsecurity_tokenr   r   	__class__r   r   r!   J   s   
zELBConnection.__init__c                 C   s   dgS )Nzhmac-v4r   )r#   r   r   r   _required_auth_capabilityc   s   z'ELBConnection._required_auth_capabilityc                 C   s8   t |tjr	|g}t|D ]\}}||||d  < qd S )N   )
isinstancer   string_types	enumerate)r#   paramsitemslabelindexitemr   r   r   build_list_paramsf   s
   zELBConnection.build_list_paramsc                 C   s6   i }|r|  ||d |r||d< | d|dtfgS )a  
        Retrieve all load balancers associated with your account.

        :type load_balancer_names: list
        :keyword load_balancer_names: An optional list of load balancer names.

        :type marker: string
        :param marker: Use this only when paginating results and only
            in follow-up request after you've received a response
            where the results are truncated.  Set this to the value of
            the Marker element in the response you just received.

        :rtype: :py:class:`boto.resultset.ResultSet`
        :return: A ResultSet containing instances of
            :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
        zLoadBalancerNames.member.%dMarkerDescribeLoadBalancersmember)r<   get_listr   )r#   load_balancer_namesmarkerr7   r   r   r   get_all_load_balancersl   s   z$ELBConnection.get_all_load_balancersinternet-facingc                 C   s  |s|sdS ||d}|rHt |D ]6\}	}
|	d }|
d  }|
d |d| < |
d |d| < |
d |d| < |d	ks?|d
krG|
d |d| < q|rt |D ]D\}	}
|	d }|
d  }|
d  }|
d |d| < |
d |d| < |
d |d| < |
d |d| < |d	ks|d
kr|
d |d| < qN|r| ||d |r| ||d |r| ||d | d|t}||_||_||_||_||_	|S )a
  
        Create a new load balancer for your account. By default the load
        balancer will be created in EC2. To create a load balancer inside a
        VPC, parameter zones must be set to None and subnets must not be None.
        The load balancer will be automatically created under the VPC that
        contains the subnet(s) specified.

        :type name: string
        :param name: The mnemonic name associated with the new load balancer

        :type zones: List of strings
        :param zones: The names of the availability zone(s) to add.

        :type listeners: List of tuples
        :param listeners: Each tuple contains three or four values,
            (LoadBalancerPortNumber, InstancePortNumber, Protocol,
            [SSLCertificateId]) where LoadBalancerPortNumber and
            InstancePortNumber are integer values between 1 and 65535,
            Protocol is a string containing either 'TCP', 'SSL', HTTP', or
            'HTTPS'; SSLCertificateID is the ARN of a AWS IAM
            certificate, and must be specified when doing HTTPS.

        :type subnets: list of strings
        :param subnets: A list of subnet IDs in your VPC to attach to
            your LoadBalancer.

        :type security_groups: list of strings
        :param security_groups: The security groups assigned to your
            LoadBalancer within your VPC.

        :type scheme: string
        :param scheme: The type of a LoadBalancer.  By default, Elastic
            Load Balancing creates an internet-facing LoadBalancer with
            a publicly resolvable DNS name, which resolves to public IP
            addresses.

            Specify the value internal for this option to create an
            internal LoadBalancer with a DNS name that resolves to
            private IP addresses.

            This option is only available for LoadBalancers attached
            to an Amazon VPC.

        :type complex_listeners: List of tuples
        :param complex_listeners: Each tuple contains four or five values,
            (LoadBalancerPortNumber, InstancePortNumber, Protocol,
             InstanceProtocol, SSLCertificateId).

            Where:
                - LoadBalancerPortNumber and InstancePortNumber are integer
                  values between 1 and 65535
                - Protocol and InstanceProtocol is a string containing
                  either 'TCP',
                  'SSL', 'HTTP', or 'HTTPS'
                - SSLCertificateId is the ARN of an SSL certificate loaded into
                  AWS IAM

        :rtype: :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
        :return: The newly created
            :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
        N)LoadBalancerNameSchemer3      r   $Listeners.member.%d.LoadBalancerPort Listeners.member.%d.InstancePortListeners.member.%d.ProtocolHTTPSSSL   $Listeners.member.%d.SSLCertificateId$Listeners.member.%d.InstanceProtocol   AvailabilityZones.member.%dSubnets.member.%dSecurityGroups.member.%dCreateLoadBalancer)
r6   upperr<   
get_objectr   name	listenersavailability_zonessubnetssecurity_groups)r#   rW   zonesrX   rZ   r[   schemecomplex_listenersr7   r:   listeneriprotocolInstanceProtocolload_balancerr   r   r   create_load_balancer   sX   @z"ELBConnection.create_load_balancerc           
      C   s0  |s|sdS d|i}|rGt |D ]6\}}|d }|d  }|d |d| < |d |d| < |d |d| < |d	ks>|d
krF|d |d| < q|rt |D ]D\}}|d }|d  }|d  }	|d |d| < |d |d| < |d |d| < |d |d| < |d	ks|d
kr|d |d| < qM| d|S )av  
        Creates a Listener (or group of listeners) for an existing
        Load Balancer

        :type name: string
        :param name: The name of the load balancer to create the listeners for

        :type listeners: List of tuples
        :param listeners: Each tuple contains three or four values,
            (LoadBalancerPortNumber, InstancePortNumber, Protocol,
            [SSLCertificateId]) where LoadBalancerPortNumber and
            InstancePortNumber are integer values between 1 and 65535,
            Protocol is a string containing either 'TCP', 'SSL', HTTP', or
            'HTTPS'; SSLCertificateID is the ARN of a AWS IAM
            certificate, and must be specified when doing HTTPS.

        :type complex_listeners: List of tuples
        :param complex_listeners: Each tuple contains four or five values,
            (LoadBalancerPortNumber, InstancePortNumber, Protocol,
             InstanceProtocol, SSLCertificateId).

            Where:
                - LoadBalancerPortNumber and InstancePortNumber are integer
                  values between 1 and 65535
                - Protocol and InstanceProtocol is a string containing
                  either 'TCP',
                  'SSL', 'HTTP', or 'HTTPS'
                - SSLCertificateId is the ARN of an SSL certificate loaded into
                  AWS IAM

        :return: The status of the request
        NrE   r3   rG   r   rH   rI   rJ   rK   rL   rM   rN   rO   rP   CreateLoadBalancerListeners)r6   rU   
get_status)
r#   rW   rX   r^   r7   r:   r_   r`   ra   rb   r   r   r   create_load_balancer_listeners   s4   "z,ELBConnection.create_load_balancer_listenersc                 C   s   d|i}|  d|S )z
        Delete a Load Balancer from your account.

        :type name: string
        :param name: The name of the Load Balancer to delete
        rE   DeleteLoadBalancerrf   )r#   rW   r7   r   r   r   delete_load_balancer<  s   z"ELBConnection.delete_load_balancerc                 C   s6   d|i}t |D ]\}}||d|d  < q| d|S )aL  
        Deletes a load balancer listener (or group of listeners)

        :type name: string
        :param name: The name of the load balancer to create the listeners for

        :type ports: List int
        :param ports: Each int represents the port on the ELB to be removed

        :return: The status of the request
        rE   zLoadBalancerPorts.member.%dr3   DeleteLoadBalancerListeners)r6   rf   )r#   rW   portsr7   r:   r'   r   r   r   delete_load_balancer_listenersF  s   z,ELBConnection.delete_load_balancer_listenersc                 C   *   d|i}|  ||d | d|t}|jS )a  
        Add availability zones to an existing Load Balancer
        All zones must be in the same region as the Load Balancer
        Adding zones that are already registered with the Load Balancer
        has no effect.

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

        :type zones: List of strings
        :param zones: The name of the zone(s) to add.

        :rtype: List of strings
        :return: An updated list of zones for this Load Balancer.

        rE   rQ   &EnableAvailabilityZonesForLoadBalancerr<   rV   r   r\   )r#   load_balancer_namezones_to_addr7   objr   r   r   enable_availability_zonesW  s   z'ELBConnection.enable_availability_zonesc                 C   rn   )aT  
        Remove availability zones from an existing Load Balancer.
        All zones must be in the same region as the Load Balancer.
        Removing zones that are not registered with the Load Balancer
        has no effect.
        You cannot remove all zones from an Load Balancer.

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

        :type zones: List of strings
        :param zones: The name of the zone(s) to remove.

        :rtype: List of strings
        :return: An updated list of zones for this Load Balancer.

        rE   rQ   'DisableAvailabilityZonesForLoadBalancerrp   )r#   rq   zones_to_remover7   rs   r   r   r   disable_availability_zoneso  s   z(ELBConnection.disable_availability_zonesc                 C   s   d}|  |v rt|tr|rd}nd}d|i}|  dkr#||d< nE|  dkrB|jr.dp/d|d< |j|d	< |j|d
< |j|d< n&|  dkrW|jrMdpNd|d< |j|d< n|  dkrc|j|d< nt	d|| j
d|ddS )a  Changes an attribute of a Load Balancer

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

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

        * crossZoneLoadBalancing - Boolean (true)
        * connectingSettings - :py:class:`ConnectionSettingAttribute` instance
        * accessLog - :py:class:`AccessLogAttribute` instance
        * connectionDraining - :py:class:`ConnectionDrainingAttribute` instance

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

        :rtype: bool
        :return: Whether the operation succeeded or not
        )crosszoneloadbalancingtruefalserE   rx   z5LoadBalancerAttributes.CrossZoneLoadBalancing.Enabled	accesslogz(LoadBalancerAttributes.AccessLog.Enabledz-LoadBalancerAttributes.AccessLog.S3BucketNamez/LoadBalancerAttributes.AccessLog.S3BucketPrefixz-LoadBalancerAttributes.AccessLog.EmitIntervalconnectiondrainingz1LoadBalancerAttributes.ConnectionDraining.Enabledz1LoadBalancerAttributes.ConnectionDraining.Timeoutconnectingsettingsz5LoadBalancerAttributes.ConnectionSettings.IdleTimeoutInvalidAttributeModifyLoadBalancerAttributesGET)verb)lowerr4   boolenableds3_bucket_names3_bucket_prefixemit_intervaltimeoutidle_timeout
ValueErrorrf   )r#   rq   	attributevalue	bool_reqsr7   r   r   r   modify_lb_attribute  s>   

z!ELBConnection.modify_lb_attributec                 C   s"   ddl m} d|i}| d||S )a  Gets all Attributes of a Load Balancer

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

        :rtype: boto.ec2.elb.attribute.LbAttributes
        :return: The attribute object of the ELB.
        r   )LbAttributesrE   DescribeLoadBalancerAttributes)boto.ec2.elb.attributesr   rV   )r#   rq   r   r7   r   r   r   get_all_lb_attributes  s
   	z#ELBConnection.get_all_lb_attributesc                 C   sX   |  |}| dkr|jS | dkr|jjS | dkr!|jS | dkr*|jS dS )a  Gets an attribute of a Load Balancer

        This will make an EC2 call for each method call.

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

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

          * accessLog - :py:class:`AccessLogAttribute` instance
          * crossZoneLoadBalancing - Boolean
          * connectingSettings - :py:class:`ConnectionSettingAttribute` instance
          * connectionDraining - :py:class:`ConnectionDrainingAttribute`
            instance

        :rtype: Attribute dependent
        :return: The new value for the attribute
        r{   rx   r|   r}   N)r   r   
access_logcross_zone_load_balancingr   connection_drainingconnecting_settings)r#   rq   r   
attributesr   r   r   get_lb_attribute  s   
zELBConnection.get_lb_attributec                 C   *   d|i}|  ||d | d|dtfgS )a  
        Add new Instances to an existing Load Balancer.

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

        :type instances: List of strings
        :param instances: The instance ID's of the EC2 instances to add.

        :rtype: List of strings
        :return: An updated list of instances for this Load Balancer.

        rE   Instances.member.%d.InstanceId!RegisterInstancesWithLoadBalancerr?   r<   r@   r   r#   rq   	instancesr7   r   r   r   register_instances     
z ELBConnection.register_instancesc                 C   r   )a  
        Remove Instances from an existing Load Balancer.

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

        :type instances: List of strings
        :param instances: The instance ID's of the EC2 instances to remove.

        :rtype: List of strings
        :return: An updated list of instances for this Load Balancer.

        rE   r   #DeregisterInstancesFromLoadBalancerr?   r   r   r   r   r   deregister_instances   r   z"ELBConnection.deregister_instancesc                 C   s.   d|i}|r|  ||d | d|dtfgS )aH  
        Get current state of all Instances registered to an Load Balancer.

        :type load_balancer_name: string
        :param load_balancer_name: The name of the Load Balancer

        :type instances: List of strings
        :param instances: The instance ID's of the EC2 instances
                          to return status for.  If not provided,
                          the state of all instances will be returned.

        :rtype: List of :class:`boto.ec2.elb.instancestate.InstanceState`
        :return: list of state info for instances in this Load Balancer.

        rE   r   DescribeInstanceHealthr?   )r<   r@   r   r   r   r   r   describe_instance_health  s   z&ELBConnection.describe_instance_healthc                 C   s*   ||j |j|j|j|jd}| d|tS )a  
        Define a health check for the EndPoints.

        :type name: string
        :param name: The mnemonic name associated with the load balancer

        :type health_check: :class:`boto.ec2.elb.healthcheck.HealthCheck`
        :param health_check: A HealthCheck object populated with the desired
                             values.

        :rtype: :class:`boto.ec2.elb.healthcheck.HealthCheck`
        :return: The updated :class:`boto.ec2.elb.healthcheck.HealthCheck`
        )rE   zHealthCheck.TimeoutzHealthCheck.TargetzHealthCheck.IntervalzHealthCheck.UnhealthyThresholdzHealthCheck.HealthyThresholdConfigureHealthCheck)r   targetintervalunhealthy_thresholdhealthy_thresholdrV   r   )r#   rW   health_checkr7   r   r   r   configure_health_check+  s   z$ELBConnection.configure_health_checkc                 C      |||d}|  d|S )z
        Sets the certificate that terminates the specified listener's SSL
        connections. The specified certificate replaces any prior certificate
        that was used on the same LoadBalancer and port.
        )rE   LoadBalancerPortSSLCertificateId%SetLoadBalancerListenerSSLCertificateri   )r#   lb_namelb_portssl_certificate_idr7   r   r   r   set_lb_listener_SSL_certificateA  s
   z-ELBConnection.set_lb_listener_SSL_certificatec                 C   r   )a  
        Generates a stickiness policy with sticky session lifetimes that follow
        that of an application-generated cookie. This policy can only be
        associated with HTTP listeners.

        This policy is similar to the policy created by
        CreateLBCookieStickinessPolicy, except that the lifetime of the special
        Elastic Load Balancing cookie follows the lifetime of the
        application-generated cookie specified in the policy configuration. The
        load balancer only inserts a new stickiness cookie when the application
        response includes a new application cookie.

        If the application cookie is explicitly removed or expires, the session
        stops being sticky until a new application cookie is issued.
        )
CookieNamerE   
PolicyNameCreateAppCookieStickinessPolicyri   )r#   rW   r   policy_namer7   r   r   r   #create_app_cookie_stickiness_policyM  s
   z1ELBConnection.create_app_cookie_stickiness_policyc                 C   s&   ||d}|dur||d< |  d|S )a1  
        Generates a stickiness policy with sticky session lifetimes controlled
        by the lifetime of the browser (user-agent) or a specified expiration
        period. This policy can only be associated only with HTTP listeners.

        When a load balancer implements this policy, the load balancer uses a
        special cookie to track the backend server instance for each request.
        When the load balancer receives a request, it first checks to see if
        this cookie is present in the request. If so, the load balancer sends
        the request to the application server specified in the cookie. If not,
        the load balancer sends the request to a server that is chosen based on
        the existing load balancing algorithm.

        A cookie is inserted into the response for binding subsequent requests
        from the same user to that server. The validity of the cookie is based
        on the cookie expiration time, which is specified in the policy
        configuration.

        None may be passed for cookie_expiration_period.
        rE   r   NCookieExpirationPeriodCreateLBCookieStickinessPolicyri   )r#   cookie_expiration_periodr   r   r7   r   r   r   "create_lb_cookie_stickiness_policyb  s   z0ELBConnection.create_lb_cookie_stickiness_policyc           	      C   sV   |||d}t t|dD ]\}\}}||d| < ||d| < qd|d< | d|S )a  
        Creates a new policy that contains the necessary attributes
        depending on the policy type. Policies are settings that are
        saved for your load balancer and that can be applied to the
        front-end listener, or the back-end application server.

        )rE   r   PolicyTypeNamer3   z(PolicyAttributes.member.%d.AttributeNamez)PolicyAttributes.member.%d.AttributeValue PolicyAttributesCreateLoadBalancerPolicy)r6   r   	iteritemsrf   )	r#   r   r   policy_typepolicy_attributesr7   r:   rW   r   r   r   r   create_lb_policy~  s   	zELBConnection.create_lb_policyc                 C   s   ||d}|  d|S )z}
        Deletes a policy from the LoadBalancer. The specified policy must not
        be enabled for any listeners.
        r   DeleteLoadBalancerPolicyri   )r#   r   r   r7   r   r   r   delete_lb_policy  s   zELBConnection.delete_lb_policyc                 C   s6   ||d}t |r| ||d nd|d< | d|S )z
        Associates, updates, or disables a policy with a listener on the load
        balancer. Currently only zero (0) or one (1) policy can be associated
        with a listener.
        )rE   r   PolicyNames.member.%dr   PolicyNames!SetLoadBalancerPoliciesOfListener)lenr<   rf   )r#   r   r   policiesr7   r   r   r   set_lb_policies_of_listener  s   z)ELBConnection.set_lb_policies_of_listenerc                 C   s2   ||d}|r|  ||d nd|d< | d|S )z
        Replaces the current set of policies associated with a port on which
        the back-end server is listening with a new set of policies.
        )rE   InstancePortr   r   r   'SetLoadBalancerPoliciesForBackendServer)r<   rf   )r#   r   instance_portr   r7   r   r   r   !set_lb_policies_of_backend_server  s   z/ELBConnection.set_lb_policies_of_backend_serverc                 C   $   d|i}|  ||d | d|dS )a  
        Associates one or more security groups with the load balancer.
        The provided security groups will override any currently applied
        security groups.

        :type name: string
        :param name: The name of the Load Balancer

        :type security_groups: List of strings
        :param security_groups: The name of the security group(s) to add.

        :rtype: List of strings
        :return: An updated list of security groups for this Load Balancer.

        rE   rS   !ApplySecurityGroupsToLoadBalancerNr<   r@   )r#   rW   r[   r7   r   r   r   apply_security_groups_to_lb     z)ELBConnection.apply_security_groups_to_lbc                 C   r   )a  
        Attaches load balancer to one or more subnets.
        Attaching subnets that are already registered with the
        Load Balancer has no effect.

        :type name: string
        :param name: The name of the Load Balancer

        :type subnets: List of strings
        :param subnets: The name of the subnet(s) to add.

        :rtype: List of strings
        :return: An updated list of subnets for this Load Balancer.

        rE   rR   AttachLoadBalancerToSubnetsNr   r#   rW   rZ   r7   r   r   r   attach_lb_to_subnets  r   z"ELBConnection.attach_lb_to_subnetsc                 C   r   )a\  
        Detaches load balancer from one or more subnets.

        :type name: string
        :param name: The name of the Load Balancer

        :type subnets: List of strings
        :param subnets: The name of the subnet(s) to detach.

        :rtype: List of strings
        :return: An updated list of subnets for this Load Balancer.

        rE   rR   DetachLoadBalancerFromSubnetsNr   r   r   r   r   detach_lb_from_subnets  s   z$ELBConnection.detach_lb_from_subnets)NNTNNNNNr   NNr   NTN)NN)NNNrD   N)N)%__name__
__module____qualname__botoconfigget
APIVersionr   r   r!   r2   r<   rC   rd   rg   rj   rm   rt   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r0   r   r   B   sX    

r
B
7
	r   )__doc__boto.connectionr   boto.ec2.instanceinfor   boto.ec2.elb.loadbalancerr   r   boto.ec2.elb.instancestater   boto.ec2.elb.healthcheckr   boto.regioninfor   r	   r
   r   r   boto.compatr   r   
RegionDatar   r   r   r   r   r   r   <module>   s   
