ipv6gateway_client.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 IPv6Gateway.
  16. """
  17. import copy
  18. import json
  19. import logging
  20. import uuid
  21. import sys
  22. from baidubce import bce_base_client
  23. from baidubce.auth import bce_v1_signer
  24. from baidubce.http import bce_http_client
  25. from baidubce.http import handler
  26. from baidubce.http import http_methods
  27. from baidubce.services.ipv6gateway import ipv6gateway_model
  28. from baidubce import utils
  29. from baidubce.utils import required
  30. from baidubce import compat
  31. if sys.version < '3':
  32. reload(sys)
  33. sys.setdefaultencoding('utf-8')
  34. _logger = logging.getLogger(__name__)
  35. default_billing_to_purchase_created = ipv6gateway_model.Billing('Postpaid')
  36. default_billing_to_purchase_reserved = ipv6gateway_model.Billing()
  37. class IPv6GatewayClient(bce_base_client.BceBaseClient):
  38. """
  39. IPv6Gateway sdk client
  40. """
  41. version = b'/v1'
  42. def __init__(self, config=None):
  43. bce_base_client.BceBaseClient.__init__(self, config)
  44. def _merge_config(self, config=None):
  45. """
  46. :param config:
  47. :type config: baidubce.BceClientConfiguration
  48. """
  49. if config is None:
  50. return self.config
  51. else:
  52. new_config = copy.copy(self.config)
  53. new_config.merge_non_none_values(config)
  54. return new_config
  55. def _send_request(self, http_method, path,
  56. body=None, headers=None, params=None,
  57. config=None, body_parser=None):
  58. config = self._merge_config(config)
  59. if body_parser is None:
  60. body_parser = handler.parse_json
  61. if headers is None:
  62. headers = {b'Accept': b'*/*', b'Content-Type':
  63. b'application/json;charset=utf-8'}
  64. return bce_http_client.send_request(
  65. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  66. http_method, path, body, headers, params)
  67. @required(name=(bytes, str),
  68. vpc_id=(bytes, str),
  69. bandwidthInMbps=(bytes, int))
  70. def create_ipv6_gateway(self, name, vpc_id, bandwidthInMbps, billing=None,
  71. client_token=None, config=None):
  72. """
  73. Create a ipv6-gateway with the specified options.
  74. :param client_token:
  75. An ASCII string whose length is less than 64.
  76. The request will be idempotent if client token is provided.
  77. :type client_token: string
  78. :param name:
  79. The name of ipv6-gateway that will be created.
  80. :type name: string
  81. :param vpc_id:
  82. The id of VPC.
  83. :type vpc_id: string
  84. :param bandwidthInMbps:
  85. The number of bandwidth.
  86. :type bandwidthInMbps: int
  87. :param billing:
  88. Billing information.
  89. :type billing: nat_model.Billing
  90. :param config:
  91. :type config: baidubce.BceClientConfiguration
  92. :return:
  93. :rtype baidubce.bce_response.BceResponse
  94. """
  95. path = utils.append_uri(self.version, 'IPv6Gateway')
  96. if client_token is None:
  97. client_token = generate_client_token()
  98. params = {b'clientToken': client_token}
  99. if billing is None:
  100. billing = default_billing_to_purchase_created
  101. body = {
  102. 'name': compat.convert_to_string(name),
  103. 'vpcId': compat.convert_to_string(vpc_id),
  104. 'bandwidthInMbps': compat.convert_to_string(bandwidthInMbps),
  105. 'billing': billing.__dict__
  106. }
  107. return self._send_request(http_methods.POST,
  108. path, body=json.dumps(body),
  109. params=params, config=config)
  110. @required(vpc_id=(bytes, str))
  111. def list_ipv6_gateways(self, vpc_id, config=None):
  112. """
  113. Return a list of ipv6-gateways, according to the VPC ID,
  114. will return a full list of ipv6 gateways in VPC.
  115. :param vpc_id:
  116. The id of VPC.
  117. :type vpc_id: string
  118. :param config:
  119. :type config: baidubce.BceClientConfiguration
  120. :return:
  121. :rtype baidubce.bce_response.BceResponse
  122. """
  123. path = utils.append_uri(self.version, 'IPv6Gateway')
  124. params = {b'vpcId': vpc_id}
  125. return self._send_request(http_methods.GET, path,
  126. params=params, config=config)
  127. @required(gateway_id=(bytes, str))
  128. def delete_ipv6_gateway(self, gateway_id, client_token=None, config=None):
  129. """
  130. delete a specified ipv6-gateway.
  131. :param gateway_id:
  132. The id of specified ipv6-gateway.
  133. :type gateway_id: string
  134. :param client_token:
  135. An ASCII string whose length is less than 64.
  136. The request will be idempotent if clientToken is provided.
  137. If the clientToken is not specified by user,
  138. a random String generated by default algorithm will be used.
  139. :type client_token: string
  140. :param config:
  141. :type config: baidubce.BceClientConfiguration
  142. :return:
  143. :rtype baidubce.bce_response.BceResponse
  144. """
  145. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id)
  146. if client_token is None:
  147. client_token = generate_client_token()
  148. params = {
  149. b'clientToken': client_token
  150. }
  151. return self._send_request(http_methods.DELETE,
  152. path, params=params, config=config)
  153. @required(gateway_id=(bytes, str),
  154. bandwidthInMbps=(bytes, int))
  155. def resize_ipv6_gateway(self, gateway_id, bandwidthInMbps, client_token=None, config=None):
  156. """
  157. resize a specified ipv6-gateway bandwidth.
  158. :param gateway_id:
  159. The id of specified ipv6-gateway.
  160. :type gateway_id: string
  161. :param bandwidthInMbps:
  162. The number of bandwidth.
  163. :type bandwidthInMbps: int
  164. :param client_token:
  165. An ASCII string whose length is less than 64.
  166. The request will be idempotent if clientToken is provided.
  167. If the clientToken is not specified by user,
  168. a random String generated by default algorithm will be used.
  169. :type client_token: string
  170. :param config:
  171. :type config: baidubce.BceClientConfiguration
  172. :return:
  173. :rtype baidubce.bce_response.BceResponse
  174. """
  175. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id)
  176. if client_token is None:
  177. client_token = generate_client_token()
  178. params = {
  179. b'resize': None,
  180. b'clientToken': client_token
  181. }
  182. body = {
  183. 'bandwidthInMbps': compat.convert_to_string(bandwidthInMbps)
  184. }
  185. return self._send_request(http_methods.PUT,
  186. path, body=json.dumps(body), params=params, config=config)
  187. @required(gateway_id=(bytes, str),
  188. cidr=(bytes, str))
  189. def create_ipv6_gateway_egress_only_rule(self, gateway_id, cidr,
  190. client_token=None, config=None):
  191. """
  192. Create a ipv6-gateway egress only rule with the specified options.
  193. :param client_token:
  194. An ASCII string whose length is less than 64.
  195. The request will be idempotent if client token is provided.
  196. :type client_token: string
  197. :param gateway_id:
  198. The id of ipv6 gateway.
  199. :type gateway_id: string
  200. :param cidr:
  201. The cidr of egress only rule.
  202. :type cidr: string
  203. :param config:
  204. :type config: baidubce.BceClientConfiguration
  205. :return:
  206. :rtype baidubce.bce_response.BceResponse
  207. """
  208. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id, 'egressOnlyRule')
  209. if client_token is None:
  210. client_token = generate_client_token()
  211. params = {b'clientToken': client_token}
  212. body = {
  213. 'cidr': compat.convert_to_string(cidr),
  214. }
  215. return self._send_request(http_methods.POST,
  216. path, body=json.dumps(body),
  217. params=params, config=config)
  218. @required(gateway_id=(bytes, str))
  219. def list_ipv6_gateway_egress_only_rules(self, gateway_id, marker=None, max_keys=None, config=None):
  220. """
  221. Return a list of ipv6-gateway egress only rules, according to the ipv6 gateway ID,
  222. will return a full list of ipv6 gateway egress only rules in ipv6 gateway.
  223. :param gateway_id:
  224. The id of ipv6 gateway.
  225. :type gateway_id: string
  226. :param marker:
  227. The optional parameter marker specified in the original
  228. request to specify where in the results to begin listing.
  229. Together with the marker, specifies the list result which
  230. listing should begin. If the marker is not specified,
  231. the list result will listing from the first one.
  232. :type marker: string
  233. :param max_keys:
  234. The optional parameter to specifies the max number of list
  235. result to return.
  236. The default value is 1000.
  237. :type max_keys: int
  238. :param config:
  239. :type config: baidubce.BceClientConfiguration
  240. :return:
  241. :rtype baidubce.bce_response.BceResponse
  242. """
  243. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id, 'egressOnlyRule')
  244. params = {}
  245. if marker is not None:
  246. params[b'marker'] = marker
  247. if max_keys is not None:
  248. params[b'maxKeys'] = max_keys
  249. return self._send_request(http_methods.GET, path,
  250. params=params, config=config)
  251. @required(gateway_id=(bytes, str),
  252. egress_only_rule_id=(bytes, str))
  253. def delete_ipv6_gateway_egress_only_rule(self, gateway_id, egress_only_rule_id, client_token=None, config=None):
  254. """
  255. delete a specified ipv6-gateway egress only rule.
  256. :param gateway_id:
  257. The id of specified ipv6-gateway.
  258. :type gateway_id: string
  259. :param egress_only_rule_id:
  260. The id of specified ipv6-gateway egress only rule.
  261. :type egress_only_rule_id: string
  262. :param client_token:
  263. An ASCII string whose length is less than 64.
  264. The request will be idempotent if clientToken is provided.
  265. If the clientToken is not specified by user,
  266. a random String generated by default algorithm will be used.
  267. :type client_token: string
  268. :param config:
  269. :type config: baidubce.BceClientConfiguration
  270. :return:
  271. :rtype baidubce.bce_response.BceResponse
  272. """
  273. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id, 'egressOnlyRule', egress_only_rule_id)
  274. if client_token is None:
  275. client_token = generate_client_token()
  276. params = {
  277. b'clientToken': client_token
  278. }
  279. return self._send_request(http_methods.DELETE,
  280. path, params=params, config=config)
  281. @required(gateway_id=(bytes, str),
  282. ipv6_address=(bytes, str),
  283. ingress_bandwidthInMbps=(bytes, int),
  284. egress_bandwidthInMbps=(bytes, int))
  285. def create_ipv6_gateway_rate_limit_rule(self, gateway_id, ipv6_address,
  286. ingress_bandwidthInMbps, egress_bandwidthInMbps,
  287. client_token=None, config=None):
  288. """
  289. Create a ipv6-gateway rate limit rule with the specified options.
  290. :param client_token:
  291. An ASCII string whose length is less than 64.
  292. The request will be idempotent if client token is provided.
  293. :type client_token: string
  294. :param gateway_id:
  295. The id of ipv6 gateway.
  296. :type gateway_id: string
  297. :param ipv6_address:
  298. The ipv6 addrss of rate limit rule.
  299. :type ipv6_address: string
  300. :param ingress_bandwidthInMbps:
  301. The ingress bandwidth of rate limit rule.
  302. :type ingress_bandwidthInMbps: int
  303. :param egress_bandwidthInMbps:
  304. The egress bandwidth of rate limit rule.
  305. :type egress_bandwidthInMbps: int
  306. :param config:
  307. :type config: baidubce.BceClientConfiguration
  308. :return:
  309. :rtype baidubce.bce_response.BceResponse
  310. """
  311. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id, 'rateLimitRule')
  312. if client_token is None:
  313. client_token = generate_client_token()
  314. params = {b'clientToken': client_token}
  315. body = {
  316. 'ipv6Address': compat.convert_to_string(ipv6_address),
  317. 'ingressBandwidthInMbps': compat.convert_to_string(ingress_bandwidthInMbps),
  318. 'egressBandwidthInMbps': compat.convert_to_string(egress_bandwidthInMbps),
  319. }
  320. return self._send_request(http_methods.POST,
  321. path, body=json.dumps(body),
  322. params=params, config=config)
  323. @required(gateway_id=(bytes, str))
  324. def list_ipv6_gateway_rate_limit_rules(self, gateway_id, marker=None, max_keys=None, config=None):
  325. """
  326. Return a list of ipv6-gateway rate limit rules, according to the ipv6 gateway ID,
  327. will return a full list of ipv6 gateway rate limit rules in ipv6 gateway.
  328. :param gateway_id:
  329. The id of ipv6 gateway.
  330. :type gateway_id: string
  331. :param marker:
  332. The optional parameter marker specified in the original
  333. request to specify where in the results to begin listing.
  334. Together with the marker, specifies the list result which
  335. listing should begin. If the marker is not specified,
  336. the list result will listing from the first one.
  337. :type marker: string
  338. :param max_keys:
  339. The optional parameter to specifies the max number of list
  340. result to return.
  341. The default value is 1000.
  342. :type max_keys: int
  343. :param config:
  344. :type config: baidubce.BceClientConfiguration
  345. :return:
  346. :rtype baidubce.bce_response.BceResponse
  347. """
  348. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id, 'rateLimitRule')
  349. params = {}
  350. if marker is not None:
  351. params[b'marker'] = marker
  352. if max_keys is not None:
  353. params[b'maxKeys'] = max_keys
  354. return self._send_request(http_methods.GET, path,
  355. params=params, config=config)
  356. @required(gateway_id=(bytes, str),
  357. rate_limit_rule_id=(bytes, str))
  358. def delete_ipv6_gateway_rate_limit_rule(self, gateway_id, rate_limit_rule_id, client_token=None, config=None):
  359. """
  360. delete a specified ipv6-gateway rate limit rule.
  361. :param gateway_id:
  362. The id of specified ipv6-gateway.
  363. :type gateway_id: string
  364. :param rate_limit_rule_id:
  365. The id of specified ipv6-gateway rate limit rule.
  366. :type rate_limit_rule_id: string
  367. :param client_token:
  368. An ASCII string whose length is less than 64.
  369. The request will be idempotent if clientToken is provided.
  370. If the clientToken is not specified by user,
  371. a random String generated by default algorithm will be used.
  372. :type client_token: string
  373. :param config:
  374. :type config: baidubce.BceClientConfiguration
  375. :return:
  376. :rtype baidubce.bce_response.BceResponse
  377. """
  378. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id, 'rateLimitRule', rate_limit_rule_id)
  379. if client_token is None:
  380. client_token = generate_client_token()
  381. params = {
  382. b'clientToken': client_token
  383. }
  384. return self._send_request(http_methods.DELETE,
  385. path, params=params, config=config)
  386. @required(gateway_id=(bytes, str),
  387. rate_limit_rule_id=(bytes, str),
  388. ingress_bandwidthInMbps=(bytes, int),
  389. egress_bandwidthInMbps=(bytes, int))
  390. def update_ipv6_gateway_rate_limit_rule(self, gateway_id, rate_limit_rule_id,
  391. ingress_bandwidthInMbps, egress_bandwidthInMbps,
  392. client_token=None, config=None):
  393. """
  394. update a specified ipv6-gateway rate limit rule.
  395. :param gateway_id:
  396. The id of specified ipv6-gateway.
  397. :type gateway_id: string
  398. :param rate_limit_rule_id:
  399. The id of specified ipv6-gateway rate limit rule.
  400. :type rate_limit_rule_id: string
  401. :param ingress_bandwidthInMbps:
  402. The ingress bandwidth of rate limit rule.
  403. :type ingress_bandwidthInMbps: int
  404. :param egress_bandwidthInMbps:
  405. The egress bandwidth of rate limit rule.
  406. :type egress_bandwidthInMbps: int
  407. :param client_token:
  408. An ASCII string whose length is less than 64.
  409. The request will be idempotent if clientToken is provided.
  410. If the clientToken is not specified by user,
  411. a random String generated by default algorithm will be used.
  412. :type client_token: string
  413. :param config:
  414. :type config: baidubce.BceClientConfiguration
  415. :return:
  416. :rtype baidubce.bce_response.BceResponse
  417. """
  418. path = utils.append_uri(self.version, 'IPv6Gateway', gateway_id, 'rateLimitRule', rate_limit_rule_id)
  419. if client_token is None:
  420. client_token = generate_client_token()
  421. params = {
  422. b'clientToken': client_token
  423. }
  424. body = {
  425. 'ingressBandwidthInMbps': compat.convert_to_string(ingress_bandwidthInMbps),
  426. 'egressBandwidthInMbps': compat.convert_to_string(egress_bandwidthInMbps),
  427. }
  428. return self._send_request(http_methods.PUT,
  429. path, body=json.dumps(body),
  430. params=params, config=config)
  431. def generate_client_token_by_uuid():
  432. """
  433. The default method to generate the random string for client_token
  434. if the optional parameter client_token is not specified by the user.
  435. :return:
  436. :rtype string
  437. """
  438. return str(uuid.uuid4())
  439. generate_client_token = generate_client_token_by_uuid