VPC
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: VPC API supports both HTTP and HTTPS calling methods. To enhance data security, it is recommended to call via HTTPS.
Retrieve AK/SK
To use Baidu AI Cloud VPC, you need a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. AK/SK are system-generated strings used to identify users and authenticate service access requests. You can retrieve your AK/SK information by following these steps:
Create VpcClient
VPC client serves as the client for VPC services, providing developers with a range of methods to interact with ACL services. When creating a VPC client, first configure a BCE client configuration-type config instance using endpoint, AK and SK, and then use the config instance to configure the VPC client. The specific configuration method is as follows:
1HOST = b''
2AK = b''
3SK = b''
4config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
5self.the_client = vpc_client.VpcClient(config)
Create VPC
Function declaration
1@required(name=(bytes, str), cidr=(bytes, str))
2def create_vpc(self, name, cidr, description=None, client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Njwvyuaq5
Usage example
1vpc_name = 'test_vpc_name'
2vpc_cidr = '192.168.240.0/20'
3description = 'test_vpc_descrition'
4self.the_client.create_vpc(vpc_name, vpc_cidr, description))
Response value
Operation succeeded:
1{
2 "vpcId":"vpc-1849ef6b"
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_vpc.py
List VPCs
Function declaration
1@required(marker=(bytes, str), max_Keys=int, is_Default=bool)
2def list_vpcs(self, marker=None, max_Keys=None, isDefault=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/wjwvyub23
Usage example
1self.the_client.list_vpcs()
Response value
Operation succeeded:
1{
2"nextMarker": "vpc-xktdeMSf",
3"marker": "vpc-IyWRtII7",
4"maxKeys": 1,
5"isTruncated": true,
6"vpcs": [
7 {
8 "vpcId': "vpc-IyWRtII7",
9 "name": "VPC Name",
10 "isDefault": True,
11 "cidr": "0.0.0.0/0",
12 "description": "default",
13 "ipv6Cidr": "2400:da00:e003:4c00::/56",
14 "tags":[
15 {
16 "tagKey": "tagKey",
17 "tagValue": "tagValue"
18 }
19 ]
20 }
21 ]
22}
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, refer to :example_list_vpc.py
Query VPC
Function declaration
1@required(vpc_id=(bytes, str))
2def get_vpc(self, vpc_id, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/yjwvyuaeo
Usage example
1self.the_client.get_vpc(vpc_id)
Response value
Operation succeeded:
1{
2 "vpc": {
3 "vpcId': "vpc-IyWRtII7",
4 "name": u"\u9ed8\u8ba4\u79c1\u6709\u7f51\u7edc",
5 "isDefault": True,
6 "cidr": "0.0.0.0/0",
7 "ipv6Cidr": "2400:da00:e003:4c00::/56",
8 "description": "default",
9 "subnets": [
10 {
11 "name": "System predefined subnet",
12 "subnetId": "sbn-IyWRnII7",
13 "zoneName": "cn-bj-a",
14 "cidr": "192.168.0.0/20",
15 "ipv6Cidr": "2400:da00:e003:4c11::/64",
16 "vpcId": "vpc-IyrqYIQ7",
17 "subnetType": "BCC",
18 "description": ""
19 "createdTime": "2018-03-14T14:45:02Z"
20 }
21 ],
22 "tags":[
23 {
24 "tagKey": "tagKey",
25 "tagValue": "tagValue"
26 }
27 ]
28 }
29}
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, refer to example_get_vpc.py
Update VPC
Function declaration
1@required(vpc_id=(bytes, str), name=(bytes, str))
2def update_vpc(self, vpc_id, name, description=None, client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/tjwvyubez
Usage example
1self.the_client.update_vpc(vpc_id, 'test_update_name', 'test_update_description')
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_vpc.py
Note: Only the name and description fields of the VPC can be updated; the cidr field cannot be updated
Delete VPC
Function declaration
1@required(vpc_id=(bytes, str))
2def delete_vpc(self, vpc_id, client_token=None, config=None):
Parameter meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/Ijwvyua0d
Usage example
1self.the_client.delete_vpc(vpc_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_vpc.py
Query VPC intranet IP
Function declaration
1@required(vpc_id=(bytes, str))
2def get_private_ip_address_info(self, vpc_id, private_ip_range=None, private_ip_addresses=None, config=None):
Parameter Meaning
Refer to the OpenAPI documentation: https://cloud.baidu.com/doc/VPC/s/2kih0m069
Usage example
1self.the_client.get_private_ip_address_info(vpc_id=vpc_id, private_ip_addresses=private_ip_addresses)
Response value
Operation succeeded:
1{
2 "vpc_private_ip_addresses": [
3 {
4 "cidr": "192.168.0.0/20",
5 "private_ip_address": "192.168.0.1",
6 "private_ip_address_type": "sys_occupancy",
7 "created_time": "1999-01-01T01:01:01Z"
8 }
9 ]
10}
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_vpc_ip.py
