NAT
Retrieve Endpoint
Before configuring the endpoint for SDK usage, please refer to the developer guide section on VPC Service Domain Name to understand endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer to the VPC section in the Region Selection Guide. NAT service is part of the VPC service and adopts the VPC service domain name.
Retrieve access key
To use the Baidu AI Cloud products, you need a Baidu AI Cloud account, a valid AK (Access Key ID) and SK (Secret Access Key) for signature certification. Your AK/SK information can be obtained and understood through the following steps:
nat_client
nat_client is the Python client for the NAT service. It packages the NAT service API, simplifying interactions for developers. Users can use nat_client methods to configure NAT gateways.
Create nat_client
When creating a nat_client, first configure a BceClientConfiguration-type config instance using endpoint, AK and SK, and then use the config instance to configure the nat_client. The specific configuration method is as follows:
1HOST = b''
2AK = b''
3SK = b''
4config = BceClientConfiguration(
5 credentials=BceCredentials(AK, SK), endpoint=HOST)
6self.client = nat_client.NatClient(config)
Create NAT gateway
Use the following code to create a NAT gateway
Function declaration
1def create_nat(self, name, vpc_id, spec=None, billing=None, eips=None,
2 dnat_eips=None, client_token=None, config=None, cu_num=None):
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.py
Query NAT gateway list
Use the following code to query the NAT gateway list.
Function declaration
1 def list_nats(self, vpc_id=None, nat_id=None, name=None,
2 ip=None, marker=None, max_keys=None, config=None):
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.py
Query NAT gateway details
Use the following code to query the NAT gateway details
Function declaration
1 def get_nat(self, nat_id, config=None):
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.py
Update NAT gateway name
Use the following code to update the NAT gateway name
Function declaration
1 def update_nat(self, nat_id, name, client_token=None, config=None):
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.py
Release NAT gateway
Use the following code to release a NAT gateway
Function declaration
1 def delete_nat(self, nat_id, client_token=None, config=None):
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.py
NAT gateway renewal
Use the following code to renew a NAT gateway
Function declaration
1 def purchase_reserved_nat(self, nat_id, billing, client_token=None,
2 config=None):
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_purchase_reserved_nat.py
Bind SNAT EIP to the NAT gateway
Use the following code to bind an SNAT EIP
Function declaration
1 def bind_eip(self, nat_id, eips, client_token=None, config=None):
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.py
Unbind SNAT EIP from the NAT gateway
Use the following code to unbind a SNAT EIP
Function declaration
1 def unbind_eip(self, nat_id, eips, client_token=None, config=None):
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.py
Bind DNAT EIP to the NAT gateway
Use the following code to bind an DNAT EIP
Function declaration
1 def bind_dnat_eip(self, nat_id, eips, client_token=None, config=None):
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.py
Unbind DNAT EIP from the NAT gateway
Use the following code to unbind a DNAT EIP
Function declaration
1 def unbind_dnat_eip(self, nat_id, eips, client_token=None, config=None):
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.py
Create SNAT rules
Users can create SNAT rules with the following code.
Function declaration
1 def create_snat_rule(self, nat_id, rule_name, source_cidr, public_ip_address, client_token=None, config=None):
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.py
Create SNAT rules in batches
Users can create SNAT rules in batches with the following code.
Function declaration
1 def batch_create_snat_rule(self, nat_id, snat_rules, client_token=None, config=None):
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.py
Delete SNAT rules
Users can delete SNAT rules with the following code
Function declaration
1 def delete_snat_rule(self, nat_id, snat_rule_id, client_token=None, config=None):
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.py
Update SNAT rules
Users can update SNAT rules with the following code
Function declaration
1def update_snat_rule(self, nat_id, snat_rule_id, rule_name=None, source_cidr=None,
2 public_ip_address=None, client_token=None, config=None):
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.py
Query SNAT rules
Users can query SNAT rules with the following code
Function declaration
1 def list_snat_rule(self, nat_id, marker=None, maxKeys=None, client_token=None, config=None):
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.py
Create DNAT rules
Users can create DNAT rules with the following code.
Function declaration
1 def create_dnat_rule(self, nat_id, public_ip_address, private_ip_address, protocol, rule_name=None,
2 public_port=None, private_port=None, client_token=None, config=None):
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.py
Create DNAT rules in batches
Users can create DNAT rules in batches with the following code.
Function declaration
1 def batch_create_dnat_rule(self, nat_id, dnat_rules, client_token=None, config=None):
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.py
Delete DNAT rules
Users can delete DNAT rules with the following code
Function declaration
1 def delete_dnat_rule(self, nat_id, dnat_rule_id, client_token=None, config=None):
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.py
Update DNAT rules
Users can update DNAT rules with the following code
Function declaration
1 def update_dnat_rule(self, nat_id, dnat_rule_id,
2 public_ip_address=None, private_ip_address=None,
3 rule_name=None, protocol=None, public_port=None,
4 private_port=None, client_token=None, config=None):
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.py
Query DNAT rules
Users can query DNAT rules with the following code
Function declaration
1 def list_dnat_rule(self, nat_id, marker=None, maxKeys=None, client_token=None, config=None):
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.py
