eip_group_client.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing,
  11. # software distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions
  14. # and limitations under the License.
  15. """
  16. This module provides a client class for EIP group.
  17. """
  18. import copy
  19. import json
  20. import logging
  21. import uuid
  22. from baidubce import utils
  23. from baidubce import bce_base_client
  24. from baidubce.auth import bce_v1_signer
  25. from baidubce.http import bce_http_client
  26. from baidubce.http import handler
  27. from baidubce.http import http_methods
  28. from baidubce.services.eip import eip_group_model
  29. from baidubce.utils import required
  30. _logger = logging.getLogger(__name__)
  31. default_billing_to_purchase_created = eip_group_model.Billing('Prepaid')
  32. default_billing_to_purchase_reserved = eip_group_model.Billing()
  33. class EipGroupClient(bce_base_client.BceBaseClient):
  34. """
  35. EIP group sdk client
  36. """
  37. version = b'/v1'
  38. version_v2 = b'/v2'
  39. prefix = b'/eipgroup'
  40. def __init__(self, config=None):
  41. bce_base_client.BceBaseClient.__init__(self, config)
  42. def _merge_config(self, config=None):
  43. """
  44. :param config:
  45. :type config: baidubce.BceClientConfiguration
  46. :return:
  47. """
  48. if config is None:
  49. return self.config
  50. else:
  51. new_config = copy.copy(self.config)
  52. new_config.merge_non_none_values(config)
  53. return new_config
  54. def _send_request(self, http_method, path,
  55. body=None, headers=None, params=None,
  56. config=None, body_parser=None):
  57. config = self._merge_config(config)
  58. if body_parser is None:
  59. body_parser = handler.parse_json
  60. if headers is None:
  61. headers = {b'Accept': b'*/*',
  62. b'Content-Type': b'application/json;charset=utf-8'}
  63. return bce_http_client.send_request(
  64. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  65. http_method, path, body, headers, params)
  66. @required(eip_count=int,
  67. bandwidth_in_mbps=int)
  68. def create_eip_group(self, eip_count, bandwidth_in_mbps,
  69. name=None, client_token=None,
  70. billing=None, route_type=None, idc=None,
  71. tags=None, config=None):
  72. """
  73. Create a shared bandwidth EIP group with specified options.
  74. Real-name authentication is required before creating EIP groups.
  75. Only prepaid EIP groups is supported.
  76. :param client_token:
  77. An ASCII string whose length is less than 64.
  78. The request will be idempotent if client token is provided.
  79. :type client_token: string
  80. :param eip_count:
  81. Numbers of EIP addresses in the EIP group.
  82. The minimum number of public IP addresses is two,
  83. and the maximum number multiplies 5Mbps mustn't exceed the
  84. total amount of shared bandwidth package.
  85. :type eip_count: int
  86. :param bandwidth_in_mbps:
  87. Public Internet bandwidth in unit Mbps. For Prepaid EIP groups,
  88. this value must be integer between 10 and 200.
  89. :type bandwidth_in_mbps: int
  90. :param billing:
  91. Billing information.
  92. :type billing: eip_group_model.Billing
  93. :param name:
  94. The name of EIP group that will be created.
  95. The name, beginning with letter, should have the length between
  96. 1 and 65 bytes, and could contain alphabets, numbers or '-_/.'.
  97. If not specified, the service will generate it automatically.
  98. :type name: string
  99. :param config:
  100. :type config: baidubce.BceClientConfiguration
  101. :return:
  102. :rtype baidubce.bce_response.BceResponse
  103. """
  104. path = self._get_path()
  105. if client_token is None:
  106. client_token = generate_client_token()
  107. params = {b'clientToken': client_token}
  108. if billing is None:
  109. billing = default_billing_to_purchase_created
  110. body = {
  111. 'eipCount': eip_count,
  112. 'bandwidthInMbps': bandwidth_in_mbps,
  113. 'billing': billing.__dict__
  114. }
  115. if route_type is not None:
  116. body['routeType'] = route_type
  117. if name is not None:
  118. body['name'] = name
  119. if idc is not None:
  120. body['idc'] = idc
  121. if tags is not None:
  122. body['tags'] = tags
  123. return self._send_request(http_methods.POST,
  124. path, body=json.dumps(body),
  125. params=params, config=config)
  126. def list_eip_groups(self, id=None, name=None, status=None,
  127. marker=None, max_keys=None, config=None):
  128. """
  129. Return a list of EIP groups, according to the ID,
  130. name or status of EIP group. If not specified,
  131. returns a full list of EIP groups in VPC.
  132. :param id:
  133. The id of specified EIP group.
  134. :type id: string
  135. :param name:
  136. The name of specified EIP group.
  137. :type name: string
  138. :param status:
  139. The status of specified EIP group.
  140. :type status: string
  141. :param marker:
  142. The optional parameter marker specified in the original
  143. request to specify where in the results to begin listing.
  144. Together with the marker, specifies the list result which
  145. listing should begin. If the marker is not specified,
  146. the list result will listing from the first one.
  147. :type marker: string
  148. :param max_keys:
  149. The optional parameter to specifies the max number of
  150. list result to return.
  151. The default value is 1000.
  152. :type max_keys: int
  153. :param config:
  154. :type config: baidubce.BceClientConfiguration
  155. :return:
  156. :rtype baidubce.bce_response.BceResponse
  157. """
  158. path = self._get_path()
  159. params = {}
  160. if id is not None:
  161. params[b'id'] = id
  162. if name is not None:
  163. params[b'name'] = name
  164. if status is not None:
  165. params[b'status'] = status
  166. if marker is not None:
  167. params[b'marker'] = marker
  168. if max_keys is not None:
  169. params[b'maxKeys'] = max_keys
  170. return self._send_request(http_methods.GET, path,
  171. params=params, config=config)
  172. @required(id=(bytes, str))
  173. def get_eip_group(self, id, config=None):
  174. """
  175. Get the detail information of specified EIP group.
  176. :param id:
  177. The id of specified EIP group.
  178. :type id: string
  179. :param config:
  180. :type config: baidubce.BceClientConfiguration
  181. :return:
  182. :rtype baidubce.bce_response.BceResponse
  183. """
  184. path = utils.append_uri(self._get_path(), id)
  185. return self._send_request(http_methods.GET, path, config=config)
  186. @required(id=(bytes, str), name=(bytes, str))
  187. def update_eip_group(self, id, name, client_token=None, config=None):
  188. """
  189. Update the name of specified EIP group.
  190. :param id:
  191. The id of specified EIP group.
  192. :type id: string
  193. :param name:
  194. The new name of the EIP group
  195. :type name: string
  196. :param client_token:
  197. An ASCII string whose length is less than 64.
  198. The request will be idempotent if clientToken is provided.
  199. If the clientToken is not specified by user,
  200. a random String generated by default algorithm will be used.
  201. :type client_token: string
  202. :param config:
  203. :type config: baidubce.BceClientConfiguration
  204. :return:
  205. :rtype baidubce.bce_response.BceResponse
  206. """
  207. path = utils.append_uri(self._get_path(), id)
  208. if client_token is None:
  209. client_token = generate_client_token()
  210. params = {
  211. b'update': None,
  212. b'clientToken': client_token
  213. }
  214. body = {
  215. 'name': name
  216. }
  217. return self._send_request(http_methods.PUT,
  218. path, body=json.dumps(body),
  219. params=params, config=config)
  220. @required(id=(bytes, str), bandwidth_in_mbps=int)
  221. def resize_eip_group_bandwidth(self, id, bandwidth_in_mbps,
  222. client_token=None, config=None):
  223. """
  224. Resize the bandwidth of a specified EIP group.
  225. :param id:
  226. The id of specified EIP group.
  227. :type id: string
  228. :param client_token:
  229. An ASCII string whose length is less than 64.
  230. The request will be idempotent if clientToken is provided.
  231. If the clientToken is not specified by user,
  232. a random String generated by default algorithm will be used.
  233. :type client_token: string
  234. :param bandwidth_in_mbps:
  235. The new bandwidth of EIP group.
  236. For prepaid EIP groups, this value must be integer
  237. between 10 and 200.
  238. :type bandwidth_in_mbps: int
  239. :param config:
  240. :type config: baidubce.BceClientConfiguration
  241. :return:
  242. :rtype baidubce.bce_response.BceResponse
  243. """
  244. path = utils.append_uri(self._get_path(), id)
  245. if client_token is None:
  246. client_token = generate_client_token()
  247. params = {
  248. b'resize': None,
  249. b'clientToken': client_token
  250. }
  251. body = {
  252. 'bandwidthInMbps': bandwidth_in_mbps
  253. }
  254. return self._send_request(http_methods.PUT,
  255. path, body=json.dumps(body),
  256. params=params, config=config)
  257. @required(id=(bytes, str), eip_add_count=int)
  258. def resize_eip_group_count(self, id, eip_add_count,
  259. client_token=None, config=None):
  260. """
  261. Resize the EIP count of a specified EIP group.
  262. :param id:
  263. The id of specified EIP group.
  264. :type id: string
  265. :param client_token:
  266. An ASCII string whose length is less than 64.
  267. The request will be idempotent if clientToken is provided.
  268. If the clientToken is not specified by user,
  269. a random String generated by default algorithm will be used.
  270. :type client_token: string
  271. :param eip_add_count:
  272. The increase number of EIP addresses in the EIP group.
  273. This value must larger than zero, and the maximum number multiplies
  274. 5Mbps mustn't exceed the total amount of shared bandwidth package.
  275. :type eip_add_count: int
  276. :param config:
  277. :type config: baidubce.BceClientConfiguration
  278. :return:
  279. :rtype baidubce.bce_response.BceResponse
  280. """
  281. path = utils.append_uri(self._get_path(), id)
  282. if client_token is None:
  283. client_token = generate_client_token()
  284. params = {
  285. b'resize': None,
  286. b'clientToken': client_token
  287. }
  288. body = {
  289. 'eipAddCount': eip_add_count
  290. }
  291. return self._send_request(http_methods.PUT,
  292. path, body=json.dumps(body),
  293. params=params, config=config)
  294. @required(id=(bytes, str), eip_add_count=int)
  295. def resize_eip_group_count_v2(self, id, eip_add_count,
  296. client_token=None, config=None):
  297. """
  298. Resize the EIP count of a specified EIP group.
  299. :param id:
  300. The id of specified EIP group.
  301. :type id: string
  302. :param client_token:
  303. An ASCII string whose length is less than 64.
  304. The request will be idempotent if clientToken is provided.
  305. If the clientToken is not specified by user,
  306. a random String generated by default algorithm will be used.
  307. :type client_token: string
  308. :param eip_add_count:
  309. The increase number of EIP addresses in the EIP group.
  310. This value must larger than zero, and the maximum number multiplies
  311. 5Mbps mustn't exceed the total amount of shared bandwidth package.
  312. :type eip_add_count: int
  313. :param config:
  314. :type config: baidubce.BceClientConfiguration
  315. :return:
  316. :rtype add ip list.
  317. """
  318. path = utils.append_uri(self._get_v2_path(), id)
  319. if client_token is None:
  320. client_token = generate_client_token()
  321. params = {
  322. b'resize': None,
  323. b'clientToken': client_token
  324. }
  325. body = {
  326. 'eipAddCount': eip_add_count
  327. }
  328. return self._send_request(http_methods.PUT,
  329. path, body=json.dumps(body),
  330. params=params, config=config)
  331. @required(id=(bytes, str))
  332. def purchase_reserved_eip_group(self, id, client_token=None,
  333. billing=None, config=None):
  334. """
  335. Renew specified EIP group.
  336. EIP groups cannot can not be renewed during resizing process.
  337. :param id:
  338. The id of EIP group.
  339. :type id: string
  340. :param client_token:
  341. An ASCII string whose length is less than 64.
  342. The request will be idempotent if clientToken is provided.
  343. If the clientToken is not specified by user,
  344. a random String generated by default algorithm will be used.
  345. :type client_token: string
  346. :param billing:
  347. Billing information.
  348. :type billing: eip_group_model.Billing
  349. :param config:
  350. :type config: baidubce.BceClientConfiguration
  351. :return:
  352. :rtype baidubce.bce_response.BceResponse
  353. """
  354. path = utils.append_uri(self._get_path(), id)
  355. if client_token is None:
  356. client_token = generate_client_token()
  357. params = {
  358. b'purchaseReserved': None,
  359. b'clientToken': client_token
  360. }
  361. if billing is None:
  362. billing = default_billing_to_purchase_reserved
  363. body = {
  364. 'billing': billing.__dict__
  365. }
  366. return self._send_request(http_methods.PUT,
  367. path, body=json.dumps(body),
  368. params=params, config=config)
  369. @required(id=(bytes, str))
  370. def delete_eip_group(self, id, client_token=None, config=None):
  371. """
  372. Delete an EIP group.
  373. :type id: string
  374. :param id: The ID of the EIP group to delete.
  375. :type client_token: string
  376. :param client_token: A unique token for identifying the request.
  377. :type config: baidubce.BceClientConfiguration
  378. :param config: None
  379. :return: baidubce.bce_response.BceResponse
  380. """
  381. path = utils.append_uri(self._get_path(), id)
  382. if client_token is None:
  383. client_token = generate_client_token()
  384. params = {
  385. b'clientToken': client_token
  386. }
  387. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  388. @required(move_out_args=(list,))
  389. def eip_group_move_out(self, id, move_out_args, client_token=None, config=None):
  390. """
  391. Move out an EIP from a group.
  392. :type id: string
  393. :param id: The ID of the EIP group.
  394. :type move_out_args: List[dict]
  395. :param move_out_args: List of dictionaries for moving out the EIPs,
  396. each dict containing 'eip', 'bandwidth_in_mbps',
  397. and 'billing' keys.
  398. :type client_token: string
  399. :param client_token: A unique token for identifying the request.
  400. :type config: baidubce.BceClientConfiguration
  401. :param config: None
  402. :return: baidubce.bce_response.BceResponse
  403. """
  404. path = utils.append_uri(self._get_path(), id)
  405. if client_token is None:
  406. client_token = generate_client_token()
  407. params = {
  408. b'move_out': None,
  409. b'clientToken': client_token
  410. }
  411. move_out_eips = []
  412. for arg in move_out_args:
  413. move_out_eip = {}
  414. move_out_eip['eip'] = arg['eip']
  415. if arg.get('bandwidthInMbps') is not None:
  416. move_out_eip['bandwidthInMbps'] = arg['bandwidthInMbps']
  417. if arg.get('billing') is not None:
  418. move_out_eip['billing'] = arg['billing'].__dict__
  419. move_out_eips.append(move_out_eip)
  420. body = {
  421. "moveOutEips": move_out_eips
  422. }
  423. json_body = json.dumps(body)
  424. return self._send_request(http_methods.PUT, path, params=params, body=json_body, config=config)
  425. @required(eips=(list,))
  426. def eip_group_move_in(self, id, eips, client_token=None, config=None):
  427. """
  428. Move in an EIP to a group.
  429. :type id: string
  430. :param id: The ID of the EIP group.
  431. :type eips: List[]
  432. :param eips: The list of EIPs to move in.
  433. :type client_token: string
  434. :param client_token: A unique token for identifying the request.
  435. :type config: baidubce.BceClientConfiguration
  436. :param config: None
  437. :return: baidubce.bce_response.BceResponse
  438. """
  439. path = utils.append_uri(self._get_path(), id)
  440. if client_token is None:
  441. client_token = generate_client_token()
  442. params = {
  443. b'move_in': None,
  444. b'clientToken': client_token
  445. }
  446. body = {
  447. "eips": eips
  448. }
  449. json_body = json.dumps(body)
  450. return self._send_request(http_methods.PUT, path, params=params, body=json_body, config=config)
  451. @staticmethod
  452. def _get_path(prefix=None):
  453. """
  454. :type prefix: string
  455. :param prefix: path prefix
  456. """
  457. if prefix is None:
  458. prefix = EipGroupClient.prefix
  459. return utils.append_uri(EipGroupClient.version, prefix)
  460. @staticmethod
  461. def _get_v2_path(prefix=None):
  462. """
  463. :type prefix: string
  464. :param prefix: path prefix
  465. """
  466. if prefix is None:
  467. prefix = EipGroupClient.prefix
  468. return utils.append_uri(EipGroupClient.version_v2, prefix)
  469. def generate_client_token_by_uuid():
  470. """
  471. The default method to generate the random string for client_token
  472. if the optional parameter client_token is not specified by the user.
  473. :return:
  474. :rtype string
  475. """
  476. return str(uuid.uuid4())
  477. generate_client_token = generate_client_token_by_uuid