o
    R+ i^                     @   s   d Z ddlZddlZddlZddl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 eeZG d
d de
ZdS )z.
This module provides a client class for EIP.
    N)utils)bce_v1_signer)BceBaseClient)bce_http_client)handler)http_methods)	EipStatusc                   @   s   e Zd ZdZdZdZd/ddZ		d0ddZ		d1d	d
Z		d2ddZ			d3ddZ
d1ddZ		d1ddZd1ddZd1ddZd1ddZd/ddZ		d4ddZd5ddZd6d!d"Zd1d#d$Zd1d%d&Zed'd( Zed/d)d*Zd+d, Z		d0d-d.ZdS )7	EipClientz
    eip sdk client
    s   /v1s   /eipNc                 C   s   t | | dS )zV
        :type config: baidubce.BceClientConfiguration
        :param config:
        N)r   __init__)selfconfig r   l/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/baidubce/services/eip/eip_client.pyr
   ,   s   zEipClient.__init__c           
      C   s   ||d}|dur||d< |du rddd|d< n|j |j|j|jdd	|d< |  }|du r4|  }d
|i}	| jtj|t	
||	|dS )a  
        Create an eip with the specified options.

        :type bandwidth_in_mbps: int
        :param bandwidth_in_mbps: specify the bandwidth in Mbps

        :type name: string
        :param name: name of eip. The optional parameter

        :type billing: Billing
        :param billing: billing information. The optional parameter,
         default paymentTiming is Postpaid

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: created eip address, for example,{"eip":"x.x.x.x"}
        )nameZbandwidthInMbpsNZ	routeTypeZPostpaidZByBandwidth)paymentTimingbillingMethodbillingZreservationLengthZreservationTimeUnit)r   r   reservation   clientToken)bodyparamsr   )Zpayment_timingZbilling_methodreservation_lengthreservation_time_unit	_get_path_generate_default_client_token_send_requestr   POSTjsondumps)
r   Zbandwidth_in_mbpsr   r   Z
route_typeclient_tokenr   r   pathr   r   r   r   
create_eip3   s0   


zEipClient.create_eipc                 C   sN   d|i}t |  |}|du r|  }d|d}| jtj||t||dS )a  
        Resizing eip

        :type eip: string
        :param eip: eip address to be resized

        :type new_bandwidth_in_mbps: int
        :param new_bandwidth_in_mbps: specify new bandwidth in Mbps for eip

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        ZnewBandwidthInMbpsN    )s   resizer   r   r   r   	r   
append_urir   r   r   r   PUTr   r   )r   eipZnew_bandwidth_in_mbpsr    r   r   r!   r   r   r   r   
resize_eipj   s   
zEipClient.resize_eipc                 C   sx   |du rdddddii}ndd|j |jdii}t|  |}|du r)|  }d|d}| jtj||t	
||d	S )
aQ  
        PurchaseReserved eip with fixed duration,only Prepaid eip can do this

        :type eip: string
        :param eip: eip address to be renewed

        :type billing: Billing
        :param billing: billing information. The optional parameter,
         default reservationLength is 1

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        Nr   r      ZMonthr   r#   )s   purchaseReservedr   r$   )r   r   r   r&   r   r   r   r   r'   r   r   )r   r(   r   r    r   r   r!   r   r   r   r   purchase_reserved_eip   s0   

zEipClient.purchase_reserved_eipc           	      C   sh   |du rd}|du rd}||d}t |  |}|du r!|  }d|d}| jtj||t||dS )a  
        Enable auto renew for specified EIP.

        :type eip: string
        :param eip: eip address to be enable to auto-renew.

        :type auto_renew_time_unit: string
        :param auto_renew_time_unit: time unit of auto_renew_time, default 'Month'.

        :type auto_renew_time: int
        :param auto_renew_time: the unit of time for auto renew,
        default auto_renew_time is 1

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        Nmonthr*   )ZautoRenewTimeUnitZautoRenewTimer#   )s   startAutoRenewr   r$   r%   )	r   r(   Zauto_renew_time_unitZauto_renew_timer    r   r   r!   r   r   r   r   start_auto_renew_eip   s    
zEipClient.start_auto_renew_eipc                 C   >   t |  |}|du r|  }d|d}| jtj|||dS )a  
        Disable auto renew for specified EIP.

        :type eip: string
        :param eip: eip address to be disable to auto-renew.

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        Nr#   )s   stopAutoRenewr   r   r   r   r&   r   r   r   r   r'   r   r(   r    r   r!   r   r   r   r   stop_auto_renew_eip   s   zEipClient.stop_auto_renew_eipc           
      C   s`   ||d}|dur||d< t |  |}|du r|  }d|d}	| jtj||	t||dS )a{  
        bind the eip to a specified instanceId and instanceType

        :type eip: string
        :param eip: eip address to be bound

        :type instance_type: string
        :param instance_type: type of instance to be bound(BCC BLB et.)

        :type instance_id: string
        :param instance_id: id of instance to be bound

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        )ZinstanceTypeZ
instanceIdNZ
instanceIpr#   )s   bindr   r$   r%   )
r   r(   instance_typeinstance_idZinstance_ipr    r   r   r!   r   r   r   r   bind_eip  s   
zEipClient.bind_eipc                 C   r.   )a  
        unbind the eip from a specified instance

        :type eip: string
        :param eip: eip address to be unbound

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        Nr#   )s   unbindr   r/   r0   r1   r   r   r   
unbind_eip+  s   zEipClient.unbind_eipc                 C   s<   t |  |}|du r|  }d|i}| jtj|||dS )a2  
        release the eip(delete operation)
        Only the Postpaid instance or Prepaid which is expired can be released.
        if the eip has been bound, must unbind before releasing.

        :type eip: string
        :param eip: eip address to be released

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        Nr   r/   r   r&   r   r   r   r   DELETEr1   r   r   r   release_eipE  s   zEipClient.release_eipc                 C   r.   )a  
        turn on EIP pass through with the specific parameters.

        :type eip: string
        :param eip: the specific EIP.

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user, 
        a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config: None

        :return: BceResponse
        Nr#   )s   directr   r/   r0   r1   r   r   r   
direct_eip`     zEipClient.direct_eipc                 C   r.   )a  
        Remove the direction of an EIP from a specific instance.
        
        :type eip: string
        :param eip: the EIP address to undirect.

        :type client_token: string
        :param client_token: If the clientToken is not specified by the user, 
        a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config: None

        :return: BceResponse
        Nr#   )s   unDirectr   r/   r0   r1   r   r   r   undirect_eipy  r;   zEipClient.undirect_eip  c	                 C   s   |   }	i }
|dur||
d< |dur||
d< |dur||
d< |dur,t|tr,|j|
d< |dur4||
d< |dur<||
d< |durD||
d< | jtj|	|
|d	S )
a!
  
        get a list of eip owned by the authenticated user and specified
        conditions. we can Also get a single eip function  through this
        interface by eip condition

        :type eip: string
        :param eip: eip address condition

        :type instance_type: string
        :param instance_type: bound instance type condition

        :type instance_id: string
        :param instance_id: bound instance id condition
        if query by the instanceId or instanceType condition, must provides
         both of them at the same time

        :type status: string
        :param status of eip condition
        if query by the status condition, must provides

        :type marker: string
        :param marker: The optional parameter marker specified in the original
         request to specify where in the results to begin listing.

        :type max_keys: int
        :param max_keys: The optional parameter to specifies the max number
         of list result to return. The default value is 1000.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: list of eip model, for example:
                {
                    "eipList": [
                        {
                            "name":"eip-xxxxxxx-1",
                            "eip": "x.x.x.x",
                            "status":"binded",
                            "instanceType": "BCC",
                            "instanceId": "i-xxxxxxx",
                            "bandwidthInMbps": 5,
                            "paymentTiming":"Prepaid",
                            "billingMethod":"ByBandwidth",
                            "createTime":"2016-03-08T08:13:09Z",
                            "expireTime":"2016-04-08T08:13:09Z"
                        },
                        {
                            "name":"eip-xxxxxxx-1",
                            "eip": "x.x.x.x",
                            "status":"binded",
                            "instanceType": "BCC",
                            "instanceId": "i-xxxxxxx",
                            "bandwidthInMbps": 1,
                            "paymentTiming":"Postpaid",
                            "billingMethod":"ByTraffic",
                            "createTime":"2016-03-08T08:13:09Z",
                            "expireTime":null
                        },
                    ],
                    "marker":"eip-xxxxxxx-1",
                    "isTruncated": true,
                    "nextMarker": "eip-DCB50387",
                    "maxKeys": 2
                }
        N   eips   instanceTypes
   instanceIds   status   marker   maxKeyss	   ipVersionr/   )r   
isinstancer   valuer   r   GET)r   r(   r3   Z
ip_versionr4   statusmarkermax_keysr   r!   r   r   r   r   	list_eips  s&   C
zEipClient.list_eipsc                 C   sh   t |  d}i }|dur||d< |dur||d< |dur"||d< |dur*||d< | jtj|||dS )aQ  
        list all EIP in the recycle bin with the specific parameters.

        :type eip: string
        :param eip: eip address condition

        :type name: string
        :param name: eip name condition

        :type marker: string
        :param marker: The optional parameter marker specified in the original
         request to specify where in the results to begin listing.

        :type max_keys: int
        :param max_keys: The optional parameter to specifies the max number
         of list result to return. The default value is 1000.

        :type config: baidubce.BceClientConfiguration
        :param config: None

        :return: list of eip model, for example:
                {
                    "eipList": [
                        {
                            "name":"eip-xxxxxxx-1",
                            "eip": "x.x.x.x",
                            "eip_id":"ip-xxxxxxxx",
                            "status": "paused",
                            "route_type": "BGP",
                            "bandwidth_in_mbps": 5,
                            "payment_timing":"Prepaid",
                            "billing_method":"ByBandwidth",
                            "recycle_time":"2016-03-08T08:13:09Z",
                            "scheduled_delete_time":"2016-04-08T08:13:09Z"
                        },
                        {
                            "name":"eip-xxxxxxx-2",
                            "eip": "x.x.x.x",
                            "eip_id":"ip-xxxxxxxx",
                            "status": "paused",
                            "route_type": "BGP",
                            "bandwidth_in_mbps": 10,
                            "payment_timing":"Postpaid",
                            "billing_method":"ByBandwidth",
                            "recycle_time":"2016-03-08T08:13:09Z",
                            "scheduled_delete_time":"2016-04-08T08:13:09Z"
                        },
                    ],
                    "marker":"ip-xxxxxxxx",
                    "is_truncated": true,
                    "max_keys": 1000
                }
        recycleNr>   s   namer?   r@   r/   )r   r&   r   r   r   rC   )r   r(   r   rE   rF   r   r!   r   r   r   r   list_eip_recycle  s   6zEipClient.list_eip_recycleFc                 C   s>   t |  |}|du r|  }||d}| jtj|||dS )a  
        delete an EIP with additional options.

        :type eip: string
        :param eip: the EIP address to delete.

        :type release_to_recycle: bool
        :param release_to_recycle: flag to release EIP to recycle bin.

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        N)r   s   releaseToRecycler/   r7   )r   r(   Zrelease_to_recycler    r   r!   r   r   r   r   optional_delete_eip+  s   zEipClient.optional_delete_eipc                 C   s@   t |  d|}|du r|  }d|d}| jtj|||dS )a  
        restore an EIP from the recycle bin.

        :type eip: string
        :param eip: eip address to be restored from recycle bin.

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        rH   Nr#   )s   restorer   r/   r0   r1   r   r   r   restore_recycle_eipI  s   zEipClient.restore_recycle_eipc                 C   s>   t |  d|}|du r|  }d|i}| jtj|||dS )a  
        delete an EIP from the recycle bin.

        :type eip: string
        :param eip: eip address to be delete from recycle bin.

        :type client_token: string
        :param client_token: if the clientToken is not specified by the user,
         a random String generated by default algorithm will be used.

        :type config: baidubce.BceClientConfiguration
        :param config:

        :return: BceResponse
        rH   Nr   r/   r7   r1   r   r   r   delete_recycle_eipd  s   zEipClient.delete_recycle_eipc                   C   s   t  S )z/
        default client token by uuid1
        )uuiduuid1r   r   r   r   r   ~  s   z(EipClient._generate_default_client_tokenc                 C   s   | du rt j} tt j| S )zI
        :type prefix: string
        :param prefix: path prefix
        N)r	   prefixr   r&   version)rO   r   r   r   r     s   zEipClient._get_pathc                 C   s(   |du r| j S t| j }|| |S )zh

        :type config: baidubce.BceClientConfiguration
        :param config:
        :return:
        N)r   copyZmerge_non_none_values)r   r   Z
new_configr   r   r   _merge_config  s
   
zEipClient._merge_configc              
   C   sJ   |  |}|du rtj}|du rddd}t|tjtj|g|||||S )a  

        :param http_method:
        :param path:
        :param body:
        :param headers:
        :param params:

        :type config: baidubce.BceClientConfiguration
        :param config:

        :param body_parser:

        :return: baidubce.BceResponse
        Ns   */*s   application/json;charset=utf-8)s   Accepts   Content-Type)rR   r   Z
parse_jsonr   send_requestr   signZparse_error)r   Zhttp_methodr!   r   headersr   r   Zbody_parserr   r   r   r     s   

zEipClient._send_request)N)NNNNN)NN)NNN)NNNN)NNNNNNr=   N)NNNr=   N)FNN)__name__
__module____qualname____doc__rP   rO   r
   r"   r)   r+   r-   r2   r5   r6   r9   r:   r<   rG   rI   rJ   rK   rL   staticmethodr   r   rR   r   r   r   r   r   r	   %   sP    

7
!
1

*

'




V
B


	r	   )rY   rQ   r   loggingrM   Zbaidubcer   Zbaidubce.authr   Zbaidubce.bce_base_clientr   Zbaidubce.httpr   r   r   Zbaidubce.services.eip.modelr   	getLoggerrV   Z_loggerr	   r   r   r   r   <module>   s   
