template_client.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 parameter template.
  16. """
  17. import copy
  18. import json
  19. import logging
  20. import uuid
  21. from baidubce import bce_base_client
  22. from baidubce.auth import bce_v1_signer
  23. from baidubce.http import bce_http_client
  24. from baidubce.http import handler
  25. from baidubce.http import http_methods
  26. from baidubce.utils import required
  27. from baidubce import compat
  28. _logger = logging.getLogger(__name__)
  29. class TemplateClient(bce_base_client.BceBaseClient):
  30. """
  31. Tempalte base sdk client
  32. """
  33. prefix = b'/v1'
  34. def __init__(self, config=None):
  35. '''
  36. :param config:
  37. :type config: baidubce.BceClientConfiguration
  38. :return:
  39. '''
  40. bce_base_client.BceBaseClient.__init__(self, config)
  41. def _merge_config(self, config=None):
  42. """
  43. :param config:
  44. :type config: baidubce.BceClientConfiguration
  45. :return:
  46. """
  47. if config is None:
  48. return self.config
  49. else:
  50. new_config = copy.copy(self.config)
  51. new_config.merge_non_none_values(config)
  52. return new_config
  53. def _send_request(self, http_method, path,
  54. body=None, headers=None, params=None,
  55. config=None, body_parser=None):
  56. '''
  57. :param http_method:
  58. :type http_method: string
  59. :param path:
  60. :type path: string
  61. :param body:
  62. :type body: dict
  63. :param headers:
  64. :type headers: dict
  65. :param params:
  66. :type params: dict
  67. :param config:
  68. :type config: baidubce.BceClientConfiguration
  69. :param body_parser:
  70. :type body_parser: function
  71. :return: baidubce.BceResponse
  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'*/*', b'Content-Type': b'application/json;charset=utf-8'}
  78. return bce_http_client.send_request(
  79. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  80. http_method, TemplateClient.prefix + path, body, headers, params)
  81. @required(name=(bytes, str), ip_version=(bytes, str), ip_address_info=list)
  82. def create_ip_set(self, name, ip_version, ip_address_info, description=None, client_token=None, config=None):
  83. """
  84. Create a new ip set.
  85. :param name:
  86. The name of ip set to be created.
  87. :type name: string
  88. :param ip_version:
  89. The IP version of the ip set.
  90. :type ip_version: string
  91. :param ip_address_info:
  92. The list of IpAddressInfo to be created in this ip set.
  93. :type ip_address_info: List[IpAddressInfo]
  94. :param description:
  95. The description of the ip set.
  96. :type description: string
  97. :param client_token:
  98. An ASCII string whose length is less than 64.
  99. The request will be idempotent if clientToken is provided.
  100. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  101. :type client_token: string
  102. :param config:
  103. :type config: baidubce.BceClientConfiguration
  104. :return:
  105. :rtype baidubce.bce_response.BceResponse
  106. """
  107. path = b'/ipSet'
  108. params = {}
  109. if client_token is None:
  110. params[b'clientToken'] = generate_client_token()
  111. else:
  112. params[b'clientToken'] = client_token
  113. body = {
  114. 'name': compat.convert_to_string(name),
  115. 'ipVersion': compat.convert_to_string(ip_version),
  116. }
  117. if ip_address_info is not None:
  118. address_info_set = []
  119. for ip_address in ip_address_info:
  120. address_info_set.append({"ipAddress": ip_address.ip_address, "description": ip_address.description})
  121. body['ipAddressInfo'] = address_info_set
  122. if description is not None:
  123. body['description'] = compat.convert_to_string(description)
  124. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params,
  125. config=config)
  126. @required(ip_set_id=(bytes, str), ip_address_info=list)
  127. def add_ip_address_to_ip_set(self, ip_set_id, ip_address_info, client_token=None, config=None):
  128. """
  129. Add a list of IpAddressInfo to specified ip set.
  130. :param ip_set_id:
  131. The id of the ip set which you want to add IpAddressInfo into.
  132. :type ip_set_id: string
  133. :param ip_address_info:
  134. The list of IpAddressInfo to be added in this ip set.
  135. :type ip_address_info: List[IpAddressInfo]
  136. :param client_token:
  137. An ASCII string whose length is less than 64.
  138. The request will be idempotent if clientToken is provided.
  139. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  140. :type client_token: string
  141. :param config:
  142. :type config: baidubce.BceClientConfiguration
  143. :return:
  144. :rtype baidubce.bce_response.BceResponse
  145. """
  146. path = b'/ipSet/' + compat.convert_to_string(ip_set_id) + b'/ipAddress'
  147. params = {}
  148. if client_token is None:
  149. params[b'clientToken'] = generate_client_token()
  150. else:
  151. params[b'clientToken'] = client_token
  152. body = {
  153. "ipAddressInfo": []
  154. }
  155. if ip_address_info is not None:
  156. for ip_address in ip_address_info:
  157. body["ipAddressInfo"].append({"ipAddress": ip_address.ip_address,
  158. "description": ip_address.description})
  159. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config=config)
  160. @required(ip_set_id=(bytes, str), ip_address=list)
  161. def delete_ip_address(self, ip_set_id, ip_address, client_token=None, config=None):
  162. """
  163. Delete a list of IpAddressInfo from specified ip set.
  164. :param ip_set_id:
  165. The id of the ip set which you want to delete IpAddressInfo into.
  166. :type ip_set_id: string
  167. :param ip_address:
  168. The list of IpAddress to be deleted in this ip set.
  169. :type ip_address: List[string]
  170. :param client_token:
  171. An ASCII string whose length is less than 64.
  172. The request will be idempotent if clientToken is provided.
  173. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  174. :type client_token: string
  175. :param config:
  176. :type config: baidubce.BceClientConfiguration
  177. :return:
  178. :rtype baidubce.bce_response.BceResponse
  179. """
  180. path = b'/ipSet/' + compat.convert_to_string(ip_set_id) + b'/deleteIpAddress'
  181. params = {}
  182. if client_token is None:
  183. params[b'clientToken'] = generate_client_token()
  184. else:
  185. params[b'clientToken'] = client_token
  186. body = {
  187. "ipAddressInfo": []
  188. }
  189. if ip_address is not None:
  190. for address in ip_address:
  191. body["ipAddressInfo"].append(address)
  192. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config = config)
  193. @required(ip_set_id=(bytes, str))
  194. def update_ip_set(self, ip_set_id, name=None, description=None, client_token=None, config=None):
  195. """
  196. Update the specified IpSet.
  197. :param ip_set_id:
  198. The id of the IpSet which you want to update.
  199. :type ip_set_id: string
  200. :param name:
  201. The new name of this IpSet.
  202. :type name: string
  203. :param description:
  204. The new description of this IpSet.
  205. :type description: string
  206. :param client_token:
  207. An ASCII string whose length is less than 64.
  208. The request will be idempotent if clientToken is provided.
  209. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  210. :type client_token: string
  211. :param config:
  212. :type config: baidubce.BceClientConfiguration
  213. :return:
  214. :rtype baidubce.bce_response.BceResponse
  215. """
  216. path = b'/ipSet/' + compat.convert_to_string(ip_set_id)
  217. params = {}
  218. if client_token is None:
  219. params[b'clientToken'] = generate_client_token()
  220. else:
  221. params[b'clientToken'] = client_token
  222. params[b'modifyAttribute'] = None
  223. body = {
  224. "name": name,
  225. "description": description
  226. }
  227. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params, config=config)
  228. @required(ip_set_id=(bytes, str))
  229. def delete_ip_set(self, ip_set_id, client_token=None, config=None):
  230. """
  231. Delete the specified IpSet.
  232. :param ip_set_id:
  233. The id of the IpSet which you want to update.
  234. :type ip_set_id: string
  235. :param client_token:
  236. An ASCII string whose length is less than 64.
  237. The request will be idempotent if clientToken is provided.
  238. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  239. :type client_token: string
  240. :param config:
  241. :type config: baidubce.BceClientConfiguration
  242. :return:
  243. :rtype baidubce.bce_response.BceResponse
  244. """
  245. path = b'/ipSet/' + compat.convert_to_string(ip_set_id)
  246. params = {}
  247. if client_token is None:
  248. params[b'clientToken'] = generate_client_token()
  249. else:
  250. params[b'clientToken'] = client_token
  251. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  252. def list_ip_set(self, ip_version=None, marker=None, max_keys=None, config=None):
  253. """
  254. Return a list of all IpSets owned by the authenticated user.
  255. :param ip_version:
  256. The version of IP addresses that are returned.
  257. Valid values are IPv4 and IPv6.
  258. :type ip_version: string
  259. :param marker:
  260. The optional parameter marker specified in the original request to specify
  261. where in the results to begin listing.
  262. Together with the marker, specifies the list result which listing should begin.
  263. If the marker is not specified, the list result will listing from the first one.
  264. :type marker: string
  265. :param max_keys:
  266. The optional parameter to specifies the max number of list result to return.
  267. The default value is 1000.
  268. :type max_keys: int
  269. :param config:
  270. :type config: baidubce.BceClientConfiguration
  271. :return:
  272. :rtype baidubce.bce_response.BceResponse
  273. """
  274. path = b'/ipSet'
  275. params = {"maxKeys": 1000}
  276. if ip_version is not None:
  277. params[b'ip_version'] = ip_version
  278. if marker is not None:
  279. params[b'marker'] = marker
  280. if max_keys is not None:
  281. params[b'maxKeys'] = max_keys
  282. return self._send_request(http_methods.GET, path, params=params, config=config)
  283. @required(ip_set_id=(bytes, str))
  284. def get_ip_set_detail(self, ip_set_id, config=None):
  285. """
  286. Get the detail information of specified IpSet.
  287. :param ip_set_id:
  288. The id of the IpSet which you want to query.
  289. :type ip_set_id: string
  290. :param config:
  291. :type config: baidubce.BceClientConfiguration
  292. :return:
  293. :rtype baidubce.bce_response.BceResponse
  294. """
  295. path = b'/ipSet/' + compat.convert_to_string(ip_set_id)
  296. return self._send_request(http_methods.GET, path, config=config)
  297. @required(name=(bytes, str), ip_version=(bytes, str), ip_set_ids=list)
  298. def create_ip_group(self, name, ip_version, ip_set_ids, description=None, client_token=None, config=None):
  299. """
  300. Create an IpGroup.
  301. :param name:
  302. The name of the IpGroup.
  303. :type name: string
  304. :param ip_version:
  305. The version of IP addresses that are returned.
  306. Valid values are IPv4 and IPv6.
  307. :type ip_version: string
  308. :param ip_set_ids:
  309. A list of IpSet ids.
  310. :type ip_set_ids: list
  311. :param description:
  312. The description of the IpGroup.
  313. :type description: string
  314. :param client_token:
  315. An ASCII string whose length is less than 64.
  316. The request will be idempotent if clientToken is provided.
  317. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  318. :type client_token: string
  319. :param config:
  320. :type config: baidubce.BceClientConfiguration
  321. :return:
  322. :rtype baidubce.bce_response.BceResponse
  323. """
  324. path = b'/ipGroup'
  325. params = {}
  326. if client_token is None:
  327. params[b'clientToken'] = generate_client_token()
  328. else:
  329. params[b'clientToken'] = client_token
  330. body = {
  331. "name": name,
  332. "ipVersion": ip_version,
  333. "ipSetIds": ip_set_ids
  334. }
  335. if description is None:
  336. body["description"] = description
  337. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config=config)
  338. @required(ip_group_id=(bytes, str), ip_set_ids=list)
  339. def bind_ip_set(self, ip_group_id, ip_set_ids, client_token=None, config=None):
  340. """
  341. Bind IpSet to IpGroup.
  342. :param ip_group_id:
  343. The id of the IpGroup which you want to bind IpSet.
  344. :type ip_group_id: string
  345. :param ip_set_ids:
  346. A list of IpSet ids.
  347. :type ip_set_ids: list
  348. :param client_token:
  349. An ASCII string whose length is less than 64.
  350. The request will be idempotent if clientToken is provided.
  351. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  352. :type client_token: string
  353. :param config:
  354. :type config: baidubce.BceClientConfiguration
  355. :return:
  356. :rtype baidubce.bce_response.BceResponse
  357. """
  358. path = b'/ipGroup/' + compat.convert_to_string(ip_group_id) + b'/bindIpSet'
  359. params = {}
  360. if client_token is None:
  361. params[b'clientToken'] = generate_client_token()
  362. else:
  363. params[b'clientToken'] = client_token
  364. body = {
  365. "ipSetIds": ip_set_ids
  366. }
  367. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config=config)
  368. @required(ip_group_id=(bytes, str), ip_set_ids=list)
  369. def unbind_ip_set(self, ip_group_id, ip_set_ids, client_token=None, config=None):
  370. """
  371. Unbind IpSet from IpGroup.
  372. :param ip_group_id:
  373. The id of the IpGroup which you want to remove IpSet.
  374. :type ip_group_id: string
  375. :param ip_set_ids:
  376. A list of IpSet ids.
  377. :type ip_set_ids: list
  378. :param client_token:
  379. An ASCII string whose length is less than 64.
  380. The request will be idempotent if clientToken is provided.
  381. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  382. :type client_token: string
  383. :param config:
  384. :type config: baidubce.BceClientConfiguration
  385. :return:
  386. :rtype baidubce.bce_response.BceResponse
  387. """
  388. path = b'/ipGroup/' + compat.convert_to_string(ip_group_id) + b'/unbindIpSet'
  389. params = {}
  390. if client_token is None:
  391. params[b'clientToken'] = generate_client_token()
  392. else:
  393. params[b'clientToken'] = client_token
  394. body = {
  395. "ipSetIds": ip_set_ids
  396. }
  397. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config=config)
  398. @required(ip_group_id=(bytes, str))
  399. def update_ip_group(self, ip_group_id, name=None, description=None, client_token=None, config=None):
  400. """
  401. Update the IpGroup.
  402. :param ip_group_id:
  403. The id of the IpGroup which you want to update.
  404. :type ip_group_id: string
  405. :param name:
  406. The new name of the IpGroup.
  407. :type name: string
  408. :param description:
  409. The new description of the IpGroup.
  410. :type description: string
  411. :param client_token:
  412. An ASCII string whose length is less than 64.
  413. The request will be idempotent if clientToken is provided.
  414. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  415. :type client_token: string
  416. :param config:
  417. :type config: baidubce.BceClientConfiguration
  418. :return:
  419. :rtype baidubce.bce_response.BceResponse
  420. """
  421. path = b'/ipGroup/' + compat.convert_to_string(ip_group_id)
  422. params = {}
  423. if client_token is None:
  424. params[b'clientToken'] = generate_client_token()
  425. else:
  426. params[b'clientToken'] = client_token
  427. params[b'modifyAttribute'] = None
  428. body = {}
  429. if name is not None:
  430. body['name'] = name
  431. if description is not None:
  432. body['description'] = description
  433. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params, config=config)
  434. @required(ip_group_id=(bytes, str))
  435. def delete_ip_group(self, ip_group_id, client_token=None, config=None):
  436. """
  437. Delete the specified IpGroup.
  438. :param ip_group_id:
  439. The id of the IpGroup which you want to update.
  440. :type ip_group_id: string
  441. :param client_token:
  442. An ASCII string whose length is less than 64.
  443. The request will be idempotent if clientToken is provided.
  444. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  445. :type client_token: string
  446. :param config:
  447. :type config: baidubce.BceClientConfiguration
  448. :return:
  449. :rtype baidubce.bce_response.BceResponse
  450. """
  451. path = b'/ipGroup/' + compat.convert_to_string(ip_group_id)
  452. params = {}
  453. if client_token is None:
  454. params[b'clientToken'] = generate_client_token()
  455. else:
  456. params[b'clientToken'] = client_token
  457. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  458. def list_ip_group(self, ip_version=None, marker=None, max_keys=None, config=None):
  459. """
  460. Return a list of all IpGroups owned by the authenticated user.
  461. :param ip_version:
  462. The version of IP addresses that are returned.
  463. Valid values are IPv4 and IPv6.
  464. :type ip_version: string
  465. :param marker:
  466. The optional parameter marker specified in the original request to specify
  467. where in the results to begin listing.
  468. Together with the marker, specifies the list result which listing should begin.
  469. If the marker is not specified, the list result will listing from the first one.
  470. :type marker: string
  471. :param max_keys:
  472. The optional parameter to specifies the max number of list result to return.
  473. The default value is 1000.
  474. :type max_keys: int
  475. :param config:
  476. :type config: baidubce.BceClientConfiguration
  477. :return:
  478. :rtype baidubce.bce_response.BceResponse
  479. """
  480. path = b'/ipGroup'
  481. params = {"maxKeys": 1000}
  482. if ip_version is not None:
  483. params[b'ip_version'] = ip_version
  484. if marker is not None:
  485. params[b'marker'] = marker
  486. if max_keys is not None:
  487. params[b'maxKeys'] = max_keys
  488. return self._send_request(http_methods.GET, path, params=params, config=config)
  489. @required(ip_group_id=(bytes, str))
  490. def get_ip_group_detail(self, ip_group_id, config=None):
  491. """
  492. Get the detail information of specified IpGroup.
  493. :param ip_group_id:
  494. The id of the IpGroup which you want to query.
  495. :type ip_group_id: string
  496. :param config:
  497. :type config: baidubce.BceClientConfiguration
  498. :return:
  499. :rtype baidubce.bce_response.BceResponse
  500. """
  501. path = b'/ipGroup/' + compat.convert_to_string(ip_group_id)
  502. return self._send_request(http_methods.GET, path, config=config)
  503. def generate_client_token_by_uuid():
  504. """
  505. The default method to generate the random string for client_token
  506. if the optional parameter client_token is not specified by the user.
  507. :return:
  508. :rtype string
  509. """
  510. return str(uuid.uuid4())
  511. generate_client_token = generate_client_token_by_uuid