oos_client.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  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 OOS.
  14. """
  15. import copy
  16. import json
  17. import uuid
  18. from baidubce.auth import bce_v1_signer
  19. from baidubce.http import handler, bce_http_client, http_methods
  20. from baidubce import bce_base_client
  21. class OosClient(bce_base_client.BceBaseClient):
  22. """
  23. OOS base sdk client
  24. """
  25. prefix = b'/api/logic/oos'
  26. headers = {
  27. b"x-bce-request-id": uuid.uuid4(),
  28. b"content-type": b"application/json;charset=utf-8"
  29. }
  30. def __init__(self, config=None):
  31. bce_base_client.BceBaseClient.__init__(self, config)
  32. def _merge_config(self, config=None):
  33. if config is None:
  34. return self.config
  35. else:
  36. new_config = copy.copy(self.config)
  37. new_config.merge_non_none_values(config)
  38. return new_config
  39. def _send_request(self, http_method, path,
  40. body=None, headers=None, params=None,
  41. config=None, body_parser=None):
  42. config = self._merge_config(config)
  43. if body_parser is None:
  44. body_parser = handler.parse_json
  45. return bce_http_client.send_request(
  46. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  47. http_method, OosClient.prefix + path, body, headers, params)
  48. def create_template(self, name, operators, description="", linear=True, config=None):
  49. """
  50. Create oos template.
  51. This site may help you: https://cloud.baidu.com/doc/OOS/s/lli1cjcjh
  52. :param name:
  53. Template name.
  54. :type name: string
  55. :param operators:
  56. Include template operators to execute.
  57. :type operators: OperatorModel array
  58. :param description:
  59. Template description.
  60. :type description: string
  61. :param linear:
  62. Operator execute linearly.
  63. :type linear: bool
  64. :param config:
  65. :type config: baidubce.BceClientConfiguration
  66. :return:
  67. :rtype baidubce.bce_response.BceResponse
  68. """
  69. path = b'/v2/template'
  70. body = {
  71. "description": description,
  72. "name": name,
  73. "operators": operators,
  74. "linear": linear
  75. }
  76. return self._send_request(http_methods.POST, path, body=json.dumps(body),
  77. headers=OosClient.headers, config=config)
  78. def check_template(self, name, operators, description="", linear=True, config=None):
  79. """
  80. Check oos template.
  81. This site may help you: https://cloud.baidu.com/doc/OOS/s/lli1cjcjh
  82. :param name:
  83. Template name.
  84. :type name: string
  85. :param operators:
  86. Include template operators to execute.
  87. :type operators: OperatorModel array
  88. :param description:
  89. Template description.
  90. :type description: string
  91. :param linear:
  92. Operator execute linearly.
  93. :type linear: bool
  94. :param config:
  95. :type config: baidubce.BceClientConfiguration
  96. :return:
  97. :rtype baidubce.bce_response.BceResponse
  98. """
  99. path = b'/v2/template/check'
  100. body = {
  101. "description": description,
  102. "name": name,
  103. "operators": operators,
  104. "linear": linear
  105. }
  106. return self._send_request(http_methods.POST, path, body=json.dumps(body),
  107. headers=OosClient.headers, config=config)
  108. def update_template(self, template_id, name, operators, description="", linear=True, config=None):
  109. """
  110. Update oos template.
  111. This site may help you: https://cloud.baidu.com/doc/OOS/s/lli1cjcjh
  112. :param template_id:
  113. Template id.
  114. :type template_id: string
  115. :param name:
  116. Template name.
  117. :type name: string
  118. :param operators:
  119. Include template operators to execute.
  120. :type operators: OperatorModel array
  121. :param description:
  122. Template description.
  123. :type description: string
  124. :param linear:
  125. Operator execute linearly.
  126. :type linear: bool
  127. :param config:
  128. :type config: baidubce.BceClientConfiguration
  129. :return:
  130. :rtype baidubce.bce_response.BceResponse
  131. """
  132. path = b'/v2/template'
  133. body = {
  134. "id": template_id,
  135. "description": description,
  136. "name": name,
  137. "operators": operators,
  138. "linear": linear
  139. }
  140. return self._send_request(http_methods.PUT, path, body=json.dumps(body),
  141. headers=OosClient.headers, config=config)
  142. def delete_template(self, template_id, config=None):
  143. """
  144. Delete oos template by template id.
  145. This site may help you: https://cloud.baidu.com/doc/OOS/s/lli1cjcjh
  146. :param template_id:
  147. Template id.
  148. :type template_id: string
  149. :param config:
  150. :type config: baidubce.BceClientConfiguration
  151. :return:
  152. :rtype baidubce.bce_response.BceResponse
  153. """
  154. path = b'/v2/template'
  155. params = {
  156. "id": template_id
  157. }
  158. return self._send_request(http_methods.DELETE, path, params=params, headers=OosClient.headers, config=config)
  159. def get_template_detail(self, name, config=None):
  160. """
  161. Get template detail by template name.
  162. This site may help you: https://cloud.baidu.com/doc/OOS/s/lli1cjcjh
  163. :param name:
  164. Template name.
  165. :type name: string
  166. :param config:
  167. :type config: baidubce.BceClientConfiguration
  168. :return:
  169. :rtype baidubce.bce_response.BceResponse
  170. """
  171. path = b'/v2/template'
  172. params = {
  173. "name": name
  174. }
  175. return self._send_request(http_methods.GET, path, params=params, headers=OosClient.headers, config=config)
  176. def get_template_list(self, page_no, page_size, sort="createTime", ascending=False, config=None):
  177. """
  178. Get template list.
  179. This site may help you: https://cloud.baidu.com/doc/OOS/s/lli1cjcjh
  180. :param page_no:
  181. Page number.
  182. :type page_no: int
  183. :param page_size:
  184. Page size.
  185. :type page_size: int
  186. :param sort:
  187. Template list sort by.
  188. :type sort: string
  189. :param ascending:
  190. ascend or not.
  191. :type ascending: bool
  192. :param config:
  193. :type config: baidubce.BceClientConfiguration
  194. :return:
  195. :rtype baidubce.bce_response.BceResponse
  196. """
  197. path = b'/v2/template/list'
  198. body = {
  199. "sort": sort,
  200. "ascending": ascending,
  201. "pageNo": page_no,
  202. "pageSize": page_size
  203. }
  204. return self._send_request(http_methods.POST, path, body=json.dumps(body),
  205. headers=OosClient.headers, config=config)
  206. def get_operator_list(self, page_no, page_size, config=None):
  207. """
  208. Get operator list.
  209. This site may help you: https://cloud.baidu.com/doc/OOS/s/Oli1pd1bq
  210. :param page_no:
  211. Page number.
  212. :type page_no: int
  213. :param page_size:
  214. Page size.
  215. :type page_size: int
  216. :param config:
  217. :type config: baidubce.BceClientConfiguration
  218. :return:
  219. :rtype baidubce.bce_response.BceResponse
  220. """
  221. path = b'/v1/operator/list'
  222. body = {
  223. "pageNo": page_no,
  224. "pageSize": page_size
  225. }
  226. return self._send_request(http_methods.POST, path, body=json.dumps(body),
  227. headers=OosClient.headers, config=config)
  228. def create_execution(self, template, properties={}, description="", tags=[], config=None):
  229. """
  230. Create oos execution by template.
  231. This site may help you: https://cloud.baidu.com/doc/OOS/s/fli1q43ih
  232. :param template:
  233. Template to execute.
  234. :type template: TemplateModel
  235. :param properties:
  236. Template execute global parameters.
  237. :type properties: dict
  238. :param description:
  239. Template description.
  240. :type description: string
  241. :param tags:
  242. Execution binding tags.
  243. :type tags: TagModel array
  244. :param config:
  245. :type config: baidubce.BceClientConfiguration
  246. :return:
  247. :rtype baidubce.bce_response.BceResponse
  248. """
  249. path = b'/v2/execution'
  250. body = {
  251. "template": template,
  252. "properties": properties,
  253. "description": description,
  254. "tags": tags
  255. }
  256. return self._send_request(http_methods.POST, path, body=json.dumps(body),
  257. headers=OosClient.headers, config=config)
  258. def get_execution_detail(self, execution_id, config=None):
  259. """
  260. Get execution detail by execution id.
  261. This site may help you: https://cloud.baidu.com/doc/OOS/s/fli1q43ih
  262. :param execution_id:
  263. Execution id.
  264. :type execution_id: string
  265. :param config:
  266. :type config: baidubce.BceClientConfiguration
  267. :return:
  268. :rtype baidubce.bce_response.BceResponse
  269. """
  270. path = b'/v2/execution'
  271. params = {
  272. "id": execution_id
  273. }
  274. return self._send_request(http_methods.GET, path, params=params, headers=OosClient.headers, config=config)