o
    e8                     @   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		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edd ZedddZdd Z		d#ddZdS )$	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   PD:\Projects\ConvertPro\env\Lib\site-packages\baidubce/services/eip/eip_client.pyr
   ,   s   zEipClient.__init__c           	      C   s|   ||d}|du rddd|d< n|j |j|j|jdd|d< |  }|du r,|  }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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   client_tokenr   r   pathr   r   r   r   
create_eip3   s,   


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_eiph   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   sP   ||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
instanceIdNr#   )s   bindr   r$   r%   )	r   r(   instance_typeinstance_idr    r   r   r!   r   r   r   r   bind_eip   s   
zEipClient.bind_eipc                 C   s>   t |  |}|du r|  }d|d}| jtj|||dS )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   r   )r   r&   r   r   r   r   r'   r   r(   r    r   r!   r   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   DELETEr0   r   r   r   release_eip   s   zEipClient.release_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< | 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
                }
        Ns   eips   instanceTypes
   instanceIds   statuss   markers   maxKeysr/   )r   
isinstancer   valuer   r   GET)
r   r(   r,   r-   statusmarkerZmax_keysr   r!   r   r   r   r   	list_eips  s"   C
zEipClient.list_eipsc                   C   s   t  S )z/
        default client token by uuid1
        )uuiduuid1r   r   r   r   r   h  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)r=   r   r   r   r   o  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_configy  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)r@   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)NNNN)NN)NNN)NNNNNr4   N)NNNNN)__name__
__module____qualname____doc__r>   r=   r
   r"   r)   r+   r.   r1   r3   r:   staticmethodr   r   r@   r   r   r   r   r   r	   %   s<    

5
!
1

%

T
	r	   )rG   r?   r   loggingr;   Zbaidubcer   Zbaidubce.authr   Zbaidubce.bce_base_clientr   Zbaidubce.httpr   r   r   Zbaidubce.services.eip.modelr   	getLoggerrD   Z_loggerr	   r   r   r   r   <module>   s   
