vpn_client.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. """
  2. This module provides a client class for VPN.
  3. """
  4. import copy
  5. import json
  6. import logging
  7. import uuid
  8. from baidubce import bce_base_client
  9. from baidubce.auth import bce_v1_signer
  10. from baidubce.http import bce_http_client
  11. from baidubce.http import handler
  12. from baidubce.http import http_methods
  13. from baidubce import compat
  14. _logger = logging.getLogger(__name__)
  15. class VpnClient(bce_base_client.BceBaseClient):
  16. """
  17. VPN base sdk client
  18. """
  19. prefix = b'/v1'
  20. path = b'/vpn'
  21. def __init__(self, config=None):
  22. bce_base_client.BceBaseClient.__init__(self, config)
  23. def _merge_config(self, config=None):
  24. """
  25. :param config:
  26. :type config: baidubce.BceClientConfiguration
  27. :return:
  28. """
  29. if config is None:
  30. return self.config
  31. else:
  32. new_config = copy.copy(self.config)
  33. new_config.merge_non_none_values(config)
  34. return new_config
  35. def _send_request(self, http_method, path,
  36. body=None, headers=None, params=None,
  37. config=None, body_parser=None):
  38. config = self._merge_config(config)
  39. if body_parser is None:
  40. body_parser = handler.parse_json
  41. if headers is None:
  42. headers = {b'Accept': b'*/*', b'Content-Type': b'application/json;charset=utf-8'}
  43. return bce_http_client.send_request(
  44. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  45. http_method, VpnClient.prefix + path, body, headers, params)
  46. def list_vpns(self, vpc_id, eip=None, marker=None, max_Keys=None, config=None, vpn_type=None):
  47. """
  48. return all vpn about vpc
  49. :param vpc_id:
  50. vpc id
  51. :type vpcId:string
  52. :param eip:
  53. eip
  54. :type eip:string
  55. :param marker:
  56. The optional parameter marker specified in the original request to specify
  57. where in the results to begin listing.
  58. Together with the marker, specifies the list result which listing should begin.
  59. If the marker is not specified, the list result will listing from the first one.
  60. :type marker: string
  61. :param max_Keys:
  62. The optional parameter to specifies the max number of list result to return.
  63. The default value is 1000.
  64. :type max_Keys: int
  65. :param config:
  66. :type config: baidubce.BceClientConfiguration
  67. :param vpn_type:
  68. type of vpn
  69. :type vpn_type: string
  70. :return:
  71. :rtype baidubce.bce_response.BceResponse
  72. """
  73. params = {b'vpcId': vpc_id}
  74. if marker is not None:
  75. params[b'marker'] = marker
  76. if max_Keys is not None:
  77. params[b'maxKeys'] = max_Keys
  78. if eip is not None:
  79. params[b'eip'] = eip
  80. if vpn_type is not None:
  81. params[b'type'] = vpn_type
  82. return self._send_request(http_methods.GET, VpnClient.path, params=params, config=config)
  83. def create_vpn(self, vpc_id, vpn_name, billing,
  84. vpn_type=None, max_connections=None,
  85. client_token=None, description=None,
  86. eip=None, config=None, subnetId=None,
  87. tags=None, resourceGroupId=None, delete_protect=False):
  88. """
  89. The method of vpn to be created.
  90. :param vpc_id:
  91. vpc id
  92. :type vpc_id: str
  93. :param vpn_name:
  94. the name of name
  95. :type vpn_name: str
  96. :param billing:
  97. order_configuration
  98. :type billing:Billing
  99. :param description:
  100. The description of the vpn.
  101. :type description: string
  102. :param client_token:
  103. An ASCII string whose length is less than 64.
  104. The request will be idempotent if clientToken is provided.
  105. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  106. :type client_token: string
  107. :param eip:
  108. bind eip
  109. :type eip:str
  110. :param config:
  111. :type config: baidubce.BceClientConfiguration
  112. :param subnetId:
  113. subnetId
  114. :type subnetId:str
  115. :param tags:
  116. The tags of the vpn.
  117. :type tags: list
  118. :param resourceGroupId:
  119. The resource group ID of the vpn.
  120. :type resourceGroupId: str
  121. :param delete_protect:
  122. Whether to enable deletion protection on the vpn.
  123. :type delete_protect: bool
  124. :return:
  125. :rtype baidubce.bce_response.BceResponse
  126. """
  127. params = {}
  128. if client_token is None:
  129. params[b'clientToken'] = generate_client_token()
  130. else:
  131. params[b'clientToken'] = client_token
  132. body = {'vpcId': vpc_id,
  133. 'vpnName': vpn_name,
  134. 'billing': {
  135. 'paymentTiming': billing.payment_timing,
  136. 'billingMethod': billing.billing_method,
  137. 'reservation': {
  138. 'reservationLength': billing.reservation_length,
  139. 'reservationTimeUnit': billing.reservation_time_unit
  140. }
  141. },
  142. 'deleteProtect': delete_protect
  143. }
  144. if description is not None:
  145. body['description'] = description
  146. if eip is not None:
  147. body['eip'] = eip
  148. if vpn_type is not None:
  149. body['type'] = vpn_type
  150. if max_connections is not None:
  151. body['maxConnection'] = max_connections
  152. if subnetId is not None:
  153. body['subnetId'] = subnetId
  154. if tags is not None:
  155. tag_list = [tag.__dict__ for tag in tags]
  156. body['tags'] = tag_list
  157. if resourceGroupId is not None:
  158. body['resourceGroupId'] = resourceGroupId
  159. return self._send_request(http_methods.POST, VpnClient.path, body=json.dumps(body), params=params,
  160. config=config)
  161. def update_vpn(self, vpn_id, vpn_name=None, description=None, client_token=None, config=None):
  162. """
  163. The method of vpn to be update.
  164. :param vpn_id: vpn id
  165. :type vpn_id: string
  166. :param vpn_name: vpn name
  167. :type vpn_name: str
  168. :param description: the description of vpn
  169. :type description: str
  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 = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id)
  181. params = {b'modifyAttribute': None}
  182. if client_token is None:
  183. params[b'clientToken'] = generate_client_token()
  184. else:
  185. params[b'clientToken'] = client_token
  186. body = {}
  187. if description is not None:
  188. body['description'] = description
  189. if vpn_name is not None:
  190. body['vpnName'] = vpn_name
  191. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
  192. config=config)
  193. def get_vpn(self, vpn_id, config=None):
  194. """
  195. Get the detail information of vpn.
  196. :param vpn_id:
  197. The id of vpn.
  198. :type vpn_id: string
  199. :param config:
  200. :type config: baidubce.BceClientConfiguration
  201. :return:
  202. :rtype baidubce.bce_response.BceResponse
  203. """
  204. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id)
  205. return self._send_request(http_methods.GET, path, config=config)
  206. def delete_vpn(self, vpn_id, client_token=None, config=None):
  207. """
  208. release VPN
  209. :param vpn_id:
  210. The id of instance.
  211. :type vpn_id: string
  212. :param client_token:
  213. An ASCII string whose length is less than 64.
  214. The request will be idempotent if clientToken is provided.
  215. If the clientToken is not specified by the user, a random String generated by default algorithm will
  216. be used.
  217. :type client_token: string
  218. :param config:
  219. :type config: baidubce.BceClientConfiguration
  220. :return:
  221. :rtype baidubce.bce_response.BceResponse
  222. """
  223. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id)
  224. params = {}
  225. if client_token is None:
  226. params[b'clientToken'] = generate_client_token()
  227. else:
  228. params[b'clientToken'] = client_token
  229. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  230. def bind_eip(self, vpn_id, eip=None, client_token=None, config=None):
  231. """
  232. bind eip
  233. :param vpn_id:
  234. The id of instance.
  235. :type vpn_id: string
  236. :param eip:
  237. The address of eip.
  238. :type eip: string
  239. :param client_token:
  240. An ASCII string whose length is less than 64.
  241. The request will be idempotent if clientToken is provided.
  242. If the clientToken is not specified by the user, a random String generated by default algorithm will
  243. be used.
  244. :type client_token: string
  245. :param config:
  246. :type config: baidubce.BceClientConfiguration
  247. :return:
  248. :rtype baidubce.bce_response.BceResponse
  249. """
  250. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id)
  251. params = {b'bind': None}
  252. if client_token is None:
  253. params[b'clientToken'] = generate_client_token()
  254. else:
  255. params[b'clientToken'] = client_token
  256. body = {'eip': eip}
  257. return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body), config=config)
  258. def unbind_eip(self, vpn_id, client_token=None, config=None):
  259. """
  260. unbind eip
  261. :param vpn_id:
  262. The id of instance.
  263. :type vpn_id: string
  264. :param client_token:
  265. An ASCII string whose length is less than 64.
  266. The request will be idempotent if clientToken is provided.
  267. If the clientToken is not specified by the user, a random String generated by default algorithm will
  268. be used.
  269. :type client_token: string
  270. :param config:
  271. :type config: baidubce.BceClientConfiguration
  272. :return:
  273. :rtype baidubce.bce_response.BceResponse
  274. """
  275. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id)
  276. params = {b'unbind': None}
  277. if client_token is None:
  278. params[b'clientToken'] = generate_client_token()
  279. else:
  280. params[b'clientToken'] = client_token
  281. return self._send_request(http_methods.PUT, path, params=params, config=config)
  282. def renew_vpn(self, vpn_id, billing, client_token=None, config=None):
  283. """
  284. renew vpn
  285. :param vpn_id:
  286. The id of instance.
  287. :type vpn_id: string
  288. :param billing:
  289. order_configuration
  290. :type billing:Billing
  291. :param client_token:
  292. An ASCII string whose length is less than 64.
  293. The request will be idempotent if clientToken is provided.
  294. If the clientToken is not specified by the user, a random String generated by default algorithm will
  295. be used.
  296. :type client_token: string
  297. :param config:
  298. :type config: baidubce.BceClientConfiguration
  299. :return:
  300. :rtype baidubce.bce_response.BceResponse
  301. """
  302. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id)
  303. params = {b'purchaseReserved': None}
  304. if client_token is None:
  305. params[b'clientToken'] = generate_client_token()
  306. else:
  307. params[b'clientToken'] = client_token
  308. body = {'billing': {
  309. 'paymentTiming': billing.payment_timing,
  310. 'billingMethod': billing.billing_method,
  311. 'reservation': {
  312. 'reservationLength': billing.reservation_length,
  313. 'reservationTimeUnit': billing.reservation_time_unit
  314. }
  315. }}
  316. return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body), config=config)
  317. def create_vpn_conn(self, vpn_id, secret_key, local_subnets, remote_ip, remote_subnets, vpn_conn_name,
  318. ike_config, ipsec_config, description=None, client_token=None, config=None):
  319. """
  320. :param vpn_id: vpn id
  321. :type vpn_id: string
  322. :param secret_key:shared key, 8~17 characters, english, numbers and symbols must exist at
  323. the same time,and the symbols are limited to @#$%^*()_
  324. :type secret_key: string
  325. :param local_subnets:local network cidr list
  326. :type local_subnets: list
  327. :param remote_ip:peer vpn gateway public network ip
  328. :type remote_ip: string
  329. :param remote_subnets:peer network cidr list
  330. :type remote_subnets: list
  331. :param vpn_conn_name:vpn tunnel name, uppercase and lowercase letters, numbers and -_/. special
  332. characters, must start with a letter, length 1-6
  333. :type vpn_conn_name: string
  334. :param ike_config:IKE config
  335. :type ike_config: IkeConfig
  336. :param ipsec_config:IPSec config
  337. :type ipsec_config: IpsecConfig
  338. :param description:description
  339. :type description: description
  340. :param client_token:
  341. An ASCII string whose length is less than 64.
  342. The request will be idempotent if clientToken is provided.
  343. If the clientToken is not specified by the user, a random String generated by default algorithm will
  344. be used.
  345. :type client_token: string
  346. :param config:
  347. :type config: baidubce.BceClientConfiguration
  348. :return:
  349. :rtype baidubce.bce_response.BceResponse
  350. """
  351. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) + b'/vpnconn'
  352. params = {}
  353. if client_token is None:
  354. params[b'clientToken'] = generate_client_token()
  355. else:
  356. params[b'clientToken'] = client_token
  357. body = {
  358. 'secretKey': secret_key,
  359. 'localSubnets': local_subnets,
  360. 'remoteIp': remote_ip,
  361. 'remoteSubnets': remote_subnets,
  362. 'vpnConnName': vpn_conn_name,
  363. 'ikeConfig': {
  364. 'ikeVersion': ike_config.ike_version,
  365. 'ikeMode': ike_config.ike_mode,
  366. 'ikeEncAlg': ike_config.ike_enc_alg,
  367. 'ikeAuthAlg': ike_config.ike_auth_alg,
  368. 'ikePfs': ike_config.ike_pfs,
  369. 'ikeLifeTime': ike_config.ike_lifeTime
  370. },
  371. 'ipsecConfig': {
  372. 'ipsecEncAlg': ipsec_config.ipsec_enc_alg,
  373. 'ipsecAuthAlg': ipsec_config.ipsec_auth_alg,
  374. 'ipsecPfs': ipsec_config.ipsec_pfs,
  375. 'ipsecLifetime': ipsec_config.ipsec_lifetime
  376. },
  377. 'description': description,
  378. }
  379. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body), config=config)
  380. def update_vpn_conn(self, vpn_conn_id, vpn_id, secret_key, local_subnets, remote_ip, remote_subnets, vpn_conn_name,
  381. ike_config, ipsec_config, description=None, client_token=None, config=None):
  382. """
  383. :param vpn_conn_id:vpnconn id
  384. :type vpn_conn_id: string
  385. :param vpn_id: vpn id
  386. :type vpn_id: string
  387. :param secret_key:shared key, 8~17 characters, english, numbers and symbols must exist at
  388. the same time,and the symbols are limited to @#$%^*()_
  389. :type secret_key: string
  390. :param local_subnets:local network cidr list
  391. :type local_subnets: list
  392. :param remote_ip:peer vpn gateway public network ip
  393. :type remote_ip: string
  394. :param remote_subnets:peer network cidr list
  395. :type remote_subnets: list
  396. :param vpn_conn_name:vpn tunnel name, uppercase and lowercase letters, numbers and -_/. special
  397. characters, must start with a letter, length 1-6
  398. :type vpn_conn_name: list
  399. :param ike_config:IKE config
  400. :type ike_config: IkeConfig
  401. :param ipsec_config:IPSec config
  402. :type ipsec_config: IpsecConfig
  403. :param description:description
  404. :type description: description
  405. :param client_token:
  406. An ASCII string whose length is less than 64.
  407. The request will be idempotent if clientToken is provided.
  408. If the clientToken is not specified by the user, a random String generated by default algorithm will
  409. be used.
  410. :type client_token: string
  411. :param config:
  412. :type config: baidubce.BceClientConfiguration
  413. :return:
  414. :rtype baidubce.bce_response.BceResponse
  415. """
  416. path = VpnClient.path + b'/vpnconn/' + compat.convert_to_bytes(vpn_conn_id)
  417. params = {}
  418. if client_token is None:
  419. params[b'clientToken'] = generate_client_token()
  420. else:
  421. params[b'clientToken'] = client_token
  422. body = {
  423. 'vpnId': vpn_id,
  424. 'secretKey': secret_key,
  425. 'localSubnets': local_subnets,
  426. 'remoteIp': remote_ip,
  427. 'remoteSubnets': remote_subnets,
  428. 'vpnConnName': vpn_conn_name,
  429. 'ikeConfig': {
  430. 'ike_version': ike_config.ike_version,
  431. 'ike_mode': ike_config.ike_mode,
  432. 'ike_enc_alg': ike_config.ike_enc_alg,
  433. 'ike_auth_alg': ike_config.ike_auth_alg,
  434. 'ike_pfs': ike_config.ike_pfs,
  435. 'ike_lifeTime': ike_config.ike_lifeTime
  436. },
  437. 'ipsecConfig': {
  438. 'ipsec_enc_alg': ipsec_config.ipsec_enc_alg,
  439. 'ipsec_auth_alg': ipsec_config.ipsec_auth_alg,
  440. 'ipsec_pfs': ipsec_config.ipsec_pfs,
  441. 'ipsec_lifetime': ipsec_config.ipsec_lifetime
  442. },
  443. 'description': description,
  444. }
  445. return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body), config=config)
  446. def get_vpn_conn(self, vpn_id, client_token=None, config=None):
  447. """
  448. :param vpn_id: vpn id
  449. :type vpn_id: string
  450. :param client_token:
  451. An ASCII string whose length is less than 64.
  452. The request will be idempotent if clientToken is provided.
  453. If the clientToken is not specified by the user, a random String generated by default algorithm will
  454. be used.
  455. :type client_token: string
  456. :param config:
  457. :type config: baidubce.BceClientConfiguration
  458. :return:
  459. :rtype baidubce.bce_response.BceResponse
  460. """
  461. path = VpnClient.path + b'/vpnconn/' + compat.convert_to_bytes(vpn_id)
  462. params = {}
  463. if client_token is None:
  464. params[b'clientToken'] = generate_client_token()
  465. else:
  466. params[b'clientToken'] = client_token
  467. return self._send_request(http_methods.GET, path, params=params, config=config)
  468. def delete_vpn_conn(self, vpn_conn_id, client_token=None, config=None):
  469. """
  470. :param vpn_conn_id:vpn conn id
  471. :type vpn_conn_id: string
  472. :param client_token:
  473. An ASCII string whose length is less than 64.
  474. The request will be idempotent if clientToken is provided.
  475. If the clientToken is not specified by the user, a random String generated by default algorithm will
  476. be used.
  477. :type client_token: string
  478. :param config:
  479. :type config: baidubce.BceClientConfiguration
  480. :return:
  481. :rtype baidubce.bce_response.BceResponse
  482. """
  483. path = VpnClient.path + b'/vpnconn/' + compat.convert_to_bytes(vpn_conn_id)
  484. params = {}
  485. if client_token is None:
  486. params[b'clientToken'] = generate_client_token()
  487. else:
  488. params[b'clientToken'] = client_token
  489. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  490. def create_vpn_sslservice(self, vpn_id=None, sslservice_name=None, local_routes=None, address_pool=None,
  491. interface_type=None, client_dns=None, client_token=None, config=None):
  492. """
  493. :param vpn_id: vpn id
  494. :type vpn_id: string
  495. :param sslservice_name: ssl service name, uppercase and lowercase letters, numbers and -_/. special
  496. characters, must start with a letter, length 1-6
  497. :type sslservice_name: string
  498. :param local_routes: these cidrs will be configured on the client, and the next hop points to the SSL tunnel. Usually vpc cidrs
  499. :type local_routes: list
  500. :param address_pool: Client IP address pool. The VPN gateway will assign an IP address to the client on this cidr.
  501. :type address_pool: string
  502. :param interface_type: l2 or l3, default is l3, l2 is tap, l3 is tun
  503. :type interface_type: string
  504. :param client_dns: DNS server address
  505. :type client_dns: string
  506. :return:
  507. :rtype baidubce.bce_response.BceResponse
  508. """
  509. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) + b'/sslVpnServer'
  510. params = {}
  511. if client_token is None:
  512. params[b'clientToken'] = generate_client_token()
  513. else:
  514. params[b'clientToken'] = client_token
  515. body = {
  516. 'sslVpnServerName': sslservice_name,
  517. 'localSubnets': local_routes,
  518. 'remoteSubnet': address_pool,
  519. }
  520. if interface_type is not None:
  521. body[b'interfaceType'] = interface_type
  522. else:
  523. body[b'interfaceType'] = b'tun'
  524. if client_dns is not None:
  525. body[b'clientDns'] = client_dns
  526. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body), config=config)
  527. def update_vpn_sslservice(self, vpn_id=None, sslservice_id=None, sslservice_name=None, local_routes=None,
  528. address_pool=None, client_dns=None, client_token=None, config=None):
  529. """
  530. :param vpn_id: vpn id
  531. :type vpn_id: string
  532. :param sslservice_id: id
  533. :type sslservice_id: string
  534. :param sslservice_name: ssl service name, uppercase and lowercase letters, numbers and -_/. special
  535. characters, must start with a letter, length 1-6
  536. :type sslservice_name: string
  537. :param local_routes: these cidrs will be configured on the client, and the next hop points to the SSL tunnel. Usually vpc cidrs
  538. :type local_routes: list
  539. :param address_pool: Client IP address pool. The VPN gateway will assign an IP address to the client on this cidr.
  540. :type address_pool: string
  541. :param client_dns: DNS server address
  542. :type client_dns: string
  543. :return:
  544. :rtype baidubce.bce_response.BceResponse
  545. """
  546. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) \
  547. + b'/sslVpnServer' + b'/' + compat.convert_to_bytes(sslservice_id)
  548. params = {}
  549. if client_token is None:
  550. params[b'clientToken'] = generate_client_token()
  551. else:
  552. params[b'clientToken'] = client_token
  553. body = {}
  554. if sslservice_name is not None:
  555. body[b'sslVpnServerName'] = sslservice_name
  556. if local_routes is not None:
  557. body[b'localSubnets'] = local_routes
  558. if address_pool is not None:
  559. body[b'remoteSubnet'] = address_pool
  560. if client_dns is not None:
  561. body[b'clientDns'] = client_dns
  562. return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body), config=config)
  563. def get_vpn_sslservice(self, vpn_id, client_token=None, config=None):
  564. """
  565. :param vpn_id: vpn id
  566. :type vpn_id: string
  567. :return:
  568. :rtype baidubce.bce_response.BceResponse
  569. """
  570. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) \
  571. + b'/sslVpnServer'
  572. params = {}
  573. if client_token is None:
  574. params[b'clientToken'] = generate_client_token()
  575. else:
  576. params[b'clientToken'] = client_token
  577. return self._send_request(http_methods.GET, path, config=config)
  578. def delete_vpn_sslservice(self, vpn_id, sslservice_id, client_token=None, config=None):
  579. """
  580. :param vpn_id: vpn id
  581. :type vpn_id: string
  582. :param sslservice_id: sslservice id
  583. :type sslservice_id: string
  584. :return:
  585. :rtype baidubce.bce_response.BceResponse
  586. """
  587. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) \
  588. + b'/sslVpnServer' + b'/' + compat.convert_to_bytes(sslservice_id)
  589. params = {}
  590. if client_token is None:
  591. params[b'clientToken'] = generate_client_token()
  592. else:
  593. params[b'clientToken'] = client_token
  594. return self._send_request(http_methods.DELETE, path, config=config)
  595. def create_vpn_sslusers(self, vpn_id, sslusers, client_token=None, config=None):
  596. """
  597. :param vpn_id: vpn id
  598. :type vpn_id: string
  599. :param sslusers: User information list
  600. :type sslusers: list
  601. :return:
  602. :rtype baidubce.bce_response.BceResponse
  603. """
  604. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) + b'/sslVpnUser'
  605. params = {}
  606. if client_token is None:
  607. params[b'clientToken'] = generate_client_token()
  608. else:
  609. params[b'clientToken'] = client_token
  610. body = {
  611. 'sslVpnUsers': []
  612. }
  613. for ssluser in sslusers:
  614. body[b'sslVpnUsers'].append({
  615. 'userName': ssluser.user_name,
  616. 'password': ssluser.password,
  617. 'description': ssluser.description
  618. })
  619. return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body), config=config)
  620. def update_vpn_ssl_user(self, vpn_id, ssluser_id, password=None, description=None, client_token=None, config=None):
  621. """
  622. :param vpn_id: vpn id
  623. :type vpn_id: string
  624. :param ssluser_id: ssluser id
  625. :type ssluser_id: string
  626. :param password: password id
  627. :type password: string
  628. :param description: description
  629. :type description: string
  630. :return:
  631. :rtype baidubce.bce_response.BceResponse
  632. """
  633. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) + b'/sslVpnUser' \
  634. + b'/' + compat.convert_to_bytes(ssluser_id)
  635. params = {}
  636. if client_token is None:
  637. params[b'clientToken'] = generate_client_token()
  638. else:
  639. params[b'clientToken'] = client_token
  640. body = {}
  641. if password is not None:
  642. body[b'password'] = password
  643. if description is not None:
  644. body[b'description'] = description
  645. return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body), config=config)
  646. def get_vpn_ssl_user(self, vpn_id, client_token=None, config=None, marker=None, max_keys=None, user_name=None):
  647. """
  648. :param vpn_id: vpn id
  649. :type vpn_id: string
  650. :param marker:
  651. :param marker:
  652. The optional parameter marker specified in the original request to specify
  653. where in the results to begin listing.
  654. Together with the marker, specifies the list result which listing should begin.
  655. If the marker is not specified, the list result will listing from the first one.
  656. :type marker: string
  657. :param max_keys:
  658. The optional parameter to specifies the max number of list result to return.
  659. The default value is 1000.
  660. :type max_keys: int
  661. :param user_name: user name
  662. :type user_name: string
  663. :return:
  664. :rtype baidubce.bce_response.BceResponse
  665. """
  666. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) + b'/sslVpnUser'
  667. params = {}
  668. if client_token is None:
  669. params[b'clientToken'] = generate_client_token()
  670. else:
  671. params[b'clientToken'] = client_token
  672. if marker is not None:
  673. params[b'marker'] = marker
  674. if max_keys is not None:
  675. params[b'maxKeys'] = max_keys
  676. if user_name is not None:
  677. params[b'userName'] = user_name
  678. return self._send_request(http_methods.GET, path, params=params, config=config)
  679. def delete_vpn_ssl_user(self, vpn_id, ssluser_id, client_token=None, config=None):
  680. """
  681. :param vpn_id: vpn id
  682. :type vpn_id: string
  683. :return:
  684. :rtype baidubce.bce_response.BceResponse
  685. """
  686. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) + b'/sslVpnUser' \
  687. + b'/' + compat.convert_to_bytes(ssluser_id)
  688. params = {}
  689. if client_token is None:
  690. params[b'clientToken'] = generate_client_token()
  691. else:
  692. params[b'clientToken'] = client_token
  693. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  694. def update_vpn_delete_protect(self, vpn_id, delete_protect=False, client_token=None, config=None):
  695. """
  696. :param vpn_id: vpn id
  697. :type vpn_id: string
  698. :param delete_protect:
  699. Whether to enable deletion protection on the vpn.
  700. :type delete_protect: bool
  701. :return:
  702. :rtype baidubce.bce_response.BceResponse
  703. """
  704. path = VpnClient.path + b'/' + compat.convert_to_bytes(vpn_id) + b'/deleteProtect'
  705. params = {}
  706. if client_token is None:
  707. params[b'clientToken'] = generate_client_token()
  708. else:
  709. params[b'clientToken'] = client_token
  710. body = {
  711. "deleteProtect": delete_protect
  712. }
  713. return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body), config=config)
  714. def generate_client_token_by_uuid():
  715. """
  716. The default method to generate the random string for client_token
  717. if the optional parameter client_token is not specified by the user.
  718. :return:
  719. :rtype string
  720. """
  721. return str(uuid.uuid4())
  722. generate_client_token = generate_client_token_by_uuid