ddc_client.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 use this file
  5. # except in compliance with the License. You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software distributed under the
  10. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  11. # either express or implied. See the License for the specific language governing permissions
  12. # and limitations under the License.
  13. """
  14. This module provides a client class for DDC.
  15. """
  16. import copy
  17. import json
  18. from baidubce import utils
  19. from baidubce.auth import bce_v1_signer
  20. from baidubce.bce_base_client import BceBaseClient
  21. from baidubce.http import bce_http_client
  22. from baidubce.http import handler
  23. from baidubce.http import http_methods
  24. from baidubce.utils import required
  25. class DdcClient(BceBaseClient):
  26. """
  27. Ddc base sdk client
  28. """
  29. version = '/v1'
  30. prefix = '/ddc'
  31. def __init__(self, config=None):
  32. """
  33. :type config: baidubce.BceClientConfiguration
  34. :param config:
  35. """
  36. BceBaseClient.__init__(self, config)
  37. @required(instance_id=(str), db_name=(str), table_name=(str))
  38. def lazydrop_create_hard_link(self, instance_id=None, db_name=None, table_name=None, config=None):
  39. """
  40. Create hard link.
  41. :param instance_id:
  42. The id of instance
  43. :type instance_id: string
  44. :param db_name:
  45. The database name
  46. :type db_name: string
  47. :param table_name:
  48. The table name
  49. :type table_name: string
  50. :param config:
  51. :type config: baidubce.BceClientConfiguration
  52. :return:
  53. :rtype baidubce.bce_response.BceResponse
  54. """
  55. path = DdcClient.version + DdcClient.prefix\
  56. + "/instance/" + instance_id + "/database/" + db_name + "/table/link"
  57. body = {}
  58. if table_name is not None:
  59. body['tableName'] = table_name
  60. return self._send_request(http_methods.POST, path, body=json.dumps(body), config=config)
  61. @required(instance_id=(str), db_name=(str), table_name=(str))
  62. def lazydrop_delete_hard_link(self, instance_id=None, db_name=None, table_name=None, config=None):
  63. """
  64. Delete hard link.
  65. :param instance_id:
  66. The id of instance
  67. :type instance_id: string
  68. :param db_name:
  69. The database name
  70. :type db_name: string
  71. :param table_name:
  72. The table name
  73. :type table_name: string
  74. :param config:
  75. :type config: baidubce.BceClientConfiguration
  76. :return:
  77. :rtype baidubce.bce_response.BceResponse
  78. """
  79. path = DdcClient.version + DdcClient.prefix\
  80. + "/instance/" + instance_id + "/database/" + db_name + "/table/" + table_name + "/link"
  81. return self._send_request(http_methods.DELETE, path, config=config)
  82. @required(instance_id=(str), log_type=(str), datetime=(str))
  83. def list_log_by_instance_id(self, instance_id=None, log_type=None, datetime=None, config=None):
  84. """
  85. Delete hard link.
  86. :param instance_id:
  87. The id of instance
  88. :type instance_id: string
  89. :param log_type:
  90. LogType
  91. :type log_type: string
  92. :param datetime:
  93. Datetime
  94. :type datetime: string
  95. :param config:
  96. :type config: baidubce.BceClientConfiguration
  97. :return:
  98. :rtype baidubce.bce_response.BceResponse
  99. """
  100. path = '/v2' + DdcClient.prefix + '/instance/' + instance_id + '/logs'
  101. params = {}
  102. if log_type is not None:
  103. params['logType'] = log_type
  104. if datetime is not None:
  105. params['datetime'] = datetime
  106. return self._send_request(http_methods.GET, path, params=params, config=config)
  107. @required(instance_id=(str), log_id=(str))
  108. def get_log_by_id(self, instance_id=None, log_id=None, download_valid_time_in_sec=None, config=None):
  109. """
  110. Delete hard link.
  111. :param instance_id:
  112. The id of instance
  113. :type instance_id: string
  114. :param log_id:
  115. LogId
  116. :type log_id: string
  117. :param download_valid_time_in_sec:
  118. downloadValidTimeInSec
  119. :type download_valid_time_in_sec: Integer
  120. :param config:
  121. :type config: baidubce.BceClientConfiguration
  122. :return:
  123. :rtype baidubce.bce_response.BceResponse
  124. """
  125. path = DdcClient.version + DdcClient.prefix + "/instance/" + instance_id + "/logs/" + log_id
  126. params = {}
  127. if download_valid_time_in_sec is not None:
  128. params['downloadValidTimeInSec'] = download_valid_time_in_sec
  129. return self._send_request(http_methods.GET, path, params=params, config=config)
  130. @staticmethod
  131. def _get_path(prefix=None):
  132. """
  133. :type prefix: string
  134. :param prefix: path prefix
  135. """
  136. if prefix is None:
  137. prefix = DdcClient.prefix
  138. return utils.append_uri(DdcClient.version, prefix)
  139. def _merge_config(self, config):
  140. """
  141. :type config: baidubce.BceClientConfiguration
  142. :param config:
  143. :return:
  144. """
  145. if config is None:
  146. return self.config
  147. else:
  148. new_config = copy.copy(self.config)
  149. new_config.merge_non_none_values(config)
  150. return new_config
  151. def _send_request(self, http_method, path, body=None, headers=None, params=None,
  152. config=None, body_parser=None):
  153. """
  154. :param http_method:
  155. :param path:
  156. :param body:
  157. :param headers:
  158. :param params:
  159. :type config: baidubce.BceClientConfiguration
  160. :param config:
  161. :param body_parser:
  162. :return: baidubce.BceResponse
  163. """
  164. config = self._merge_config(config)
  165. if body_parser is None:
  166. body_parser = handler.parse_json
  167. if headers is None:
  168. headers = {b'Accept': b'*/*', b'Content-Type': b'application/json;charset=utf-8'}
  169. return bce_http_client.send_request(config, bce_v1_signer.sign,
  170. [handler.parse_error, body_parser],
  171. http_method, path.encode(), body, headers, params)