user_service_client.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions
  13. # and limitations under the License.
  14. """
  15. This module provides a client class for User Service.
  16. """
  17. import copy
  18. import json
  19. import logging
  20. import uuid
  21. import sys
  22. from baidubce import bce_base_client
  23. from baidubce.auth import bce_v1_signer
  24. from baidubce.http import bce_http_client
  25. from baidubce.http import handler
  26. from baidubce.http import http_methods
  27. from baidubce import utils
  28. from baidubce.utils import required
  29. from baidubce import compat
  30. if sys.version < '3':
  31. reload(sys)
  32. sys.setdefaultencoding('utf-8')
  33. _logger = logging.getLogger(__name__)
  34. class UserServiceClient(bce_base_client.BceBaseClient):
  35. """
  36. BLB base sdk client
  37. """
  38. version = b'/v1'
  39. def __init__(self, config=None):
  40. """初始化BceClient对象。
  41. Args:
  42. config (dict,可选参数,默认值为None): BceClient的配置字典。
  43. """
  44. bce_base_client.BceBaseClient.__init__(self, config)
  45. def _merge_config(self, config=None):
  46. """
  47. :param config:
  48. :type config: baidubce.BceClientConfiguration
  49. :return:
  50. """
  51. if config is None:
  52. return self.config
  53. else:
  54. new_config = copy.copy(self.config)
  55. new_config.merge_non_none_values(config)
  56. return new_config
  57. def _send_request(self, http_method, path,
  58. body=None, headers=None, params=None,
  59. config=None, body_parser=None):
  60. """
  61. 发送请求方法
  62. Args:
  63. http_method (str): 请求的方法,可选值包括"GET", "POST", "PUT", "DELETE"。
  64. path (str): 请求路径。
  65. body (Any, optional): 请求体内容,可以是任意类型。默认值为None。
  66. headers (dict[bytes, bytes], optional): 请求头信息,以键值对形式存储。默认值为None。
  67. params (dict[str, str], optional): 请求参数,以键值对形式存储。默认值为None。
  68. config (dict, optional): 配置信息。
  69. body_parser (func, optional): 解析响应体的函数。如果为空则默认使用parse_json方法。
  70. Returns:
  71. Any: 返回请求返回的内容。
  72. """
  73. config = self._merge_config(config)
  74. if body_parser is None:
  75. body_parser = handler.parse_json
  76. if headers is None:
  77. headers = {b'Accept': b'*/*',
  78. b'Content-Type': b'application/json;charset=utf-8'}
  79. return bce_http_client.send_request(
  80. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  81. http_method, path, body, headers, params)
  82. @required(name=(bytes, str),
  83. serviceName=(bytes, str),
  84. instanceId=(bytes, str))
  85. def create_user_service(self, name, desc, serviceName,
  86. instanceId, client_token=None,
  87. authList=None, config=None):
  88. """
  89. Create user service for specified LoadBalancer
  90. :param name:
  91. name of user service
  92. :type name: string
  93. :param desc:
  94. description of user service
  95. :type desc: string
  96. :param serviceName:
  97. The name of service
  98. :type serviceName: string
  99. :param instanceId:
  100. The id of LoadBalancer
  101. :type instanceId: string
  102. :param client_token:
  103. If the clientToken is not specified by the user,
  104. a random String generated by default algorithm will be used.
  105. :type client_token: string
  106. :param authList:
  107. List of Auth information
  108. :type service: list<Auth>
  109. :param config:
  110. :type config: baidubce.BceClientConfiguration
  111. :return:
  112. :rtype baidubce.bce_response.BceResponse
  113. """
  114. path = utils.append_uri(self.version, 'service')
  115. params = {}
  116. if client_token is None:
  117. params[b'clientToken'] = generate_client_token()
  118. else:
  119. params[b'clientToken'] = client_token
  120. body = {}
  121. if desc is not None:
  122. body['description'] = compat.convert_to_string(desc)
  123. body['name'] = name
  124. body['serviceName'] = serviceName
  125. body['instanceId'] = instanceId
  126. if authList is not None:
  127. body['authList'] = authList
  128. else:
  129. body['authList'] = [{"uid": "*", "auth": "deny"}]
  130. return self._send_request(http_methods.POST, path,
  131. body=json.dumps(body), params=params,
  132. config=config)
  133. @required(service=(bytes, str))
  134. def update_user_service(self, name=None, desc=None, service=None,
  135. client_token=None, config=None):
  136. """
  137. Update user service for specified LoadBalancer
  138. :param name:
  139. name of user service
  140. :type name: string
  141. :param desc:
  142. description of user service
  143. :type desc: string
  144. :param service:
  145. domain name of the service
  146. :type service: string
  147. :param client_token:
  148. If the clientToken is not specified by the user,
  149. a random String generated by default algorithm will be used.
  150. :type client_token: string
  151. :param config:
  152. :type config: baidubce.BceClientConfiguration
  153. :return:
  154. :rtype baidubce.bce_response.BceResponse
  155. """
  156. path = utils.append_uri(self.version, 'service', service)
  157. params = {}
  158. if client_token is None:
  159. params[b'clientToken'] = generate_client_token()
  160. else:
  161. params[b'clientToken'] = client_token
  162. params[b'modifyAttribute'] = None
  163. body = {}
  164. if desc is not None:
  165. body['description'] = compat.convert_to_string(desc)
  166. if name is not None:
  167. body["name"] = name
  168. return self._send_request(http_methods.PUT, path,
  169. body=json.dumps(body), params=params,
  170. config=config)
  171. @required(service=(bytes, str),
  172. instanceId=(bytes, str))
  173. def user_service_bind_instance(self, instanceId, service,
  174. client_token=None, config=None):
  175. """
  176. Bind a specified LoadBalancer to user service
  177. :param instanceId:
  178. The id of LoadBalancer
  179. :type instanceId: string
  180. :param service:
  181. domain name of the service
  182. :type service: string
  183. :param client_token:
  184. If the clientToken is not specified by the user,
  185. a random String generated by default algorithm will be used.
  186. :type client_token: string
  187. :param config:
  188. :type config: baidubce.BceClientConfiguration
  189. :return:
  190. :rtype baidubce.bce_response.BceResponse
  191. """
  192. path = utils.append_uri(self.version, 'service', service)
  193. params = {}
  194. if client_token is None:
  195. params[b'clientToken'] = generate_client_token()
  196. else:
  197. params[b'clientToken'] = client_token
  198. params[b'bind'] = None
  199. body = {}
  200. body['instanceId'] = instanceId
  201. return self._send_request(http_methods.PUT, path,
  202. body=json.dumps(body), params=params,
  203. config=config)
  204. @required(service=(bytes, str))
  205. def user_service_unbind_instance(self, service,
  206. client_token=None, config=None):
  207. """
  208. Unbind a specified LoadBalancer from user service
  209. :param service:
  210. domain name of the service
  211. :type service: string
  212. :param client_token:
  213. If the clientToken is not specified by the user,
  214. a random String generated by default algorithm will be used.
  215. :type client_token: string
  216. :param config:
  217. :type config: baidubce.BceClientConfiguration
  218. :return:
  219. :rtype baidubce.bce_response.BceResponse
  220. """
  221. path = utils.append_uri(self.version, 'service', service)
  222. params = {}
  223. if client_token is None:
  224. params[b'clientToken'] = generate_client_token()
  225. else:
  226. params[b'clientToken'] = client_token
  227. params[b'unbind'] = None
  228. body = {}
  229. return self._send_request(http_methods.PUT, path,
  230. body=json.dumps(body), params=params,
  231. config=config)
  232. @required(service=(bytes, str),
  233. authList=list)
  234. def user_service_add_auth(self, service,
  235. authList, client_token=None, config=None):
  236. """
  237. Add a new auth information to user service
  238. :param service:
  239. domain name of the service
  240. :type service: string
  241. :param authList:
  242. List of Auth information
  243. :type service: list<Auth>
  244. :param client_token:
  245. If the clientToken is not specified by the user,
  246. a random String generated by default algorithm will be used.
  247. :type client_token: string
  248. :return:
  249. :rtype baidubce.bce_response.BceResponse
  250. """
  251. path = utils.append_uri(self.version, 'service', service)
  252. params = {}
  253. if client_token is None:
  254. params[b'clientToken'] = generate_client_token()
  255. else:
  256. params[b'clientToken'] = client_token
  257. params[b'addAuth'] = None
  258. body = {}
  259. body['authList'] = authList
  260. return self._send_request(http_methods.PUT, path,
  261. body=json.dumps(body), params=params,
  262. config=config)
  263. @required(service=(bytes, str),
  264. authList=list)
  265. def user_service_edit_auth(self, service,
  266. authList, client_token=None, config=None):
  267. """
  268. Edit auth information to user service
  269. :param service:
  270. domain name of the service
  271. :type service: string
  272. :param authList:
  273. List of Auth information
  274. :type service: list<Auth>
  275. :param client_token:
  276. If the clientToken is not specified by the user,
  277. a random String generated by default algorithm will be used.
  278. :type client_token: string
  279. :param config:
  280. :type config: baidubce.BceClientConfiguration
  281. :return:
  282. :rtype baidubce.bce_response.BceResponse
  283. """
  284. path = utils.append_uri(self.version, 'service', service)
  285. params = {}
  286. if client_token is None:
  287. params[b'clientToken'] = generate_client_token()
  288. else:
  289. params[b'clientToken'] = client_token
  290. params[b'editAuth'] = None
  291. body = {}
  292. body['authList'] = authList
  293. return self._send_request(http_methods.PUT, path,
  294. body=json.dumps(body), params=params,
  295. config=config)
  296. @required(service=(bytes, str),
  297. uidList=list)
  298. def user_service_remove_auth(self, service,
  299. uidList, client_token=None, config=None):
  300. """
  301. Remove auth information from user service
  302. :param service:
  303. domain name of the service
  304. :type service: string
  305. :param uidList:
  306. List of uid
  307. :type service: list<String>
  308. :param client_token:
  309. If the clientToken is not specified by the user,
  310. a random String generated by default algorithm will be used.
  311. :type client_token: string
  312. :param config:
  313. :type config: baidubce.BceClientConfiguration
  314. :return:
  315. :rtype baidubce.bce_response.BceResponse
  316. """
  317. path = utils.append_uri(self.version, 'service', service)
  318. params = {}
  319. if client_token is None:
  320. params[b'clientToken'] = generate_client_token()
  321. else:
  322. params[b'clientToken'] = client_token
  323. params[b'removeAuth'] = None
  324. body = {}
  325. body['uidList'] = uidList
  326. return self._send_request(http_methods.PUT, path,
  327. body=json.dumps(body), params=params,
  328. config=config)
  329. def get_user_service_list(self, marker=None,
  330. maxKeys=None, client_token=None, config=None):
  331. """
  332. Get list of user service
  333. :param marker:
  334. Mark the start position of query
  335. :type service: string
  336. :param maxKeys:
  337. Max number of one page, default number is 1000
  338. :type service: integer
  339. :param client_token:
  340. If the clientToken is not specified by the user,
  341. a random String generated by default algorithm will be used.
  342. :type client_token: string
  343. :param config:
  344. :type config: baidubce.BceClientConfiguration
  345. :return:
  346. :rtype baidubce.bce_response.BceResponse
  347. """
  348. path = utils.append_uri(self.version, b'service')
  349. params = {}
  350. if client_token is None:
  351. params[b'clientToken'] = generate_client_token()
  352. else:
  353. params[b'clientToken'] = client_token
  354. if marker is not None:
  355. params[b'marker'] = marker
  356. if maxKeys is not None:
  357. params[b'maxKeys'] = maxKeys
  358. else:
  359. params[b'maxKeys'] = 1000
  360. body = {}
  361. return self._send_request(http_methods.GET, path,
  362. body=json.dumps(body), params=params,
  363. config=config)
  364. @required(service=(bytes, str))
  365. def get_user_service_detail(self, service,
  366. client_token=None, config=None):
  367. """
  368. Get user service detail information
  369. :param service:
  370. domain name of the service
  371. :type service: string
  372. :param client_token:
  373. If the clientToken is not specified by the user,
  374. a random String generated by default algorithm will be used.
  375. :type client_token: string
  376. :param config:
  377. :type config: baidubce.BceClientConfiguration
  378. :return:
  379. :rtype baidubce.bce_response.BceResponse
  380. """
  381. path = utils.append_uri(self.version, b'service', service)
  382. params = {}
  383. if client_token is None:
  384. params[b'clientToken'] = generate_client_token()
  385. else:
  386. params[b'clientToken'] = client_token
  387. body = {}
  388. return self._send_request(http_methods.GET, path,
  389. body=json.dumps(body), params=params,
  390. config=config)
  391. @required(service=(bytes, str))
  392. def delete_user_service(self, service,
  393. client_token=None, config=None):
  394. """
  395. Delete user service
  396. :param service:
  397. domain name of the service
  398. :type service: string
  399. :param client_token:
  400. If the clientToken is not specified by the user,
  401. a random String generated by default algorithm will be used.
  402. :type client_token: string
  403. :param config:
  404. :type config: baidubce.BceClientConfiguration
  405. :return:
  406. :rtype baidubce.bce_response.BceResponse
  407. """
  408. path = utils.append_uri(self.version, 'service', service)
  409. params = {}
  410. if client_token is None:
  411. params[b'clientToken'] = generate_client_token()
  412. else:
  413. params[b'clientToken'] = client_token
  414. body = {}
  415. return self._send_request(http_methods.DELETE, path,
  416. body=json.dumps(body), params=params,
  417. config=config)
  418. def generate_client_token_by_uuid():
  419. """
  420. The default method to generate the random string for client_token
  421. if the optional parameter client_token is not specified by the user.
  422. :return:
  423. :rtype string
  424. """
  425. return str(uuid.uuid4())
  426. generate_client_token = generate_client_token_by_uuid