Route
Acquire Endpoint
When confirming the Endpoint configured when you use the SDK, you can first read the section on Service Domain Name in the Developer Guide to understand the Endpoint concept. Baidu AI Cloud currently opens the multi-region support. Please refer to the part of network product VPC in Region Selection Introduction. The Route services are a part of VPC services, and use the VPC service domain name.
Get the Key
To use Baidu Cloud products, you need to have a Baidu Cloud account and a valid AK (Access Key ID) and SK (Secret Access Key) for signature verification. You can obtain and understand your AK/SK information through the following steps:
1.[Register Baidu AI Cloud Account](https://login.bce.baidu.com/reg.html? tpl=bceplat&from=portal)
2.[Create AK/SK](https://console.bce.baidu.com/iam/? _=1513940574695#/iam/accesslist)
RouteClient
Being the client of the Route service, RouteClient provides a series of methods for developers to interact with the Route service. When creating RouteClient, you need to first use Endpoint, AK and SK to configure the array type config instance, and then use the config instance to configure the RouteClient. The specific configuration method is as follows:
function __construct(array $config)
{
parent:__construct($config, 'route');
$this->signer = new BceV1Signer();
$this->httpClient = new BceHttpClient();
}
Query Route Table
For query of a route table, the request's parameter routeTableId and vpcId cannot be simultaneously empty.
The instance code for query of a route table is as below:
public function getRouteTable( $vpcId = null, $routeTableId = null, $options = array()) {
list($config) = $this->parseOptions($options, 'config');
$params = array();
if (empty($routeTableId) && empty($vpcId )) {
throw new \InvalidArgumentException(
'request $routeTableId and $vpcId should not be empty at the same time.'
);
}
if (!empty($routeTableId)) {
$params['routeTableId'] = $routeTableId;
}
if (!empty($vpcId)) {
$params['vpcId'] = $vpcId;
}
return $this->sendRequest(
HttpMethod:GET,
array(
'config' => $config,
'params' => $params,
),
'/route'
);
}
The parameters are described as follows:
Parameter name | Type | Required or not | Description |
---|---|---|---|
vpcId | string | No | The id of VPC and the routeTableId cannot be null simultaneously. |
routeTableId | string | No | The id of route table and the vpcId cannot be null simultaneously. |
options | array | No | Config in initializing routeClient by default |
Create Route Rule
To create route table rules, you need to pay attention to the following points:
- When "Custom" is selected for the source segment, the customized segment need to be within the existing subnet range, excluding 0.0.0.0/0;
- The target network and the current VPC cidr cannot overlap (except when the target segment or the VPC cidr is 0.0.0.0/0);
- The source segment and target segment of added route entries cannot be completely consistent with those of existing entries in the route table.
The code for creating route rules is as follows:
public function createRouteRule($routeTableId, $sourceAddress, $destinationAddress,
$nexthopType, $description, $nexthopId = null, $clientToken = null, $options = array()) {
list($config) = $this->parseOptions($options, 'config');
$params = array();
$body = array();
//Verify whether the clientToken is set.
if (empty($clientToken)) {
$params['clientToken'] = $this->generateClientToken();
}
else {
$params['clientToken'] = $clientToken;
}
//Verify whether the routeTableId is null.
if (empty($routeTableId)) {
throw new \InvalidArgumentException(
'request $routeTableId should not be empty .'
);
}
//Verify whether the sourceAddress is null.
if (empty($sourceAddress)) {
throw new \InvalidArgumentException(
'request $sourceAddress should not be empty .'
);
}
//Verify whether the destinationAddress is null.
if (empty($destinationAddress)) {
throw new \InvalidArgumentException(
'request $destinationAddress should not be empty .'
);
}
//Verify whether the nexthopType is null.
if (empty($nexthopType)) {
throw new \InvalidArgumentException(
'request $nexthopType should not be empty .'
);
}
//Verify whether the description is null.
if (empty($description)) {
throw new \InvalidArgumentException(
'request $descrption should not be empty .'
);
}
$body['routeTableId'] = $routeTableId;
$body['sourceAddress'] = $sourceAddress;
$body['destinationAddress'] = $destinationAddress;
$body['nexthopId'] = $nexthopId;
$body['nexthopType'] = $nexthopType;
$body['description'] = $description;
return $this->sendRequest(
HttpMethod:POST,
array(
'config' => $config,
'params' => $params,
'body' => json_encode($body),
),
'/route/rule'
);
}
The parameters are described as follows:
Parameter name | Type | Required or not | Description |
---|---|---|---|
routeTableId | string | Yes | Route table id |
sourceAddress | string | Yes | For the source segment, you can fill in all network segments 0.0.0.0/0, existing subnet segments in VPC or segments in the subnet range. |
destinationAddress | string | Yes | The target segment can be 0.0.0.0/0, or the destination address and the current VPC cidr cannot overlap (except when the destination segment or the VPC cidr is 0.0.0.0/0). |
nexthopType | string | Yes | Route type. The Bcc type is "custom"; the VPN type is "vpn"; the NAT type is "nat". |
description | string | Yes | Description |
nexthopId | string | No | Next hop id |
clientToken | string | No | The idempotence Token is a ASCII string with the length of no more than 64 bits. |
options | array | No | Config in initializing routeClient by default |
Delete Route Rule
The code for deleting route rules according to the routeRuleId is as follows:
public function deleteRouteRule($routeRuleId, $clientToken = null, $options = array()) {
$params = array();
list($config) = $this->parseOptions($options, 'config');
//Verify whether the routeruleid is null.
if(empty($routeRuleId) ) {
throw new \InvalidArgumentException(
'request $routeRuleId should not be empty.'
);
}
if (empty($clientToken)) {
$params['clientToken'] = $this->generateClientToken();
} else {
$params['clientToken'] = $clientToken;
}
return $this->sendRequest(
HttpMethod:DELETE,
array(
'config' => $config,
'params' => $params,
),
'/route/rule/' .$routeRuleId
);
}
The parameters are described as follows:
Parameter name | Type | Required or not | Description |
---|---|---|---|
routeRuleId | string | Yes | Route rule id |
clientToken | string | No | The idempotence Token is a ASCII string with the length of no more than 64 bits. |
options | array | No | Config in initializing routeClient by default |