peerconn_client.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions
  13. # and limitations under the License.
  14. """
  15. This module provides a client class for peer connection.
  16. """
  17. import copy
  18. import json
  19. import logging
  20. import uuid
  21. from baidubce import utils
  22. from baidubce import bce_base_client
  23. from baidubce.auth import bce_v1_signer
  24. from baidubce.http import bce_http_client
  25. from baidubce.http import handler
  26. from baidubce.http import http_methods
  27. from baidubce.services.vpc import peerconn_model
  28. from baidubce.utils import required
  29. _logger = logging.getLogger(__name__)
  30. default_billing_to_purchase_created = peerconn_model.Billing('Postpaid')
  31. default_billing_to_purchase_reserved = peerconn_model.Billing()
  32. class PeerConnClient(bce_base_client.BceBaseClient):
  33. """
  34. Peer connection sdk client
  35. """
  36. version = b'/v1'
  37. prefix = b'/peerconn'
  38. def __init__(self, config=None):
  39. bce_base_client.BceBaseClient.__init__(self, config)
  40. def _merge_config(self, config=None):
  41. """
  42. :param config:
  43. :type config: baidubce.BceClientConfiguration
  44. :return:
  45. """
  46. if config is None:
  47. return self.config
  48. else:
  49. new_config = copy.copy(self.config)
  50. new_config.merge_non_none_values(config)
  51. return new_config
  52. def _send_request(self, http_method, path,
  53. body=None, headers=None, params=None,
  54. config=None, body_parser=None):
  55. config = self._merge_config(config)
  56. if body_parser is None:
  57. body_parser = handler.parse_json
  58. if headers is None:
  59. headers = {b'Accept': b'*/*',
  60. b'Content-Type': b'application/json;charset=utf-8'}
  61. return bce_http_client.send_request(
  62. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  63. http_method, path, body, headers, params)
  64. @required(bandwidth_in_mbps=int,
  65. local_vpc_id=(bytes, str),
  66. peer_vpc_id=(bytes, str),
  67. peer_region=(bytes, str))
  68. def create_peerconn(self, bandwidth_in_mbps, local_vpc_id,
  69. peer_vpc_id, peer_region, description=None,
  70. local_if_name=None, peer_account_id=None,
  71. peer_if_name=None, client_token=None,
  72. billing=None, config=None, tags=None,
  73. resourceGroupId=None, deleteProtect=False):
  74. """
  75. Create Peer connection.
  76. For peer connections within the same region, only postpaid is
  77. supported.
  78. For peer connections between different accounts, the peer connections
  79. are available only after the remote account accepts the connections.
  80. For peer connections within the same account, peer connection will be
  81. accepted automatically.
  82. There can be only one peer connection between any two VPCs.
  83. Peer connection endpoints cannot be the same VPC.
  84. If both local VPC and remote VPC are transit VPCs, peer connection
  85. cannot be established.
  86. :param client_token:
  87. An ASCII string whose length is less than 64.
  88. The request will be idempotent if client token is provided.
  89. :type client_token: string
  90. :param bandwidth_in_mbps:
  91. Network bandwidth (in unit of Mbps) of peer connection.
  92. :type bandwidth_in_mbps: int
  93. :param description:
  94. Description of peer connection.
  95. :type description: string
  96. :param local_if_name:
  97. Name of local interface of peer connection.
  98. :type local_if_name: string
  99. :param local_vpc_id:
  100. Local side VPC id of peer connection.
  101. :type local_vpc_id: string
  102. :param peer_account_id:
  103. Remote account id of peer connection.
  104. Used only when the peer connection connects two different accounts.
  105. :type peer_account_id: string
  106. :param peer_vpc_id:
  107. Remote side VPC id of peer connection.
  108. :type peer_vpc_id: string
  109. :param peer_region:
  110. Remote side region of peer connection.
  111. :type peer_region: string
  112. :param peer_if_name:
  113. Name of remote interface of peer connection.
  114. :type peer_if_name: string
  115. :param billing:
  116. Billing information.
  117. :type billing: nat_model.Billing
  118. :param config:
  119. :type config: baidubce.BceClientConfiguration
  120. :param tags:
  121. Tags of peer connection.
  122. :type tags: list[TagModel]
  123. :param resourceGroupId:
  124. Resource group ID of peer connection.
  125. :type resourceGroupId: string
  126. :param deleteProtect:
  127. Whether or not enable delete protection on this resource.
  128. Default false.
  129. :type deleteProtect: bool
  130. :return:
  131. :rtype baidubce.bce_response.BceResponse
  132. """
  133. path = self._get_path()
  134. if client_token is None:
  135. client_token = generate_client_token()
  136. params = {
  137. b'clientToken': client_token
  138. }
  139. if billing is None:
  140. billing = default_billing_to_purchase_created
  141. body = {
  142. 'bandwidthInMbps': bandwidth_in_mbps,
  143. 'localVpcId': local_vpc_id,
  144. 'peerVpcId': peer_vpc_id,
  145. 'peerRegion': peer_region,
  146. 'billing': billing.__dict__,
  147. 'deleteProtect': deleteProtect
  148. }
  149. if description is not None:
  150. body['description'] = description
  151. if local_if_name is not None:
  152. body['localIfName'] = local_if_name
  153. if peer_account_id is not None:
  154. body['peerAccountId'] = peer_account_id
  155. if peer_if_name is not None:
  156. body['peerIfName'] = peer_if_name
  157. if tags is not None:
  158. tag_list = [tag.__dict__ for tag in tags]
  159. body['tags'] = tag_list
  160. if resourceGroupId is not None:
  161. body['resourceGroupId'] = resourceGroupId
  162. return self._send_request(http_methods.POST,
  163. path, body=json.dumps(body),
  164. params=params, config=config)
  165. @required(vpc_id=(bytes, str))
  166. def list_peerconns(self, vpc_id, marker=None,
  167. max_keys=None, config=None):
  168. """
  169. Return a list of peer connections.
  170. :param vpc_id:
  171. VPC id that peer connections connect to.
  172. :type vpc_id: string
  173. :param marker:
  174. The optional parameter marker specified in the original
  175. request to specify where in the results to begin listing.
  176. Together with the marker, specifies the list result which
  177. listing should begin. If the marker is not specified,
  178. the list result will listing from the first one.
  179. :type marker: string
  180. :param max_keys:
  181. The optional parameter to specifies the max number of
  182. list result to return.
  183. The default value is 1000.
  184. :type max_keys: int
  185. :param config:
  186. :type config: baidubce.BceClientConfiguration
  187. :return:
  188. :rtype baidubce.bce_response.BceResponse
  189. """
  190. path = self._get_path()
  191. params = {
  192. b'vpcId': vpc_id
  193. }
  194. if marker is not None:
  195. params[b'marker'] = marker
  196. if max_keys is not None:
  197. params[b'maxKeys'] = max_keys
  198. return self._send_request(http_methods.GET,
  199. path, params=params, config=config)
  200. @required(peer_conn_id=(bytes, str))
  201. def get_peerconn(self, peer_conn_id, config=None):
  202. """
  203. Get the detail information of specified peer connection.
  204. :param peer_conn_id:
  205. The id of specified peer connection.
  206. :type peer_conn_id: string
  207. :param config:
  208. :type config: baidubce.BceClientConfiguration
  209. :return:
  210. :rtype baidubce.bce_response.BceResponse
  211. """
  212. path = utils.append_uri(self._get_path(), peer_conn_id)
  213. return self._send_request(http_methods.GET, path, config=config)
  214. @required(peer_conn_id=(bytes, str), local_if_id=(bytes, str))
  215. def update_peerconn(self, peer_conn_id, local_if_id, description=None,
  216. local_if_name=None, client_token=None, config=None):
  217. """
  218. Update the interface name or description of specified peer connection.
  219. :param peer_conn_id:
  220. The id of specified peer connection.
  221. :type peer_conn_id: string
  222. :param local_if_id:
  223. Local interface id of peer connection
  224. :type local_if_id: string
  225. :param description:
  226. Description of peer connection.
  227. :type description: string
  228. :param local_if_name:
  229. Name of local interface of peer connection.
  230. :type local_if_name: string
  231. :param client_token:
  232. An ASCII string whose length is less than 64.
  233. The request will be idempotent if clientToken is provided.
  234. If the clientToken is not specified by user,
  235. a random String generated by default algorithm will be used.
  236. :type client_token: string
  237. :param config:
  238. :type config: baidubce.BceClientConfiguration
  239. :return:
  240. :rtype baidubce.bce_response.BceResponse
  241. """
  242. path = utils.append_uri(self._get_path(), peer_conn_id)
  243. if client_token is None:
  244. client_token = generate_client_token()
  245. params = {
  246. b'clientToken': client_token
  247. }
  248. body = {
  249. 'localIfId': local_if_id
  250. }
  251. if description is not None:
  252. body['description'] = description
  253. if local_if_name is not None:
  254. body['localIfName'] = local_if_name
  255. return self._send_request(http_methods.PUT,
  256. path, body=json.dumps(body),
  257. params=params, config=config)
  258. @required(peer_conn_id=(bytes, str), action=(bytes, str))
  259. def handle_peerconn(self, peer_conn_id, action, client_token=None,
  260. config=None):
  261. """
  262. Accept or reject peer connection request.
  263. Timeout period of connection request is 7 days.
  264. When timeout or the remote side rejects the connection,
  265. the status of peer connection on initiator side is consulting failed.
  266. :param peer_conn_id:
  267. The id of specified peer connection.
  268. :type peer_conn_id: string
  269. :param action:
  270. 'accept': when accepting the peer connection.
  271. 'reject': when rejecting the peer connection.
  272. :type action: string
  273. :param client_token:
  274. An ASCII string whose length is less than 64.
  275. The request will be idempotent if clientToken is provided.
  276. If the clientToken is not specified by user,
  277. a random String generated by default algorithm will be used.
  278. :type client_token: string
  279. :param config:
  280. :type config: baidubce.BceClientConfiguration
  281. :return:
  282. :rtype baidubce.bce_response.BceResponse
  283. """
  284. path = utils.append_uri(self._get_path(), peer_conn_id)
  285. if client_token is None:
  286. client_token = generate_client_token()
  287. params = {
  288. b'clientToken': client_token
  289. }
  290. params[action] = None
  291. return self._send_request(http_methods.PUT,
  292. path, params=params, config=config)
  293. @required(peer_conn_id=(bytes, str))
  294. def delete_peerconn(self, peer_conn_id, client_token=None,
  295. config=None):
  296. """
  297. Delete peer connection.
  298. For peer connections between different accounts,
  299. only initiator can perform this operation.
  300. One cannot delete prepaid peer connections that are not expired.
  301. Consulting failed prepaid peer connections can be deleted.
  302. :param peer_conn_id:
  303. The id of specified peer connection.
  304. :type peer_conn_id: string
  305. :param client_token:
  306. An ASCII string whose length is less than 64.
  307. The request will be idempotent if clientToken is provided.
  308. If the clientToken is not specified by user,
  309. a random String generated by default algorithm will be used.
  310. :type client_token: string
  311. :param config:
  312. :type config: baidubce.BceClientConfiguration
  313. :return:
  314. :rtype baidubce.bce_response.BceResponse
  315. """
  316. path = utils.append_uri(self._get_path(), peer_conn_id)
  317. if client_token is None:
  318. client_token = generate_client_token()
  319. params = {
  320. b'clientToken': client_token
  321. }
  322. return self._send_request(http_methods.DELETE,
  323. path, params=params, config=config)
  324. @required(peer_conn_id=(bytes, str), new_bandwidth_in_mbps=int)
  325. def resize_peerconn(self, peer_conn_id, new_bandwidth_in_mbps,
  326. client_token=None, config=None):
  327. """
  328. Scale down/up the bandwidth of specified peer connection.
  329. For peer connections between different accounts,
  330. only initiator can perform this operation.
  331. Prepaid peer connection can only scale up.
  332. Postpaid peer connection can scale up or down.
  333. :param peer_conn_id:
  334. The id of specified peer connection.
  335. :type peer_conn_id: string
  336. :param client_token:
  337. An ASCII string whose length is less than 64.
  338. The request will be idempotent if clientToken is provided.
  339. If the clientToken is not specified by user,
  340. a random String generated by default algorithm will be used.
  341. :type client_token: string
  342. :param new_bandwidth_in_mbps:
  343. The new bandwidth of the peer connection.
  344. :type new_bandwidth_in_mbps: int
  345. :param config:
  346. :type config: baidubce.BceClientConfiguration
  347. :return:
  348. :rtype baidubce.bce_response.BceResponse
  349. """
  350. path = utils.append_uri(self._get_path(), peer_conn_id)
  351. if client_token is None:
  352. client_token = generate_client_token()
  353. params = {
  354. b'resize': None,
  355. b'clientToken': client_token
  356. }
  357. body = {
  358. 'newBandwidthInMbps': new_bandwidth_in_mbps
  359. }
  360. return self._send_request(http_methods.PUT,
  361. path, body=json.dumps(body),
  362. params=params, config=config)
  363. @required(peer_conn_id=(bytes, str))
  364. def purchase_reserved_peerconn(self, peer_conn_id, client_token=None,
  365. billing=None, config=None):
  366. """
  367. Renew specified peer connection.
  368. Postpaid peer connection cannot be renewed.
  369. For peer connections between different accounts, only the initiator can
  370. perform this operation.
  371. :param peer_conn_id:
  372. The id of specified peer connection.
  373. :type peer_conn_id: string
  374. :param client_token:
  375. An ASCII string whose length is less than 64.
  376. The request will be idempotent if clientToken is provided.
  377. If the clientToken is not specified by user,
  378. a random String generated by default algorithm will be used.
  379. :type client_token: string
  380. :param billing:
  381. Billing information.
  382. :type billing: peerconn_model.Billing
  383. :param config:
  384. :type config: baidubce.BceClientConfiguration
  385. :return:
  386. :rtype baidubce.bce_response.BceResponse
  387. """
  388. path = utils.append_uri(self._get_path(), peer_conn_id)
  389. if client_token is None:
  390. client_token = generate_client_token()
  391. params = {
  392. b'purchaseReserved': None,
  393. b'clientToken': client_token
  394. }
  395. if billing is None:
  396. billing = default_billing_to_purchase_reserved
  397. body = {
  398. 'billing': billing.__dict__
  399. }
  400. return self._send_request(http_methods.PUT,
  401. path, body=json.dumps(body),
  402. params=params, config=config)
  403. @required(peer_conn_id=(bytes, str), role=(bytes, str))
  404. def open_peerconn_dns_sync(self, peer_conn_id, role,
  405. client_token=None, config=None):
  406. """
  407. Open DNS sync between VPCs connected by peer connection.
  408. DNS sync can be opened only when the status of
  409. peer connection is available.
  410. DNS sync cannot be opened when the DNS status of
  411. peer connection is syncing or closing.
  412. :param peer_conn_id:
  413. The id of specified peer connection.
  414. :type peer_conn_id: string
  415. :param role:
  416. 'initiator': for VPC where peer connection is initiated.
  417. 'acceptor': for VPC where peer connection is accepted.
  418. :type role: string
  419. :param client_token:
  420. An ASCII string whose length is less than 64.
  421. The request will be idempotent if clientToken is provided.
  422. If the clientToken is not specified by user,
  423. a random String generated by default algorithm will be used.
  424. :type client_token: string
  425. :param config:
  426. :type config: baidubce.BceClientConfiguration
  427. :return:
  428. :rtype baidubce.bce_response.BceResponse
  429. """
  430. path = utils.append_uri(self._get_path(), peer_conn_id)
  431. if client_token is None:
  432. client_token = generate_client_token()
  433. params = {
  434. b'open': None,
  435. b'role': role,
  436. b'clientToken': client_token
  437. }
  438. return self._send_request(http_methods.PUT,
  439. path, params=params, config=config)
  440. @required(peer_conn_id=(bytes, str), role=(bytes, str))
  441. def close_peerconn_dns_sync(self, peer_conn_id, role,
  442. client_token=None, config=None):
  443. """
  444. Close DNS sync between VPCs connected by peer connection.
  445. DNS sync can be closed only when the status of peer connection
  446. is available. DNS sync cannot be closed when the DNS status
  447. of peer connection is syncing or closing.
  448. :param peer_conn_id:
  449. The id of specified peer connection.
  450. :type peer_conn_id: string
  451. :param role:
  452. 'initiator': for VPC where peer connection is initiated.
  453. 'acceptor': for VPC where peer connection is accepted.
  454. :type role: string
  455. :param client_token:
  456. An ASCII string whose length is less than 64.
  457. The request will be idempotent if clientToken is provided.
  458. If the clientToken is not specified by user,
  459. a random String generated by default algorithm will be used.
  460. :type client_token: string
  461. :param config:
  462. :type config: baidubce.BceClientConfiguration
  463. :return:
  464. :rtype baidubce.bce_response.BceResponse
  465. """
  466. path = utils.append_uri(self._get_path(), peer_conn_id)
  467. if client_token is None:
  468. client_token = generate_client_token()
  469. params = {
  470. b'close': None,
  471. b'role': role,
  472. b'clientToken': client_token
  473. }
  474. return self._send_request(http_methods.PUT,
  475. path, params=params, config=config)
  476. @required(peer_conn_id=(bytes, str), delete_protect=(bool,))
  477. def update_peerconn_delete_protect(self, peer_conn_id, delete_protect,
  478. client_token=None, config=None):
  479. """
  480. Update the delete protect of peer connection.
  481. :param peer_conn_id:
  482. The id of specified peer connection.
  483. :type peer_conn_id: string
  484. :param delete_protect:
  485. Whether enable the delete protect of peer connection.
  486. :type delete_protect: boolean
  487. :param client_token:
  488. An ASCII string whose length is less than 64.
  489. The request will be idempotent if clientToken is provided.
  490. If the clientToken is not specified by user,
  491. a random String generated by default algorithm will be used.
  492. :type client_token: string
  493. :param config:
  494. :type config: baidubce.BceClientConfiguration
  495. :return:
  496. :rtype baidubce.bce_response.BceResponse
  497. """
  498. path = utils.append_uri(self._get_path(), peer_conn_id, b'deleteProtect')
  499. if client_token is None:
  500. client_token = generate_client_token()
  501. params = {
  502. b'clientToken': client_token
  503. }
  504. body = {
  505. 'deleteProtect': delete_protect,
  506. }
  507. return self._send_request(http_methods.PUT,
  508. path, body=json.dumps(body), params=params, config=config)
  509. @staticmethod
  510. def _get_path(prefix=None):
  511. """
  512. :type prefix: string
  513. :param prefix: path prefix
  514. """
  515. if prefix is None:
  516. prefix = PeerConnClient.prefix
  517. return utils.append_uri(PeerConnClient.version, prefix)
  518. def generate_client_token_by_uuid():
  519. """
  520. The default method to generate the random string for client_token
  521. if the optional parameter client_token is not specified by the user.
  522. :return:
  523. :rtype string
  524. """
  525. return str(uuid.uuid4())
  526. generate_client_token = generate_client_token_by_uuid