NAT
Initialization
Confirm Endpoint
Before configuring the endpoint for SDK usage, please refer to the developer guide section on NAT 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 NAT, you must have a valid AK (Access Key ID) and SK (Secret Access Key) for signature certification. AK/SK are system-generated identifiers assigned for user authentication and signature certification in NAT.
Your AK/SK information can be obtained and understood through the following steps:
Register a Baidu AI Cloud account
Create a new NAT client
A NAT client serves as the interface for accessing NAT services, offering developers various methods to interact with these services.
Create a new NAT client with AK/SK
Users can refer to the following code to create a new NAT Client to access NAT with AK/SK:
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 NATClient
10 natClient, 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 NAT client with STS
Request STS Token
NAT provides temporary third-party access authorization using the STS mechanism. STS (Security Token Service) is a temporary authorization service from Baidu AI Cloud. With STS, you can issue temporary access credentials with customized validity periods and permissions to third-party users. These users can use the credentials to directly invoke Baidu AI Cloud APIs or SDKs to access cloud resources.
To access NAT via STS, users must first acquire a certification string through the STS client.
Create NAT Client with STS Token
After acquiring the STS token, configure it in the NAT Client to support STS-based NAT client creation.
Code example
The GO SDK implements the STS service API. Below is a complete example for requesting an STS Token and creating an NAT 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 NAT Client object using the requested temporary STS, with the default endpoint
29 natClient, err := vpc.NewClient(stsObj.AccessKeyId, stsObj.SecretAccessKey, "bcc.bj.baidubce.com")
30 if err != nil {
31 fmt.Println("create nat 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 natClient.Config.Credentials = stsCredential
43}
Note: Currently, when configuring an NAT client with STS, regardless of where the corresponding NAT 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 access to NAT
NAT supports the HTTPS transport protocol. To use HTTPS to access NAT services with the NAT Go SDK, specify HTTPS in the endpoint when creating the NAT client object.
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>
4natClient, _ := vpc.NewClient(AK, SK, ENDPOINT)
Configure the NAT client
If users need to configure specific parameters for the NAT Client, they can customize the configuration using the exported Config field of the NAT 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 NAT service using a proxy:
1// import "github.com/baidubce/bce-sdk-go/services/vpc"
2 // Create an NAT Client object
3AK, SK := <your-access-key-id>, <your-secret-access-key>
4ENDPOINT := "bcc.bj.baidubce.com"
5client, _ := vpc.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 NAT, the Config field of the created NAT 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 an NAT 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.
NAT gateway management
The network address translation (NAT) gateway provides Internet access for virtual private clouds and supports SNAT and DNAT. It allows multiple cloud server instances to share public IPs for Internet access and enables cloud server instances to deliver Internet services. The NAT gateway can link EIP instances and EIPGROUP to facilitate many-to-one or many-to-many address translation between intranet IPs and public IPs for cloud servers.
Create NAT gateway
Use the following code to create a NAT gateway
Function declaration
1type CreateNatGatewayArgs struct {
2 ClientToken string `json:"-"`
3 Name string `json:"name"`
4 VpcId string `json:"vpcId"`
5 Spec NatGatewaySpecType `json:"spec"`
6 CuNum string `json:"cuNum,omitempty"`
7 Eips []string `json:"eips,omitempty"`
8 DnatEips []string `json:"dnatEips,omitempty"`
9 Billing *Billing `json:"billing"`
10 Tags []model.TagModel `json:"tags,omitempty"`
11}
12type CreateNatGatewayResult struct {
13 NatId string `json:"natId"`
14}
15func (c *Client) CreateNatGateway(args *CreateNatGatewayArgs) (*CreateNatGatewayResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Xjwvyul3d
Response Value
Operation succeeded:
1{
2 "natId": "nat-b58rnkn1g98h"
3}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_create_nat.go
Query NAT gateway list
Use the following code to query the NAT gateway list.
Function declaration
1type ListNatGatewayArgs struct {
2 VpcId string
3 NatId string
4 Name string
5 Ip string
6 Marker string
7 MaxKeys int
8}
9type ListNatGatewayResult struct {
10 Nats []NAT `json:"nats"`
11 Marker string `json:"marker"`
12 IsTruncated bool `json:"isTruncated"`
13 NextMarker string `json:"nextMarker"`
14 MaxKeys int `json:"maxKeys"`
15}
16func (c *Client) ListNatGateway(args *ListNatGatewayArgs) (*ListNatGatewayResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Tjwvyukbd
Response value
Operation succeeded:
1{
2 "nats":[
3 {
4 "id":"nat-bir8biqwr45e",
5 "name":"mynat",
6 "vpcId":"vpc-13vuxu016dew",
7 "spec":"small",
8 "status":"unconfigured",
9 "eips":[
10 ],
11 "paymentTiming":"Prepaid",
12 "expiredTime":"2018-08-13T08:10:59Z",
13 "createTime": "2021-04-25 17:22:34"
14 },
15 {
16 "id":"nat-b9q8n98mdxwc",
17 "name":"mynat",
18 "vpcId":"vpc-13vuxu016dew",
19 "spec":"small",
20 "status":"unconfigured",
21 "eips":[
22 ],
23 "paymentTiming":"Prepaid",
24 "expiredTime":"2018-07-27T13:18:00Z",
25 "createTime": "2021-04-25 17:22:34"
26 }
27 ]
28}
29"marker":"nat-b58rnkn1g98h",
30"isTruncated": true,
31"nextMarker": "nat-bi72s924x5xu",
32"maxKeys": 2
33}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_list_nat.go
Query NAT gateway details
Use the following code to query the NAT gateway details
Function declaration
1type NAT struct {
2 Id string `json:"id"`
3 Name string `json:"name"`
4 VpcId string `json:"vpcId"`
5 Spec string `json:"spec,omitempty"`
6 CuNum int `json:"cuNum,omitempty"`
7 Status NatStatusType `json:"status"`
8 Eips []string `json:"eips"`
9 DnatEips []string `json:"dnatEips"`
10 PaymentTiming string `json:"paymentTiming"`
11 ExpiredTime string `json:"expiredTime"`
12 Tags []model.TagModel `json:"tags"`
13}
14func (c *Client) GetNatGatewayDetail(natId string) (*NAT, error)
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Mjwvyuj7m
Response value
Operation succeeded:
1{
2 "id":"nat-bir8biqwr45e",
3 "name":"mynat",
4 "vpcId":"vpc-13vuxu016dew",
5 "spec":"small",
6 "status":"unconfigured",
7 "eips":[
8 ],
9 "dnatEips":[
10 ],
11 "paymentTiming":"Prepaid",
12 "expiredTime":"2018-08-13T08:10:59Z",
13 "createTime": "2021-04-25 17:22:34",
14 "tags":[
15 {
16 "tagKey": "tagKey",
17 "tagValue": "tagValue"
18 }
19 ]
20}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_get_nat_detail.go
Update NAT gateway name
Use the following code to update the NAT gateway name
Function declaration
1type UpdateNatGatewayArgs struct {
2 ClientToken string `json:"-"`
3 Name string `json:"name"`
4}
5func (c *Client) UpdateNatGateway(natId string, args *UpdateNatGatewayArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/ojwvyuisy
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_update_nat.go
Release NAT gateway
Use the following code to release a NAT gateway
Function declaration
1func (c *Client) DeleteNatGateway(natId, clientToken string) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/yjwvyujjq
Response Value
Operation succeeded:
No special response parameters are available.
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_delete_nat.go
NAT gateway renewal
Use the following code to renew a NAT gateway
Function declaration
1type RenewNatGatewayArgs struct {
2 ClientToken string `json:"-"`
3 Billing *Billing `json:"billing"`
4}
5func (c *Client) RenewNatGateway(natId string, args *RenewNatGatewayArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Vjwvyukqv
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_renew_nat.go
Bind SNAT EIP to the NAT gateway
Use the following code to bind an SNAT EIP
Function declaration
1type BindEipsArgs struct {
2 ClientToken string `json:"-"`
3 Eips []string `json:"eips"`
4}
5func (c *Client) BindEips(natId string, args *BindEipsArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Ljwvyujy8
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_bind_eip.go
Unbind SNAT EIP from the NAT gateway
Use the following code to unbind a SNAT EIP
Function declaration
1type BindEipsArgs struct {
2 ClientToken string `json:"-"`
3 Eips []string `json:"eips"`
4}
5func (c *Client) BindEips(natId string, args *BindEipsArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/qjwvyuigl
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_unbind_eip.go
Bind DNAT EIP to the NAT gateway
Use the following code to bind an DNAT EIP
Function declaration
1type BindDnatEipsArgs struct {
2 ClientToken string `json:"-"`
3 DnatEips []string `json:"dnatEips"`
4}
5func (c *Client) BindDnatEips(natId string, args *BindDnatEipsArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/iki4chiu4
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_bind_dnat_eip.go
Unbind DNAT EIP from the NAT gateway
Use the following code to unbind a DNAT EIP
Function declaration
1type UnBindDnatEipsArgs struct {
2 ClientToken string `json:"-"`
3 DnatEips []string `json:"dnatEips"`
4}
5func (c *Client) UnBindDnatEips(natId string, args *UnBindDnatEipsArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Uki4cj64l
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_unbind_dnat_eip.go
Create SNAT rules
Users can create SNAT rules with the following code.
Function declaration
1type CreateNatGatewaySnatRuleArgs struct {
2 ClientToken string `json:"-"`
3 RuleName string `json:"ruleName,omitempty"`
4 SourceCIDR string `json:"sourceCIDR,omitempty"`
5 PublicIpAddresses []string `json:"publicIpsAddress,omitempty"`
6}
7type CreateNatGatewaySnatRuleResult struct {
8 RuleId string `json:"ruleId"`
9}
10func (c *Client) CreateNatGatewaySnatRule(natId string, args *CreateNatGatewaySnatRuleArgs) (*CreateNatGatewaySnatRuleResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Iki4clqjv
Response Value
Operation succeeded:
1{
2 "ruleId": "rule-zrsaybxm7nrn"
3}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_create_snat_rule.go
Create SNAT rules in batches
Users can create SNAT rules in batches with the following code.
Function declaration
1type BatchCreateNatGatewaySnatRuleArgs struct {
2 ClientToken string `json:"-"`
3 NatId string `json:"natId"`
4 SnatRules []SnatRuleArgs `json:"snatRules"`
5}
6type BatchCreateNatGatewaySnatRuleResult struct {
7 SnatRuleIds []string `json:"snatRuleIds"`
8}
9func (c *Client) BatchCreateNatGatewaySnatRule(args *BatchCreateNatGatewaySnatRuleArgs) (*BatchCreateNatGatewaySnatRuleResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/oktlo8qfr
Response Value
Operation succeeded:
1{
2 "snatRuleIds": [
3 "rule-zrsaybxm7nrn",
4 "rule-f5kid5g50nua"
5 ]
6}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_batch_create_snat_rule.go
Delete SNAT rules
Users can delete SNAT rules with the following code
Function declaration
1func (c *Client) DeleteNatGatewaySnatRule(natId string, snatRuleId string, clientToken string) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/vki4cot86
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_delete_snat_rule.go
Update SNAT rules
Users can update SNAT rules with the following code
Function declaration
1type UpdateNatGatewaySnatRuleArgs struct {
2 ClientToken string `json:"-"`
3 RuleName string `json:"ruleName,omitempty"`
4 SourceCIDR string `json:"sourceCIDR,omitempty"`
5 PublicIpAddresses []string `json:"publicIpsAddress,omitempty"`
6}
7func (c *Client) UpdateNatGatewaySnatRule(natId string, snatRuleId string, args *UpdateNatGatewaySnatRuleArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Vki4cn14y
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_update_snat_rule.go
Query SNAT rules
Users can query SNAT rules with the following code
Function declaration
1type ListNatGatewaySnatRuleArgs struct {
2 NatId string `json:"natId"`
3 Marker string `json:"marker"`
4 MaxKeys int `json:"maxKeys"`
5}
6type ListNatGatewaySnatRulesResult struct {
7 Rules []SnatRule `json:"rules"`
8 Marker string `json:"marker"`
9 IsTruncated bool `json:"isTruncated"`
10 NextMarker string `json:"nextMarker"`
11 MaxKeys int `json:"maxKeys"`
12}
13func (c *Client) ListNatGatewaySnatRules(args *ListNatGatewaySnatRuleArgs) (*ListNatGatewaySnatRulesResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Pki4cq2nu
Response Value
Operation succeeded:
1{
2 "isTruncated": true,
3 "nextMarker": "rule-vza86i2k6dqu",
4 "maxKeys": 2,
5 "rules": [
6 {
7 "ruleId": "rule-k1jenum3v9s6",
8 "ruleName": "s3",
9 "publicIpsAddress": [
10 "100.88.10.185"
11 ],
12 "sourceCIDR": "1.1.1.1/32",
13 "status": "configuring"
14 },
15 {
16 "ruleId": "rule-kdgqkwpske2q",
17 "ruleName": "s2",
18 "publicIpsAddress": [
19 "100.88.10.185"
20 ],
21 "sourceCIDR": "192.168.1.0/24",
22 "status": "configuring"
23 }
24 ]
25}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_list_snat_rule.go
Create DNAT rules
Users can create DNAT rules with the following code.
Function declaration
1type CreateNatGatewayDnatRuleArgs struct {
2 ClientToken string `json:"-"`
3 RuleName string `json:"ruleName,omitempty"`
4 PublicIpAddress string `json:"publicIpAddress,omitempty"`
5 PrivateIpAddress string `json:"privateIpAddress,omitempty"`
6 Protocol string `json:"protocol,omitempty"`
7 PublicPort string `json:"publicPort,omitempty"`
8 PrivatePort string `json:"privatePort,omitempty"`
9}
10type CreateNatGatewayDnatRuleResult struct {
11 RuleId string `json:"ruleId"`
12}
13func (c *Client) CreateNatGatewayDnatRule(natId string, args *CreateNatGatewayDnatRuleArgs) (*CreateNatGatewayDnatRuleResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/fki4c1gb3
Response Value
Operation succeeded:
1{
2 "ruleId": "rule-zrsaybxm7nrn"
3}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_create_dnat_rule.go
Create DNAT rules in batches
Users can create DNAT rules in batches with the following code.
Function declaration
1type BatchCreateNatGatewayDnatRuleArgs struct {
2 ClientToken string `json:"-"`
3 Rules []DnatRuleArgs `json:"rules"`
4}
5type BatchCreateNatGatewayDnatRuleResult struct {
6 RuleIds []string `json:"ruleIds"`
7}
8func (c *Client) BatchCreateNatGatewayDnatRule(natId string, args *BatchCreateNatGatewayDnatRuleArgs) (*BatchCreateNatGatewayDnatRuleResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Aktl8mzvd
Response Value
Operation succeeded:
1{
2 "ruleIds": [
3 "rule-zrsaybxm7nrn",
4 "rule-f5kid5g50nua"
5 ]
6}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_batch_create_dnat_rule.go
Delete DNAT rules
Users can delete DNAT rules with the following code
Function declaration
1func (c *Client) DeleteNatGatewayDnatRule(natId string, dnatRuleId string, clientToken string) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/zki4cbkkf
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_delete_dnat_rule.go
Update DNAT rules
Users can update DNAT rules with the following code
Function declaration
1type UpdateNatGatewayDnatRuleArgs struct {
2 ClientToken string `json:"-"`
3 RuleName string `json:"ruleName,omitempty"`
4 PublicIpAddress string `json:"publicIpAddress,omitempty"`
5 PrivateIpAddress string `json:"privateIpAddress,omitempty"`
6 Protocol string `json:"protocol,omitempty"`
7 PublicPort string `json:"publicPort,omitempty"`
8 PrivatePort string `json:"privatePort,omitempty"`
9}
10func (c *Client) UpdateNatGatewayDnatRule(natId string, dnatRuleId string, args *UpdateNatGatewayDnatRuleArgs) error
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Aki4c9crm
Response Value
Operation succeeded:
There are no special response parameters
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_update_dnat_rule.go
Query DNAT rules
Users can query DNAT rules with the following code
Function declaration
1type ListNatGatewaDnatRuleArgs struct {
2 Marker string `json:"marker"`
3 MaxKeys int `json:"maxKeys"`
4}
5type ListNatGatewayDnatRulesResult struct {
6 Rules []DnatRule `json:"rules"`
7 Marker string `json:"marker"`
8 IsTruncated bool `json:"isTruncated"`
9 NextMarker string `json:"nextMarker"`
10 MaxKeys int `json:"maxKeys"`
11}
12func (c *Client) ListNatGatewayDnatRules(natId string, args *ListNatGatewaDnatRuleArgs) (*ListNatGatewayDnatRulesResult, error)
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Hki4cf6bf
Response Value
Operation succeeded:
1{
2 "isTruncated": true,
3 "nextMarker": "rule-pej10dar6pxd",
4 "maxKeys": 2,
5 "rules": [
6 {
7 "ruleId": "rule-29d9jpprcm75",
8 "ruleName": "sg",
9 "publicIpAddress": "100.88.6.197",
10 "privateIpAddress": "192.168.3.3",
11 "publicPort": 333,
12 "privatePort": 333,
13 "status": "active",
14 "protocol": "TCP"
15 },
16 {
17 "ruleId": "rule-72z6p8ni6rce",
18 "ruleName": "sd",
19 "publicIpAddress": "100.88.6.197",
20 "privateIpAddress": "192.168.2.2",
21 "publicPort": 222,
22 "privatePort": 222,
23 "status": "active",
24 "protocol": "UDP"
25 }
26 ]
27}
Operation failed:
Throw an exception. For the exception list, refer to: NAT Gateway Exception List
Code example
For specific code examples, refer to example_list_dnat_rule.go
