eip_bp_client.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2020 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.utils import required
  29. _logger = logging.getLogger(__name__)
  30. class EipBpClient(BceBaseClient):
  31. """
  32. EIP_BP sdk client
  33. """
  34. version = b'/v1'
  35. prefix = b'/eipbp'
  36. def __init__(self, config=None):
  37. """
  38. :type config: baidubce.BceClientConfiguration
  39. :param config:
  40. """
  41. BceBaseClient.__init__(self, config)
  42. @required(bandwidth_in_mbps=int)
  43. def create_eip_bp(self, eip, eip_group_Id, bandwidth_in_mbps, name=None, autoReleaseTime=None,
  44. client_token=None, config=None):
  45. """
  46. Create an eip_bp with the specified options.
  47. :type eip: string
  48. :param eip: the eip address that the eip_bp attach.
  49. Param "eip" and "eip_group_Id" will has only one param take effect.
  50. :type eip_group_Id: string
  51. :param eip_group_Id: the eipgroupId that the eip_bp attach.
  52. Param "eip" and "eip_group_Id" will has only one param take effect.
  53. :type bandwidth_in_mbps: int
  54. :param bandwidth_in_mbps: the bandwidth of eip_bp in Mbps.
  55. :type name: string
  56. :param name:the name of eipbp. The optional parameter.
  57. :type auto_release_time: string (UTC format,like yyyy:mm:ddThh:mm:ssZ)
  58. :param autoReleaseTime: the autoReleaseTime of eipbp. The optional parameter.
  59. :type client_token: string
  60. :param client_token: if the clientToken is not specified by the user,
  61. a random string generated by default algorithm will be used.
  62. :type config: baidubce.BceClientConfiguration
  63. :param config:
  64. :return: created eip_bp id, for example,{"id":"bw-xxxxxxxx"}
  65. """
  66. body = {
  67. 'bandwidthInMbps': bandwidth_in_mbps
  68. }
  69. if eip is None and eip_group_Id is None:
  70. raise ValueError(b"eip and eip_group_Id can't be none at the same time.")
  71. if eip is not None:
  72. body['eip'] = eip
  73. if eip_group_Id is not None:
  74. body['eipGroupId'] = eip_group_Id
  75. if name is not None:
  76. body['name'] = name
  77. if autoReleaseTime is not None:
  78. body['autoReleaseTime'] = autoReleaseTime
  79. path = self._get_path()
  80. if client_token is None:
  81. client_token = self._generate_default_client_token()
  82. params = {
  83. b'clientToken': client_token
  84. }
  85. return self._send_request(http_methods.POST, path,
  86. body=json.dumps(body), params=params,
  87. config=config)
  88. @required(id=str, new_bandwidth_in_mbps=int)
  89. def resize_eip_bp(self, id, new_bandwidth_in_mbps, client_token=None,
  90. config=None):
  91. """
  92. Resizing eip_bp
  93. :type id: string
  94. :param id: eip_bp's id to be resized
  95. :type new_bandwidth_in_mbps: int
  96. :param new_bandwidth_in_mbps: specify new bandwidth in Mbps for eip_bp
  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. 'bandwidthInMbps': new_bandwidth_in_mbps
  106. }
  107. path = utils.append_uri(self._get_path(), id)
  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. @required(id=str)
  117. def get_eip_bp_detail(self, id, config=None):
  118. """
  119. get eip_bp's detail owned by the authenticated user and givened eip_bp_id.
  120. :type id: string
  121. :param eip: eip_bp's id.
  122. :type config: baidubce.BceClientConfiguration
  123. :param config:
  124. :return: detail of eip_bp, for example:
  125. {
  126. "autoReleaseTime": "2020-05-30T06:46:44Z",
  127. "name": "EIP_BP1588821183401",
  128. "instanceId": "ip-9340430e",
  129. "createTime": "2020-05-07T03:13:03Z",
  130. "id": "bw-5fb3ce39",
  131. "eips": [
  132. "100.88.9.120"
  133. ],
  134. "instanceBandwidthInMbps": 1,
  135. "bandwidthInMbps": 2,
  136. "bindType": "eip"
  137. }
  138. """
  139. path = utils.append_uri(self._get_path(), id)
  140. return self._send_request(http_methods.GET, path, params=None,
  141. config=config)
  142. def list_eip_bps(self, id=None, name=None, bind_type=None,
  143. marker=None, max_keys=1000, config=None):
  144. """
  145. get a list of eip_bp owned by the authenticated user and specified
  146. conditions. we can Also get a single eip_bp function through this
  147. interface by eip_bp condition
  148. :type id: string
  149. :param id: eip_bp 's id conditions
  150. :type name: string
  151. :param name: eip_bp 's name condition
  152. :type bind_type: string
  153. :param bind_type: eip_bp 's bind_type condition, 'eip' or 'eipgroup'
  154. :type marker: string
  155. :param marker: The optional parameter marker specified in the original
  156. request to specify where in the results to begin listing.
  157. :type max_keys: int
  158. :param max_keys: The optional parameter to specifies the max number
  159. of list result to return. The default value is 1000.
  160. :type config: baidubce.BceClientConfiguration
  161. :param config:
  162. :return: list of eip_bp model, for example:
  163. {
  164. "marker": "bw-5fb3ce39",
  165. "maxKeys": 1000,
  166. "nextMarker": null,
  167. "bpList": [
  168. {
  169. "autoReleaseTime": "2020-05-30T06:46:44Z",
  170. "name": "EIP_BP1588821183401",
  171. "instanceId": "ip-9340430e",
  172. "createTime": "2020-05-07T03:13:03Z",
  173. "id": "bw-5fb3ce39",
  174. "eips": [
  175. "100.88.9.120"
  176. ],
  177. "bandwidthInMbps": 2,
  178. "bindType": "eip"
  179. }
  180. ],
  181. "isTruncated": false
  182. }
  183. """
  184. path = self._get_path()
  185. params = {}
  186. if id is not None:
  187. params[b'id'] = id
  188. if name is not None:
  189. params[b'name'] = name
  190. if bind_type is not None:
  191. params[b'bindType'] = bind_type
  192. if marker is not None:
  193. params[b'marker'] = marker
  194. if max_keys is not None:
  195. params[b'maxKeys'] = max_keys
  196. return self._send_request(http_methods.GET, path, params=params,
  197. config=config)
  198. @required(id=str, auto_release_time=str)
  199. def update_eip_bp_autoReleaseTime(self, id, auto_release_time, client_token=None,
  200. config=None):
  201. """
  202. Update eip_bp's auto_release_time
  203. :type id: string
  204. :param id: eip_bp's id
  205. :type auto_release_time: string (UTC format,like yyyy:mm:ddThh:mm:ssZ)
  206. :param auto_release_time: specify auto_release_time for eip_bp
  207. :type client_token: string
  208. :param client_token: if the clientToken is not specified by the user,
  209. a random string generated by default algorithm will be used.
  210. :type config: baidubce.BceClientConfiguration
  211. :param config:
  212. :return: BceResponse
  213. """
  214. body = {
  215. 'autoReleaseTime': auto_release_time
  216. }
  217. path = utils.append_uri(self._get_path(), id)
  218. if client_token is None:
  219. client_token = self._generate_default_client_token()
  220. params = {
  221. b'retime': b'',
  222. b'clientToken': client_token
  223. }
  224. return self._send_request(http_methods.PUT, path, params=params,
  225. body=json.dumps(body), config=config)
  226. @required(id=str, name=str)
  227. def rename_eip_bp(self, id, name, client_token=None,
  228. config=None):
  229. """
  230. Update eip_bp's name
  231. :type id: string
  232. :param id: eip_bp's id
  233. :type name: string
  234. :param name: eip_bp's name
  235. :type client_token: string
  236. :param client_token: if the clientToken is not specified by the user,
  237. a random string generated by default algorithm will be used.
  238. :type config: baidubce.BceClientConfiguration
  239. :param config:
  240. :return: BceResponse
  241. """
  242. body = {
  243. 'name': name
  244. }
  245. path = utils.append_uri(self._get_path(), id)
  246. if client_token is None:
  247. client_token = self._generate_default_client_token()
  248. params = {
  249. b'rename': b'',
  250. b'clientToken': client_token
  251. }
  252. return self._send_request(http_methods.PUT, path, params=params,
  253. body=json.dumps(body), config=config)
  254. @required(id=str)
  255. def release_eip_bp(self, id, client_token=None, config=None):
  256. """
  257. release the eip_bp(delete operation)
  258. :type id: string
  259. :param id: eip_bp's id to be released
  260. :type client_token: string
  261. :param client_token: if the clientToken is not specified by the user,
  262. a random string generated by default algorithm will be used.
  263. :type config: baidubce.BceClientConfiguration
  264. :param config:
  265. :return: BceResponse
  266. """
  267. path = utils.append_uri(self._get_path(), id)
  268. if client_token is None:
  269. client_token = self._generate_default_client_token()
  270. params = {
  271. b'clientToken': client_token
  272. }
  273. return self._send_request(http_methods.DELETE, path, params=params,
  274. config=config)
  275. @staticmethod
  276. def _generate_default_client_token():
  277. """
  278. default client token by uuid1
  279. """
  280. return uuid.uuid1()
  281. @staticmethod
  282. def _get_path(prefix=None):
  283. """
  284. :type prefix: string
  285. :param prefix: path prefix
  286. """
  287. if prefix is None:
  288. prefix = EipBpClient.prefix
  289. return utils.append_uri(EipBpClient.version, prefix)
  290. def _merge_config(self, config):
  291. """
  292. :type config: baidubce.BceClientConfiguration
  293. :param config:
  294. :return:
  295. """
  296. if config is None:
  297. return self.config
  298. else:
  299. new_config = copy.copy(self.config)
  300. new_config.merge_non_none_values(config)
  301. return new_config
  302. def _send_request(self, http_method, path, body=None, headers=None,
  303. params=None, config=None, body_parser=None):
  304. """
  305. :param http_method:
  306. :param path:
  307. :param body:
  308. :param headers:
  309. :param params:
  310. :type config: baidubce.BceClientConfiguration
  311. :param config:
  312. :param body_parser:
  313. :return: baidubce.BceResponse
  314. """
  315. config = self._merge_config(config)
  316. if body_parser is None:
  317. body_parser = handler.parse_json
  318. if headers is None:
  319. headers = {b'Accept': b'*/*',
  320. b'Content-Type': b'application/json;charset=utf-8'}
  321. return bce_http_client.send_request(config, bce_v1_signer.sign,
  322. [handler.parse_error, body_parser],
  323. http_method, path, body, headers,
  324. params)