路由
获取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对BceClientConfigurationl类型的config实例进行配置,再使用config实例对RouteClient进行配置,具体配置方法如下:
1HOST = b''
2AK = b''
3SK = b''
4config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
5self.the_client = route_client.RouteClient(config)
查询路由表
查询路由表,请求参数routeTableId和vpcId不可以同时为空。
函数声明
1 @required(vpc_id=(bytes, str),
2 route_table_id=(bytes, str))
3 def get_route(self, vpc_id=None, route_table_id=None, config=None):
参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/jjwvyuh0v
返回值
操作成功:
1{
2"routeTableId": "rt-q1zg3i8mx8p6",
3"vpcId": "vpc-56rj0s4ha42a",
4"routeRules": [
5 {
6 "routeTableId": "rt-q1zg3i8mx8p6",
7 "description": "",
8 "nexthopId": "vpn-snx074964j9d",
9 "destinationAddress": "10.0.0.1/32",
10 "sourceAddress": "192.168.0.0/20",
11 "routeRuleId": "rr-dvq3cxpghw5e",
12 "nexthopType": "vpn"
13 },
14 {
15 "routeTableId": "rt-q1zg3i8mx8p6",
16 "description": "",
17 "nexthopId": "",
18 "destinationAddress": "192.168.0.0/20",
19 "sourceAddress": "0.0.0.0/0",
20 "routeRuleId": "",
21 "nexthopType": "sys"
22 },
23 {
24 "routeTableId": "rt-q1zg3i8mx8p6",
25 "description": "",
26 "nexthopId": "",
27 "destinationAddress": "192.168.0.0/20",
28 "sourceAddress": "0.0.0.0/0",
29 "routeRuleId": "",
30 "nexthopType": "sys"
31 }
32 ]
33}
操作失败:
返回错误,错误代码列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
代码示例
具体代码示例参考: example_get_route.py
创建路由规则
函数声明
1@required(route_table_id=(bytes, str),
2 source_address=(bytes, str),
3 destination_address=(bytes, str),
4 description=(bytes, str))
5def create_route(self, route_table_id, source_address, destination_address, next_hop_type=None, description="", next_hop_id=None, ip_version=None, next_hops=None, client_token=None, config=None):
参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Ljwvyugpl
返回值
操作成功:
1{"routeRuleId": "rr-dup3cxpebi5e"}
操作失败:
返回错误,错误代码列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
代码示例
具体代码示例参考: example_create_route_rule.py
删除路由规则
函数声明
1@required(route_rule_id=(bytes, str))
2def delete_route(self, route_rule_id, client_token=None, config=None):
参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Vjwvyuge9
返回值
操作成功:
1{}
操作失败:
返回错误,错误代码列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
代码示例
具体代码示例参考: example_delete_route_rule.py
更新路由规则
函数声明
1@required(route_rule_id=(bytes, str),
2 source_address=(bytes, str),
3 destination_address=(bytes, str),
4 description=(bytes, str))
5def update_route(self, route_rule_id, source_address, destination_address, next_hop_type=None, description="", next_hop_id=None, ip_version=None, next_hops=None, client_token=None, config=None):
参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Bl6c4kpb4
返回值
操作成功:
1{}
操作失败:
返回错误,错误代码列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
代码示例
具体代码示例参考: example_update_route_rule.py
查询路由规则
函数声明
1@required(version=(bytes, str),
2 routeTableId=(bytes, str),
3 vpcId=(bytes, str),
4 marker=(bytes, str),
5 maxKeys=int)
6def get_route_rule(self, routeTableId=None, vpcId=None, marker=None, maxKeys=None, config=None):
参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Dl6xal44j
返回值
操作成功:
1{
2"nextMarker": "rr-rbn5yyz6rtn8",
3"marker": "rr-y43tr5disam1",
4"maxKeys": 1000,
5"isTruncated": true,
6"routeRules": [{
7 "routeTableId": "rt-q1zg3i8mx8p6",
8 "description": "",
9 "nexthopId": "vpn-snx074964j9d",
10 "destinationAddress": "10.0.0.1/32",
11 "sourceAddress": "192.168.0.0/20",
12 "routeRuleId": "rr-dvq3cxpghw5e",
13 "nexthopType": "vpn"
14 },
15 {
16 "routeTableId": "rt-q1zg3i8mx8p6",
17 "description": "",
18 "nexthopId": "",
19 "destinationAddress": "192.168.0.0/20",
20 "sourceAddress": "0.0.0.0/0",
21 "routeRuleId": "rt-2e9fi8chbxvh",
22 "nexthopType": "sys"
23 },
24 {
25 "routeTableId": "rt-q1zg3i8mx8p6",
26 "description": "",
27 "nexthopId": "",
28 "destinationAddress": "192.168.0.0/20",
29 "sourceAddress": "0.0.0.0/0",
30 "routeRuleId": "rr-rbn5yyz6rtn8",
31 "nexthopType": "sys"
32 }
33]
34}
操作失败:
返回错误,错误代码列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
代码示例
具体代码示例参考: example_get_route_rule.py
主备切换
函数声明
1func (c *Client) SwitchRoute(routeRuleId, clientToken string) error
参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/xlalxb0tr
返回值
操作成功:
1{}
操作失败:
返回错误,错误代码列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7
代码示例
具体代码示例参考:example_switchroute.py