Route
Retrieve Endpoint
Before configuring the Endpoint for SDK usage, please refer to the developer guide section on Service Domains to understand Endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer to the VPC section in the Region Selection Guide. Route service is part of the VPC service and adopts the VPC service domain name.
Retrieve access key
To use Baidu AI Cloud products, you need a Baidu AI Cloud account along with valid AK (Access Key ID) and SK (Secret Access Key) credentials for signature authorization. You can obtain and understand your AK/SK information through the following steps:
RouteClient
Route client serves as the client for route services, providing developers with a range of methods to interact with route services. When creating a new RouteClient, first configure an array-type config instance using Endpoint, AK and SK, and then use the config instance to configure the RouteClient. The specific configuration method is as follows:
1 function __construct(array $config)
2 {
3 parent::__construct($config, 'route');
4 $this->signer = new BceV1Signer();
5 $this->httpClient = new BceHttpClient();
6 }
Query route table
Query route table. The request parameters routeTableId and vpcId cannot both be left empty.
The code for querying the route table instance is as follows
1public function getRouteTable( $vpcId = null, $routeTableId = null, $options = array()) {
2 list($config) = $this->parseOptions($options, 'config');
3 $params = array();
4 if (empty($routeTableId) && empty($vpcId )) {
5 throw new \InvalidArgumentException(
6 'request $routeTableId and $vpcId should not be empty at the same time.'
7 );
8 }
9 if (!empty($routeTableId)) {
10 $params['routeTableId'] = $routeTableId;
11 }
12 if (!empty($vpcId)) {
13 $params['vpcId'] = $vpcId;
14 }
15 return $this->sendRequest(
16 HttpMethod::GET,
17 array(
18 'config' => $config,
19 'params' => $params,
20 ),
21 '/route'
22 );
23}
The parameter description is as follows:
| Parameter name | Types | Whether required | Description |
|---|---|---|---|
| vpcId | string | No | VPC ID. This parameter and routeTableId cannot be empty simultaneously |
| routeTableId | string | No | Route Table ID. This parameter and vpcId cannot be empty simultaneously |
| options | array | No | Default as the config at Initialize routeClient |
Create route rules
When creating route table rules, note the following points:
- When selecting a custom source network segment, the custom segment must fall within the range of existing subnets, excluding 0.0.0.0/0;
- The destination segment must not overlap with the current VPC CIDR (except when either is 0.0.0.0/0);
- The source and destination network segments of a new route entry must not duplicate those of any existing entries in the route table.
The code for creating route rules is as follows:
1public function createRouteRule($routeTableId, $sourceAddress, $destinationAddress,
2 $nexthopType, $description, $nexthopId = null, $clientToken = null, $options = array()) {
3 list($config) = $this->parseOptions($options, 'config');
4 $params = array();
5 $body = array();
6 // Validate whether clientToken is set
7 if (empty($clientToken)) {
8 $params['clientToken'] = $this->generateClientToken();
9 }
10 else {
11 $params['clientToken'] = $clientToken;
12 }
13 // Validate whether routeTableId is empty
14 if (empty($routeTableId)) {
15 throw new \InvalidArgumentException(
16 'request $routeTableId should not be empty .'
17 );
18 }
19 // Validate whether sourceAddress is empty
20 if (empty($sourceAddress)) {
21 throw new \InvalidArgumentException(
22 'request $sourceAddress should not be empty .'
23 );
24 }
25 // Validate whether destinationAddress is empty
26 if (empty($destinationAddress)) {
27 throw new \InvalidArgumentException(
28 'request $destinationAddress should not be empty .'
29 );
30 }
31 // Validate whether nexthopType is empty
32 if (empty($nexthopType)) {
33 throw new \InvalidArgumentException(
34 'request $nexthopType should not be empty .'
35 );
36 }
37 // Validate whether the description is empty
38 if (empty($description)) {
39 throw new \InvalidArgumentException(
40 'request $descrption should not be empty .'
41 );
42 }
43 $body['routeTableId'] = $routeTableId;
44 $body['sourceAddress'] = $sourceAddress;
45 $body['destinationAddress'] = $destinationAddress;
46 $body['nexthopId'] = $nexthopId;
47 $body['nexthopType'] = $nexthopType;
48 $body['description'] = $description;
49 return $this->sendRequest(
50 HttpMethod::POST,
51 array(
52 'config' => $config,
53 'params' => $params,
54 'body' => json_encode($body),
55 ),
56 '/route/rule'
57 );
58}
The parameter description is as follows:
| Parameter name | Types | Whether required | Description |
|---|---|---|---|
| routeTableId | string | Yes | Route table ID |
| sourceAddress | string | Yes | Source network segment (can be 0.0.0.0/0 for all segments, existing VPC subnet segments, or subnet private segments) |
| destinationAddress | string | Yes | Destination network segment, which can be 0.0.0.0/0; otherwise, the destination address must not overlap with the local VPC CIDR (except when either is 0.0.0.0/0) |
| nexthopType | string | Yes | Route types: The "BCC" type is "custom," the "VPN" type is "vpn," and the "NAT" type is "nat.\ |
| description | string | Yes | Description |
| nexthopId | string | No | Next hop ID |
| clientToken | string | No | Idempotence Token, an ASCII string with a maximum length of 64 bits. |
| options | array | No | Default as the config at Initialize routeClient |
Delete route rules
To delete a routing rule based on routeRuleId, use the following code:
1public function deleteRouteRule($routeRuleId, $clientToken = null, $options = array()) {
2 $params = array();
3 list($config) = $this->parseOptions($options, 'config');
4 // Validate whether routeRuleId is empty
5 if(empty($routeRuleId) ) {
6 throw new \InvalidArgumentException(
7 'request $routeRuleId should not be empty.'
8 );
9 }
10 if (empty($clientToken)) {
11 $params['clientToken'] = $this->generateClientToken();
12 } else {
13 $params['clientToken'] = $clientToken;
14 }
15 return $this->sendRequest(
16 HttpMethod::DELETE,
17 array(
18 'config' => $config,
19 'params' => $params,
20 ),
21 '/route/rule/' .$routeRuleId
22 );
23}
The parameter description is as follows:
| Parameter name | Types | Whether required | Description |
|---|---|---|---|
| routeRuleId | string | Yes | Routing rule ID |
| clientToken | string | No | Idempotence Token, an ASCII string with a maximum length of 64 bits. |
| options | array | No | Default as the config at Initialize routeClient |
