Route
更新时间:2019-10-10
获取Endpoint
在确认您使用SDK配置Endpoint时,可先阅读开发人员指南中关于服务域名的部分,理解Endpoint相关的概念。 百度智能云目前开放了多区域支持,请参考区域选择说明中网络产品VPC的部分,Route服务是VPC服务的一部分,使用VPC服务域名。
获取密钥
要使用百度云产品,您需要拥有一个百度云账号和一个有效的 AK(Access Key ID)、SK(Secret Access Key)用来进行签名认证。可以通过如下步骤获得并了解您的AK/SK信息:
RouteClient
RouteClient是Route服务的客户端,为开发者与Route服务进行交互提供了一系列的方法。 新建RouteClient时,需要先使用Endpoint、AK、SK对array类型的config实例进行配置,再使用config实例对RouteClient进行配置,具体配置方法如下:
                Plain Text
                
            
            1    function __construct(array $config)
2    {
3        parent::__construct($config, 'route');
4        $this->signer = new BceV1Signer();
5        $this->httpClient = new BceHttpClient();
6    }
            查询路由表
查询路由表,请求参数routeTableId和vpcId不可以同时为空。
查询路由表实例代码如下
                Plain Text
                
            
            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}
            参数说明如下:
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| vpcId | string | 否 | VPC的id,该参数和routeTableId不能同时为空 | 
| routeTableId | string | 否 | 路由表id,该参数和vpcId不能同时为空 | 
| options | array | 否 | 默认为初始化routeClient时的config | 
创建路由规则
创建路由表规则,有以下几点需要注意:
- 源网段选择自定义时,自定义网段需在已有子网范围内,0.0.0.0/0除外;
 - 目标网段不能与当前所在VPC cidr重叠(目标网段或本VPC cidr为0.0.0.0/0时例外);
 - 新增路由条目的源网段和目标网段,不能与路由表中已有条目源网段和目标网段完全一致。
 
创建路由规则代码如下:
                Plain Text
                
            
            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    // 校验是否设置 clientToken 
7    if (empty($clientToken)) {
8        $params['clientToken'] = $this->generateClientToken();
9    }
10    else {
11        $params['clientToken'] = $clientToken;
12    }
13    // 校验 routeTableId 是否为空
14    if (empty($routeTableId)) {
15        throw new \InvalidArgumentException(
16            'request $routeTableId  should not be empty .'
17        );
18    }
19    // 校验 sourceAddress 是否为空
20    if (empty($sourceAddress)) {
21        throw new \InvalidArgumentException(
22            'request $sourceAddress  should not be empty .'
23        );
24    }
25    // 校验 destinationAddress 是否为空
26    if (empty($destinationAddress)) {
27        throw new \InvalidArgumentException(
28            'request $destinationAddress  should not be empty .'
29        );
30    }
31    // 校验 nexthopType 是否为空
32    if (empty($nexthopType)) {
33        throw new \InvalidArgumentException(
34            'request $nexthopType  should not be empty .'
35        );
36    }
37    // 校验 description 是否为空
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}
            参数说明如下:
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| routeTableId | string | 是 | 路由表id | 
| sourceAddress | string | 是 | 源网段,可填全部网段0.0.0.0/0、VPC内已有子网网段或子网范围内网段 | 
| destinationAddress | string | 是 | 目标网段,可以是0.0.0.0/0,否则目的地址不能与本VPC cidr重叠(目的网段或本VPC cidr为0.0.0.0/0时例外) | 
| nexthopType | string | 是 | 路由类型。Bcc类型是"custom";VPN类型是"vpn";NAT类型是"nat" | 
| description | string | 是 | 描述 | 
| nexthopId | string | 否 | 下一跳id | 
| clientToken | string | 否 | 幂等性Token,是一个长度不超过64位的ASCII字符串。 | 
| options | array | 否 | 默认为初始化routeClient时的config | 
删除路由规则
根据routeRuleId删除路由规则,代码如下:
                Plain Text
                
            
            1public function deleteRouteRule($routeRuleId, $clientToken = null, $options = array()) {
2    $params = array();
3    list($config) = $this->parseOptions($options, 'config');
4    // 校验 routeRuleId 是否为空
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}
            参数说明如下:
| 参数名称 | 类型 | 是否必需 | 描述 | 
|---|---|---|---|
| routeRuleId | string | 是 | 路由规则id | 
| clientToken | string | 否 | 幂等性Token,是一个长度不超过64位的ASCII字符串。 | 
| options | array | 否 | 默认为初始化routeClient时的config | 
