eip_tp_client.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2021 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 TP.
  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 EipTpClient(BceBaseClient):
  31. """
  32. EIP_TP sdk client
  33. """
  34. version = b'/v1'
  35. prefix = b'/eiptp'
  36. def __init__(self, config=None):
  37. """
  38. :type config: baidubce.BceClientConfiguration
  39. :param config:
  40. """
  41. BceBaseClient.__init__(self, config)
  42. @required(reservation_length=int,
  43. capacity=str)
  44. def create_eip_tp(self, reservation_length, capacity, deduct_policy=None, package_type=None,
  45. client_token=None, config=None):
  46. """
  47. Create an eip_tp with the specified options.
  48. :type reservation_length: int
  49. :param reservation_length: the reservation length of the eip_tp including 1, 6 and 12 months.
  50. :type capacity: string
  51. :param capacity: the capacity of the eip_tp.
  52. When reservationLength = 1 => capacity: {"10G"/"50G"/"100G"/"500G"/"1T"/"5T"/"10T"/"50T"}
  53. When reservationLength = 6 => capacity: {"60G"/"300G"/"600G"/"3T"/"6T"/"30T"/"60T"/"300T"}
  54. When reservationLength = 12 => capacity: {"1T"/"10T"/"50T"/"100T"/"500T"/"1P"}
  55. :type deduct_policy: string
  56. :param deduct_policy: the deduct policy of the eip_tp including 'FullTimeDurationPackage'
  57. and 'TimeDurationPackage'.
  58. The default deduct policy is 'FullTimeDurationPackage', the optional parameter.
  59. :type package_type: string
  60. :param package_type: the eip_tp package type.
  61. The default package type is 'WebOutBytes', the optional parameter.
  62. :type client_token: string
  63. :param client_token: if the clientToken is not specified by the user,
  64. a random string generated by default algorithm will be used.
  65. :type config: baidubce.BceClientConfiguration
  66. :param config:
  67. :return: created eip_tp id, for example,{"id":"tp-xxxxxxxxxx"}
  68. """
  69. body = {
  70. 'reservationLength': reservation_length,
  71. 'capacity': capacity
  72. }
  73. if deduct_policy is not None:
  74. body['deductPolicy'] = deduct_policy
  75. if package_type is not None:
  76. body['packageType'] = package_type
  77. path = self._get_path()
  78. if client_token is None:
  79. client_token = self._generate_default_client_token()
  80. params = {
  81. b'clientToken': client_token
  82. }
  83. return self._send_request(http_methods.POST, path,
  84. body=json.dumps(body), params=params,
  85. config=config)
  86. @required(id=str)
  87. def get_eip_tp_detail(self, id, config=None):
  88. """
  89. get the eip_tp's detail owned by the authenticated user by the passed eip_tp_id.
  90. :type id: string
  91. :param id: eip_tp's id.
  92. :type config: baidubce.BceClientConfiguration
  93. :param config:
  94. :return: detail of eip_tp, for example:
  95. {
  96. "id":"tp-87V5cnkwqO",
  97. "deductPolicy":"TimeDurationPackage",
  98. "packageType":"WebOutBytes",
  99. "status":"RUNNING",
  100. "capacity": 10737418240,
  101. "usedCapacity": 0,
  102. "createTime":"2021-04-10T11:40:57Z",
  103. "activeTime": "2021-04-10T11:41:16Z",
  104. "expireTime:" "2021-05-10T11:41:16Z"
  105. }
  106. """
  107. path = utils.append_uri(self._get_path(), id)
  108. return self._send_request(http_methods.GET, path, params=None,
  109. config=config)
  110. def list_eip_tps(self, id=None, deduct_policy=None, status=None,
  111. marker=None, max_keys=1000, config=None):
  112. """
  113. get a list of eip_tp owned by the authenticated filtered by specific conditions.
  114. :type id: string
  115. :param id: eip_tp's id, the optional parameter.
  116. :type deduct_policy: string
  117. :param deduct_policy: eip_tp's deduct policy, 'FullTimeDurationPackage' or 'TimeDurationPackage', the optional parameter.
  118. :type status: string
  119. :param status: eip_tp's status, 'RUNNING', 'EXPIRED' or 'USED_UP', the optional parameter.
  120. :type marker: string
  121. :param marker: The optional parameter marker specified in the original
  122. request to specify where in the results to begin listing.
  123. :type max_keys: int
  124. :param max_keys: The optional parameter to specifies the max number
  125. of list result to return. The default value is 1000.
  126. :type config: baidubce.BceClientConfiguration
  127. :param config:
  128. :return: list of eip_tp model, for example:
  129. {
  130. "marker": "tp-87V5cnkwqO",
  131. "maxKeys": 1,
  132. "nextMarker": "tp-Qn65tYXAx3",
  133. "isTruncated": true,
  134. "packageList": [
  135. {
  136. "id":"tp-87V5cnkwqO",
  137. "deductPolicy":"TimeDurationPackage",
  138. "packageType":"WebOutBytes",
  139. "status":"RUNNING",
  140. "capacity": 10737418240,
  141. "usedCapacity": 0,
  142. "createTime":"2021-04-10T11:40:57Z",
  143. "activeTime": "2021-04-10T11:41:16Z",
  144. "expireTime:" "2021-05-10T11:41:16Z"
  145. }
  146. ]
  147. }
  148. """
  149. path = self._get_path()
  150. params = {}
  151. if id is not None:
  152. params[b'id'] = id
  153. if deduct_policy is not None:
  154. params[b'deductPolicy'] = deduct_policy
  155. if status is not None:
  156. params[b'status'] = status
  157. if marker is not None:
  158. params[b'marker'] = marker
  159. if max_keys is not None:
  160. params[b'maxKeys'] = max_keys
  161. return self._send_request(http_methods.GET, path, params=params,
  162. config=config)
  163. @staticmethod
  164. def _generate_default_client_token():
  165. """
  166. default client token by uuid1
  167. """
  168. return uuid.uuid1()
  169. @staticmethod
  170. def _get_path(prefix=None):
  171. """
  172. :type prefix: string
  173. :param prefix: path prefix
  174. """
  175. if prefix is None:
  176. prefix = EipTpClient.prefix
  177. return utils.append_uri(EipTpClient.version, prefix)
  178. def _merge_config(self, config):
  179. """
  180. :type config: baidubce.BceClientConfiguration
  181. :param config:
  182. :return:
  183. """
  184. if config is None:
  185. return self.config
  186. else:
  187. new_config = copy.copy(self.config)
  188. new_config.merge_non_none_values(config)
  189. return new_config
  190. def _send_request(self, http_method, path, body=None, headers=None,
  191. params=None, config=None, body_parser=None):
  192. """
  193. :param http_method:
  194. :param path:
  195. :param body:
  196. :param headers:
  197. :param params:
  198. :type config: baidubce.BceClientConfiguration
  199. :param config:
  200. :param body_parser:
  201. :return: baidubce.BceResponse
  202. """
  203. config = self._merge_config(config)
  204. if body_parser is None:
  205. body_parser = handler.parse_json
  206. if headers is None:
  207. headers = {b'Accept': b'*/*',
  208. b'Content-Type': b'application/json;charset=utf-8'}
  209. return bce_http_client.send_request(config, bce_v1_signer.sign,
  210. [handler.parse_error, body_parser],
  211. http_method, path, body, headers,
  212. params)