ca_client.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. """
  2. This module provides a client class for CA.
  3. """
  4. import copy
  5. import json
  6. import uuid
  7. from baidubce import bce_base_client, compat
  8. from baidubce.auth import bce_v1_signer
  9. from baidubce.http import handler, bce_http_client, http_methods
  10. from baidubce.services.ca import ca_handler
  11. from baidubce.utils import required
  12. class CaClient(bce_base_client.BceBaseClient):
  13. """
  14. CA base sdk client
  15. """
  16. version = b'/v1'
  17. content_type_header_key = b"content-type"
  18. content_type_header_value = b"application/json;charset=UTF-8"
  19. request_id_header_key = b"x-bce-request-id"
  20. def __init__(self, config=None):
  21. bce_base_client.BceBaseClient.__init__(self, config)
  22. def _merge_config(self, config=None):
  23. if config is None:
  24. return self.config
  25. else:
  26. new_config = copy.copy(self.config)
  27. new_config.merge_non_none_values(config)
  28. return new_config
  29. def _send_request(self, http_method, path,
  30. body=None, headers=None, params=None, config=None, body_parser=None):
  31. config = self._merge_config(config)
  32. if body_parser is None:
  33. body_parser = handler.parse_json
  34. if headers is None:
  35. headers = {}
  36. if self.content_type_header_key not in headers:
  37. headers[self.content_type_header_key] = self.content_type_header_value
  38. if self.request_id_header_key not in headers:
  39. headers[self.request_id_header_key] = uuid.uuid4()
  40. return bce_http_client.send_request(
  41. config, bce_v1_signer.sign, [ca_handler.parse_error, body_parser],
  42. http_method, CaClient.version + path, body, headers, params)
  43. def batch_get_agent(self, instance_list=None):
  44. """
  45. Batch get agent info
  46. :param instance_list:
  47. Instance List
  48. :type instance: dict
  49. """
  50. path = b'/ca/agent/batch'
  51. body = {
  52. "hosts": instance_list
  53. }
  54. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  55. def create_action(self, execution=None, user_id=None, action=None, targets=None,
  56. parameters=None, target_selector=None, target_selector_type=None):
  57. """
  58. create execution
  59. :param execution:
  60. execution info
  61. :type execution: string
  62. :param user_id:
  63. user_id
  64. :type user_id: string
  65. :param action:
  66. action info
  67. :type action: dict
  68. :param targets:
  69. targets info
  70. :type targets:list
  71. :param parameters:
  72. parameters info
  73. :type parameters:dict
  74. :param target_selector:
  75. target_selector info
  76. :type target_selector:dict
  77. :param target_selector_type:
  78. target_selector_type info
  79. :type target_selector_type:string
  80. """
  81. path = b'/ca/action'
  82. body = {
  83. "execution": execution,
  84. "action": action,
  85. "targets": targets,
  86. "userId": user_id,
  87. "parameters": parameters,
  88. "targetSelector": target_selector,
  89. "targetSelectorType": target_selector_type
  90. }
  91. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  92. def delete_action(self, id):
  93. """
  94. Delete action
  95. :param id: The ID of the execution to be deleted
  96. :type id: str
  97. """
  98. id = compat.convert_to_bytes(id)
  99. path = b'/ca/action/%s' % id
  100. return self._send_request(http_methods.DELETE, path)
  101. def get_action(self, id, user_id):
  102. """
  103. Query id
  104. :param id: The ID of the execution to be queried
  105. :type id: str
  106. Query user_id
  107. :param user_id: user_id
  108. :type user_id: str
  109. """
  110. path = b'/ca/action'
  111. params = {
  112. "userId": user_id,
  113. "id": id
  114. }
  115. return self._send_request(http_methods.GET, path, params=params)
  116. def update_action(self, action=None, execution=None):
  117. """
  118. :param action:
  119. action info
  120. :type action: dict
  121. :param execution
  122. execution info
  123. :type execution: string
  124. """
  125. path = b'/ca/action'
  126. body = {
  127. "action": action,
  128. "execution": execution,
  129. }
  130. return self._send_request(http_methods.PUT, path, body=json.dumps(body))
  131. def action_list(self, action=None, page_no=None, page_size=None, sort=None, ascending=None, user_id=None):
  132. """
  133. :param action:
  134. action info
  135. :type action: dict
  136. :param page_no:
  137. page number
  138. :type page_no: int
  139. :param page_size:
  140. page number
  141. :type page_size: int
  142. :param sort:
  143. sort order
  144. :type sort: str
  145. :param ascending:
  146. ascending order
  147. :type ascending: bool
  148. :param user_id:
  149. :type user_id: str
  150. """
  151. path = b'/ca/action/list'
  152. body = {
  153. "action": action,
  154. "pageNo": page_no,
  155. "pageSize": page_size,
  156. "sort": sort,
  157. "ascending": ascending,
  158. "userId": user_id
  159. }
  160. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  161. def action_run(self, action=None, parameters=None, user_id=None,
  162. target_selector_type=None, targets=None, target_selector=None):
  163. """
  164. :param action:
  165. action info
  166. :type action: dict
  167. :param parameters:
  168. :param target_selector_type:map
  169. :param user_id:
  170. :type user_id: str
  171. target selector type
  172. :type target_selector_type: str
  173. :param targets:
  174. :type targets: list
  175. :param target_selector:
  176. :type target_selector: dict
  177. """
  178. path = b'/ca/actionRun'
  179. body = {
  180. "action": action,
  181. "parameters": parameters,
  182. "targetSelectorType": target_selector_type,
  183. "targets": targets,
  184. "targetSelector": target_selector,
  185. "userId": user_id,
  186. "execution": "RUN"
  187. }
  188. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  189. def get_action_run(self, id=None, user_id=None):
  190. """
  191. Query id
  192. :param id: The ID of the execution to be queried
  193. :type id: str
  194. user_id
  195. :user_id id: str
  196. """
  197. params = {
  198. "userId": user_id,
  199. "id": id
  200. }
  201. path = b'/ca/actionRun'
  202. return self._send_request(http_methods.GET, path, params=params)
  203. def action_run_list(self, action=None, page_no=None, page_size=None, sort=None, ascending=None, user_id=None,
  204. start_time=None, end_time=None, keyword=None, keyword_type=None, run_id=None, is_inited=False):
  205. """
  206. :param action:
  207. action info
  208. :type action: dict
  209. :param page_no:
  210. page number
  211. :type page_no: int
  212. :param page_size:
  213. page number
  214. :type page_size: int
  215. :param sort:
  216. sort order
  217. :type sort: str
  218. :param ascending:
  219. ascending order
  220. :type ascending: bool
  221. :param user_id:
  222. :type user_id: str
  223. :param start_time:
  224. start time
  225. :type start_time:
  226. :param end_time:
  227. end time
  228. :type end_time:
  229. :param keyword:
  230. keyword type
  231. :type keyword: str
  232. :param keyword_type:
  233. keyword type
  234. :type keyword_type: str
  235. :param run_id:
  236. run id
  237. :type run_id: str
  238. :param is_inited:
  239. is inited or not inited
  240. :type is_inited: bool
  241. """
  242. body = {
  243. "action": action,
  244. "pageNo": page_no,
  245. "pageSize": page_size,
  246. "sort": sort,
  247. "ascending": ascending,
  248. "userId": user_id,
  249. "startTime": start_time,
  250. "endTime": end_time,
  251. "keyword": keyword,
  252. "keyword_type": keyword_type,
  253. "runId": run_id,
  254. "isInited": is_inited
  255. }
  256. path = b'/ca/actionRun/list'
  257. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  258. def action_log(self, run_id=None, child_id=None, cursor=None):
  259. """
  260. :param run_id:
  261. :type run_id: str
  262. :param child_id:
  263. :type child_id: str
  264. :param cursor:
  265. :type cursor: int
  266. """
  267. body = {
  268. "runId": run_id,
  269. "childId": child_id,
  270. "cursor": cursor
  271. }
  272. path = b'/ca/log'
  273. return self._send_request(http_methods.POST, path, body=json.dumps(body))