lbdc_client.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  6. # the License. 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, software distributed under the License is distributed on
  11. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  12. # specific language governing permissions and limitations under the License.
  13. """
  14. This module provides a client class for LBDC.
  15. """
  16. import copy
  17. import json
  18. import logging
  19. import uuid
  20. from baidubce.bce_base_client import BceBaseClient
  21. from baidubce.auth import bce_v1_signer
  22. from baidubce.http import bce_http_client
  23. from baidubce.http import handler
  24. from baidubce.http import http_methods
  25. from baidubce import compat
  26. _logger = logging.getLogger(__name__)
  27. class LbdcClient(BceBaseClient):
  28. """
  29. LBDC base sdk client
  30. """
  31. prefix = b'/v1'
  32. path = b'/lbdc'
  33. def __init__(self, config=None):
  34. """
  35. :param config: client config info
  36. reuse BceBaseClient init func
  37. """
  38. BceBaseClient.__init__(self, config)
  39. @staticmethod
  40. def _generate_default_client_token():
  41. """
  42. default client token by uuid1
  43. """
  44. return uuid.uuid1()
  45. def _merge_config(self, config):
  46. """
  47. :type config: baidubce.BceClientConfiguration
  48. :param config:
  49. :return:
  50. """
  51. if config is None:
  52. return self.config
  53. else:
  54. new_config = copy.copy(self.config)
  55. new_config.merge_non_none_values(config)
  56. return new_config
  57. def _send_request(self, http_method, path, body=None, headers=None,
  58. params=None, config=None, body_parser=None):
  59. """
  60. :param http_method:
  61. :param path:
  62. :param body:
  63. :param headers:
  64. :param params:
  65. :type config: baidubce.BceClientConfiguration
  66. :param config:
  67. :param body_parser:
  68. :return: baidubce.BceResponse
  69. """
  70. config = self._merge_config(config)
  71. if body_parser is None:
  72. body_parser = handler.parse_json
  73. if headers is None:
  74. headers = {b'Accept': b'*/*',
  75. b'Content-Type': b'application/json;charset=utf-8'}
  76. return bce_http_client.send_request(config, bce_v1_signer.sign,
  77. [handler.parse_error, body_parser],
  78. http_method, LbdcClient.prefix + path, body, headers,
  79. params)
  80. def create_lbdc(self, name, type, ccu_count, billing, desc=None, renew=None, client_token=None, config=None):
  81. """
  82. The method of lbdc to be created.
  83. :param name:
  84. name
  85. :type name: str
  86. :param type:
  87. lbdc type
  88. :type type: str
  89. :param ccu_count:
  90. count of Cluster Capacity Unit
  91. :type ccu_count: int
  92. :param billing:
  93. order_configuration
  94. :type billing:Billing
  95. :param desc:
  96. description of the cluster
  97. :type desc: string
  98. :param renew:
  99. reservation info of order_configuration
  100. :type renew:Billing.reservation
  101. :param client_token:
  102. An ASCII string whose length is less than 64.
  103. The request will be idempotent if clientToken is provided.
  104. If the clientToken is not specified by the user, a random String
  105. generated by default algorithm will be used.
  106. :type client_token: string
  107. :param config:
  108. :type config: baidubce.BceClientConfiguration
  109. :return:
  110. :rtype baidubce.bce_response.BceResponse
  111. """
  112. if client_token is None:
  113. client_token = self._generate_default_client_token()
  114. params = {
  115. b'clientToken': client_token
  116. }
  117. body = {
  118. b'name': name,
  119. b'type': type,
  120. b'ccuCount': ccu_count,
  121. b'billing': {
  122. b'paymentTiming': billing.payment_timing,
  123. b'reservation': {
  124. b'reservationLength': billing.reservation_length
  125. }
  126. }
  127. }
  128. if desc is not None:
  129. body[b'desc'] = desc
  130. if renew is not None:
  131. body[b'renewReservation'] = {
  132. b'reservationLength': billing.reservation_length
  133. }
  134. return self._send_request(http_methods.POST, LbdcClient.path, body=json.dumps(body), params=params,
  135. config=config)
  136. def upgrade_lbdc(self, lbdc_id, ccu_count, action=b'resize', client_token=None, config=None):
  137. """
  138. The method of lbdc to be upgrade.
  139. :param lbdc_id: lbdc id
  140. :type lbdc_id: str
  141. :param ccu_count: count of Cluster Capacity Unit
  142. :type ccu_count: int
  143. :param action: action to upgrade lbdc
  144. :type action: str
  145. :param client_token:
  146. An ASCII string whose length is less than 64.
  147. The request will be idempotent if clientToken is provided.
  148. If the clientToken is not specified by the user, a random String
  149. generated by default algorithm will be used.
  150. :type client_token: string
  151. :param config:
  152. :type config: baidubce.BceClientConfiguration
  153. :return:
  154. :rtype baidubce.bce_response.BceResponse
  155. """
  156. path = LbdcClient.path + b'/' + compat.convert_to_bytes(lbdc_id)
  157. if client_token is None:
  158. client_token = self._generate_default_client_token()
  159. params = {
  160. b'clientToken': client_token,
  161. action: '',
  162. }
  163. body = {
  164. b'ccuCount': ccu_count,
  165. }
  166. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
  167. config=config)
  168. def renew_lbdc(self, lbdc_id, billing, action=b'purchaseReserved', client_token=None, config=None):
  169. """
  170. renew lbdc
  171. :param lbdc_id:
  172. The id of lbdc.
  173. :type lbdc_id: str
  174. :param billing: order_configuration
  175. :type billing:Billing
  176. :param action: action to update lbdc
  177. :type action: str
  178. :param client_token:
  179. An ASCII string whose length is less than 64.
  180. The request will be idempotent if clientToken is provided.
  181. If the clientToken is not specified by the user, a random String generated by default algorithm will
  182. be used.
  183. :type client_token: string
  184. :param config:
  185. :type config: baidubce.BceClientConfiguration
  186. :return:
  187. :rtype baidubce.bce_response.BceResponse
  188. """
  189. path = LbdcClient.path + b'/' + compat.convert_to_bytes(lbdc_id)
  190. if client_token is None:
  191. client_token = self._generate_default_client_token()
  192. params = {
  193. b'clientToken': client_token,
  194. action: '',
  195. }
  196. body = {
  197. b'billing': {
  198. b'reservation': {
  199. b'reservationLength': billing.reservation_length
  200. }
  201. }
  202. }
  203. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
  204. config=config)
  205. def list_lbdc(self, lbdc_id=None, name=None, config=None):
  206. """
  207. return all lbdc list about query info
  208. :param lbdc_id: lbdc id
  209. :type lbdc_id: str
  210. :param name: lbdc name
  211. :type name: str
  212. :param config:
  213. :type config: baidubce.BceClientConfiguration
  214. :return:
  215. :rtype baidubce.bce_response.BceResponse
  216. """
  217. params = {}
  218. if lbdc_id is not None:
  219. params[b'id'] = lbdc_id
  220. if name is not None:
  221. params[b'name'] = name
  222. return self._send_request(http_methods.GET, LbdcClient.path, params=params, config=config)
  223. def get_lbdc(self, lbdc_id, config=None):
  224. """
  225. Get the detail information of lbdc.
  226. :param lbdc_id:
  227. The id of lbdc.
  228. :type lbdc_id: str
  229. :param config:
  230. :type config: baidubce.BceClientConfiguration
  231. :return:
  232. :rtype baidubce.bce_response.BceResponse
  233. """
  234. path = LbdcClient.path + b'/' + compat.convert_to_bytes(lbdc_id)
  235. return self._send_request(http_methods.GET, path, config=config)
  236. def update_lbdc(self, lbdc_id, name=None, desc=None, client_token=None, config=None):
  237. """
  238. renew lbdc
  239. :param lbdc_id:
  240. The id of lbdc.
  241. :type lbdc_id: string
  242. :param name:
  243. The name of lbdc
  244. :type name: str
  245. :param desc: description
  246. :type desc: str
  247. :param client_token:
  248. An ASCII string whose length is less than 64.
  249. The request will be idempotent if clientToken is provided.
  250. If the clientToken is not specified by the user, a random String generated by default algorithm will
  251. be used.
  252. :type client_token: string
  253. :param config:
  254. :type config: baidubce.BceClientConfiguration
  255. :return:
  256. :rtype baidubce.bce_response.BceResponse
  257. """
  258. path = LbdcClient.path + b'/' + compat.convert_to_bytes(lbdc_id)
  259. if client_token is None:
  260. client_token = self._generate_default_client_token()
  261. params = {
  262. b'clientToken': client_token
  263. }
  264. body = {}
  265. if name is not None:
  266. body[b'name'] = name
  267. if desc is not None:
  268. body[b'desc'] = desc
  269. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
  270. config=config)
  271. def list_lbdc_blb(self, lbdc_id, config=None):
  272. """
  273. return all lbdc list about query info
  274. :param lbdc_id: lbdc id
  275. :type lbdc_id:string
  276. :param config:
  277. :type config: baidubce.BceClientConfiguration
  278. :return:
  279. :rtype baidubce.bce_response.BceResponse
  280. """
  281. params = {}
  282. if lbdc_id is not None:
  283. params[b'id'] = lbdc_id
  284. return self._send_request(http_methods.GET, LbdcClient.path, params=params, config=config)