Subnet
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 network product VPC section in Region Selection Guide.
Note: Subnet API supports both HTTP and HTTPS calling methods. To enhance data security, it is recommended to call via HTTPS.
Retrieve AK/SK
To use the subnets of Baidu AI Cloud, you must have a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. AK/SK are system-generated strings that verify users and ensure secure access to services. You can obtain and manage your AK/SK using the following steps:
Create SubnetClient
SubnetClient serves as the client for subnet services, providing developers with a range of methods to interact with subnet services. When creating a SubnetClient, first configure a BCE client configuration-type config instance using endpoint, AK and SK, and then use the config instance to configure the SubnetClient. The specific configuration method is as follows:
1HOST = b'bcc.bj.baidubce.com'
2AK = b''
3SK = b''
4config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
5self.the_client = subnet_client.SubnetClient(config)
Create subnet
Function declaration
1@required(name=(bytes, str), zone_name=(bytes, str), cidr=(bytes, str), vpc_id=(bytes, str))
2def create_subnet(self, name, zone_name, cidr, vpc_id, subnet_type=None, description=None, client_token=None, config=None)
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Yjwvyu8o1
Usage example
1subnet_name = 'test_subnet_name'
2subnet_cidr = '192.168.0.64/26'
3vpc_id = 'vpc-51csm6rxs9mg'
4self.the_client.create_subnet(subnet_name, 'cn-bj-a', subnet_cidr, vpc_id))
Response value
Operation succeeded:
1{
2 "subnetId":"sbn-1A09ef6b"
3}
Operation failed:
Throw an exception. For the exception list, refer to https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to example_create_subnet.py
List subnets
Function declaration
1@required(marker=(bytes, str),
2 max_Keys=int,
3 vpc_id=(bytes, str),
4 zone_name=(bytes, str),
5 subnet_type=(bytes, str))
6def list_subnets(self, marker=None, max_keys=None, vpc_id=None,
7 zone_name=None, subnet_type=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/xjwvyu8zu
Usage example
1self.the_client.list_subnets()
Response value
Operation succeeded:
1{
2 "nextMarker": "sbn-IyarenI7",
3 "marker": "sbn-IyWRnII7",
4 "maxKeys": 1,
5 "isTruncated": true,
6 "subnets": [
7 {
8 "name": "System predefined subnet",
9 "subnetId": "sbn-IyWRnII7",
10 "zoneName": "cn-bj-a",
11 "cidr": "192.168.0.0/20",
12 "ipv6Cidr": "",
13 "vpcId": "vpc-IyrqYIQ7",
14 "subnetType": "BCC",
15 "description": "",
16 "createdTime": "2020-11-19T12:46:01Z",
17 "tags": [
18 {
19 "tagKey": "aa",
20 "tagValue": "bb"
21 }
22 ]
23 }
24 ]
25}
Operation failed:
Throw an exception. For the exception list, refer to https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to example_list_subnet.py
Query subnet
Function declaration
1@required(subnet_id=(bytes, str))
2def get_subnet(self, subnet_id, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Sjwvyu89a
Usage example
1self.the_client.get_subnet(subnet_id)
Response value
Operation succeeded:
1{
2 "subnet":
3 {
4 "name": "System predefined subnet",
5 "subnetId": "sbn-IyWRnII7",
6 "zoneName": "cn-bj-a",
7 "cidr": "192.168.0.0/20",
8 "ipv6Cidr": "",
9 "vpcId": "vpc-IyrqYIQ7",
10 "subnetType": "BCC",
11 "availableIp": 4090,
12 "description": "",
13 "createdTime": "2020-11-19T12:46:01Z"
14 }
15}
Operation failed:
Throw an exception. For the exception list, refer to https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to example_get_subnet.py
Update subnet
Function declaration
1@required(subnet_id=(bytes, str),
2 name=(bytes, str),
3 description=(bytes, str))
4def update_subnet(self, subnet_id, name, description=None, client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/3jwvyu7wm
Usage example
1self.the_client.update_subnet(subnet_id, 'test_update_name1', 'test_update_description1')
Response value
Operation succeeded:
1{
2}
Operation failed:
Throw an exception. For the exception list, refer to https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to example_update_subnet.py
Note: Only the name and description fields of the subnet can be updated; the cidr, zone_name, and type fields cannot be modified
Delete subnet
Function declaration
1@required(subnet_id=(bytes, str))
2def delete_subnet(self, subnet_id, client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/6jwvyu9b8
Usage example
1self.the_client.delete_subnet(subnet_id)
Response Value
Operation succeeded:
1{
2}
Operation failed:
Throw an exception. For the exception list, refer to https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to example_delete_subnet.py
Create reserved network segment
Function declaration
1@required(subnet_id=(bytes, str),
2 ip_cidr=(bytes, str),
3 ip_version=int,
4 description=(bytes, str))
5def create_subnet_ipreserve(self, subnet_id, ip_cidr, ip_version, description=None,client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/xlgyjw2kn
Response value
Operation succeeded:
1{
2 "ipReserveId":"ipr-te2pa9cv4ikk"
3}
Operation failed:
Return error. Refer to the error code list at: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to: example_create_subnet_ipreserve.py
Query the reserved network segment list
Function declaration
1@required(subnet_id=(bytes, str),
2 marker=(bytes, str),
3 max_keys=int)
4def list_subnet_ipreserve(self, subnet_id=None, marker=None, max_keys=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Flgyjya1o
Response value
Operation succeeded:
1{
2 "marker": "",
3 "nextMarker": "ipr-7xp6wvzqpmhd",
4 "maxKeys": 1,
5 "isTruncated": true,
6 "ipReserves":[
7 {
8 "ipReserveId":"ipr-tqsbpf5hcvbp",
9 "subnetId":"sbn-a4cikyt7756r",
10 "ipCidr":"192.168.0.200/30",
11 "ipVersion":"4",
12 "description":"test",
13 "createdTime":"2023-04-27T03:44:38Z",
14 "updatedTime":"2023-04-27T03:45:38Z"
15 }
16 ]
17}
Operation failed: Return error. Refer to the error code list at: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to: example_list_subnet_ipreserve.py
Delete reserved network segment
Function declaration
1@required(ip_reserve_id=(bytes, str))
2def delete_subnet_ipreserve(self, ip_reserve_id, lient_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/6lgyjz8jn
Response value
Operation succeeded:
1{}
Operation failed:
Return error. Refer to the error code list at: https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
Code example
For specific code examples, please refer to: example_delete_subnet_ipreserve.py
