nat_client.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions
  15. # and limitations under the License.
  16. """
  17. This module provides a client class for NAT.
  18. """
  19. import copy
  20. import json
  21. import logging
  22. import uuid
  23. import sys
  24. from baidubce import bce_base_client
  25. from baidubce.auth import bce_v1_signer
  26. from baidubce.http import bce_http_client
  27. from baidubce.http import handler
  28. from baidubce.http import http_methods
  29. from baidubce.services.vpc import nat_model
  30. from baidubce import utils
  31. from baidubce.utils import required
  32. from baidubce import compat
  33. if sys.version < '3':
  34. reload(sys)
  35. sys.setdefaultencoding('utf-8')
  36. _logger = logging.getLogger(__name__)
  37. default_billing_to_purchase_created = nat_model.Billing('Postpaid')
  38. default_billing_to_purchase_reserved = nat_model.Billing()
  39. class NatClient(bce_base_client.BceBaseClient):
  40. """
  41. NAT sdk client
  42. """
  43. version = b'/v1'
  44. def __init__(self, config=None):
  45. bce_base_client.BceBaseClient.__init__(self, config)
  46. def _merge_config(self, config=None):
  47. """
  48. :param config:
  49. :type config: baidubce.BceClientConfiguration
  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. config = self._merge_config(config)
  61. if body_parser is None:
  62. body_parser = handler.parse_json
  63. if headers is None:
  64. headers = {b'Accept': b'*/*', b'Content-Type':
  65. b'application/json;charset=utf-8'}
  66. return bce_http_client.send_request(
  67. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  68. http_method, path, body, headers, params)
  69. @required(name=(bytes, str),
  70. vpc_id=(bytes, str))
  71. def create_nat(self, name, vpc_id, spec=None, billing=None, eips=None, dnat_eips=None, bind_eips=None,
  72. client_token=None, config=None, cu_num=None, tags=None, resource_group_id=None, delete_protect=None):
  73. """
  74. Create a nat-gateway with the specified options.
  75. A nat gateway can bind only one public EIP,
  76. but can bind one or more EIPs in shared-bandwidth group
  77. :param client_token:
  78. An ASCII string whose length is less than 64.
  79. The request will be idempotent if client token is provided.
  80. :type client_token: string
  81. :param name:
  82. The name of nat-gateway that will be created.
  83. :type name: string
  84. :param vpc_id:
  85. The id of VPC.
  86. :type vpc_id: string
  87. :param spec:
  88. The size of nat-gateway that needs to be created.
  89. It includes the following three types:
  90. small: support 5 public-IP-bindings at most,
  91. medium: support 10 public-IP-bindings at most,
  92. large: support 15 public-IP-bindings at most.
  93. :type spec: string
  94. :param cu_num: The number of CU.
  95. :type cu_num: int
  96. :param billing:
  97. Billing information.
  98. :type billing: nat_model.Billing
  99. :param eips:
  100. A public EIP or one/more EIPs in shared-bandwidth group,
  101. which will be bound with nat-gateway.
  102. :type eips: list<String>
  103. :param dnat_eips:
  104. :type dnat_eips: list<String>
  105. :param bind_eips:
  106. A public EIP or one/more EIPs in shared-bandwidth group,
  107. which will be bound with nat-gateway, only for enhance nat.
  108. :type bind_eips: list<String>
  109. :param tags:
  110. The tags of nat-gateway that will be created.
  111. :type tags: list<String>
  112. :param resource_group_id:
  113. The resource group id of nat-gateway that will be created.
  114. :type resource_group_id: string
  115. :param delete_protect:
  116. The delete protect switch of nat-gateway that will be created.
  117. :type delete_protect: bool
  118. :param config:
  119. :type config: baidubce.BceClientConfiguration
  120. :return:
  121. :rtype baidubce.bce_response.BceResponse
  122. """
  123. path = utils.append_uri(self.version, 'nat')
  124. if client_token is None:
  125. client_token = generate_client_token()
  126. params = {b'clientToken': client_token}
  127. if billing is None:
  128. billing = default_billing_to_purchase_created
  129. body = {
  130. 'name': compat.convert_to_string(name),
  131. 'vpcId': compat.convert_to_string(vpc_id),
  132. 'billing': billing.__dict__,
  133. 'deleteProtect': delete_protect
  134. }
  135. if eips is not None:
  136. body['eips'] = eips
  137. if dnat_eips is not None:
  138. body['dnatEips'] = dnat_eips
  139. if bind_eips is not None:
  140. body['bindEips'] = bind_eips
  141. if cu_num is not None:
  142. body['cuNum'] = compat.convert_to_string(cu_num)
  143. if spec is not None:
  144. body['spec'] = compat.convert_to_string(spec)
  145. if tags is not None:
  146. tag_list = [tag.__dict__ for tag in tags]
  147. body['tags'] = tag_list
  148. if resource_group_id is not None:
  149. body['resourceGroupId'] = resource_group_id
  150. return self._send_request(http_methods.POST,
  151. path, body=json.dumps(body),
  152. params=params, config=config)
  153. @required(vpc_id=(bytes, str))
  154. def list_nats(self, vpc_id=None, nat_id=None, name=None,
  155. ip=None, marker=None, max_keys=None, config=None):
  156. """
  157. Return a list of nat-gateways, according to the ID,
  158. name or bound EIP of nat-gateways. If not specified,
  159. will return a full list of nat gateways in VPC.
  160. :param vpc_id:
  161. The id of VPC.
  162. :type vpc_id: string
  163. :param nat_id:
  164. The id of specified nat-gateway.
  165. :type nat_id: string
  166. :param name:
  167. The name of specified nat-gateway.
  168. :type name: string
  169. :param ip:
  170. The EIP associated with nat-gateway.
  171. :type ip: string
  172. :param marker:
  173. The optional parameter marker specified in the original
  174. request to specify where in the results to begin listing.
  175. Together with the marker, specifies the list result which
  176. listing should begin. If the marker is not specified,
  177. the list result will listing from the first one.
  178. :type marker: string
  179. :param max_keys:
  180. The optional parameter to specifies the max number of list
  181. result to return.
  182. The default value is 1000.
  183. :type max_keys: int
  184. :param config:
  185. :type config: baidubce.BceClientConfiguration
  186. :return:
  187. :rtype baidubce.bce_response.BceResponse
  188. """
  189. path = utils.append_uri(self.version, 'nat')
  190. params = {}
  191. if vpc_id is not None:
  192. params[b'vpcId'] = vpc_id
  193. if nat_id is not None:
  194. params[b'natId'] = nat_id
  195. if name is not None:
  196. params[b'name'] = name
  197. if ip is not None:
  198. params[b'ip'] = ip
  199. if marker is not None:
  200. params[b'marker'] = marker
  201. if max_keys is not None:
  202. params[b'maxKeys'] = max_keys
  203. return self._send_request(http_methods.GET, path,
  204. params=params, config=config)
  205. @required(nat_id=(bytes, str))
  206. def get_nat(self, nat_id, config=None):
  207. """
  208. Get the detail information of a specified nat-gateway.
  209. :param nat_id:
  210. The id of specified nat-gateway.
  211. :type nat_id: string
  212. :param config:
  213. :type config: baidubce.BceClientConfiguration
  214. :return:
  215. :rtype baidubce.bce_response.BceResponse
  216. """
  217. path = utils.append_uri(self.version, 'nat', nat_id)
  218. return self._send_request(http_methods.GET, path, config=config)
  219. @required(nat_id=(bytes, str), name=(bytes, str))
  220. def update_nat(self, nat_id, name, client_token=None, config=None):
  221. """
  222. Update the name of a specified nat-gateway.
  223. :param nat_id:
  224. The id of specified nat-gateway.
  225. :type nat_id: string
  226. :param name:
  227. The new name of the nat-gateway
  228. :type name: string
  229. :param client_token:
  230. An ASCII string whose length is less than 64.
  231. The request will be idempotent if clientToken is provided.
  232. If the clientToken is not specified by user,
  233. a random String generated by default algorithm will be used.
  234. :type client_token: string
  235. :param config:
  236. :type config: baidubce.BceClientConfiguration
  237. :return:
  238. :rtype baidubce.bce_response.BceResponse
  239. """
  240. path = utils.append_uri(self.version, 'nat', nat_id)
  241. if client_token is None:
  242. client_token = generate_client_token()
  243. params = {
  244. b'clientToken': client_token
  245. }
  246. body = {
  247. 'name': compat.convert_to_string(name)
  248. }
  249. return self._send_request(http_methods.PUT,
  250. path, body=json.dumps(body),
  251. params=params, config=config)
  252. @required(nat_id=(bytes, str), eips=list)
  253. def bind_eip(self, nat_id, eips, client_token=None, config=None):
  254. """
  255. Bind EIPs to a specified nat gateway.
  256. If a EIP is already bound to the nat gateway, unbind it first.
  257. If a shared_bandwidth EIP is bound to the nat gateway,
  258. one can bind more shared_bandwidth EIPs.
  259. :param nat_id:
  260. The id of specified nat-gateway.
  261. :type nat_id: string
  262. :param eips:
  263. A public EIP or one/more EIPs in shared-bandwidth group,
  264. which will be bound with the nat-gateway.
  265. :type eips: list<String>
  266. :param client_token:
  267. An ASCII string whose length is less than 64.
  268. The request will be idempotent if clientToken is provided.
  269. If the clientToken is not specified by user,
  270. a random String generated by default algorithm will be used.
  271. :type client_token: string
  272. :param config:
  273. :type config: baidubce.BceClientConfiguration
  274. :return:
  275. :rtype baidubce.bce_response.BceResponse
  276. """
  277. path = utils.append_uri(self.version, 'nat', nat_id)
  278. if client_token is None:
  279. client_token = generate_client_token()
  280. params = {
  281. b'bind': None,
  282. b'clientToken': client_token
  283. }
  284. body = {
  285. 'eips': eips
  286. }
  287. return self._send_request(http_methods.PUT,
  288. path, body=json.dumps(body),
  289. params=params, config=config)
  290. @required(nat_id=(bytes, str), eips=list)
  291. def unbind_eip(self, nat_id, eips, client_token=None, config=None):
  292. """
  293. Unbind EIPs of a specified nat gateway.
  294. :param nat_id:
  295. The id of specified nat-gateway.
  296. :type nat_id: string
  297. :param eips:
  298. EIP address list to be unbound
  299. :type eips: list<String>
  300. :param client_token:
  301. An ASCII string whose length is less than 64.
  302. The request will be idempotent if clientToken is provided.
  303. If the clientToken is not specified by user,
  304. a random String generated by default algorithm will be used.
  305. :type client_token: string
  306. :param config:
  307. :type config: baidubce.BceClientConfiguration
  308. :return:
  309. :rtype baidubce.bce_response.BceResponse
  310. """
  311. path = utils.append_uri(self.version, 'nat', nat_id)
  312. if client_token is None:
  313. client_token = generate_client_token()
  314. params = {
  315. b'unbind': None,
  316. b'clientToken': client_token
  317. }
  318. body = {
  319. 'eips': eips
  320. }
  321. return self._send_request(http_methods.PUT,
  322. path, body=json.dumps(body),
  323. params=params, config=config)
  324. @required(nat_id=(bytes, str))
  325. def delete_nat(self, nat_id, client_token=None, config=None):
  326. """
  327. Delete specified nat-gateway.
  328. If prepaid nat-gateway is not expired, it cannot be deleted.
  329. :param nat_id:
  330. The id of specified nat-gateway.
  331. :type nat_id: string
  332. :param client_token:
  333. An ASCII string whose length is less than 64.
  334. The request will be idempotent if clientToken is provided.
  335. If the clientToken is not specified by user,
  336. a random String generated by default algorithm will be used.
  337. :type client_token: string
  338. :param config:
  339. :type config: baidubce.BceClientConfiguration
  340. :return:
  341. :rtype baidubce.bce_response.BceResponse
  342. """
  343. path = utils.append_uri(self.version, 'nat', nat_id)
  344. if client_token is None:
  345. client_token = generate_client_token()
  346. params = {
  347. b'clientToken': client_token
  348. }
  349. return self._send_request(http_methods.DELETE,
  350. path, params=params, config=config)
  351. @required(nat_id=(bytes, str))
  352. def purchase_reserved_nat(self, nat_id, billing, client_token=None,
  353. config=None):
  354. """
  355. Renew specified nat-gateway. Postpaid NAT cannot be renewed
  356. :param nat_id:
  357. The id of specified nat-gateway.
  358. :type nat_id: string
  359. :param client_token:
  360. An ASCII string whose length is less than 64.
  361. The request will be idempotent if clientToken is provided.
  362. If the clientToken is not specified by user,
  363. a random String generated by default algorithm will be used.
  364. :type client_token: string
  365. :param billing:
  366. Billing information.
  367. :type billing: nat_model.Billing
  368. :param config:
  369. :type config: baidubce.BceClientConfiguration
  370. :return:
  371. :rtype baidubce.bce_response.BceResponse
  372. """
  373. path = utils.append_uri(self.version, 'nat', nat_id)
  374. if client_token is None:
  375. client_token = generate_client_token()
  376. params = {
  377. b'purchaseReserved': None,
  378. b'clientToken': client_token
  379. }
  380. if billing is None:
  381. billing = default_billing_to_purchase_reserved
  382. body = {
  383. 'billing': billing.__dict__
  384. }
  385. return self._send_request(http_methods.PUT,
  386. path, body=json.dumps(body),
  387. params=params, config=config)
  388. @required(nat_id=(bytes, str), cu_num=int)
  389. def resize_nat(self, nat_id, cu_num, client_token=None, config=None):
  390. """
  391. Resize a specified enhance nat-gateway.
  392. :param nat_id:
  393. The id of specified enhance nat-gateway.
  394. :type nat_id: string
  395. :param cu_num:
  396. The number of CU.
  397. :type cu_num: int
  398. :param client_token:
  399. An ASCII string whose length is less than 64.
  400. The request will be idempotent if clientToken is provided.
  401. If the clientToken is not specified by user,
  402. a random String generated by default algorithm will be used.
  403. :type client_token: string
  404. :param config:
  405. :type config: baidubce.BceClientConfiguration
  406. :return:
  407. :rtype baidubce.bce_response.BceResponse
  408. """
  409. path = utils.append_uri(self.version, 'nat', nat_id)
  410. if client_token is None:
  411. client_token = generate_client_token()
  412. params = {
  413. b'resize': None,
  414. b'clientToken': client_token
  415. }
  416. body = {
  417. 'cuNum': compat.convert_to_string(cu_num)
  418. }
  419. return self._send_request(http_methods.PUT,
  420. path, body=json.dumps(body),
  421. params=params, config=config)
  422. @required(nat_id=(bytes, str), delete_protect=bool)
  423. def update_delete_protect(self, nat_id, delete_protect, client_token=None, config=None):
  424. """
  425. :param nat_id:
  426. The id of specified nat-gateway.
  427. :type nat_id: string
  428. :param delete_protect:
  429. Whether to enable deletion protection on the nat.
  430. :type delete_protect: bool
  431. :return:
  432. :rtype baidubce.bce_response.BceResponse
  433. """
  434. path = utils.append_uri(self.version, 'nat', nat_id, 'deleteProtect')
  435. params = {}
  436. if client_token is None:
  437. params[b'clientToken'] = generate_client_token()
  438. else:
  439. params[b'clientToken'] = client_token
  440. body = {
  441. "deleteProtect": delete_protect
  442. }
  443. return self._send_request(http_methods.PUT,
  444. path, body=json.dumps(body),
  445. params=params, config=config)
  446. @required(nat_id=(bytes, str), eips=list)
  447. def bind_dnat_eip(self, nat_id, eips, client_token=None, config=None):
  448. """
  449. Bind Dnat EIPs to a specified nat gateway.
  450. If a EIP is already bound to nat gateway, need unbind it first.
  451. If a shared_bandwidth EIP is bound to the nat gateway,
  452. one can bind more shared_bandwidth EIPs.
  453. :param nat_id:
  454. The id of specified nat-gateway.
  455. :type nat_id: string
  456. :param eips:
  457. Public EIPs or one/more EIPs in shared-bandwidth group,
  458. which will be bound with the nat-gateway.
  459. :type eips: list<String>
  460. :param client_token:
  461. An ASCII string whose length is less than 64.
  462. The request will be idempotent if clientToken is provided.
  463. If the clientToken is not specified by user,
  464. a random String generated by default algorithm will be used.
  465. :type client_token: string
  466. :param config:
  467. :type config: baidubce.BceClientConfiguration
  468. :return:
  469. :rtype baidubce.bce_response.BceResponse
  470. """
  471. path = utils.append_uri(self.version, 'nat', nat_id)
  472. if client_token is None:
  473. client_token = generate_client_token()
  474. params = {
  475. b'bind': None,
  476. b'clientToken': client_token
  477. }
  478. body = {
  479. 'dnatEips': eips
  480. }
  481. return self._send_request(http_methods.PUT,
  482. path, body=json.dumps(body),
  483. params=params, config=config)
  484. @required(nat_id=(bytes, str), eips=list)
  485. def unbind_dnat_eip(self, nat_id, eips, client_token=None, config=None):
  486. """
  487. Unbind Dnat EIPs of a specified nat gateway.
  488. :param nat_id:
  489. The id of specified nat-gateway.
  490. :type nat_id: string
  491. :param eips:
  492. EIP address list to be unbound
  493. :type eips: list<String>
  494. :param client_token:
  495. An ASCII string whose length is less than 64.
  496. The request will be idempotent if clientToken is provided.
  497. If the clientToken is not specified by user,
  498. a random String generated by default algorithm will be used.
  499. :type client_token: string
  500. :param config:
  501. :type config: baidubce.BceClientConfiguration
  502. :return:
  503. :rtype baidubce.bce_response.BceResponse
  504. """
  505. path = utils.append_uri(self.version, 'nat', nat_id)
  506. if client_token is None:
  507. client_token = generate_client_token()
  508. params = {
  509. b'unbind': None,
  510. b'clientToken': client_token
  511. }
  512. body = {
  513. 'dnatEips': eips
  514. }
  515. return self._send_request(http_methods.PUT,
  516. path, body=json.dumps(body),
  517. params=params, config=config)
  518. @required(nat_id=(bytes, str), eips=list)
  519. def enhance_nat_bind_eip(self, nat_id, bind_eips, client_token=None, config=None):
  520. """
  521. Bind EIPs to a specified enhance nat gateway.
  522. :param nat_id:
  523. The id of specified enhance nat-gateway.
  524. :type nat_id: string
  525. :param bind_eips:
  526. Public EIPs or one/more EIPs in shared-bandwidth group,
  527. which will be bound with the nat-gateway.
  528. :type bind_eips: list<String>
  529. :param client_token:
  530. An ASCII string whose length is less than 64.
  531. The request will be idempotent if clientToken is provided.
  532. If the clientToken is not specified by user,
  533. a random String generated by default algorithm will be used.
  534. :type client_token: string
  535. :param config:
  536. :type config: baidubce.BceClientConfiguration
  537. :return:
  538. :rtype baidubce.bce_response.BceResponse
  539. """
  540. path = utils.append_uri(self.version, 'nat', nat_id)
  541. if client_token is None:
  542. client_token = generate_client_token()
  543. params = {
  544. b'bind': None,
  545. b'clientToken': client_token
  546. }
  547. body = {
  548. 'bindEips': bind_eips
  549. }
  550. return self._send_request(http_methods.PUT,
  551. path, body=json.dumps(body),
  552. params=params, config=config)
  553. @required(nat_id=(bytes, str), eips=list)
  554. def enhance_nat_unbind_eip(self, nat_id, bind_eips, client_token=None, config=None):
  555. """
  556. Unbind EIPs of a specified enhance nat gateway.
  557. :param nat_id:
  558. The id of specified nat-gateway.
  559. :type nat_id: string
  560. :param bind_eips:
  561. EIP address list to be unbound
  562. :type bind_eips: list<String>
  563. :param client_token:
  564. An ASCII string whose length is less than 64.
  565. The request will be idempotent if clientToken is provided.
  566. If the clientToken is not specified by user,
  567. a random String generated by default algorithm will be used.
  568. :type client_token: string
  569. :param config:
  570. :type config: baidubce.BceClientConfiguration
  571. :return:
  572. :rtype baidubce.bce_response.BceResponse
  573. """
  574. path = utils.append_uri(self.version, 'nat', nat_id)
  575. if client_token is None:
  576. client_token = generate_client_token()
  577. params = {
  578. b'unbind': None,
  579. b'clientToken': client_token
  580. }
  581. body = {
  582. 'bindEips': bind_eips
  583. }
  584. return self._send_request(http_methods.PUT,
  585. path, body=json.dumps(body),
  586. params=params, config=config)
  587. @required(nat_id=(bytes, str),
  588. rule_name=(bytes, str),
  589. source_cidr=(bytes, str),
  590. public_ip_address=list)
  591. def create_snat_rule(self, nat_id, rule_name, source_cidr, public_ip_address, client_token=None, config=None):
  592. """
  593. create snat rule of a specified nat gateway.
  594. :param nat_id:
  595. The id of specified nat-gateway.
  596. :type nat_id: string
  597. :param rule_name:
  598. The name of snat rule.
  599. :type rule_name: string
  600. :param source_cidr:
  601. The source cidr of this snat rule.
  602. :type source_cidr: string
  603. :param public_ip_address:
  604. EIP address list to be bound
  605. :type eips: list<String>
  606. :param client_token:
  607. An ASCII string whose length is less than 64.
  608. The request will be idempotent if clientToken is provided.
  609. If the clientToken is not specified by user,
  610. a random String generated by default algorithm will be used.
  611. :type client_token: string
  612. :param config:
  613. :type config: baidubce.BceClientConfiguration
  614. :return:
  615. :rtype baidubce.bce_response.BceResponse
  616. """
  617. path = utils.append_uri(self.version, 'nat', nat_id, 'snatRule')
  618. if client_token is None:
  619. client_token = generate_client_token()
  620. params = {
  621. b'clientToken': client_token
  622. }
  623. body = {
  624. 'ruleName': compat.convert_to_string(rule_name),
  625. 'publicIpsAddress': public_ip_address,
  626. 'sourceCIDR': compat.convert_to_string(source_cidr),
  627. }
  628. return self._send_request(http_methods.POST,
  629. path, body=json.dumps(body),
  630. params=params, config=config)
  631. @required(nat_id=(bytes, str), snat_rules=list)
  632. def batch_create_snat_rule(self, nat_id, snat_rules, client_token=None, config=None):
  633. """
  634. batch create snat rules of a specified nat gateway.
  635. :param nat_id:
  636. The id of specified nat-gateway.
  637. :type nat_id: string
  638. :param snat_rules:
  639. Snat rules, every rule contains ruleName, sourceCIDR and publicIpsAddress.
  640. :type snat_rules: list<dict>
  641. :param client_token:
  642. An ASCII string whose length is less than 64.
  643. The request will be idempotent if clientToken is provided.
  644. If the clientToken is not specified by user,
  645. a random String generated by default algorithm will be used.
  646. :type client_token: string
  647. :param config:
  648. :type config: baidubce.BceClientConfiguration
  649. :return:
  650. :rtype baidubce.bce_response.BceResponse
  651. """
  652. path = utils.append_uri(self.version, 'nat', 'snatRule', 'batchCreate')
  653. if client_token is None:
  654. client_token = generate_client_token()
  655. params = {
  656. b'clientToken': client_token
  657. }
  658. body = {
  659. "natId": compat.convert_to_string(nat_id),
  660. "snatRules": snat_rules
  661. }
  662. return self._send_request(http_methods.POST,
  663. path, body=json.dumps(body),
  664. params=params, config=config)
  665. @required(nat_id=(bytes, str), snat_rule_id=(bytes, str))
  666. def delete_snat_rule(self, nat_id, snat_rule_id, client_token=None, config=None):
  667. """
  668. delete snat rule of a specified nat gateway.
  669. :param nat_id:
  670. The id of specified nat-gateway.
  671. :type nat_id: string
  672. :param snat_rule_id:
  673. The id of specified snat rule.
  674. :type snat_rule_id: string
  675. :param client_token:
  676. An ASCII string whose length is less than 64.
  677. The request will be idempotent if clientToken is provided.
  678. If the clientToken is not specified by user,
  679. a random String generated by default algorithm will be used.
  680. :type client_token: string
  681. :param config:
  682. :type config: baidubce.BceClientConfiguration
  683. :return:
  684. :rtype baidubce.bce_response.BceResponse
  685. """
  686. path = utils.append_uri(self.version, 'nat', nat_id, 'snatRule', snat_rule_id)
  687. if client_token is None:
  688. client_token = generate_client_token()
  689. params = {
  690. b'clientToken': client_token
  691. }
  692. return self._send_request(http_methods.DELETE,
  693. path, params=params, config=config)
  694. @required(nat_id=(bytes, str), snat_rule_id=(bytes, str))
  695. def update_snat_rule(self, nat_id, snat_rule_id, rule_name=None, source_cidr=None,
  696. public_ip_address=None, client_token=None, config=None):
  697. """
  698. update snat rule of a specified nat gateway.
  699. :param nat_id:
  700. The id of specified nat-gateway.
  701. :type nat_id: string
  702. :param snat_rule_id:
  703. The id of specified snat rule.
  704. :type snat_rule_id: string
  705. :param rule_name:
  706. The name of snat rule.
  707. :type rule_name: string
  708. :param source_cidr:
  709. The source cidr of this snat rule.
  710. :type source_cidr: string
  711. :param public_ip_address:
  712. EIP address list to be update.
  713. :type eips: list<String>
  714. :param client_token:
  715. An ASCII string whose length is less than 64.
  716. The request will be idempotent if clientToken is provided.
  717. If the clientToken is not specified by user,
  718. a random String generated by default algorithm will be used.
  719. :type client_token: string
  720. :param config:
  721. :type config: baidubce.BceClientConfiguration
  722. :return:
  723. :rtype baidubce.bce_response.BceResponse
  724. """
  725. path = utils.append_uri(self.version, 'nat', nat_id, 'snatRule', snat_rule_id)
  726. if client_token is None:
  727. client_token = generate_client_token()
  728. params = {
  729. b'clientToken': client_token
  730. }
  731. body = {}
  732. if rule_name is not None:
  733. body['ruleName'] = compat.convert_to_string(rule_name)
  734. if public_ip_address is not None:
  735. body['publicIpsAddress'] = public_ip_address
  736. if source_cidr is not None:
  737. body['sourceCIDR'] = compat.convert_to_string(source_cidr)
  738. return self._send_request(http_methods.PUT,
  739. path, body=json.dumps(body),
  740. params=params, config=config)
  741. @required(nat_id=(bytes, str))
  742. def list_snat_rule(self, nat_id, marker=None, maxKeys=None, client_token=None, config=None):
  743. """
  744. list snat rules of a specified nat gateway.
  745. :param nat_id:
  746. The id of specified nat-gateway.
  747. :type nat_id: string
  748. :param marker:
  749. The optional parameter marker specified in the original
  750. request to specify where in the results to begin listing.
  751. Together with the marker, specifies the list result which
  752. listing should begin. If the marker is not specified,
  753. the list result will listing from the first one.
  754. :type marker: string
  755. :param max_keys:
  756. The optional parameter to specifies the max number of list
  757. result to return.
  758. The default value is 1000.
  759. :type max_keys: int
  760. :param client_token:
  761. An ASCII string whose length is less than 64.
  762. The request will be idempotent if clientToken is provided.
  763. If the clientToken is not specified by user,
  764. a random String generated by default algorithm will be used.
  765. :type client_token: string
  766. :param config:
  767. :type config: baidubce.BceClientConfiguration
  768. :return:
  769. :rtype baidubce.bce_response.BceResponse
  770. """
  771. path = utils.append_uri(self.version, 'nat', nat_id, 'snatRule')
  772. if client_token is None:
  773. client_token = generate_client_token()
  774. params = {
  775. b'clientToken': client_token
  776. }
  777. if marker is not None:
  778. params[b'marker'] = marker
  779. if maxKeys is not None:
  780. params[b'maxKeys'] = maxKeys
  781. return self._send_request(http_methods.GET,
  782. path, params=params, config=config)
  783. @required(nat_id=(bytes, str),
  784. public_ip_address=(bytes, str),
  785. private_ip_address=(bytes, str),
  786. protocol=(bytes, str))
  787. def create_dnat_rule(self, nat_id, public_ip_address, private_ip_address, protocol, rule_name=None,
  788. public_port=None, private_port=None, client_token=None, config=None):
  789. """
  790. create dnat rule of a specified nat gateway.
  791. :param nat_id:
  792. The id of specified nat-gateway.
  793. :type nat_id: string
  794. :param rule_name:
  795. The name of this dnat rule.
  796. :type rule_name: string
  797. :param public_ip_address:
  798. The public ip address of this dnat rule.
  799. :type public_ip_address: string
  800. :param private_ip_address:
  801. The private ip address of this dnat rule.
  802. :type private_ip_address: string
  803. :param protocol:
  804. protocol, support TCP、UDP、all
  805. :type protocol: string
  806. :param public_port:
  807. public port
  808. :type public_port: string
  809. :param private_port:
  810. private port
  811. :type private_port: string
  812. :param client_token:
  813. An ASCII string whose length is less than 64.
  814. The request will be idempotent if clientToken is provided.
  815. If the clientToken is not specified by user,
  816. a random String generated by default algorithm will be used.
  817. :type client_token: string
  818. :param config:
  819. :type config: baidubce.BceClientConfiguration
  820. :return:
  821. :rtype baidubce.bce_response.BceResponse
  822. """
  823. path = utils.append_uri(self.version, 'nat', nat_id, 'dnatRule')
  824. if client_token is None:
  825. client_token = generate_client_token()
  826. params = {
  827. b'clientToken': client_token
  828. }
  829. body = {
  830. 'ruleName': compat.convert_to_string(rule_name),
  831. 'publicIpAddress': compat.convert_to_string(public_ip_address),
  832. 'privateIpAddress': compat.convert_to_string(private_ip_address),
  833. 'protocol': compat.convert_to_string(protocol),
  834. }
  835. if public_port is not None:
  836. body['publicPort'] = compat.convert_to_string(public_port)
  837. if private_port is not None:
  838. body['privatePort'] = compat.convert_to_string(private_port)
  839. return self._send_request(http_methods.POST,
  840. path, body=json.dumps(body),
  841. params=params, config=config)
  842. @required(nat_id=(bytes, str), dnat_rules=list)
  843. def batch_create_dnat_rule(self, nat_id, dnat_rules, client_token=None, config=None):
  844. """
  845. batch create dnat rule of a specified nat gateway.
  846. :param nat_id:
  847. The id of specified nat-gateway.
  848. :type nat_id: string
  849. :param dnat_rules:
  850. Dnat rules, every rule contains ruleName, publicIpAddress, privateIpAddress, protocol, privatePort, publicPort.
  851. :type snat_rules: list<dict>
  852. :param client_token:
  853. An ASCII string whose length is less than 64.
  854. The request will be idempotent if clientToken is provided.
  855. If the clientToken is not specified by user,
  856. a random String generated by default algorithm will be used.
  857. :type client_token: string
  858. :param config:
  859. :type config: baidubce.BceClientConfiguration
  860. :return:
  861. :rtype baidubce.bce_response.BceResponse
  862. """
  863. path = utils.append_uri(self.version, 'nat', nat_id, 'dnatRule', 'batchCreate')
  864. if client_token is None:
  865. client_token = generate_client_token()
  866. params = {
  867. b'clientToken': client_token
  868. }
  869. body = {
  870. "rules": dnat_rules
  871. }
  872. return self._send_request(http_methods.POST,
  873. path, body=json.dumps(body),
  874. params=params, config=config)
  875. @required(nat_id=(bytes, str), snat_rule_id=(bytes, str))
  876. def delete_dnat_rule(self, nat_id, dnat_rule_id, client_token=None, config=None):
  877. """
  878. delete dnat rule of a specified nat gateway.
  879. :param nat_id:
  880. The id of specified nat-gateway.
  881. :type nat_id: string
  882. :param dnat_rule_id:
  883. The id of specified snat rule.
  884. :type snat_rule_id: string
  885. :param client_token:
  886. An ASCII string whose length is less than 64.
  887. The request will be idempotent if clientToken is provided.
  888. If the clientToken is not specified by user,
  889. a random String generated by default algorithm will be used.
  890. :type client_token: string
  891. :param config:
  892. :type config: baidubce.BceClientConfiguration
  893. :return:
  894. :rtype baidubce.bce_response.BceResponse
  895. """
  896. path = utils.append_uri(self.version, 'nat', nat_id, 'dnatRule', dnat_rule_id)
  897. if client_token is None:
  898. client_token = generate_client_token()
  899. params = {
  900. b'clientToken': client_token
  901. }
  902. return self._send_request(http_methods.DELETE,
  903. path, params=params, config=config)
  904. @required(nat_id=(bytes, str), rule_id=(bytes, str))
  905. def update_dnat_rule(self, nat_id, dnat_rule_id,
  906. public_ip_address=None, private_ip_address=None,
  907. rule_name=None, protocol=None, public_port=None,
  908. private_port=None, client_token=None, config=None):
  909. """
  910. update dnat rule of a specified nat gateway.
  911. :param nat_id:
  912. The id of specified nat-gateway.
  913. :type nat_id: string
  914. :param rule_id:
  915. The id of specified dnat rule.
  916. :type rule_id: string
  917. :param rule_name:
  918. The name of dnat rule.
  919. :type rule_name: string
  920. :param public_ip_address:
  921. The public ip address of this dnat rule.
  922. :type public_ip_address: string
  923. :param private_ip_address:
  924. The private ip address of this dnat rule.
  925. :type private_ip_address: string
  926. :param protocol:
  927. protocol
  928. :type protocol: string
  929. :param public_port:
  930. public port
  931. :type public_port: string
  932. :param private_port:
  933. private port
  934. :type private_port: string
  935. :param client_token:
  936. An ASCII string whose length is less than 64.
  937. The request will be idempotent if clientToken is provided.
  938. If the clientToken is not specified by user,
  939. a random String generated by default algorithm will be used.
  940. :type client_token: string
  941. :param config:
  942. :type config: baidubce.BceClientConfiguration
  943. :return:
  944. :rtype baidubce.bce_response.BceResponse
  945. """
  946. path = utils.append_uri(self.version, 'nat', nat_id, 'dnatRule', dnat_rule_id)
  947. if client_token is None:
  948. client_token = generate_client_token()
  949. params = {
  950. b'clientToken': client_token
  951. }
  952. body = {}
  953. if rule_name is not None:
  954. body[b'ruleName'] = compat.convert_to_string(rule_name)
  955. if protocol is not None:
  956. body[b'protocol'] = compat.convert_to_string(protocol)
  957. if public_ip_address is not None:
  958. body[b'publicIpAddress'] = compat.convert_to_string(public_ip_address)
  959. if private_ip_address is not None:
  960. body[b'privateIpAddress'] = compat.convert_to_string(private_ip_address)
  961. if public_port is not None:
  962. body[b'publicPort'] = compat.convert_to_string(public_port)
  963. if private_port is not None:
  964. body[b'privatePort'] = compat.convert_to_string(private_port)
  965. return self._send_request(http_methods.POST,
  966. path, body=json.dumps(body),
  967. params=params, config=config)
  968. @required(nat_id=(bytes, str))
  969. def list_dnat_rule(self, nat_id, marker=None, maxKeys=None, client_token=None, config=None):
  970. """
  971. list dnat rules of a specified nat gateway.
  972. :param nat_id:
  973. The id of specified nat-gateway.
  974. :type nat_id: string
  975. :param marker:
  976. The optional parameter marker specified in the original
  977. request to specify where in the results to begin listing.
  978. Together with the marker, specifies the list result which
  979. listing should begin. If the marker is not specified,
  980. the list result will listing from the first one.
  981. :type marker: string
  982. :param max_keys:
  983. The optional parameter to specifies the max number of list
  984. result to return.
  985. The default value is 1000.
  986. :type max_keys: int
  987. :param client_token:
  988. An ASCII string whose length is less than 64.
  989. The request will be idempotent if clientToken is provided.
  990. If the clientToken is not specified by user,
  991. a random String generated by default algorithm will be used.
  992. :type client_token: string
  993. :param config:
  994. :type config: baidubce.BceClientConfiguration
  995. :return:
  996. :rtype baidubce.bce_response.BceResponse
  997. """
  998. path = utils.append_uri(self.version, 'nat', nat_id, 'dnatRule')
  999. if client_token is None:
  1000. client_token = generate_client_token()
  1001. params = {
  1002. b'clientToken': client_token
  1003. }
  1004. if marker is not None:
  1005. params[b'marker'] = marker
  1006. if maxKeys is not None:
  1007. params[b'maxKeys'] = maxKeys
  1008. return self._send_request(http_methods.GET,
  1009. path, params=params, config=config)
  1010. def generate_client_token_by_uuid():
  1011. """
  1012. The default method to generate the random string for client_token
  1013. if the optional parameter client_token is not specified by the user.
  1014. :return:
  1015. :rtype string
  1016. """
  1017. return str(uuid.uuid4())
  1018. generate_client_token = generate_client_token_by_uuid