tsdb_admin_client.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright 2014 Baidu, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides a client class for TSDB.
  14. """
  15. import copy
  16. import json
  17. import logging
  18. from baidubce.auth import bce_v1_signer
  19. from baidubce.bce_base_client import BceBaseClient
  20. from baidubce.http import bce_http_client
  21. from baidubce.http import handler
  22. from baidubce.http import http_content_types
  23. from baidubce.http import http_headers
  24. from baidubce.http import http_methods
  25. from baidubce.services.tsdb import tsdb_handler
  26. _logger = logging.getLogger(__name__)
  27. class TsdbAdminClient(BceBaseClient):
  28. """
  29. sdk client
  30. """
  31. def __init__(self, config=None):
  32. BceBaseClient.__init__(self, config)
  33. def create_database(
  34. self,
  35. client_token,
  36. database_name,
  37. ingest_datapoints_monthly,
  38. purchase_length,
  39. description=None,
  40. store_bytes_quota=None,
  41. coupon_name=None):
  42. """
  43. create_database
  44. :param client_token: a unique id for idempotence
  45. :type client_token: string
  46. :param database_name: name of database
  47. :type database_name: string
  48. :param description: optional, description for database
  49. :type description: string
  50. :param ingest_datapoints_monthly: max ingest datapoints count per month,unit Million
  51. :type ingest_datapoints_monthly: int
  52. :param store_bytes_quota: optional, unit GB
  53. :type store_bytes_quota: int
  54. :param purchase_length: purchase length, unit Month
  55. :type purchase_length: int
  56. :param coupon_name: optional, coupon number
  57. :type coupon_name: type
  58. :return: {database_id:,charge:,expired_time:order_id:}
  59. :rtype: baidubce.bce_response.BceResponse
  60. """
  61. path = b"/v1/database"
  62. params = {"clientToken": client_token}
  63. body = json.dumps({
  64. "databaseName": database_name.decode(),
  65. "description": description.decode(),
  66. "ingestDataPointsMonthly": ingest_datapoints_monthly,
  67. "storeBytesQuota": store_bytes_quota,
  68. "purchaseLength": purchase_length,
  69. "couponName": coupon_name.decode()
  70. }).encode('utf-8')
  71. return self._send_request(http_methods.POST, path=path, body=body,
  72. params=params, body_parser=tsdb_handler.parse_json)
  73. def delete_database(self, database_id):
  74. """
  75. delete database
  76. :param database_id: database id to delete
  77. :type database_id: string
  78. :return: bce_request_id
  79. :rtype: baidubce.bce_response.BceResponses
  80. """
  81. path = b'/v1/database/' + database_id
  82. return self._send_request(http_methods.DELETE, path, body_parser=tsdb_handler.parse_json)
  83. def get_database(self, database_id):
  84. """
  85. get database
  86. :param database_id: database id to delete
  87. :type database_id: string
  88. :return: database info
  89. :rtype: baidubce.bce_response.BceResponse
  90. """
  91. path = b'/v1/database/' + database_id
  92. return self._send_request(http_methods.GET, path, body_parser=tsdb_handler.parse_json)
  93. def get_all_databases(self):
  94. """
  95. get all databases
  96. :return: database dict
  97. :rtype: baidubce.bce_response.BceResponse
  98. """
  99. path = b'/v1/database'
  100. return self._send_request(http_methods.GET, path, body_parser=tsdb_handler.parse_json)
  101. def _merge_config(self, config):
  102. if config is None:
  103. return self.config
  104. else:
  105. new_config = copy.copy(self.config)
  106. new_config.merge_non_none_values(config)
  107. return new_config
  108. def _send_request(
  109. self, http_method, path,
  110. body=None,
  111. params=None,
  112. headers=None,
  113. config=None,
  114. body_parser=None):
  115. config = self._merge_config(config)
  116. if headers is None:
  117. headers = {http_headers.CONTENT_TYPE: http_content_types.JSON}
  118. if body_parser is None:
  119. body_parser = handler.parse_json
  120. return bce_http_client.send_request(
  121. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  122. http_method, path, body, headers, params)