o
    emN                     @   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 dd
lmZ eeZedZe ZG dd dejZdd ZeZdS )z:
This module provides a client class for peer connection.
    N)utils)bce_base_client)bce_v1_signer)bce_http_client)handler)http_methods)peerconn_model)requiredZPostpaidc                   @   s  e Zd ZdZdZdZd(ddZd(ddZ		d)d	d
Ze	e
eefeefeefd				d*ddZe	eefd		d+ddZe	eefdd(ddZe	eefeefd		d,ddZe	eefeefd		d-ddZe	eefd		d-ddZe	eefe
d	d-ddZe	eefd		d+dd Ze	eefeefd!	d-d"d#Ze	eefeefd!	d-d$d%Zed(d&d'ZdS ).PeerConnClientz$
    Peer connection sdk client
    s   /v1s	   /peerconnNc                 C   s   t j| | d S N)r   BceBaseClient__init__)selfconfig r   UD:\Projects\ConvertPro\env\Lib\site-packages\baidubce/services/vpc/peerconn_client.pyr   /   s   zPeerConnClient.__init__c                 C   s(   |du r| j S t| j }|| |S )zg
        :param config:
        :type config: baidubce.BceClientConfiguration
        :return:
        N)r   copyZmerge_non_none_values)r   r   Z
new_configr   r   r   _merge_config2   s
   
zPeerConnClient._merge_configc              
   C   sJ   |  |}|d u rtj}|d u rddd}t|tjtj|g|||||S )Ns   */*s   application/json;charset=utf-8)s   Accepts   Content-Type)r   r   Z
parse_jsonr   send_requestr   signZparse_error)r   Zhttp_methodpathbodyheadersparamsr   Zbody_parserr   r   r   _send_request?   s   

zPeerConnClient._send_request)bandwidth_in_mbpslocal_vpc_idpeer_vpc_idpeer_regionc                 C   s   |   }|	du rt }	d|	i}|
du rt}
|||||
jd}|dur&||d< |dur.||d< |dur6||d< |dur>||d< | jtj|t|||dS )	a6  
        Create Peer connection.
        For peer connections within the same region, only postpaid is
        supported.
        For peer connections between different accounts, the peer connections
        are available only after the remote account accepts the connections.
        For peer connections within the same account, peer connection will be
        accepted automatically.
        There can be only one peer connection between any two VPCs.
        Peer connection endpoints cannot be the same VPC.
        If both local VPC and remote VPC are transit VPCs, peer connection
        cannot be established.

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if client token is provided.
        :type client_token: string

        :param bandwidth_in_mbps:
            Network bandwidth (in unit of Mbps) of peer connection.
        :type bandwidth_in_mbps: int

        :param description:
            Description of peer connection.
        :type description: string

        :param local_if_name:
            Name of local interface of peer connection.
        :type local_if_name: string

        :param local_vpc_id:
            Local side VPC id of peer connection.
        :type local_vpc_id: string

        :param peer_account_id:
            Remote account id of peer connection.
            Used only when the peer connection connects two different accounts.
        :type peer_account_id: string

        :param peer_vpc_id:
            Remote side VPC id of peer connection.
        :type peer_vpc_id: string

        :param peer_region:
            Remote side region of peer connection.
        :type peer_region: string

        :param peer_if_name:
            Name of remote interface of peer connection.
        :type peer_if_name: string

        :param billing:
            Billing information.
        :type billing: nat_model.Billing

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        N   clientToken)ZbandwidthInMbpsZ
localVpcIdZ	peerVpcIdZ
peerRegionbillingdescriptionlocalIfNameZpeerAccountIdZ
peerIfNamer   r   r   )		_get_pathgenerate_client_token#default_billing_to_purchase_created__dict__r   r   POSTjsondumps)r   r   r   r   r   r!   local_if_nameZpeer_account_idZpeer_if_nameclient_tokenr    r   r   r   r   r   r   r   create_peerconnL   s2   F
zPeerConnClient.create_peerconn)vpc_idc                 C   sD   |   }d|i}|dur||d< |dur||d< | jtj|||dS )au  
        Return a list of peer connections.

        :param vpc_id:
            VPC id that peer connections connect to.
        :type vpc_id: string

        :param marker:
            The optional parameter marker specified in the original
            request to specify where in the results to begin listing.
            Together with the marker, specifies the list result which
            listing should begin. If the marker is not specified,
            the list result will listing from the first one.
        :type marker: string

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

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        s   vpcIdNs   markers   maxKeysr   r   )r$   r   r   GET)r   r.   markerZmax_keysr   r   r   r   r   r   list_peerconns   s   zPeerConnClient.list_peerconns)peer_conn_idc                 C   s"   t |  |}| jtj||dS )aM  
        Get the detail information of specified peer connection.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        )r   )r   
append_urir$   r   r   r0   )r   r3   r   r   r   r   r   get_peerconn   s   zPeerConnClient.get_peerconn)r3   local_if_idc           
      C   sj   t |  |}|du rt }d|i}d|i}	|dur||	d< |dur'||	d< | jtj|t|	||dS )a  
        Update the interface name or description of specified peer connection.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param local_if_id:
            Local interface id of peer connection
        :type local_if_id: string

        :param description:
            Description of peer connection.
        :type description: string

        :param local_if_name:
            Name of local interface of peer connection.
        :type local_if_name: string

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if clientToken is provided.
            If the clientToken is not specified by user,
            a random String generated by default algorithm will be used.
        :type client_token: string

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        Nr   Z	localIfIdr!   r"   r#   	r   r4   r$   r%   r   r   PUTr)   r*   )
r   r3   r6   r!   r+   r,   r   r   r   r   r   r   r   update_peerconn   s   #
zPeerConnClient.update_peerconn)r3   actionc                 C   sB   t |  |}|du rt }d|i}d||< | jtj|||dS )a  
        Accept or reject peer connection request.
        Timeout period of connection request is 7 days.
        When timeout or the remote side rejects the connection,
        the status of peer connection on initiator side is consulting failed.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param action:
            'accept': when accepting the peer connection.
            'reject': when rejecting the peer connection.
        :type action: string

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if clientToken is provided.
            If the clientToken is not specified by user,
            a random String generated by default algorithm will be used.
        :type client_token: string

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        Nr   r/   r   r4   r$   r%   r   r   r8   )r   r3   r:   r,   r   r   r   r   r   r   handle_peerconn  s   zPeerConnClient.handle_peerconnc                 C   s:   t |  |}|du rt }d|i}| jtj|||dS )ah  
        Delete peer connection.
        For peer connections between different accounts,
        only initiator can perform this operation.
        One cannot delete prepaid peer connections that are not expired.
        Consulting failed prepaid peer connections can be deleted.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if clientToken is provided.
            If the clientToken is not specified by user,
            a random String generated by default algorithm will be used.
        :type client_token: string

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        Nr   r/   )r   r4   r$   r%   r   r   DELETE)r   r3   r,   r   r   r   r   r   r   delete_peerconnE  s   zPeerConnClient.delete_peerconn)r3   new_bandwidth_in_mbpsc                 C   sL   t |  |}|du rt }d|d}d|i}| jtj|t|||dS )a  
        Scale down/up the bandwidth of specified peer connection.
        For peer connections between different accounts,
        only initiator can perform this operation.
        Prepaid peer connection can only scale up.
        Postpaid peer connection can scale up or down.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if clientToken is provided.
            If the clientToken is not specified by user,
            a random String generated by default algorithm will be used.
        :type client_token: string

        :param new_bandwidth_in_mbps:
            The new bandwidth of the peer connection.
        :type new_bandwidth_in_mbps: int

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        N)s   resizer   ZnewBandwidthInMbpsr#   r7   )r   r3   r?   r,   r   r   r   r   r   r   r   resize_peerconni  s   
zPeerConnClient.resize_peerconnc                 C   sZ   t |  |}|du rt }d|d}|du rt}d|ji}| jtj|t	
|||dS )a  
        Renew specified peer connection.
        Postpaid peer connection cannot be renewed.
        For peer connections between different accounts, only the initiator can
        perform this operation.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if clientToken is provided.
            If the clientToken is not specified by user,
            a random String generated by default algorithm will be used.
        :type client_token: string

        :param billing:
            Billing information.
        :type billing: peerconn_model.Billing

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        N)s   purchaseReservedr   r    r#   )r   r4   r$   r%   $default_billing_to_purchase_reservedr'   r   r   r8   r)   r*   )r   r3   r,   r    r   r   r   r   r   r   r   purchase_reserved_peerconn  s   
z)PeerConnClient.purchase_reserved_peerconn)r3   rolec                 C   >   t |  |}|du rt }d||d}| jtj|||dS )a  
        Open DNS sync between VPCs connected by peer connection.
        DNS sync can be opened only when the status of
        peer connection is available.
        DNS sync cannot be opened when the DNS status of
        peer connection is syncing or closing.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param role:
            'initiator': for VPC where peer connection is initiated.
            'acceptor': for VPC where peer connection is accepted.
        :type role: string

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if clientToken is provided.
            If the clientToken is not specified by user,
            a random String generated by default algorithm will be used.
        :type client_token: string

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        N)s   open   roler   r/   r;   r   r3   rC   r,   r   r   r   r   r   r   open_peerconn_dns_sync  s    z%PeerConnClient.open_peerconn_dns_syncc                 C   rD   )a  
        Close DNS sync between VPCs connected by peer connection.
        DNS sync can be closed only when the status of peer connection
        is available. DNS sync cannot be closed when the DNS status
        of peer connection is syncing or closing.

        :param peer_conn_id:
            The id of specified peer connection.
        :type peer_conn_id: string

        :param role:
            'initiator': for VPC where peer connection is initiated.
            'acceptor': for VPC where peer connection is accepted.
        :type role: string

        :param client_token:
            An ASCII string whose length is less than 64.
            The request will be idempotent if clientToken is provided.
            If the clientToken is not specified by user,
            a random String generated by default algorithm will be used.
        :type client_token: string

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        N)s   closerE   r   r/   r;   rF   r   r   r   close_peerconn_dns_sync  s   z&PeerConnClient.close_peerconn_dns_syncc                 C   s   | du rt j} tt j| S )zI
        :type prefix: string
        :param prefix: path prefix
        N)r
   prefixr   r4   version)rI   r   r   r   r$     s   zPeerConnClient._get_pathr   )NNNNN)NNNNNNN)NNN)NNNN)NN)__name__
__module____qualname____doc__rJ   rI   r   r   r   r	   intbytesstrr-   r2   r5   r9   r<   r>   r@   rB   rG   rH   staticmethodr$   r   r   r   r   r
   (   sh    


](3(#,-*)r
   c                   C   s   t t S )z
    The default method to generate the random string for client_token
    if the optional parameter client_token is not specified by the user.

    :return:
    :rtype string
    )rQ   uuiduuid4r   r   r   r   generate_client_token_by_uuid$  s   rU   )rN   r   r)   loggingrS   Zbaidubcer   r   Zbaidubce.authr   Zbaidubce.httpr   r   r   Zbaidubce.services.vpcr   Zbaidubce.utilsr	   	getLoggerrK   Z_loggerZBillingr&   rA   r   r
   rU   r%   r   r   r   r   <module>   s,   

   