eip_client.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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"); you may not
  5. # use this file except in compliance with the License. You may obtain a copy
  6. # 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, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions
  14. # and limitations under the License.
  15. """
  16. This module provides a client class for EIP.
  17. """
  18. import copy
  19. import json
  20. import logging
  21. import uuid
  22. from baidubce import utils
  23. from baidubce.auth import bce_v1_signer
  24. from baidubce.bce_base_client import BceBaseClient
  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.model import EipStatus
  29. _logger = logging.getLogger(__name__)
  30. class EipClient(BceBaseClient):
  31. """
  32. eip sdk client
  33. """
  34. version = b'/v1'
  35. prefix = b'/eip'
  36. def __init__(self, config=None):
  37. """
  38. :type config: baidubce.BceClientConfiguration
  39. :param config:
  40. """
  41. BceBaseClient.__init__(self, config)
  42. def create_eip(self, bandwidth_in_mbps, name=None, billing=None,
  43. route_type=None, client_token=None, config=None):
  44. """
  45. Create an eip with the specified options.
  46. :type bandwidth_in_mbps: int
  47. :param bandwidth_in_mbps: specify the bandwidth in Mbps
  48. :type name: string
  49. :param name: name of eip. The optional parameter
  50. :type billing: Billing
  51. :param billing: billing information. The optional parameter,
  52. default paymentTiming is Postpaid
  53. :type client_token: string
  54. :param client_token: if the clientToken is not specified by the user,
  55. a random String generated by default algorithm will be used.
  56. :type config: baidubce.BceClientConfiguration
  57. :param config:
  58. :return: created eip address, for example,{"eip":"x.x.x.x"}
  59. """
  60. body = {
  61. 'name': name,
  62. 'bandwidthInMbps': bandwidth_in_mbps
  63. }
  64. if route_type is not None:
  65. body['routeType'] = route_type
  66. if billing is None:
  67. body['billing'] = {
  68. 'paymentTiming': 'Postpaid',
  69. 'billingMethod': 'ByBandwidth'
  70. }
  71. else:
  72. body['billing'] = {
  73. 'paymentTiming': billing.payment_timing,
  74. 'billingMethod': billing.billing_method,
  75. 'reservation': {
  76. 'reservationLength': billing.reservation_length,
  77. 'reservationTimeUnit': billing.reservation_time_unit
  78. }
  79. }
  80. path = self._get_path()
  81. if client_token is None:
  82. client_token = self._generate_default_client_token()
  83. params = {
  84. b'clientToken': client_token
  85. }
  86. return self._send_request(http_methods.POST, path,
  87. body=json.dumps(body), params=params,
  88. config=config)
  89. def resize_eip(self, eip, new_bandwidth_in_mbps, client_token=None,
  90. config=None):
  91. """
  92. Resizing eip
  93. :type eip: string
  94. :param eip: eip address to be resized
  95. :type new_bandwidth_in_mbps: int
  96. :param new_bandwidth_in_mbps: specify new bandwidth in Mbps for eip
  97. :type client_token: string
  98. :param client_token: if the clientToken is not specified by the user,
  99. a random String generated by default algorithm will be used.
  100. :type config: baidubce.BceClientConfiguration
  101. :param config:
  102. :return: BceResponse
  103. """
  104. body = {
  105. 'newBandwidthInMbps': new_bandwidth_in_mbps
  106. }
  107. path = utils.append_uri(self._get_path(), eip)
  108. if client_token is None:
  109. client_token = self._generate_default_client_token()
  110. params = {
  111. b'resize': b'',
  112. b'clientToken': client_token
  113. }
  114. return self._send_request(http_methods.PUT, path, params=params,
  115. body=json.dumps(body), config=config)
  116. def purchase_reserved_eip(self, eip, billing=None, client_token=None,
  117. config=None):
  118. """
  119. PurchaseReserved eip with fixed duration,only Prepaid eip can do this
  120. :type eip: string
  121. :param eip: eip address to be renewed
  122. :type billing: Billing
  123. :param billing: billing information. The optional parameter,
  124. default reservationLength is 1
  125. :type client_token: string
  126. :param client_token: if the clientToken is not specified by the user,
  127. a random String generated by default algorithm will be used.
  128. :type config: baidubce.BceClientConfiguration
  129. :param config:
  130. :return: BceResponse
  131. """
  132. if billing is None:
  133. body = {
  134. 'billing': {
  135. 'reservation': {
  136. 'reservationLength': 1,
  137. 'reservationTimeUnit': 'Month'
  138. }
  139. }
  140. }
  141. else:
  142. body = {
  143. 'billing': {
  144. 'reservation': {
  145. 'reservationLength': billing.reservation_length,
  146. 'reservationTimeUnit': billing.reservation_time_unit
  147. }
  148. }
  149. }
  150. path = utils.append_uri(self._get_path(), eip)
  151. if client_token is None:
  152. client_token = self._generate_default_client_token()
  153. params = {
  154. b'purchaseReserved': b'',
  155. b'clientToken': client_token
  156. }
  157. return self._send_request(http_methods.PUT, path, params=params,
  158. body=json.dumps(body), config=config)
  159. def start_auto_renew_eip(self, eip, auto_renew_time_unit=None, auto_renew_time=None,
  160. client_token=None, config=None):
  161. """
  162. Enable auto renew for specified EIP.
  163. :type eip: string
  164. :param eip: eip address to be enable to auto-renew.
  165. :type auto_renew_time_unit: string
  166. :param auto_renew_time_unit: time unit of auto_renew_time, default 'Month'.
  167. :type auto_renew_time: int
  168. :param auto_renew_time: the unit of time for auto renew,
  169. default auto_renew_time is 1
  170. :type client_token: string
  171. :param client_token: if the clientToken is not specified by the user,
  172. a random String generated by default algorithm will be used.
  173. :type config: baidubce.BceClientConfiguration
  174. :param config:
  175. :return: BceResponse
  176. """
  177. if auto_renew_time_unit is None:
  178. auto_renew_time_unit = 'month'
  179. if auto_renew_time is None:
  180. auto_renew_time = 1
  181. body = {
  182. 'autoRenewTimeUnit': auto_renew_time_unit,
  183. 'autoRenewTime': auto_renew_time
  184. }
  185. path = utils.append_uri(self._get_path(), eip)
  186. if client_token is None:
  187. client_token = self._generate_default_client_token()
  188. params = {
  189. b'startAutoRenew': b'',
  190. b'clientToken': client_token
  191. }
  192. return self._send_request(http_methods.PUT, path, params=params,
  193. body=json.dumps(body), config=config)
  194. def stop_auto_renew_eip(self, eip, client_token=None, config=None):
  195. """
  196. Disable auto renew for specified EIP.
  197. :type eip: string
  198. :param eip: eip address to be disable to auto-renew.
  199. :type client_token: string
  200. :param client_token: if the clientToken is not specified by the user,
  201. a random String generated by default algorithm will be used.
  202. :type config: baidubce.BceClientConfiguration
  203. :param config:
  204. :return: BceResponse
  205. """
  206. path = utils.append_uri(self._get_path(), eip)
  207. if client_token is None:
  208. client_token = self._generate_default_client_token()
  209. params = {
  210. b'stopAutoRenew': b'',
  211. b'clientToken': client_token
  212. }
  213. # Sending the HTTP request
  214. return self._send_request(http_methods.PUT, path, params=params, config=config)
  215. def bind_eip(self, eip, instance_type, instance_id, instance_ip, client_token=None,
  216. config=None):
  217. """
  218. bind the eip to a specified instanceId and instanceType
  219. :type eip: string
  220. :param eip: eip address to be bound
  221. :type instance_type: string
  222. :param instance_type: type of instance to be bound(BCC BLB et.)
  223. :type instance_id: string
  224. :param instance_id: id of instance to be bound
  225. :type client_token: string
  226. :param client_token: if the clientToken is not specified by the user,
  227. a random String generated by default algorithm will be used.
  228. :type config: baidubce.BceClientConfiguration
  229. :param config:
  230. :return: BceResponse
  231. """
  232. body = {
  233. 'instanceType': instance_type,
  234. 'instanceId': instance_id
  235. }
  236. if instance_ip is not None:
  237. body['instanceIp'] = instance_ip
  238. path = utils.append_uri(self._get_path(), eip)
  239. if client_token is None:
  240. client_token = self._generate_default_client_token()
  241. params = {
  242. b'bind': b'',
  243. b'clientToken': client_token
  244. }
  245. return self._send_request(http_methods.PUT, path, params=params,
  246. body=json.dumps(body), config=config)
  247. def unbind_eip(self, eip, client_token=None, config=None):
  248. """
  249. unbind the eip from a specified instance
  250. :type eip: string
  251. :param eip: eip address to be unbound
  252. :type client_token: string
  253. :param client_token: if the clientToken is not specified by the user,
  254. a random String generated by default algorithm will be used.
  255. :type config: baidubce.BceClientConfiguration
  256. :param config:
  257. :return: BceResponse
  258. """
  259. path = utils.append_uri(self._get_path(), eip)
  260. if client_token is None:
  261. client_token = self._generate_default_client_token()
  262. params = {
  263. b'unbind': b'',
  264. b'clientToken': client_token
  265. }
  266. return self._send_request(http_methods.PUT, path, params=params,
  267. config=config)
  268. def release_eip(self, eip, client_token=None, config=None):
  269. """
  270. release the eip(delete operation)
  271. Only the Postpaid instance or Prepaid which is expired can be released.
  272. if the eip has been bound, must unbind before releasing.
  273. :type eip: string
  274. :param eip: eip address to be released
  275. :type client_token: string
  276. :param client_token: if the clientToken is not specified by the user,
  277. a random String generated by default algorithm will be used.
  278. :type config: baidubce.BceClientConfiguration
  279. :param config:
  280. :return: BceResponse
  281. """
  282. path = utils.append_uri(self._get_path(), eip)
  283. if client_token is None:
  284. client_token = self._generate_default_client_token()
  285. params = {
  286. b'clientToken': client_token
  287. }
  288. return self._send_request(http_methods.DELETE, path, params=params,
  289. config=config)
  290. def direct_eip(self, eip, client_token=None, config=None):
  291. """
  292. turn on EIP pass through with the specific parameters.
  293. :type eip: string
  294. :param eip: the specific EIP.
  295. :type client_token: string
  296. :param client_token: if the clientToken is not specified by the user,
  297. a random String generated by default algorithm will be used.
  298. :type config: baidubce.BceClientConfiguration
  299. :param config: None
  300. :return: BceResponse
  301. """
  302. path = utils.append_uri(self._get_path(), eip)
  303. if client_token is None:
  304. client_token = self._generate_default_client_token()
  305. params = {
  306. b'direct': b'',
  307. b'clientToken': client_token
  308. }
  309. return self._send_request(http_methods.PUT, path, params=params, config=config)
  310. def undirect_eip(self, eip, client_token, config=None):
  311. """
  312. Remove the direction of an EIP from a specific instance.
  313. :type eip: string
  314. :param eip: the EIP address to undirect.
  315. :type client_token: string
  316. :param client_token: If the clientToken is not specified by the user,
  317. a random String generated by default algorithm will be used.
  318. :type config: baidubce.BceClientConfiguration
  319. :param config: None
  320. :return: BceResponse
  321. """
  322. path = utils.append_uri(self._get_path(), eip)
  323. if client_token is None:
  324. client_token = self._generate_default_client_token()
  325. params = {
  326. b'unDirect': b'',
  327. b'clientToken': client_token
  328. }
  329. return self._send_request(http_methods.PUT, path, params=params, config=config)
  330. def list_eips(self, eip=None, instance_type=None, ip_version=None, instance_id=None,
  331. status=None, marker=None, max_keys=1000, config=None):
  332. """
  333. get a list of eip owned by the authenticated user and specified
  334. conditions. we can Also get a single eip function through this
  335. interface by eip condition
  336. :type eip: string
  337. :param eip: eip address condition
  338. :type instance_type: string
  339. :param instance_type: bound instance type condition
  340. :type instance_id: string
  341. :param instance_id: bound instance id condition
  342. if query by the instanceId or instanceType condition, must provides
  343. both of them at the same time
  344. :type status: string
  345. :param status of eip condition
  346. if query by the status condition, must provides
  347. :type marker: string
  348. :param marker: The optional parameter marker specified in the original
  349. request to specify where in the results to begin listing.
  350. :type max_keys: int
  351. :param max_keys: The optional parameter to specifies the max number
  352. of list result to return. The default value is 1000.
  353. :type config: baidubce.BceClientConfiguration
  354. :param config:
  355. :return: list of eip model, for example:
  356. {
  357. "eipList": [
  358. {
  359. "name":"eip-xxxxxxx-1",
  360. "eip": "x.x.x.x",
  361. "status":"binded",
  362. "instanceType": "BCC",
  363. "instanceId": "i-xxxxxxx",
  364. "bandwidthInMbps": 5,
  365. "paymentTiming":"Prepaid",
  366. "billingMethod":"ByBandwidth",
  367. "createTime":"2016-03-08T08:13:09Z",
  368. "expireTime":"2016-04-08T08:13:09Z"
  369. },
  370. {
  371. "name":"eip-xxxxxxx-1",
  372. "eip": "x.x.x.x",
  373. "status":"binded",
  374. "instanceType": "BCC",
  375. "instanceId": "i-xxxxxxx",
  376. "bandwidthInMbps": 1,
  377. "paymentTiming":"Postpaid",
  378. "billingMethod":"ByTraffic",
  379. "createTime":"2016-03-08T08:13:09Z",
  380. "expireTime":null
  381. },
  382. ],
  383. "marker":"eip-xxxxxxx-1",
  384. "isTruncated": true,
  385. "nextMarker": "eip-DCB50387",
  386. "maxKeys": 2
  387. }
  388. """
  389. path = self._get_path()
  390. params = {}
  391. if eip is not None:
  392. params[b'eip'] = eip
  393. if instance_type is not None:
  394. params[b'instanceType'] = instance_type
  395. if instance_id is not None:
  396. params[b'instanceId'] = instance_id
  397. if status is not None and isinstance(status, EipStatus):
  398. params[b'status'] = status.value
  399. if marker is not None:
  400. params[b'marker'] = marker
  401. if max_keys is not None:
  402. params[b'maxKeys'] = max_keys
  403. if ip_version is not None:
  404. params[b'ipVersion'] = ip_version
  405. return self._send_request(http_methods.GET, path, params=params,
  406. config=config)
  407. def list_eip_recycle(self, eip=None, name=None, marker=None, max_keys=1000, config=None):
  408. """
  409. list all EIP in the recycle bin with the specific parameters.
  410. :type eip: string
  411. :param eip: eip address condition
  412. :type name: string
  413. :param name: eip name condition
  414. :type marker: string
  415. :param marker: The optional parameter marker specified in the original
  416. request to specify where in the results to begin listing.
  417. :type max_keys: int
  418. :param max_keys: The optional parameter to specifies the max number
  419. of list result to return. The default value is 1000.
  420. :type config: baidubce.BceClientConfiguration
  421. :param config: None
  422. :return: list of eip model, for example:
  423. {
  424. "eipList": [
  425. {
  426. "name":"eip-xxxxxxx-1",
  427. "eip": "x.x.x.x",
  428. "eip_id":"ip-xxxxxxxx",
  429. "status": "paused",
  430. "route_type": "BGP",
  431. "bandwidth_in_mbps": 5,
  432. "payment_timing":"Prepaid",
  433. "billing_method":"ByBandwidth",
  434. "recycle_time":"2016-03-08T08:13:09Z",
  435. "scheduled_delete_time":"2016-04-08T08:13:09Z"
  436. },
  437. {
  438. "name":"eip-xxxxxxx-2",
  439. "eip": "x.x.x.x",
  440. "eip_id":"ip-xxxxxxxx",
  441. "status": "paused",
  442. "route_type": "BGP",
  443. "bandwidth_in_mbps": 10,
  444. "payment_timing":"Postpaid",
  445. "billing_method":"ByBandwidth",
  446. "recycle_time":"2016-03-08T08:13:09Z",
  447. "scheduled_delete_time":"2016-04-08T08:13:09Z"
  448. },
  449. ],
  450. "marker":"ip-xxxxxxxx",
  451. "is_truncated": true,
  452. "max_keys": 1000
  453. }
  454. """
  455. path = utils.append_uri(self._get_path(), 'recycle')
  456. params = {}
  457. if eip is not None:
  458. params[b'eip'] = eip
  459. if name is not None:
  460. params[b'name'] = name
  461. if marker is not None:
  462. params[b'marker'] = marker
  463. if max_keys is not None:
  464. params[b'maxKeys'] = max_keys
  465. return self._send_request(http_methods.GET, path, params=params, config=config)
  466. def optional_delete_eip(self, eip, release_to_recycle=False, client_token=None, config=None):
  467. """
  468. delete an EIP with additional options.
  469. :type eip: string
  470. :param eip: the EIP address to delete.
  471. :type release_to_recycle: bool
  472. :param release_to_recycle: flag to release EIP to recycle bin.
  473. :type client_token: string
  474. :param client_token: if the clientToken is not specified by the user,
  475. a random String generated by default algorithm will be used.
  476. :type config: baidubce.BceClientConfiguration
  477. :param config:
  478. :return: BceResponse
  479. """
  480. path = utils.append_uri(self._get_path(), eip)
  481. if client_token is None:
  482. client_token = self._generate_default_client_token()
  483. params = {
  484. b'clientToken': client_token,
  485. b'releaseToRecycle': release_to_recycle
  486. }
  487. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  488. def restore_recycle_eip(self, eip, client_token=None, config=None):
  489. """
  490. restore an EIP from the recycle bin.
  491. :type eip: string
  492. :param eip: eip address to be restored from recycle bin.
  493. :type client_token: string
  494. :param client_token: if the clientToken is not specified by the user,
  495. a random String generated by default algorithm will be used.
  496. :type config: baidubce.BceClientConfiguration
  497. :param config:
  498. :return: BceResponse
  499. """
  500. path = utils.append_uri(self._get_path(), 'recycle', eip)
  501. if client_token is None:
  502. client_token = self._generate_default_client_token()
  503. params = {
  504. b'restore': b'',
  505. b'clientToken': client_token
  506. }
  507. return self._send_request(http_methods.PUT, path, params=params, config=config)
  508. def delete_recycle_eip(self, eip, client_token=None, config=None):
  509. """
  510. delete an EIP from the recycle bin.
  511. :type eip: string
  512. :param eip: eip address to be delete from recycle bin.
  513. :type client_token: string
  514. :param client_token: if the clientToken is not specified by the user,
  515. a random String generated by default algorithm will be used.
  516. :type config: baidubce.BceClientConfiguration
  517. :param config:
  518. :return: BceResponse
  519. """
  520. path = utils.append_uri(self._get_path(), 'recycle', eip)
  521. if client_token is None:
  522. client_token = self._generate_default_client_token()
  523. params = {
  524. b'clientToken': client_token
  525. }
  526. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  527. @staticmethod
  528. def _generate_default_client_token():
  529. """
  530. default client token by uuid1
  531. """
  532. return uuid.uuid1()
  533. @staticmethod
  534. def _get_path(prefix=None):
  535. """
  536. :type prefix: string
  537. :param prefix: path prefix
  538. """
  539. if prefix is None:
  540. prefix = EipClient.prefix
  541. return utils.append_uri(EipClient.version, prefix)
  542. def _merge_config(self, config):
  543. """
  544. :type config: baidubce.BceClientConfiguration
  545. :param config:
  546. :return:
  547. """
  548. if config is None:
  549. return self.config
  550. else:
  551. new_config = copy.copy(self.config)
  552. new_config.merge_non_none_values(config)
  553. return new_config
  554. def _send_request(self, http_method, path, body=None, headers=None,
  555. params=None, config=None, body_parser=None):
  556. """
  557. :param http_method:
  558. :param path:
  559. :param body:
  560. :param headers:
  561. :param params:
  562. :type config: baidubce.BceClientConfiguration
  563. :param config:
  564. :param body_parser:
  565. :return: baidubce.BceResponse
  566. """
  567. config = self._merge_config(config)
  568. if body_parser is None:
  569. body_parser = handler.parse_json
  570. if headers is None:
  571. headers = {b'Accept': b'*/*',
  572. b'Content-Type': b'application/json;charset=utf-8'}
  573. return bce_http_client.send_request(config, bce_v1_signer.sign,
  574. [handler.parse_error, body_parser],
  575. http_method, path, body, headers,
  576. params)