vpc_client.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides a client class for VPC.
  14. """
  15. import copy
  16. import json
  17. import logging
  18. import uuid
  19. from baidubce import bce_base_client
  20. from baidubce.auth import bce_v1_signer
  21. from baidubce.http import bce_http_client
  22. from baidubce.http import handler
  23. from baidubce.http import http_methods
  24. from baidubce.utils import required
  25. from baidubce import compat
  26. _logger = logging.getLogger(__name__)
  27. class VpcClient(bce_base_client.BceBaseClient):
  28. """
  29. VPC base sdk client
  30. """
  31. prefix = b'/v1'
  32. def __init__(self, config=None):
  33. bce_base_client.BceBaseClient.__init__(self, config)
  34. def _merge_config(self, config=None):
  35. """
  36. :param config:
  37. :type config: baidubce.BceClientConfiguration
  38. :return:
  39. """
  40. if config is None:
  41. return self.config
  42. else:
  43. new_config = copy.copy(self.config)
  44. new_config.merge_non_none_values(config)
  45. return new_config
  46. def _send_request(self, http_method, path,
  47. body=None, headers=None, params=None,
  48. config=None, body_parser=None):
  49. config = self._merge_config(config)
  50. if body_parser is None:
  51. body_parser = handler.parse_json
  52. if headers is None:
  53. headers = {b'Accept': b'*/*', b'Content-Type': b'application/json;charset=utf-8'}
  54. return bce_http_client.send_request(
  55. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  56. http_method, VpcClient.prefix + path, body, headers, params)
  57. @required(name=(bytes, str), cidr=(bytes, str))
  58. def create_vpc(self, name, cidr, description=None, client_token=None, enable_ipv6=None, tags=None, config=None):
  59. """
  60. The name of vpc to be created.
  61. :param name:
  62. The name of vpc to be created.
  63. :type name: string
  64. :param cidr:
  65. The CIDR of the vpc.
  66. :type cidr: string
  67. :param description:
  68. The description of the vpc.
  69. :type description: string
  70. :param enable_ipv6:
  71. The option param to enable or disable ipv6 for the subnet
  72. :type enable_ipv6: boolean
  73. :param tags:
  74. List of tags to be bind
  75. :type tags: list
  76. :param client_token:
  77. An ASCII string whose length is less than 64.
  78. The request will be idempotent if clientToken is provided.
  79. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  80. :type client_token: string
  81. :param config:
  82. :type config: baidubce.BceClientConfiguration
  83. :return:
  84. :rtype baidubce.bce_response.BceResponse
  85. """
  86. path = b'/vpc'
  87. params = {}
  88. if client_token is None:
  89. params[b'clientToken'] = generate_client_token()
  90. else:
  91. params[b'clientToken'] = client_token
  92. body = {
  93. 'name': compat.convert_to_string(name),
  94. 'cidr': compat.convert_to_string(cidr)
  95. }
  96. if description is not None:
  97. body['description'] = compat.convert_to_string(description)
  98. if tags is not None:
  99. body['tags'] = tags
  100. if enable_ipv6 is not None:
  101. body['enableIpv6'] = enable_ipv6
  102. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params,
  103. config=config)
  104. @required(marker=(bytes, str), max_Keys=int, is_Default=bool)
  105. def list_vpcs(self, marker=None, max_Keys=None, vpc_ids=None, isDefault=None, config=None):
  106. """
  107. Return a list of vpcs owned by the authenticated user.
  108. :param marker:
  109. The optional parameter marker specified in the original request to specify
  110. where in the results to begin listing.
  111. Together with the marker, specifies the list result which listing should begin.
  112. If the marker is not specified, the list result will listing from the first one.
  113. :type marker: string
  114. :param max_keys:
  115. The optional parameter to specifies the max number of list result to return.
  116. The default value is 1000.
  117. :type max_keys: int
  118. :param vpc_ids:
  119. filter by vpc_ids, optional parameter
  120. :type vpc_ids: string
  121. :param isDefault:
  122. The option param demotes whether the vpc is default vpc.
  123. :type isDefault: boolean
  124. :param config:
  125. :type config: baidubce.BceClientConfiguration
  126. :return:
  127. :rtype baidubce.bce_response.BceResponse
  128. """
  129. path = b'/vpc'
  130. params = {}
  131. if marker is not None:
  132. params[b'marker'] = marker
  133. if max_Keys is not None:
  134. params[b'maxKeys'] = max_Keys
  135. if vpc_ids is not None:
  136. params[b'vpcIds'] = vpc_ids
  137. if isDefault is not None:
  138. params[b'isDefault'] = isDefault
  139. return self._send_request(http_methods.GET, path, params=params, config=config)
  140. @required(vpc_id=(bytes, str))
  141. def get_vpc(self, vpc_id, config=None):
  142. """
  143. Get the detail information of specified vpc.
  144. :param vpc_id:
  145. The id of vpc.
  146. :type vpc_id: string
  147. :param config:
  148. :type config: baidubce.BceClientConfiguration
  149. :return:
  150. :rtype baidubce.bce_response.BceResponse
  151. """
  152. path = b'/vpc/%s' % compat.convert_to_bytes(vpc_id)
  153. return self._send_request(http_methods.GET, path, config=config)
  154. @required(vpc_id=(bytes, str))
  155. def delete_vpc(self, vpc_id, client_token=None, config=None):
  156. """
  157. Delete the specified vpc owned by the user.All resource in the vpc must be deleted before the vpc itself
  158. can be deleted.
  159. :param vpc_id:
  160. The id of instance.
  161. :type vpc_id: string
  162. :param client_token:
  163. An ASCII string whose length is less than 64.
  164. The request will be idempotent if clientToken is provided.
  165. If the clientToken is not specified by the user, a random String generated by default algorithm will
  166. be used.
  167. :type client_token: string
  168. :param config:
  169. :type config: baidubce.BceClientConfiguration
  170. :return:
  171. :rtype baidubce.bce_response.BceResponse
  172. """
  173. path = b'/vpc/%s' % compat.convert_to_bytes(vpc_id)
  174. params = {}
  175. if client_token is None:
  176. params[b'clientToken'] = generate_client_token()
  177. else:
  178. params[b'clientToken'] = client_token
  179. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  180. @required(vpc_id=(bytes, str), name=(bytes, str))
  181. def update_vpc(self, vpc_id, name, description=None, enable_ipv6=None,
  182. secondaryCidr=None, client_token=None, config=None):
  183. """
  184. Modify the special attribute to new value of the vpc owned by the user.
  185. :param vpc_id:
  186. The id of the specified vpc
  187. :type vpc_id: string
  188. :param name:
  189. The name of the specified vpc
  190. :type name: string
  191. :param description:
  192. The description of the vpc.
  193. :type description: string
  194. :param enable_ipv6:
  195. The option param to enable or disable ipv6 for the subnet
  196. :type enable_ipv6: boolean
  197. :param secondaryCidr:
  198. This parameter specifies the secondary CIDR ranges associated with the VPC.
  199. :type secondaryCidr: list
  200. :param client_token:
  201. An ASCII string whose length is less than 64.
  202. The request will be idempotent if clientToken is provided.
  203. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  204. :type client_token: string
  205. :param config:
  206. :type config: baidubce.BceClientConfiguration
  207. :return:
  208. :rtype baidubce.bce_response.BceResponse
  209. """
  210. path = b'/vpc/%s' % compat.convert_to_bytes(vpc_id)
  211. params = {
  212. b'modifyAttribute': None
  213. }
  214. body = {
  215. 'name': compat.convert_to_string(name)
  216. }
  217. if client_token is None:
  218. params[b'clientToken'] = generate_client_token()
  219. else:
  220. params[b'clientToken'] = client_token
  221. if description is not None:
  222. body['description'] = compat.convert_to_string(description)
  223. if enable_ipv6 is not None:
  224. body['enableIpv6'] = enable_ipv6
  225. if secondaryCidr is not None:
  226. body['secondaryCidr'] = secondaryCidr
  227. return self._send_request(http_methods.PUT, path, json.dumps(body),
  228. params=params, config=config)
  229. @required(vpc_id=(bytes, str))
  230. def get_private_ip_address_info(self, vpc_id, private_ip_range=None, private_ip_addresses=None, config=None):
  231. """
  232. Get the privateIpAddressesInfo from vpc.
  233. :param vpc_id:
  234. The id of vpc.
  235. :type vpc_id: string
  236. :param private_ip_range:
  237. The optional parameter specifying the private IP range.
  238. :type private_ip_range: string
  239. :param private_ip_addresses:
  240. The optional list parameter specifying the private IP addresses.
  241. :type private_ip_addresses: list
  242. :param config:
  243. :type config: baidubce.BceClientConfiguration
  244. :return:
  245. :rtype baidubce.bce_response.BceResponse
  246. """
  247. path = b'/vpc/%s/privateIpAddressInfo' % compat.convert_to_bytes(vpc_id)
  248. params = {}
  249. if private_ip_range is not None:
  250. params[b'privateIpRange'] = private_ip_range
  251. if private_ip_addresses is not None:
  252. params[b'privateIpAddresses'] = (",").join(private_ip_addresses)
  253. return self._send_request(http_methods.GET, path, params=params, config=config)
  254. @required(vpc_id=(bytes, str))
  255. def open_relay(self, vpc_id, client_token=None, config=None):
  256. """
  257. Open relay for the specified vpc.
  258. :param vpc_id:
  259. The id of vpc.
  260. :type vpc_id: string
  261. :param client_token:
  262. An ASCII string whose length is less than 64.
  263. The request will be idempotent if clientToken is provided.
  264. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  265. :type client_token: string
  266. :param config:
  267. :type config: baidubce.BceClientConfiguration
  268. :return:
  269. :rtype baidubce.bce_response.BceResponse
  270. """
  271. path = b'/vpc/openRelay/%s' % compat.convert_to_bytes(vpc_id)
  272. params = {}
  273. if client_token is None:
  274. params[b'clientToken'] = generate_client_token()
  275. else:
  276. params[b'clientToken'] = client_token
  277. return self._send_request(http_methods.PUT, path, config=config)
  278. @required(vpc_id=(bytes, str))
  279. def shutdown_relay(self, vpc_id, client_token=None, config=None):
  280. """
  281. Shutdown relay for the specified vpc.
  282. :param vpc_id:
  283. The id of vpc.
  284. :type vpc_id: string
  285. :param client_token:
  286. An ASCII string whose length is less than 64.
  287. The request will be idempotent if clientToken is provided.
  288. If the clientToken is not specified by the user, a random String generated by default algorithm will be used.
  289. :type client_token: string
  290. :param config:
  291. :type config: baidubce.BceClientConfiguration
  292. :return:
  293. :rtype baidubce.bce_response.BceResponse
  294. """
  295. path = b'/vpc/shutdownRelay/%s' % compat.convert_to_bytes(vpc_id)
  296. params = {}
  297. if client_token is None:
  298. params[b'clientToken'] = generate_client_token()
  299. else:
  300. params[b'clientToken'] = client_token
  301. return self._send_request(http_methods.PUT, path, config=config)
  302. def generate_client_token_by_uuid():
  303. """
  304. The default method to generate the random string for client_token
  305. if the optional parameter client_token is not specified by the user.
  306. :return:
  307. :rtype string
  308. """
  309. return str(uuid.uuid4())
  310. generate_client_token = generate_client_token_by_uuid