route_client.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides a client class for ROUTE.
  14. """
  15. import copy
  16. import json
  17. import logging
  18. import uuid
  19. from baidubce import bce_base_client
  20. from baidubce.auth import bce_v1_signer
  21. from baidubce.http import bce_http_client
  22. from baidubce.http import handler
  23. from baidubce.http import http_methods
  24. from baidubce.utils import required
  25. from baidubce import compat
  26. _logger = logging.getLogger(__name__)
  27. class RouteClient(bce_base_client.BceBaseClient):
  28. """
  29. Route base sdk client
  30. """
  31. prefix = b'/v1'
  32. def __init__(self, config=None):
  33. bce_base_client.BceBaseClient.__init__(self, config)
  34. def _merge_config(self, config=None):
  35. """
  36. :param config:
  37. :type config: baidubce.BceClientConfiguration
  38. :return:
  39. """
  40. if config is None:
  41. return self.config
  42. else:
  43. new_config = copy.copy(self.config)
  44. new_config.merge_non_none_values(config)
  45. return new_config
  46. def _send_request(self, http_method, path,
  47. body=None, headers=None, params=None,
  48. config=None, body_parser=None):
  49. config = self._merge_config(config)
  50. if body_parser is None:
  51. body_parser = handler.parse_json
  52. if headers is None:
  53. headers = {b'Accept': b'*/*', b'Content-Type': b'application/json;charset=utf-8'}
  54. return bce_http_client.send_request(
  55. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  56. http_method, RouteClient.prefix + path, body, headers, params)
  57. @required(route_table_id=(bytes, str),
  58. source_address=(bytes, str),
  59. destination_address=(bytes, str),
  60. description=(bytes, str))
  61. def create_route(self, route_table_id, source_address, destination_address,
  62. next_hop_type=None, description="", next_hop_id=None, ip_version=None,
  63. next_hops=None, client_token=None,
  64. config=None):
  65. """
  66. Create a route with the specified options.
  67. :param route_table_id:
  68. The id of the route table.
  69. :type route_table_id: string
  70. :param source_address:
  71. The source address of the route.
  72. :type source_address: string
  73. :param destination_address:
  74. The destination address of the route
  75. :type destination_address: string
  76. :param next_hop_type:
  77. route type
  78. the Bcc type is "custom";
  79. the VPN type is "vpn";
  80. the NAT type is "nat";
  81. the dedicatedGateway type is "dcGateway"; Multi-line mode does not require
  82. the PeerConn type is "peerConn";
  83. the ENIC type is "enic";
  84. the HaVip type is "havip";
  85. the ipv6Gateway type is "ipv6gateway";
  86. the local gateway type is "defaultGateway"
  87. :type next_hop_type: string
  88. :param description:
  89. The option param to describe the route table.
  90. :type description: string
  91. :param next_hop_id:
  92. The next hop id
  93. when the nexthopType is "defaultGateway",this field can be empty
  94. :type next_hop_id: string
  95. :param next_hop_id:
  96. The next hop id
  97. when the nexthopType is "defaultGateway",this field can be empty
  98. :type next_hop_id: string
  99. :param next_hop_list:
  100. The optional list of dcGateway Multi-line mode route to create.
  101. - NextHop.nexthopId next hop dcGateway ID
  102. - NextHop.nexthopType Route type. Currently only supports dedicatedGateway type: "dcGateway"
  103. - NextHop.pathType Multiline mode. The value of load balancing is ecmp; the values of active and
  104. standby modes are ha:active and ha:standby, which represent the active and standby routes respectively
  105. :type next_hop_list: list<route_model.NextHop>
  106. :param client_token:
  107. If the clientToken is not specified by the user, a random String
  108. generated by default algorithm will be used.
  109. :type client_token: string
  110. :param config:
  111. :type config: baidubce.BceClientConfiguration
  112. :return:
  113. :rtype baidubce.bce_response.BceResponse
  114. """
  115. path = b'/route/rule'
  116. params = {}
  117. if client_token is None:
  118. params[b'clientToken'] = generate_client_token()
  119. else:
  120. params[b'clientToken'] = client_token
  121. body = {
  122. 'routeTableId': compat.convert_to_string(route_table_id),
  123. 'sourceAddress': compat.convert_to_string(source_address),
  124. 'destinationAddress': compat.convert_to_string(destination_address),
  125. 'description': compat.convert_to_string(description)
  126. }
  127. if next_hop_type is not None:
  128. body['nexthopType'] = compat.convert_to_string(next_hop_type)
  129. if next_hop_id is not None:
  130. body['nexthopId'] = compat.convert_to_string(next_hop_id)
  131. if ip_version is not None:
  132. body['ipVersion'] = compat.convert_to_string(ip_version)
  133. if next_hops is not None:
  134. next_hop_list = [next_hop.__dict__ for next_hop in next_hops]
  135. body['nextHopList'] = next_hop_list
  136. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params,
  137. config=config)
  138. @required(vpc_id=(bytes, str), route_table_id=(bytes, str))
  139. def get_route(self, vpc_id=None, route_table_id=None, config=None):
  140. """
  141. Get the detail information of route table for specific route table or/and vpc.
  142. :param vpc_id:
  143. the vpc id
  144. vpcId and routeTableId cannot be empty at the same time
  145. :type vpc_id: string
  146. :param route_table_id:
  147. the id of the route table
  148. vpcId and routeTableId cannot be empty at the same time
  149. :type route_table_id: string
  150. :param config:
  151. :type config: baidubce.BceClientConfiguration
  152. :return:
  153. :rtype baidubce.bce_response.BceResponse
  154. """
  155. path = b'/route'
  156. params = {}
  157. if route_table_id is not None:
  158. params[b'routeTableId'] = route_table_id
  159. if vpc_id is not None:
  160. params[b'vpcId'] = vpc_id
  161. return self._send_request(http_methods.GET, path, params=params, config=config)
  162. @required(version=(bytes, str), routeTableId=(bytes, str), vpcId=(bytes, str), marker=(bytes, str), maxKeys=int)
  163. def get_route_rule(self, routeTableId=None, vpcId=None, marker=None, maxKeys=None, config=None):
  164. """
  165. Get the details of route rules in a specified route table or VPC.
  166. :param version:
  167. API version.
  168. :type version: string
  169. :param routeTableId:
  170. The ID of the route table. Either `routeTableId` or `vpcId` must be provided.
  171. :type routeTableId: string
  172. :param vpcId:
  173. The ID of the VPC. Either `routeTableId` or `vpcId` must be provided.
  174. :type vpcId: string
  175. :param marker:
  176. The marker for the start position of batch retrieval.
  177. :type marker: string
  178. :param maxKeys:
  179. The maximum number of entries per page (up to 1000).
  180. :type maxKeys: int
  181. :param config:
  182. BceClientConfiguration object.
  183. :type config: baidubce.BceClientConfiguration
  184. :return:
  185. A BceResponse object.
  186. :rtype: baidubce.bce_response.BceResponse
  187. """
  188. path = b'/route/rule'
  189. params = {}
  190. if routeTableId is not None:
  191. params[b'routeTableId'] = routeTableId
  192. if vpcId is not None:
  193. params[b'vpcId'] = vpcId
  194. if marker is not None:
  195. params[b'marker'] = marker
  196. if maxKeys is not None:
  197. params[b'maxKeys'] = maxKeys
  198. return self._send_request(http_methods.GET, path, params=params, config=config)
  199. @required(route_rule_id=(bytes, str))
  200. def delete_route(self, route_rule_id, client_token=None, config=None):
  201. """
  202. Delete the specific route rule.
  203. :param route_rule_id:
  204. The id of the specified route table.
  205. :type route_rule_id: string
  206. :param client_token:
  207. If the clientToken is not specified by the user, a random String
  208. generated by default algorithm will be used.
  209. :type route_table_id: string
  210. :param config:
  211. :type config: baidubce.BceClientConfiguration
  212. :return:
  213. :rtype baidubce.bce_response.BceResponse
  214. """
  215. path = b'/route/rule/%s' % compat.convert_to_bytes(route_rule_id)
  216. params = {
  217. 'action': 'switchRouteHA'
  218. }
  219. if client_token is None:
  220. params[b'clientToken'] = generate_client_token()
  221. else:
  222. params[b'clientToken'] = client_token
  223. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  224. @required(route_rule_id=(bytes, str))
  225. def switch_route(self, route_rule_id, client_token=None, config=None):
  226. """
  227. In a multi-line master-slave routing mode, switching the main route to the backup route.
  228. :param route_rule_id:
  229. The id of the specified route table.
  230. :type route_rule_id: string
  231. :param client_token:
  232. If the clientToken is not specified by the user, a random String
  233. generated by default algorithm will be used.
  234. :type client_token: string
  235. :param config:
  236. :type config: baidubce.BceClientConfiguration
  237. :return:
  238. :rtype baidubce.bce_response.BceResponse
  239. """
  240. path = b'/route/rule/%s' % compat.convert_to_bytes(route_rule_id)
  241. params = {}
  242. if client_token is None:
  243. params[b'clientToken'] = generate_client_token()
  244. else:
  245. params[b'clientToken'] = client_token
  246. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  247. @required(route_rule_id=(bytes, str),
  248. description=(bytes, str))
  249. def update_route(self, route_rule_id, source_address=None, destination_address=None,
  250. next_hop_type=None, description="", next_hop_id=None, ip_version=None,
  251. next_hops=None, client_token=None,
  252. config=None):
  253. """
  254. Update a route with the specified options.
  255. :param route_rule_id:
  256. The id of the route rule to be updated.
  257. :type route_rule_id: string
  258. :param source_address:
  259. The source address of the route.
  260. :type source_address: string
  261. :param destination_address:
  262. The destination address of the route.
  263. :type destination_address: string
  264. :param next_hop_type:
  265. Route type.
  266. :type next_hop_type: string
  267. :param description:
  268. The option param to describe the route rule.
  269. :type description: string
  270. :param next_hop_id:
  271. The next hop id.
  272. :type next_hop_id: string
  273. :param ip_version:
  274. IP version.
  275. :type ip_version: string
  276. :param next_hops:
  277. The optional list of next hops.
  278. :type next_hops: list<route_model.NextHop>
  279. :param client_token:
  280. If the clientToken is not specified by the user, a random String
  281. generated by default algorithm will be used.
  282. :type client_token: string
  283. :param config:
  284. :type config: baidubce.BceClientConfiguration
  285. :return:
  286. :rtype baidubce.bce_response.BceResponse
  287. """
  288. path = b'/route/rule/%s' % compat.convert_to_bytes(route_rule_id)
  289. params = {}
  290. if client_token is None:
  291. params[b'clientToken'] = generate_client_token()
  292. else:
  293. params[b'clientToken'] = client_token
  294. body = {
  295. 'description': compat.convert_to_string(description)
  296. }
  297. if next_hop_type is not None:
  298. body['nexthopType'] = compat.convert_to_string(next_hop_type)
  299. if next_hop_id is not None:
  300. body['nexthopId'] = compat.convert_to_string(next_hop_id)
  301. if ip_version is not None:
  302. body['ipVersion'] = compat.convert_to_string(ip_version)
  303. if next_hops is not None:
  304. next_hop_list = [next_hop.__dict__ for next_hop in next_hops]
  305. body['nextHopList'] = next_hop_list
  306. if destination_address is not None:
  307. body['destinationAddress'] = compat.convert_to_string(destination_address)
  308. if source_address is not None:
  309. body['sourceAddress'] = compat.convert_to_string(source_address)
  310. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params, config=config)
  311. def generate_client_token_by_uuid():
  312. """
  313. The default method to generate the random string for client_token
  314. if the optional parameter client_token is not specified by the user.
  315. :return:
  316. :rtype string
  317. """
  318. return str(uuid.uuid4())
  319. generate_client_token = generate_client_token_by_uuid