
    &`iwH                     H   d dl Z d dl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 d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z, d dl-m.Z. d dl/m0Z0  G d d          Z1dS )    N)client)ClientExceptionServerException)AllocatePublicIpAddressRequest)AuthorizeSecurityGroupRequest)CreateInstanceRequest)CreateKeyPairRequest)CreateSecurityGroupRequest)CreateVpcRequest)CreateVSwitchRequest)DeleteInstanceRequest)DeleteInstancesRequest)DeleteKeyPairsRequest)DescribeInstancesRequest)DescribeKeyPairsRequest)DescribeSecurityGroupsRequest)DescribeVpcsRequest)DescribeVSwitchesRequest)ImportKeyPairRequest)RunInstancesRequest)StartInstanceRequest)StopInstanceRequest)StopInstancesRequest)TagResourcesRequestc                       e Zd ZdZd Zd#dZ	 	 	 	 	 d$d
Z	 	 	 	 	 	 d%dZd Zd#dZ	d Z
d Zd Zd Zd&dZd Zd'dZd(dZd Zd Zd Zd Zd Zd Zd)d Zd)d!Zd" ZdS )*	AcsClienta  
    A wrapper around Aliyun SDK. We use this wrapper in aliyun node provider.

    Parameters:
        access_key: The AccessKey ID of your aliyun account.
        access_key_secret: The AccessKey secret of your aliyun account.
        region_id: A region is a geographic area where a data center resides.
                   Region_id is the ID of region (e.g., cn-hangzhou,
                   us-west-1, etc.)
        max_retries: The maximum number of retries each connection.
    c                 @    t          j        ||||          | _        d S )N)aksecretmax_retry_time	region_id)r   r   cli)self
access_keyaccess_key_secretr!   max_retriess        x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/_private/aliyun/utils.py__init__zAcsClient.__init__8   s+    #$&	
 
 
    Nc                     t                      }||                    |           ||                    |           |                     |          }|*|                    d                              d          }|S dS )zQuery the details of one or more Elastic Compute Service (ECS) instances.

        :param tags: The tags of the instance.
        :param instance_ids: The IDs of ECS instances
        :return: ECS instance list
        N	InstancesInstance)r   set_Tagsset_InstanceIds_send_requestget)r#   tagsinstance_idsrequestresponseinstance_lists         r'   describe_instanceszAcsClient.describe_instances@   s     +,,T"""###L111%%g..$LL5599*EEM  tr)   	optimizedPostPaidSpotWithPriceLimitPayByTraffic   c
                 J   t                      }
|
                    |           |
                    |           |
                    |           |
                    |           |
                    |           |
                    |           |
                    |	           |
                    |           |
	                    |           | 
                    |
          }|,|                    d          }t          j        d|           |S t          j        d           dS )a  Create a subscription or pay-as-you-go ECS instance.

        :param instance_type: The instance type of the ECS.
        :param image_id: The ID of the image used to create the instance.
        :param tags: The tags of the instance.
        :param key_pair_name: The name of the key pair to be bound to
                              the instance.
        :param optimized: Specifies whether the instance is I/O optimized
        :param instance_charge_type: The billing method of the instance.
                                     Default value: PostPaid.
        :param spot_strategy: The preemption policy for the pay-as-you-go
                              instance.
        :param internet_charge_type: The billing method for network usage.
                                     Default value: PayByTraffic.
        :param internet_max_bandwidth_out: The maximum inbound public
                                           bandwidth. Unit: Mbit/s.
        :return: The created instance ID.
        N
InstanceIdz-instance %s created task submit successfully.instance created failed.)r   set_InstanceTypeset_ImageIdset_IoOptimizedset_InstanceChargeTypeset_SpotStrategyset_InternetChargeTypeset_InternetMaxBandwidthOutset_KeyPairNamer-   r/   r0   logginginfoerror)r#   instance_typeimage_idr1   key_pair_namer7   instance_charge_typespot_strategyinternet_charge_typeinternet_max_bandwidth_outr3   r4   instance_ids                r'   create_instancezAcsClient.create_instanceR   s   < ())  ///H%%%	***&&';<<<  ///&&';<<<++,FGGG...%%g..",,|44KLH+VVV0111tr)      c                    t                      }|                    |           |                    |           |                    |           |                    |	           |                    |
           |                    |           |                    |           |                    |           |	                    |           |
                    |           |                    |           |                    |           |                     |          }|*|                    d                              d          }|S t          j        d           dS )a  Create one or more pay-as-you-go or subscription
            Elastic Compute Service (ECS) instances

        :param instance_type: The instance type of the ECS.
        :param image_id: The ID of the image used to create the instance.
        :param tags: The tags of the instance.
        :param security_group_id: The ID of the security group to which to
                                  assign the instance. Instances in the same
                                  security group can communicate with
                                  each other.
        :param vswitch_id: The ID of the vSwitch to which to connect
                           the instance.
        :param key_pair_name: The name of the key pair to be bound to
                              the instance.
        :param amount: The number of instances that you want to create.
        :param optimized: Specifies whether the instance is I/O optimized
        :param instance_charge_type: The billing method of the instance.
                                     Default value: PostPaid.
        :param spot_strategy: The preemption policy for the pay-as-you-go
                              instance.
        :param internet_charge_type: The billing method for network usage.
                                     Default value: PayByTraffic.
        :param internet_max_bandwidth_out: The maximum inbound public
                                           bandwidth. Unit: Mbit/s.
        :return: The created instance IDs.
        NInstanceIdSetsInstanceIdSetr>   )r   r?   r@   rA   rB   rC   rD   rE   r-   
set_Amountset_SecurityGroupIdset_VSwitchIdrF   r/   r0   rG   rI   )r#   rJ   rK   r1   security_group_id
vswitch_idrL   amountr7   rM   rN   rO   rP   r3   r4   r2   s                   r'   run_instanceszAcsClient.run_instances   sY   R &''  ///H%%%	***&&';<<<  ///&&';<<<++,FGGG6"""##$5666j)))...%%g..#<<(899==oNNL0111tr)   c                     t                      }|                    |           |                     |          }||                    d          }|S dS )zCreate a security group

        :param vpc_id: The ID of the VPC in which to create
                       the security group.
        :return: The created security group ID.
        NSecurityGroupId)r
   	set_VpcIdr/   r0   )r#   vpc_idr3   r4   rZ   s        r'   create_security_groupzAcsClient.create_security_group   sZ     -..&!!!%%g.. (-> ? ?$$tr)   c                 (   t                      }||                    |           ||                    |           |                     |          }|*|                    d                              d          }|S t          j        d           dS )zQuery basic information of security groups.

        :param vpc_id: The ID of the VPC to which the security group belongs.
        :param tags: The tags of the security group.
        :return: Security group list.
        NSecurityGroupsSecurityGroupzdescribe security group failed.)r   r`   r-   r/   r0   rG   rI   )r#   ra   r1   r3   r4   security_groupss         r'   describe_security_groupsz"AcsClient.describe_security_groups   s     011f%%%T"""%%g..&ll+;<<@@QQO""7888tr)   c                     t                      }|                    |           |                    |           |                    |           |                    |           |                     |           dS )a  Create an inbound security group rule.

        :param ip_protocol: The transport layer protocol.
        :param port_range: The range of destination ports relevant to
                           the transport layer protocol.
        :param security_group_id: The ID of the destination security group.
        :param source_cidr_ip: The range of source IPv4 addresses.
                               CIDR blocks and IPv4 addresses are supported.
        N)r   set_IpProtocolset_PortRangerX   set_SourceCidrIpr/   )r#   ip_protocol
port_rangerZ   source_cidr_ipr3   s         r'   authorize_security_groupz"AcsClient.authorize_security_group   sz     011{+++j)))##$5666  0007#####r)   c                 "   t                      }|                    |           |                    |           |                    |           |                     |          }||                    d          S t          j        d|           dS )a?  Create vSwitches to divide the VPC into one or more subnets

        :param vpc_id: The ID of the VPC to which the VSwitch belongs.
        :param zone_id: The ID of the zone to which
                        the target VSwitch belongs.
        :param cidr_block: The CIDR block of the VSwitch.
        :return:
        N	VSwitchIdz!create_v_switch vpc_id %s failed.)r   
set_ZoneIdr`   set_CidrBlockr/   r0   rG   rI   )r#   ra   zone_id
cidr_blockr3   r4   s         r'   create_v_switchzAcsClient.create_v_switch   s     '((7###&!!!j)))%%g..<<,,,M=vFFFtr)   c                 z    t                      }|                     |          }||                    d          S dS )zUCreates a virtual private cloud (VPC).

        :return: The created VPC ID.
        NVpcId)r   r/   r0   r#   r3   r4   s      r'   
create_vpczAcsClient.create_vpc
  s?    
 #$$%%g..<<(((tr)   c                     t                      }|                     |          }|(|                    d                              d          S dS )zJQueries one or more VPCs in a region.

        :return: VPC list.
        NVpcsVpc)r   r/   r0   ry   s      r'   describe_vpcszAcsClient.describe_vpcs  sM    
 &''%%g..<<''++E222tr)   instancec                 &   t                      }|                    |           |                    |           |                    |           |                     |          }|t          j        d|           dS t          j        d|           dS )zCreate and bind tags to specified ECS resources.

        :param resource_ids: The IDs of N resources.
        :param tags: The tags of the resource.
        :param resource_type: The type of the resource.
        Nz$instance %s create tag successfully.zinstance %s create tag failed.)r   r-   set_ResourceTypeset_ResourceIdsr/   rG   rH   rI   )r#   resource_idsr1   resource_typer3   r4   s         r'   tag_resourcezAcsClient.tag_resource   s     &''  ///---%%g..L?NNNNNM:LIIIIIr)   c                     t                      }|                    |           |                     |          }|t          j        d|           dS t          j        d|           dS )zQStart an ECS instance.

        :param instance_id: The Ecs instance ID.
        Nzinstance %s start successfully.zinstance %s start failed.)r   set_InstanceIdr/   rG   rH   rI   r#   rQ   r3   r4   s       r'   start_instancezAcsClient.start_instance1  sn    
 '(({+++%%g..L:KHHHHHM5{CCCCCr)   Fc                     t                      }|                    |           |                    |           t          j        d|           |                     |           dS )zStop an ECS instance that is in the Running state.

        :param instance_id: The Ecs instance ID.
        :param force_stop: Specifies whether to forcibly stop the instance.
        :return:
        z$Stop %s command submit successfully.N)r   r   set_ForceStoprG   rH   r/   )r#   rQ   
force_stopr3   s       r'   stop_instancezAcsClient.stop_instance?  sd     &''{+++j)));[III7#####r)   StopChargingc                     t                      }|                    |           |                    |           |                     |          }|t	          j        d           dS dS )a  Stop one or more ECS instances that are in the Running state.

        :param instance_ids: The IDs of instances.
        :param stopped_mode: Specifies whether billing for the instance
                             continues after the instance is stopped.
        Nzstop_instances failed)r   r.   set_StoppedModer/   rG   rI   )r#   r2   stopped_moder3   r4   s        r'   stop_instanceszAcsClient.stop_instancesL  so     '((------%%g..M122222 r)   c                     t                      }|                    |           |                    d           t          j        d|           |                     |           dS )zRelease a pay-as-you-go instance or
            an expired subscription instance.

        :param instance_id: The ID of the instance that you want to release.
        Tz%Delete %s command submit successfullyN)r   r   	set_ForcerG   rH   r/   )r#   rQ   r3   s      r'   delete_instancezAcsClient.delete_instanceZ  sd     ()){+++$<kJJJ7#####r)   c                     t                      }|                    d           |                    |           |                     |           dS )zRelease one or more pay-as-you-go instances or
            expired subscription instances.

        :param instance_ids: The IDs of instances that you want to release.
        TN)r   r   r.   r/   )r#   r2   r3   s      r'   delete_instanceszAcsClient.delete_instancesf  sQ     )**$---7#####r)   c                     t                      }|                    |           |                     |          }||                    d          S dS )zAssign a public IP address to an ECS instance.

        :param instance_id: The ID of the instance to which you want to
                            assign a public IP address.
        :return: The assigned ip.
        N	IpAddress)r   r   r/   r0   r   s       r'   allocate_public_addressz!AcsClient.allocate_public_addressq  sV     122{+++%%g..<<,,,  r)   c                     t                      }|                    |           |                     |          }|*t          j        d|                    d                     |S t          j        d           dS )zCreate an SSH key pair.

        :param key_pair_name: The name of the key pair.
        :return: The created keypair data.
        NzCreate Key Pair %s Successfully	KeyPairIdzCreate Key Pair Failed)r	   rF   r/   rG   rH   r0   rI   r#   rL   r3   r4   s       r'   create_key_pairzAcsClient.create_key_pair~  sv     '((...%%g..L:HLL<U<UVVVOM23334r)   c                     t                      }|                    |           |                    |           |                     |           dS )zImport the public key of an RSA-encrypted key pair
            that is generated by a third-party tool.

        :param key_pair_name: The name of the key pair.
        :param public_key_body: The public key of the key pair.
        N)r   rF   set_PublicKeyBodyr/   )r#   rL   public_key_bodyr3   s       r'   import_key_pairzAcsClient.import_key_pair  sQ     '((...!!/2227#####r)   c                 v    t                      }|                    |           |                     |           dS )zuDelete one or more SSH key pairs.

        :param key_pair_names: The name of the key pair.
        :return:
        N)r   set_KeyPairNamesr/   )r#   key_pair_namesr3   s      r'   delete_key_pairszAcsClient.delete_key_pairs  s=     ())  0007#####r)   c                     t                      }||                    |           |                     |          }|(|                    d                              d          S dS )zoQuery one or more key pairs.

        :param key_pair_name: The name of the key pair.
        :return:
        NKeyPairsKeyPair)r   rF   r/   r0   r   s       r'   describe_key_pairszAcsClient.describe_key_pairs  sg     *++$##M222%%g..<<
++//	:::4r)   c                     t                      }||                    |           |                     |          }|(|                    d                              d          S t	          j        d           dS )zQueries one or more VSwitches.

        :param vpc_id: The ID of the VPC to which the VSwitch belongs.
        :return: VSwitch list.
        N	VSwitchesVSwitchzDescribe VSwitches Failed.)r   r`   r/   r0   rG   rI   )r#   ra   r3   r4   s       r'   describe_v_switcheszAcsClient.describe_v_switches  sx     +,,f%%%%%g..<<,,00;;;M67774r)   c                 @   |                     d           	 | j                            |          }t          j        |          }|S # t
          t          f$ rE}t          j        |	                                           t          j        |           Y d}~dS d}~ww xY w)zsend open api requestjsonN)
set_accept_formatr"   do_action_with_exceptionr   loadsr   r   rG   rI   get_action_name)r#   r3   response_strresponse_detailes        r'   r/   zAcsClient._send_request  s    !!&)))	8<<WEEL"j66O""1 	 	 	M'1133444M!44444	s   /A B:BB)NN)r7   r8   r9   r:   r;   )rS   r7   r8   r9   r:   rS   )r   )F)r   )N)__name__
__module____qualname____doc__r(   r6   rR   r]   rb   rg   ro   rv   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r/    r)   r'   r   r   +   s       
 

 
 
   0 '*+#$/ / / /r '*+#$< < < <|     &$ $ $&  (	 	 		 	 	J J J J"D D D$ $ $ $3 3 3 3
$ 
$ 
$	$ 	$ 	$- - -   
$ 
$ 
$$ $ $       
 
 
 
 
r)   r   )2r   rG   aliyunsdkcorer   &aliyunsdkcore.acs_exception.exceptionsr   r   =aliyunsdkecs.request.v20140526.AllocatePublicIpAddressRequestr   <aliyunsdkecs.request.v20140526.AuthorizeSecurityGroupRequestr   4aliyunsdkecs.request.v20140526.CreateInstanceRequestr   3aliyunsdkecs.request.v20140526.CreateKeyPairRequestr	   9aliyunsdkecs.request.v20140526.CreateSecurityGroupRequestr
   /aliyunsdkecs.request.v20140526.CreateVpcRequestr   3aliyunsdkecs.request.v20140526.CreateVSwitchRequestr   4aliyunsdkecs.request.v20140526.DeleteInstanceRequestr   5aliyunsdkecs.request.v20140526.DeleteInstancesRequestr   4aliyunsdkecs.request.v20140526.DeleteKeyPairsRequestr   7aliyunsdkecs.request.v20140526.DescribeInstancesRequestr   6aliyunsdkecs.request.v20140526.DescribeKeyPairsRequestr   <aliyunsdkecs.request.v20140526.DescribeSecurityGroupsRequestr   2aliyunsdkecs.request.v20140526.DescribeVpcsRequestr   7aliyunsdkecs.request.v20140526.DescribeVSwitchesRequestr   3aliyunsdkecs.request.v20140526.ImportKeyPairRequestr   2aliyunsdkecs.request.v20140526.RunInstancesRequestr   3aliyunsdkecs.request.v20140526.StartInstanceRequestr   2aliyunsdkecs.request.v20140526.StopInstanceRequestr   3aliyunsdkecs.request.v20140526.StopInstancesRequestr   2aliyunsdkecs.request.v20140526.TagResourcesRequestr   r   r   r)   r'   <module>r      sW                 S S S S S S S S           W V V V V V T T T T T T      M L L L L L T T T T T T V V V V V V X X X X X X V V V V V V                S R R R R R      U T T T T T R R R R R R T T T T T T R R R R R R T T T T T T R R R R R Rb b b b b b b b b br)   