as_client.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. """
  2. This module provides a client class for AS.
  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.autoscaling import as_handler
  11. from baidubce.utils import required
  12. class AsClient(bce_base_client.BceBaseClient):
  13. """
  14. AS 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, [as_handler.parse_error, body_parser],
  42. http_method, AsClient.version + path, body, headers, params)
  43. def get_as_group_list(self, page_no=1, page_size=1000, keyword=None, keyword_type=None, order=None, order_by=None):
  44. """
  45. Get autoscaling group list
  46. :param page_no:
  47. page_no
  48. :type page_no: int
  49. :param keyword:
  50. query keyword
  51. :type keyword: string
  52. :param keyword_type:
  53. query keyword type
  54. :type keyword_type: string
  55. :param order:
  56. order-ascending order or descending order
  57. :type order: string
  58. :param order_by:
  59. order by param
  60. :type order_by:
  61. :return:
  62. :rtype baidubce.bce_response.BceResponse
  63. """
  64. params = {
  65. b'keyword': keyword,
  66. b'keywordType': keyword_type,
  67. b'order': order,
  68. b'orderBy': order_by,
  69. b'pageNo': page_no,
  70. b'pageSize': page_size,
  71. }
  72. path = b'/group'
  73. return self._send_request(http_methods.GET, path, params=params)
  74. def get_as_group_detail(self, group_id):
  75. """
  76. Get autoscaling group detail
  77. :param group_id:
  78. autoscaling group_id
  79. :type group_id: string
  80. :return:
  81. :rtype baidubce.bce_response.BceResponse
  82. """
  83. if len(group_id) <= 0:
  84. raise ValueError('group_id should not be none or empty string')
  85. group_id = compat.convert_to_bytes(group_id)
  86. path = b'/group/%s' % group_id
  87. return self._send_request(http_methods.GET, path)
  88. def get_as_group_node_list(self, group_id, page_no=1, page_size=1000, keyword=None, keyword_type=None, order=None,
  89. order_by=None):
  90. """
  91. Get autoscaling group node list
  92. :param group_id:
  93. autoscaling group_id
  94. :type group_id: string
  95. :param page_no:
  96. page_no
  97. :type page_no: int
  98. :param keyword:
  99. query keyword
  100. :type keyword: string
  101. :param keyword_type:
  102. query keyword type
  103. :type keyword_type: string
  104. :param order:
  105. order-ascending order or descending order
  106. :type order: string
  107. :param order_by:
  108. order by param
  109. :type order_by:
  110. :return:
  111. :rtype baidubce.bce_response.BceResponse
  112. """
  113. if len(group_id) <= 0:
  114. raise ValueError('group_id should not be none or empty string')
  115. params = {
  116. b'groupid': group_id,
  117. b'keyword': keyword,
  118. b'keywordType': keyword_type,
  119. b'order': order,
  120. b'orderBy': order_by,
  121. b'pageNo': page_no,
  122. b'pageSize': page_size,
  123. }
  124. path = b'/node'
  125. return self._send_request(http_methods.GET, path, params=params)
  126. def delete_group(self, group_ids):
  127. """
  128. Get autoscaling group node list
  129. :param group_ids:
  130. autoscaling group_ids
  131. :type group_ids: list of strings
  132. """
  133. if len(group_ids) <= 0:
  134. raise ValueError('group_id should not be none or empty string')
  135. path = b'/group/delete'
  136. body = {
  137. "groupIds": group_ids
  138. }
  139. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  140. def create_group(self, group_name=None, config=None, health_check=None, blb=None, rds=None, scs=None,
  141. shrinkage_strategy=None, zone_infos=None, assign_tag_info=None, node_list=None,
  142. eip=None, billing=None, cmd_config=None, bcc_name_config=None):
  143. """
  144. Create autoscaling group
  145. :param group_name:
  146. autoscaling group_name
  147. :type group_name: string
  148. :param config:
  149. autoscaling group config
  150. :type config: dict
  151. :param health_check:
  152. autoscaling group health check info
  153. :type health_check: dict
  154. :param blb:
  155. blb info
  156. :type blb: list of strings
  157. :param rds:
  158. rds info
  159. :type rds: list of strings
  160. :param scs:
  161. scs info
  162. :type scs: list of strings
  163. :param shrinkage_strategy:
  164. autoscaling group shrinkage strategy
  165. :type shrinkage_strategy: string
  166. :param zone_info:
  167. autoscaling group zone info
  168. :type zone_info: dict
  169. :param assign_tag_info:
  170. autoscaling group tag info
  171. :type assign_tag_info: dict
  172. :param nodes:
  173. autoscaling group nodes
  174. :type nodes: dict
  175. :param eip:
  176. autoscaling group eip info
  177. :type eip: dict
  178. :param billing:
  179. autoscaling group billing info
  180. :type billing: dict
  181. :param cmd_config:
  182. autoscaling group cmd config
  183. :type cmd_config: dict
  184. :param bcc_name_config:
  185. autoscaling group bcc name config
  186. :type bcc_name_config: dict
  187. :return:
  188. :rtype baidubce.bce_response.BceResponse
  189. """
  190. path = b'/group'
  191. body = {
  192. 'groupName': group_name,
  193. 'config': config,
  194. 'healthCheck': health_check,
  195. 'blb': blb,
  196. 'rds': rds,
  197. 'scs': scs,
  198. 'shrinkageStrategy': shrinkage_strategy,
  199. 'zoneInfo': zone_infos,
  200. 'assignTagInfo': assign_tag_info,
  201. 'nodes': node_list,
  202. 'eip': eip,
  203. 'billing': billing,
  204. 'cmdConfig': cmd_config,
  205. 'bccNameConfig': bcc_name_config
  206. }
  207. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  208. @required(group_id=str, nodes=list)
  209. def detach_node(self, group_id, nodes):
  210. """
  211. Detach nodes from group
  212. :param group_id: the id of group
  213. :type group_id: string
  214. :param nodes: the list of node
  215. :type nodes: list
  216. :return: the result of detach node
  217. :rtype: dict
  218. """
  219. path = b'/group/%s' % compat.convert_to_bytes(group_id)
  220. params = {
  221. "detachNode": ""
  222. }
  223. body = {
  224. "nodes": nodes
  225. }
  226. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params)
  227. @required(rule_name=str, group_id=str, state=str, rule_type=str, action_type=str, action_num=int,
  228. cooldown_in_sec=int)
  229. def create_rule(self, rule_name, group_id, state, rule_type, action_type, action_num, cooldown_in_sec,
  230. target_type="", target_id="", indicator="", threshold="0", unit="",
  231. comparison_operator="", cron_time="", period_type=None, period_value=None,
  232. period_start_time=None, period_end_time=None):
  233. """
  234. Create rule
  235. :param rule_name: the name of the rule
  236. :type rule_name: string
  237. :param group_id: the id of the group
  238. :type group_id: string
  239. :param state: the state of the rule, can be "ENABLE" or "DISABLE"
  240. :type state: string
  241. :param rule_type: the type of the rule, can be "ALARM", "CRONTAB", or "PERIOD"
  242. :type rule_type: string
  243. :param target_type: the type of the target
  244. :type target_type: string
  245. :param target_id: the id of the target
  246. :type target_id: string
  247. :param indicator: the indicator of the rule
  248. :type indicator: string
  249. :param threshold: the threshold of the rule
  250. :type threshold: string
  251. :param unit: the unit of the threshold
  252. :type unit: string
  253. :param comparison_operator: the comparison operator of the rule
  254. :type comparison_operator: string
  255. :param action_type: the action type of the rule, can be INCREASE, DECREASE, ADJUST
  256. :type action_type: string
  257. :param action_num: the number of actions to be performed
  258. :type action_num: int
  259. :param cron_time: the cron time of the rule
  260. :type cron_time: string
  261. :param cooldown_in_sec: the cooldown time in seconds
  262. :type cooldown_in_sec: int
  263. :param period_type: the period type of the rule
  264. :type period_type: string
  265. :param period_value: the period value of the rule
  266. :type period_value: int
  267. :param period_start_time: the start time of the period
  268. :type period_start_time: string
  269. :param period_end_time: the end time of the period
  270. :type period_end_time: string
  271. """
  272. path = b'/rule'
  273. body = {
  274. "ruleName": rule_name,
  275. "groupId": group_id,
  276. "state": state,
  277. "type": rule_type,
  278. "targetType": target_type,
  279. "targetId": target_id,
  280. "indicator": indicator,
  281. "threshold": threshold,
  282. "unit": unit,
  283. "comparisonOperator": comparison_operator,
  284. "actionType": action_type,
  285. "actionNum": action_num,
  286. "cronTime": cron_time,
  287. "cooldownInSec": cooldown_in_sec,
  288. "periodType": period_type,
  289. "periodValue": period_value,
  290. "periodStartTime": period_start_time,
  291. "periodEndTime": period_end_time
  292. }
  293. return self._send_request(http_methods.POST, path, body=json.dumps(body))
  294. @required(rule_id=str, rule_name=str, group_id=str, state=str, rule_type=str, action_type=str, action_num=int,
  295. cooldown_in_sec=int)
  296. def update_rule(self, rule_id, rule_name, group_id, state, rule_type, action_type, action_num, cooldown_in_sec,
  297. target_type="", target_id="", indicator="", threshold="0", unit="",
  298. comparison_operator="", cron_time="", period_type=None, period_value=None,
  299. period_start_time=None, period_end_time=None):
  300. """
  301. Update rule
  302. :param rule_id: the id of the rule
  303. :type rule_id: string
  304. :param rule_name: the name of the rule
  305. :type rule_name: string
  306. :param group_id: the id of the group
  307. :type group_id: string
  308. :param state: the state of the rule, can be "ENABLE" or "DISABLE"
  309. :type state: string
  310. :param rule_type: the type of the rule, can be "ALARM", "CRONTAB", or "PERIOD"
  311. :type rule_type: string
  312. :param target_type: the type of the target
  313. :type target_type: string
  314. :param target_id: the id of the target
  315. :type target_id: string
  316. :param indicator: the indicator of the rule
  317. :type indicator: string
  318. :param threshold: the threshold of the rule
  319. :type threshold: string
  320. :param unit: the unit of the threshold
  321. :type unit: string
  322. :param comparison_operator: the comparison operator of the rule
  323. :type comparison_operator: string
  324. :param action_type: the action type of the rule, can be INCREASE, DECREASE, ADJUST
  325. :type action_type: string
  326. :param action_num: the number of actions to be performed
  327. :type action_num: int
  328. :param cron_time: the cron time of the rule
  329. :type cron_time: string
  330. :param cooldown_in_sec: the cooldown time in seconds
  331. :type cooldown_in_sec: int
  332. :param period_type: the period type of the rule
  333. :type period_type: string
  334. :param period_value: the period value of the rule
  335. :type period_value: int
  336. :param period_start_time: the start time of the period
  337. :type period_start_time: string
  338. :param period_end_time: the end time of the period
  339. :type period_end_time: string
  340. """
  341. path = b'/rule/%s' % compat.convert_to_bytes(rule_id)
  342. body = {
  343. "ruleName": rule_name,
  344. "groupId": group_id,
  345. "state": state,
  346. "type": rule_type,
  347. "targetType": target_type,
  348. "targetId": target_id,
  349. "indicator": indicator,
  350. "threshold": threshold,
  351. "unit": unit,
  352. "comparisonOperator": comparison_operator,
  353. "actionType": action_type,
  354. "actionNum": action_num,
  355. "cronTime": cron_time,
  356. "cooldownInSec": cooldown_in_sec,
  357. "periodType": period_type,
  358. "periodValue": period_value,
  359. "periodStartTime": period_start_time,
  360. "periodEndTime": period_end_time
  361. }
  362. return self._send_request(http_methods.PUT, path, body=json.dumps(body))
  363. @required(rule_id=str)
  364. def get_rule(self, rule_id):
  365. """
  366. Get rule
  367. :param rule_id: the id of the rule
  368. :type rule_id: string
  369. :return: the result of get rule
  370. :rtype: dict
  371. """
  372. path = b'/rule/%s' % compat.convert_to_bytes(rule_id)
  373. return self._send_request(http_methods.GET, path)
  374. @required(group_id=str)
  375. def list_rule(self, group_id, keyword="", keyword_type="", order="desc", order_by="createTime", page_no=1,
  376. page_size=1000):
  377. """
  378. Query rule list
  379. :param group_id: the id of the group
  380. :type group_id: string
  381. :param keyword: the keyword of the rule
  382. :type keyword: string
  383. :param keyword_type: the type of the keyword
  384. :type keyword_type: string
  385. :param order: the order of the rule
  386. :type order: string
  387. :param order_by: the order by of the rule
  388. :type order_by: string
  389. :param page_no: the page number
  390. :type page_no: int
  391. :param page_size: the page size
  392. :type page_size: int
  393. """
  394. path = b'/rule'
  395. params = {
  396. "groupid": group_id,
  397. "keyword": keyword,
  398. "keywordType": keyword_type,
  399. "order": order,
  400. "orderBy": order_by,
  401. "pageNo": page_no,
  402. "pageSize": page_size
  403. }
  404. return self._send_request(http_methods.GET, path, params=params)
  405. def delete_rule(self, rule_ids=None, group_ids=None):
  406. """
  407. Delete rule
  408. :param rule_ids: the list of rule id
  409. :type rule_ids: list
  410. :param group_ids: the list of group id
  411. :type group_ids: list
  412. """
  413. if not rule_ids and not group_ids:
  414. raise ValueError("rule_ids and group_ids can not be empty at the same time")
  415. path = b'/rule'
  416. params = {
  417. "delete": ""
  418. }
  419. body = {
  420. "ruleIds": rule_ids,
  421. "groupIds": group_ids
  422. }
  423. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))
  424. def get_records(self, group_id, page_no=1, page_size=1000, order=None, order_by="startTime",
  425. start_time=None, end_time=None):
  426. """
  427. Get details of a specific group
  428. :param group_id:
  429. Group identifier
  430. :type group_id: str
  431. :param page_no:
  432. Page number
  433. :type page_no: int
  434. :param page_size:
  435. Page size
  436. :type page_size: int
  437. :param order:
  438. Order-ascending order or descending order
  439. :type order: string
  440. :param order_by:
  441. Order by parameter
  442. :type order_by: string
  443. :param start_time:
  444. Start time for filtering the results
  445. :type start_time: string
  446. :param end_time:
  447. End time for filtering the results
  448. :type end_time: string
  449. :return:
  450. rtype baidubce.bce_response.BceResponse
  451. """
  452. params = {
  453. b'groupid': group_id,
  454. b'order': order,
  455. b'orderBy': order_by,
  456. b'pageNo': page_no,
  457. b'pageSize': page_size,
  458. b'startTime': start_time,
  459. b'endTime': end_time,
  460. }
  461. path = b'/record'
  462. return self._send_request(http_methods.GET, path, params=params)
  463. def exec_rule(self, group_id, rule_id):
  464. """
  465. Execute a specific rule within a group
  466. :param group_id:
  467. Group identifier
  468. :type group_id: str
  469. :param rule_id:
  470. Rule identifier within the group
  471. :type rule_id: str
  472. :return:
  473. rtype baidubce.bce_response.BceResponse
  474. """
  475. group_id = compat.convert_to_bytes(group_id)
  476. path = b'/group/%s' % group_id
  477. params = {
  478. b'execRule': '',
  479. }
  480. body = {
  481. "ruleId": rule_id
  482. }
  483. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))
  484. def scaling_up(self, group_id, node_count, zone, expansion_strategy=None):
  485. """
  486. Scale up a specific group
  487. :param group_id:
  488. Group identifier
  489. :type group_id: str
  490. :param node_count:
  491. Number of nodes to be added
  492. :type node_count: int
  493. :param zone:
  494. Zone where the nodes will be added
  495. :type zone: str
  496. :param expansion_strategy:
  497. Strategy to be used for the expansion, optional
  498. :type expansion_strategy: str, optional
  499. :return:
  500. rtype baidubce.bce_response.BceResponse
  501. """
  502. group_id = compat.convert_to_bytes(group_id)
  503. path = b'/group/%s' % group_id
  504. params = {
  505. b'scalingUp': '',
  506. }
  507. body = {
  508. "nodeCount": node_count,
  509. "zone": zone,
  510. "expansionStrategy": expansion_strategy
  511. }
  512. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))
  513. def scaling_down(self, group_id, nodes):
  514. """
  515. Scale down a specific group
  516. :param group_id:
  517. Group identifier
  518. :type group_id: str
  519. :param nodes:
  520. Nodes to be removed
  521. :type nodes: list or str
  522. :return:
  523. rtype baidubce.bce_response.BceResponse
  524. """
  525. group_id = compat.convert_to_bytes(group_id)
  526. path = b'/group/%s' % group_id
  527. params = {
  528. b'scalingDown': '',
  529. }
  530. body = {
  531. "nodes": nodes,
  532. }
  533. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))
  534. def adjust_node(self, group_id, adjust_num):
  535. """
  536. Adjust the number of nodes in a specific group
  537. :param group_id:
  538. Group identifier
  539. :type group_id: str
  540. :param adjust_num:
  541. Number to adjust the nodes by. This could be positive (for adding nodes) or
  542. negative (for removing nodes).
  543. :type adjust_num: int
  544. :return:
  545. rtype baidubce.bce_response.BceResponse
  546. """
  547. group_id = compat.convert_to_bytes(group_id)
  548. path = b'/group/%s' % group_id
  549. params = {
  550. b'adjustNode': '',
  551. }
  552. body = {
  553. "adjustNum": adjust_num,
  554. }
  555. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))
  556. def attach_node(self, group_id, nodes):
  557. """
  558. Attach nodes to a specific group.
  559. :param group_id: str
  560. The unique identifier of the group to which the nodes will be attached.
  561. :param nodes: list or str
  562. The nodes that will be attached to the group.
  563. This can be a list of node identifiers or a single node identifier.
  564. :return: baidubce.bce_response.BceResponse
  565. rtype baidubce.bce_response.BceResponse
  566. """
  567. group_id = compat.convert_to_bytes(group_id)
  568. path = b'/group/%s' % group_id
  569. params = {
  570. b'attachNode': '',
  571. }
  572. body = {
  573. "nodes": nodes,
  574. }
  575. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))