Peering Connections
Confirm Endpoint
Before configuring the endpoint for SDK usage, please refer to the developer guide section on PeerConn Service Domain Name to understand endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer to Region Selection Guide.
Currently supported regions are: "North China-Beijing," "South China-Guangzhou," "East China-Suzhou," "Hong Kong," "Central Finance-Wuhan," and "North China-Baoding." Corresponding details:
| Access region | Endpoint | Protocol |
|---|---|---|
| BJ | bcc.bj.baidubce.com | HTTP and HTTPS |
| GZ | bcc.gz.baidubce.com | HTTP and HTTPS |
| SU | bcc.su.baidubce.com | HTTP and HTTPS |
| HKG | bcc.hkg.baidubce.com | HTTP and HTTPS |
| FWH | bcc.fwh.baidubce.com | HTTP and HTTPS |
| BD | bcc.bd.baidubce.com | HTTP and HTTPS |
Retrieve access key
To use PeerConn, users must have a valid AK (Access Key ID) and SK (Secret Access Key) for signature verification. The AK/SK are system-generated strings for identifying users and performing signature verification for PeerConn.
Your AK/SK information can be obtained and understood through the following steps:
Register a Baidu AI Cloud account
Create PeerConn client
The PeerConn client acts as the interface for PeerConn services, offering developers a variety of methods to interact with these services.
Create a PeerConn client with AK/SK
To access peering connections via AK/SK, users can refer to the following code to create a PeerConn Client:
1import (
2 "github.com/baidubce/bce-sdk-go/services/vpc"
3)
4func main() {
5 // User’s Access Key ID and Secret Access Key
6 ACCESS_KEY_ID, SECRET_ACCESS_KEY := <your-access-key-id>, <your-secret-access-key>
7 // User-specified Endpoint
8 ENDPOINT := <domain-name>
9 // Initialize a PeerConn client
10 peerConnClient, err := vpc.NewClient(AK, SK, ENDPOINT)
11}
In the code above, ACCESS_KEY_ID corresponds to “Access Key ID” in the console. SECRET_ACCESS_KEY corresponds to “Access Key Secret” in the console. Refer to the Guide - How to Retrieve AKSK. The third parameter ENDPOINT is a user-specified domain name. If left empty, the default domain name will be used as the VPC service address.
Note:
The endpointparameter must be defined with the domain name of the specified region. For example, if the service is located in Beijing, the endpoint will bebcc.bj.baidubce.com.
Create an PeerConn client with STS
Request STS Token
PeerConn supports temporary third-party access authorization via the STS mechanism. STS (Security Token Service) is a temporary authorization service offered by Baidu AI Cloud. Using STS, you can issue access credentials with customized permissions and validity periods for third-party users. These users can then use the credentials to directly call Baidu AI Cloud APIs or SDKs to access cloud resources.
To access PeerConn via STS, users must first obtain a certification string using the STS client.
Create PeerConn client with STS Token
Once the STS token is obtained, configure it in the PeerConn client to enable PeerConn client creation based on the STS token.
Code example
The GO SDK implements the STS service API. Below is a complete example for requesting an STS token and creating an PeerConn client object:
1import (
2 "fmt"
3 "github.com/baidubce/bce-sdk-go/auth" //Import the authentication module
4 "github.com/baidubce/bce-sdk-go/services/vpc" //Import VPC service module
5 "github.com/baidubce/bce-sdk-go/services/sts" //Import the Baige service module
6)
7func main() {
8 //Create a Client object for the STS service, using the default Endpoint
9 AK, SK := <your-access-key-id>, <your-secret-access-key>
10 stsClient, err := sts.NewClient(AK, SK)
11 if err != nil {
12 fmt.Println("create sts client object :", err)
13 return
14 }
15 //Obtain a temporary authentication token with a validity period of 60 seconds and an empty ACL
16 stsObj, err := stsClient.GetSessionToken(60, "")
17 if err != nil {
18 fmt.Println("get session token failed:", err)
19 return
20 }
21 fmt.Println("GetSessionToken result:")
22 fmt.Println(" accessKeyId:", stsObj.AccessKeyId)
23 fmt.Println(" secretAccessKey:", stsObj.SecretAccessKey)
24 fmt.Println(" sessionToken:", stsObj.SessionToken)
25 fmt.Println(" createTime:", stsObj.CreateTime)
26 fmt.Println(" expiration:", stsObj.Expiration)
27 fmt.Println(" userId:", stsObj.UserId)
28 //Create a PeerConn client object using the requested temporary STS, with the default endpoint
29 peerConnClient, err := peerConn.NewClient(stsObj.AccessKeyId, stsObj.SecretAccessKey, "bcc.bj.baidubce.com")
30 if err != nil {
31 fmt.Println("create PeerConn client failed:", err)
32 return
33 }
34 stsCredential, err := auth.NewSessionBceCredentials(
35 stsObj.AccessKeyId,
36 stsObj.SecretAccessKey,
37 stsObj.SessionToken)
38 if err != nil {
39 fmt.Println("create sts credential object failed:", err)
40 return
41 }
42 peerConnClient.Config.Credentials = stsCredential
43}
Note: Currently, when configuring a PeerConn client with STS, regardless of where the corresponding PeerConn service endpoint is located, the STS endpoint must be set to http://sts.bj.baidubce.com. This default is utilized when creating an STS object in the above code.
Configure HTTPS protocol to access PeerConn
PeerConn supports the HTTPS transmission protocol. Users can specify HTTPS in the Endpoint when creating the PeerConn Client Object to access the PeerConn service via HTTPS in the PeerConn GO SDK:
1// import "github.com/baidubce/bce-sdk-go/services/vpc"
2 ENDPOINT := ""https://bcc.bj.baidubce.com" // Specify the use of HTTPS protocol
3AK, SK := <your-access-key-id>, <your-secret-access-key>
4peerConnClient, _ := vpc.NewClient(AK, SK, ENDPOINT)
Configure PeerConn client
If users need to configure specific parameters for the PeerConn client, they can customize the configuration using the exported Config field of the PeerConn client object after its creation. This allows for configuring parameters such as proxy and maximum number of connections for the client.
Use a proxy
The following code snippet enables the client to access PeerConnservice using a proxy:
1// import "github.com/baidubce/bce-sdk-go/services/vpc"
2 //Create PeerConn Client Object
3AK, SK := <your-access-key-id>, <your-secret-access-key>
4ENDPOINT := "bcc.bj.baidubce.com"
5client, _ := peerConn.NewClient(AK, SK, ENDPOINT)
6 // Use the local port 8080 for the proxy
7client.Config.ProxyUrl = "127.0.0.1:8080"
Set network parameters
Users can configure network parameters using the following example code:
1// import "github.com/baidubce/bce-sdk-go/services/vpc"
2AK, SK := <your-access-key-id>, <your-secret-access-key>
3ENDPOINT := "bcc.bj.baidubce.com"
4client, _ := bcc.NewClient(AK, SK, ENDPOINT)
5 // Configure to not retry, default: Back Off retry
6client.Config.Retry = bce.NewNoRetryPolicy()
7 // Configure connection timeout to 30 seconds
8client.Config.ConnectionTimeoutInMillis = 30 * 1000
Configure options for generating signature strings
1// import "github.com/baidubce/bce-sdk-go/services/vpc"
2AK, SK := <your-access-key-id>, <your-secret-access-key>
3ENDPOINT := "bcc.bj.baidubce.com"
4client, _ := bcc.NewClient(AK, SK, ENDPOINT)
5 // Configure the HTTP request header Host for signing
6headersToSign := map[string]struct{}{"Host": struct{}{}}
7client.Config.SignOption.HeadersToSign = HeadersToSign
8 // Configure the validity period of the signature to 30 seconds
9client.Config.SignOption.ExpireSeconds = 30
Parameter description
When using the GO SDK to access PeerConn, the Config field of the created PeerConn client object supports the following parameters, as shown in the table below:
| ConfigMap name | Types | Meaning |
|---|---|---|
| Endpoint | string | Domain name for service requests |
| ProxyUrl | string | The proxy address for client requests |
| Region | string | Region for resource requests |
| UserAgent | string | User name, HTTP request’s User-Agent header |
| Credentials | *auth.BceCredentials | Authentication object for requests, divided into regular AK/SK and STS |
| SignOption | *auth.SignOptions | Options for authentication string signing |
| Retry | RetryPolicy | Retry policy for connections |
| ConnectionTimeoutInMillis | int | Connection timeout, in milliseconds, defaulting to 20 minutes |
Description:
- The
Credentialsis created using theauth.NewBceCredentialsandauth.NewSessionBceCredentialsfunctions. The former is used by default, while the latter is used for STS certification. See "Create a PeerConn client with STS" for details. - The
SignOptionfield represents options when generating a signature string, as detailed in the table below:
| Name | Types | Meaning |
|---|---|---|
| HeadersToSign | map[string]struct{} | HTTP headers used when generating the signature string |
| Timestamp | int64 | Timestamp used in the generated signature string, defaulting to the value at the time of sending request |
| ExpireSeconds | int | Validity period of the signature string |
1 Among configuration options, HeadersToSign defaults to `Host`, `Content-Type`, `Content-Length` and `Content-MD5`; TimeStamp is typically set to zero, indicating that the timestamp at the time of generating the certification string shall be used, and users generally shall not explicitly specify the value for this field; ExpireSeconds defaults to 1,800 seconds or 30 minutes.
- The
Retryfield specifies the retry policy, currently supporting two types:NoRetryPolicyandBackOffRetryPolicy. By default, the latter is used. This retry policy specifies the maximum number of retries, the maximum retry duration, and the retry base. Retries increase exponentially based on the retry base multiplied by 2 until the maximum number of retries or the maximum retry duration is reached.
Peering connection management
Peering connections provide VPC-level interconnection services, allowing traffic exchange between different virtual networks for secure, high-speed network connectivity across the same or different regions or users.
Create peering connections
Use the code below to create peering connections.
Function declaration
1type CreatePeerConnArgs struct {
2 ClientToken string `json:"-"`
3 BandwidthInMbps int `json:"bandwidthInMbps"`
4 Description string `json:"description,omitempty"`
5 LocalIfName string `json:"localIfName,omitempty"`
6 LocalVpcId string `json:"localVpcId"`
7 PeerAccountId string `json:"peerAccountId,omitempty"`
8 PeerVpcId string `json:"peerVpcId"`
9 PeerRegion string `json:"peerRegion"`
10 PeerIfName string `json:"peerIfName,omitempty"`
11 Billing *Billing `json:"billing"`
12 Tags []model.TagModel `json:"tags,omitempty"`
13}
14type CreatePeerConnResult struct {
15 PeerConnId string `json:"peerConnId"`
16}
17func (c *Client) CreatePeerConn(args *CreatePeerConnArgs) (*CreatePeerConnResult, error)
Parameter meaning Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/bjwvyue8y
Response Value
Operation succeeded:
1{
2 "peerConnId": "peerconn-9td54fmx143e"
3}
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example For specific code examples, refer to example_create_peer_conn.go
Note:
- For peering connections in the same local and peer regions, only postpay payment is supported.
- Cross-account peering connections require acceptance by the receiving end before becoming available.
- For peering connections within the same account, the system automatically accepts them on behalf of the peer.
- Only one peering connection is allowed between any two VPCs.
- The initiator's VPC and the acceptor's VPC must not be the same.
- A peering connection cannot be established if both the local and peer VPCs are relay VPCs.
List of peering connections to be queried.
Use the code below to query the list of peering connections.
Function declaration
1type ListPeerConnsArgs struct {
2 VpcId string
3 Marker string
4 MaxKeys int
5}
6type ListPeerConnsResult struct {
7 PeerConns []PeerConn `json:"peerConns"`
8 Marker string `json:"marker"`
9 IsTruncated bool `json:"isTruncated"`
10 NextMarker string `json:"nextMarker"`
11 MaxKeys int `json:"maxKeys"`
12}
13func (c *Client) ListPeerConn(args *ListPeerConnsArgs) (*ListPeerConnsResult, error)
This API retrieves details for all eligible peering connections, with vpcId being an optional parameter.
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Fjwvyuemr
Response Value
Operation succeeded:
1{
2 "peerConns":[
3 {
4 "peerConnId":"peerconn-9td54fmx143e",
5 "role":"initiator",
6 "status":"active",
7 "bandwithInMbp":500,
8 "description":"peer_diff_account",
9 "localIfId":"qpif-ken2yc9j8x56",
10 "localIfName":"int-SpLVk25R",
11 "localVpcId":"vpc-13vuxu016dew",
12 "localRegion":"bj",
13 "peerVpcId":"vpc-jcvmhw9h1a35",
14 "peerRegion":"bj",
15 "peerAccountId":"75c14a239bb24b9e88b695e8e9e47952",
16 "dnsStatus":"close",
17 "paymentTiming":"Postpaid",
18 "createdTime" :"2018-08-07 19:46:55",
19 "expiredTime":expiredTime
20 }
21 ]
22 "marker":"peerconn-9td54fmx143e",
23 "isTruncated": true,
24 "nextMarker": "peerconn-srbvvxmjn7ux",
25 "maxKeys": 1
26}
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_list_peer_conn.go
View details of peering connection
Use the code below to query the details of a specific peering connection.
Function declaration
1type PeerConn struct {
2 PeerConnId string `json:"peerConnId"`
3 Role PeerConnRoleType `json:"role"`
4 Status PeerConnStatusType `json:"status"`
5 BandwidthInMbps int `json:"bandwidthInMbps"`
6 Description string `json:"description"`
7 LocalIfId string `json:"localIfId"`
8 LocalIfName string `json:"localIfName"`
9 LocalVpcId string `json:"localVpcId"`
10 LocalRegion string `json:"localRegion"`
11 PeerVpcId string `json:"peerVpcId"`
12 PeerRegion string `json:"peerRegion"`
13 PeerAccountId string `json:"peerAccountId"`
14 PaymentTiming string `json:"paymentTiming"`
15 DnsStatus DnsStatusType `json:"dnsStatus"`
16 CreatedTime string `json:"createdTime"`
17 ExpiredTime string `json:"expiredTime"`
18 Tags []model.TagModel `json:"tags"`
19 DeleteProtect bool `json:"deleteProtect"`
20}
21func (c *Client) GetPeerConnDetail(peerConnId string, role PeerConnRoleType) (*PeerConn, error)
Note: "initiator" refers to the initiating end, and "acceptor" to the accepting end. Details of peering connections in the same region can be queried accordingly. If this parameter is not set, information for either end may be randomly returned within the same region.
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Sjwvyudwm
Response Value
Operation succeeded:
1{
2 "peerConnId":"peerconn-cbp4xrtzk3fb",
3 "role":"initiator",
4 "status":"active",
5 "bandwithInMbp":1000,
6 "description":"mypeerconn",
7 "localIfId":"qpif-qz9a61ujsizt",
8 "localIfName":"dsds",
9 "localVpcId":"20fada00-f66e-40de-bed4-954af13dc0b2",
10 "localRegion":"bj",
11 "peerVpcId":"1aa1427d-92cf-45d0-bc31-ab4d9fe71425",
12 "peerRegion":"bj",
13 "peerAccountId":"75c14a239bb24b9e88b695e8e9e47952",
14 "createdTime": "2018-08-03 20:38:43"
15 "expiredTime":null,
16 "dnsStatus":"close",
17 "paymentTiming":"Postpaid",
18 "tags":[
19 {
20 "tagKey": "tagKey",
21 "tagValue": "tagValue"
22 }
23 ],
24 "deleteProtect":true
25}
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_get_peer_conn_detail.go
Update the local API name and remarks for peering connections
Use the following code to update the local API name and comments for peering connections.
Function declaration
1type UpdatePeerConnArgs struct {
2 LocalIfId string `json:"localIfId"`
3 Description string `json:"description,omitempty"`
4 LocalIfName string `json:"localIfName,omitempty"`
5}
6func (c *Client) UpdatePeerConn(peerConnId string, args *UpdatePeerConnArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/1jwvyufpd
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_update_peer_conn.go
Application to process the peering connection
Use the following code to approve a peering connection request.
Function declaration
1func (c *Client) AcceptPeerConnApply(peerConnId, clientToken string) error
Note:
- The timeout period for a connection request initiated by the initiator is 7 days. Once the timeout occurs, the status of the peering connection changes to "negotiation failure."
- If the acceptor rejects, the initiator's peering connection status changes to "negotiation failure."
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/wjwvyud6p
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_accept_peer_conn_apply.go
Release peering connections
Use the following code to release a specific peering connection.
Function declaration
1func (c *Client) DeletePeerConn(peerConnId, clientToken string) error
Note:
- Cross-account release can only be initiated by the originating party.
- Prepay peering connections that are still valid and have not expired cannot be released.
- Prepay negotiation failures can be released.
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/0jwvyucfy
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_delete_peer_conn.go
Bandwidth resizing for peering connections
Use the following code to increase the bandwidth of specified peering connections.
Function declaration
1type ResizePeerConnArgs struct {
2 NewBandwidthInMbps int `json:"newBandwidthInMbps"`
3 ClientToken string `json:"-"`
4}
5func (c *Client) ResizePeerConn(peerConnId string, args *ResizePeerConnArgs) error
Note:
- In cross-account scenarios, only the initiator can perform bandwidth adjustments.
- Prepay peering connections support only bandwidth scaling up, not scaling down.
- Postpay peering connections support both bandwidth scaling up and scaling down.
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Wjwvyufcb
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_resize_peer_conn.go
Peering connection renewal
Use the following code to renew a peering connection and extend its validity.
Function declaration
1type RenewPeerConnArgs struct {
2 Billing *Billing `json:"billing"`
3 ClientToken string `json:"-"`
4}
5func (c *Client) RenewPeerConn(peerConnId string, args *RenewPeerConnArgs) error
Note:
- Postpay peering connections cannot be renewed.
- In cross-account scenarios, only the initiator can perform renewal operations.
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/9jwvyudie
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_renew_peer_conn.go
Enable DNS synchronization for the peering connections
Use the following code to enable DNS record synchronization for peering connections.
Function declaration
1type PeerConnSyncDNSArgs struct {
2 Role PeerConnRoleType `json:"role"`
3 ClientToken string `json:"-"`
4}
5func (c *Client) OpenPeerConnSyncDNS(peerConnId string, args *PeerConnSyncDNSArgs) error
Note:
- DNS synchronization can only be enabled when the status of the peering connection is "available."
- DNS synchronization cannot be enabled if the DNS status of the peering connection is "synchronizing" or "disabling synchronization."
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Qjwvyuf0h
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_open_peer_conn_sync_dns.go
Disable DNS synchronization for the peering connections.
Use the following code to disable DNS record synchronization for peering connections.
Function declaration
1type PeerConnSyncDNSArgs struct {
2 Role PeerConnRoleType `json:"role"`
3 ClientToken string `json:"-"`
4}
5func (c *Client) ClosePeerConnSyncDNS(peerConnId string, args *PeerConnSyncDNSArgs) error
Note:
- DNS synchronization can only be disabled when the status of the peering connection is "available.\
- DNS synchronization cannot be turned off when the DNS status of peering connections is either synchronizing or in the process of being disabled.
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/ojwvyucs2
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_close_peer_conn_sync_dns.go
Update the peering connection release protection switch
Use the code below to update the release protection switch for peering connections.
Function declaration
1type UpdatePeerConnDeleteProtectArgs struct {
2 DeleteProtect bool `json:"deleteProtect"`
3 ClientToken string `json:"-"`
4}
5func (c *Client) UpdatePeerConnDeleteProtect(peerConnId string, args *UpdatePeerConnDeleteProtectArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/4lzjn3dsi
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to Peering Connection Exception List
Code example
For specific code examples, refer to example_update_peer_conn_delete_protect.go
