model.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #! usr/bin/python
  2. # -*-coding:utf-8 -*-
  3. # Copyright 2014 Baidu, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  6. # except in compliance with 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
  11. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. # either express or implied. See the License for the specific language governing permissions
  13. # and limitations under the License.
  14. """
  15. This module defines some Response classes for BTS
  16. """
  17. from baidubce.bce_response import BceResponse
  18. from json import JSONEncoder
  19. class Billing(object):
  20. """
  21. This class define billing.
  22. param: paymentTiming:
  23. The pay time of the payment,
  24. param: reservationLength:
  25. The duration to buy in specified time unit,
  26. param: reservationTimeUnit:
  27. The time unit to specify the duration ,only "Month" can be used now.
  28. """
  29. def __init__(self, pay_method='Prepaid', reservationLength=1, reservationTimeUnit='Month'):
  30. self.paymentTiming = pay_method
  31. self.reservation = {
  32. 'reservationLength': reservationLength,
  33. 'reservationTimeUnit': reservationTimeUnit
  34. }
  35. def get_pay_method(self):
  36. """
  37. get instance current pay_method:Prepaid/Postpaid
  38. """
  39. return self.paymentTiming
  40. class SubnetMap(object):
  41. """
  42. SubnetMap:contains zoneName and subnetId
  43. """
  44. def __init__(self, zone_name, subnet_id):
  45. super(SubnetMap, self).__init__()
  46. self.zone_name = str(zone_name)
  47. self.subnet_id = str(subnet_id)
  48. class CreateInstanceResponse(BceResponse):
  49. """
  50. Create Instance Response
  51. """
  52. def __init__(self, bce_response):
  53. super(CreateInstanceResponse, self).__init__()
  54. self.instance_ids = bce_response.instance_ids
  55. self.order_id = bce_response.order_id
  56. class ListInstanceResponse(BceResponse):
  57. """
  58. List Instance Response
  59. """
  60. def __init__(self, bce_response):
  61. super(ListInstanceResponse, self).__init__()
  62. self.max_keys = bce_response.max_keys
  63. self.marker = str(bce_response.marker)
  64. self.next_marker = str(bce_response.next_marker)
  65. self.is_truncated = bce_response.is_truncated
  66. self.instances = bce_response.instances
  67. class GetInstanceResponse(BceResponse):
  68. """
  69. Get Instance Response
  70. """
  71. def __init__(self, bce_response):
  72. super(GetInstanceResponse, self).__init__()
  73. self.instance_id = str(bce_response.instance_id)
  74. self.instance_name = str(bce_response.instance_name)
  75. self.instance_status = str(bce_response.instance_status)
  76. self.cluster_type = str(bce_response.cluster_type)
  77. self.engine = str(bce_response.engine)
  78. self.engine_version = str(bce_response.engine_version)
  79. self.vnet_ip = str(bce_response.vnet_ip)
  80. self.port = str(bce_response.port)
  81. self.instance_create_time = bce_response.instance_create_time
  82. self.instance_expire_time = bce_response.instance_expire_time
  83. self.capacity = bce_response.capacity
  84. self.used_capacity = bce_response.used_capacity
  85. self.payment_timing = str(bce_response.payment_timing)
  86. self.zone_names = bce_response.zone_names
  87. self.vpc_id = str(bce_response.vpc_id)
  88. self.subnets = bce_response.subnets
  89. self.auto_renew = bce_response.auto_renew
  90. self.shard_num = bce_response.shard_num
  91. self.store_type = bce_response.store_type
  92. class ListAvailableZoneResponse(BceResponse):
  93. """
  94. List available zone.
  95. """
  96. def __init__(self, bce_response):
  97. super(ListAvailableZoneResponse, self).__init__()
  98. self.zones = bce_response.zones
  99. class ListSubnetResponse(BceResponse):
  100. """
  101. List available zone.
  102. """
  103. def __init__(self, bce_response):
  104. super(ListSubnetResponse, self).__init__()
  105. self.subnets = bce_response.subnets
  106. if self.subnets is None:
  107. self.subnets = []
  108. class ListNodeTypeResponse(BceResponse):
  109. """
  110. List nodetypes for scs.
  111. """
  112. def __init__(self, bce_response):
  113. super(ListNodeTypeResponse, self).__init__()
  114. self.default_node_type_list = bce_response.default_node_type_list
  115. self.cluster_node_type_list = bce_response.cluster_node_type_list
  116. self.hsdb_node_type_list = bce_response.hsdb_node_type_list
  117. class NodeType(object):
  118. """
  119. NodeType model
  120. """
  121. def __init__(self, obj):
  122. super(NodeType, self).__init__()
  123. self.node_type = str(obj.node_type)
  124. self.cpu_num = int(obj.cpu_num)
  125. self.instance_flavor = int(obj.instance_flavor)
  126. self.network_throughput_in_gbps = float(obj.network_throughput_in_gbps)
  127. self.peak_qps = int(obj.peak_qps)
  128. self.max_connections = int(obj.max_connections)
  129. self.allowed_node_num_list = obj.allowed_node_num_list
  130. self.allowed_replication_num_list = obj.allowed_replication_num_list
  131. class Tag(object):
  132. """
  133. Tag model
  134. """
  135. def __init__(self, key, value):
  136. super(Tag, self).__init__()
  137. self.tag_key = str(key)
  138. self.tag_value = str(value)
  139. def __repr__(self):
  140. return repr((self.tag_key, self.tag_value))
  141. class ListSecurityIpResponse(BceResponse):
  142. """
  143. List IP whitelist of instances which allow access to
  144. """
  145. def __init__(self, bce_response):
  146. super(ListSecurityIpResponse, self).__init__()
  147. self.security_ips = bce_response.security_ips
  148. class ListParameterResponse(BceResponse):
  149. """
  150. List configuration parameters and runtime parameters of scs instance
  151. """
  152. def __init__(self, bce_response):
  153. super(ListParameterResponse, self).__init__()
  154. self.parameters = bce_response.parameters
  155. class Parameter(object):
  156. """
  157. Configuration parameters and runtime parameters of scs instance
  158. """
  159. def __init__(self, obj):
  160. super(Parameter, self).__init__()
  161. self.default = str(obj.default)
  162. self.force_restart = int(obj.force_restart)
  163. self.name = str(obj.name)
  164. self.value = str(obj.value)
  165. class ModifyParameterRequest(object):
  166. """
  167. A request model to modify instance parameter
  168. """
  169. def __init__(self, obj):
  170. super(ModifyParameterRequest, self).__init__()
  171. self.name = str(obj.name)
  172. self.value = str(obj.value)
  173. class ListBackupResponse(BceResponse):
  174. """
  175. List backups of instance.
  176. """
  177. def __init__(self, bce_response):
  178. super(ListBackupResponse, self).__init__()
  179. self.total_count = bce_response.total_count
  180. self.backups = bce_response.backups
  181. class Backup(object):
  182. """
  183. Backup model
  184. """
  185. def __init__(self, obj):
  186. super(Backup, self).__init__()
  187. self.backup_type = str(obj.backup_type)
  188. self.comment = str(obj.comment)
  189. self.records = obj.records
  190. self.start_time = str(obj.start_time)
  191. class BackupRecord(object):
  192. """
  193. Backup record model
  194. """
  195. def __init__(self, obj):
  196. super(BackupRecord, self).__init__()
  197. self.backup_record_id = str(obj.backup_record_id)
  198. self.backup_status = str(obj.backup_status)
  199. self.duration = int(obj.duration)
  200. self.object_size = int(obj.object_size)
  201. self.shard_name = str(obj.shard_name)
  202. self.start_time = str(obj.start_time)
  203. class GetBackupResponse(BceResponse):
  204. """
  205. Get backup detail response model
  206. """
  207. def __init__(self, bce_response):
  208. super(GetBackupResponse, self).__init__()
  209. self.url = str(bce_response.url)
  210. self.url_expiration = str(bce_response.url_expiration)
  211. class JsonWrapper(JSONEncoder):
  212. """
  213. custom json encoder for class
  214. """
  215. def default(self, obj):
  216. if isinstance(obj, bytes):
  217. return str(obj, encoding='utf-8')
  218. if isinstance(obj, SubnetMap):
  219. return {
  220. 'zoneName': obj.zone_name,
  221. 'subnetId': obj.subnet_id
  222. }
  223. if isinstance(obj, Tag):
  224. return {
  225. 'tagKey': obj.tag_key,
  226. 'tagValue': obj.tag_value
  227. }
  228. return JSONEncoder.default(self, obj)