subnet_client.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 SUBNET.
  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 SubnetClient(bce_base_client.BceBaseClient):
  28. """
  29. Subnet 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, SubnetClient.prefix + path, body, headers, params)
  57. @required(name=(bytes, str),
  58. zone_name=(bytes, str),
  59. cidr=(bytes, str),
  60. vpc_id=(bytes, str))
  61. def create_subnet(self, name, zone_name, cidr, vpc_id, subnet_type=None, description=None,
  62. client_token=None, enable_ipv6=None, vpc_secondary_cidr=None, config=None, tags=None):
  63. """
  64. Create a subnet with the specified options.
  65. :param name:
  66. The name of subnet that will be created.
  67. type name: string
  68. :param zone_name:
  69. The name of available zone which the subnet belong
  70. through listZones, we can get all available zone info at current region
  71. ee.g. "cn-gz-a" "cn-gz-b"
  72. type zone_name: string
  73. :param cidr:
  74. The CIDR of this subnet.
  75. type cidr: string
  76. :param vpc_id:
  77. The id of vpc which this subnet belongs.
  78. type vpc_id: string
  79. :param subnet_type:
  80. The option param to describe the type of subnet create
  81. type subnet_type: string
  82. :param description:
  83. The option param to describe the subnet
  84. type description: string
  85. :param enable_ipv6:
  86. The option param to enable or disable ipv6 for the subnet
  87. :type enable_ipv6: boolean
  88. :param vpc_secondary_cidr:
  89. The secondary CIDR block of the VPC that the subnet belongs to.
  90. :type vpc_secondary_cidr: string
  91. :param client_token:
  92. An ASCII string whose length is less than 64.
  93. The request will be idempotent if clientToken is provided.
  94. If the clientToken is not specified by the user, a random String
  95. generated by default algorithm will be used.
  96. type client_token: string
  97. :param config:
  98. :type config: baidubce.BceClientConfiguration
  99. :param tags:
  100. List of tags to be bind
  101. :type tags: list
  102. :return:
  103. :rtype baidubce.bce_response.BceResponse
  104. """
  105. path = b'/subnet'
  106. params = {}
  107. if client_token is None:
  108. params[b'clientToken'] = generate_client_token()
  109. else:
  110. params[b'clientToken'] = client_token
  111. body = {
  112. 'name': compat.convert_to_string(name),
  113. 'zoneName': compat.convert_to_string(zone_name),
  114. 'cidr': compat.convert_to_string(cidr),
  115. 'vpcId': compat.convert_to_string(vpc_id)
  116. }
  117. if subnet_type is not None:
  118. body['subnetType'] = compat.convert_to_string(subnet_type)
  119. if description is not None:
  120. body['description'] = compat.convert_to_string(description)
  121. if tags is not None:
  122. body['tags'] = tags
  123. if enable_ipv6 is not None:
  124. body['enableIpv6'] = enable_ipv6
  125. if vpc_secondary_cidr is not None:
  126. body['vpcSecondaryCidr'] = compat.convert_to_string(vpc_secondary_cidr)
  127. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params,
  128. config=config)
  129. @required(marker=(bytes, str),
  130. max_Keys=int,
  131. vpc_id=(bytes, str),
  132. zone_name=(bytes, str),
  133. subnet_type=(bytes, str))
  134. def list_subnets(self, marker=None, max_keys=None, vpc_id=None, subnet_ids=None,
  135. zone_name=None, subnet_type=None, config=None):
  136. """
  137. Return a list of subnets owned by the authenticated user.
  138. :param marker:
  139. The optional parameter marker specified in the original request to specify
  140. where in the results to begin listing.
  141. Together with the marker, specifies the list result which listing should begin.
  142. If the marker is not specified, the list result will listing from the first one.
  143. :type marker: string
  144. :param max_keys:
  145. The optional parameter to specifies the max number of list result to return.
  146. The default value is 1000.
  147. :type max_keys: int
  148. :param vpc_id:
  149. The id of the vpc
  150. :type vpc_id: string
  151. :param subnet_ids:
  152. The ids of the subnet
  153. :param zone_name:
  154. The name of available zone which the subnet belong
  155. through listZones, we can get all available zone info at current region
  156. ee.g. "cn-gz-a" "cn-gz-b"
  157. :type zone_name: string
  158. :param subnet_type:
  159. The option param to describe the type of subnet to be created
  160. :type subnet_type: string
  161. :param config:
  162. :type config: baidubce.BceClientConfiguration
  163. :return:
  164. :rtype baidubce.bce_response.BceResponse
  165. """
  166. path = b'/subnet'
  167. params = {}
  168. if marker is not None:
  169. params[b'marker'] = marker
  170. if max_keys is not None:
  171. params[b'maxKeys'] = max_keys
  172. if vpc_id is not None:
  173. params[b'vpcId'] = vpc_id
  174. if subnet_ids is not None:
  175. params[b'subnetIds'] = subnet_ids
  176. if zone_name is not None:
  177. params[b'zoneName'] = zone_name
  178. if subnet_type is not None:
  179. params[b'subnetType'] = subnet_type
  180. return self._send_request(http_methods.GET, path, params=params, config=config)
  181. @required(subnet_id=(bytes, str))
  182. def get_subnet(self, subnet_id, config=None):
  183. """
  184. Get the detail information of a specified subnet.
  185. :param subnet_id:
  186. The id of the subnet.
  187. :type subnet_id: string
  188. :param config:
  189. :type config: baidubce.BceClientConfiguration
  190. :return:
  191. :rtype baidubce.bce_response.BceResponse
  192. """
  193. path = b'/subnet/%s' % compat.convert_to_bytes(subnet_id)
  194. return self._send_request(http_methods.GET, path, config=config)
  195. @required(subnet_id=(bytes, str))
  196. def delete_subnet(self, subnet_id, client_token=None, config=None):
  197. """
  198. Delete the specified subnet owned by the user.
  199. :param subnet_id:
  200. The id of the subnet to be deleted.
  201. :type subnet_id: string
  202. :param client_token:
  203. An ASCII string whose length is less than 64.
  204. The request will be idempotent if clientToken is provided.
  205. If the clientToken is not specified by the user, a random String generated by
  206. default algorithm will be used.
  207. :type client_token: string
  208. :param config:
  209. :type config: baidubce.BceClientConfiguration
  210. :return:
  211. :rtype baidubce.bce_response.BceResponse
  212. """
  213. path = b'/subnet/%s' % compat.convert_to_bytes(subnet_id)
  214. params = {}
  215. if client_token is None:
  216. params[b'clientToken'] = generate_client_token()
  217. else:
  218. params[b'clientToken'] = client_token
  219. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  220. @required(subnet_id=(bytes, str), name=(bytes, str), description=(bytes, str))
  221. def update_subnet(self, subnet_id, name, description=None, enable_ipv6=None, client_token=None, config=None):
  222. """
  223. Modify the special attribute to new value of the subnet owned by the user.
  224. :param subnet_id:
  225. The id of the specific subnet to be updated.
  226. :type subnet_id: string
  227. :param name:
  228. The name of the subnet
  229. :type name: string
  230. :param description:
  231. The option param to describe the subnet
  232. :type description: string
  233. :param enable_ipv6:
  234. The option param to enable or disable ipv6 for the subnet
  235. :type enable_ipv6: boolean
  236. :param client_token:
  237. An ASCII string whose length is less than 64.
  238. The request will be idempotent if clientToken is provided.
  239. If the clientToken is not specified by the user, a random String generated
  240. by default algorithm will be used.
  241. :type client_token: string
  242. :param config:
  243. :type config: baidubce.BceClientConfiguration
  244. :return:
  245. :rtype baidubce.bce_response.BceResponse
  246. """
  247. path = b'/subnet/%s' % compat.convert_to_bytes(subnet_id)
  248. params = {
  249. b'modifyAttribute': None
  250. }
  251. body = {
  252. 'name': compat.convert_to_string(name)
  253. }
  254. if client_token is None:
  255. params[b'clientToken'] = generate_client_token()
  256. else:
  257. params[b'clientToken'] = client_token
  258. if description is not None:
  259. body['description'] = compat.convert_to_string(description)
  260. if enable_ipv6 is not None:
  261. body['enableIpv6'] = enable_ipv6
  262. return self._send_request(http_methods.PUT, path, json.dumps(body),
  263. params=params, config=config)
  264. @required(subnet_id=(bytes, str),
  265. ip_cidr=(bytes, str),
  266. ip_version=int,
  267. description=(bytes, str))
  268. def create_subnet_ipreserve(self, subnet_id, ip_cidr, ip_version, description=None,
  269. client_token=None, config=None):
  270. """
  271. Create a ipreserve segment with the specified options.
  272. :param subnet_id:
  273. The id of the subnet that the reserved subnet belongs to.
  274. :type subnet_id: string
  275. :param ip_cidr:
  276. The IP or CIDR of the reserved subnet.
  277. :type ip_cidr: string
  278. :param ip_version:
  279. The IP version, supports IPv4 and IPv6.
  280. :type ip_version: int
  281. :param description:
  282. The description of the reserved subnet.
  283. :type description: string
  284. :param client_token:
  285. An ASCII string whose length is less than 64.
  286. The request will be idempotent if the clientToken is provided.
  287. If the clientToken is not specified by the user, a random String generated
  288. 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'/subnet/ipreserve'
  296. params = {'clientToken': generate_client_token()} if client_token is None else {'clientToken': client_token}
  297. body = {
  298. 'subnetId': compat.convert_to_string(subnet_id),
  299. 'ipCidr': compat.convert_to_string(ip_cidr),
  300. 'ipVersion': ip_version
  301. }
  302. if description is not None:
  303. body['description'] = compat.convert_to_string(description)
  304. return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params,
  305. config=config)
  306. @required(subnet_id=(bytes, str),
  307. marker=(bytes, str),
  308. max_keys=int)
  309. def list_subnet_ipreserve(self, subnet_id=None, marker=None, max_keys=None, config=None):
  310. """
  311. List reserved within subnets.
  312. :param subnet_id:
  313. The id of the subnet.
  314. :type subnet_id: string
  315. :param marker:
  316. The optional parameter marker specified in the original request to specify
  317. where in the results to begin listing.
  318. :type marker: string
  319. :param max_keys:
  320. The optional parameter to specify the max number of list results to return.
  321. :type max_keys: int
  322. :param config:
  323. :type config: baidubce.BceClientConfiguration
  324. :return:
  325. :rtype baidubce.bce_response.BceResponse
  326. """
  327. path = b'/subnet/ipreserve'
  328. params = {}
  329. if subnet_id is not None:
  330. params[b'subnetId'] = subnet_id
  331. if marker is not None:
  332. params[b'marker'] = marker
  333. if max_keys is not None:
  334. params[b'maxKeys'] = max_keys
  335. return self._send_request(http_methods.GET, path, params=params, config=config)
  336. @required(ip_reserve_id=(bytes, str))
  337. def delete_subnet_ipreserve(self, ip_reserve_id, client_token=None, config=None):
  338. """
  339. Delete the specified reserved subnet segment.
  340. :param ip_reserve_id:
  341. The ID of the reserved subnet to be deleted.
  342. :type ip_reserve_id: string
  343. :param client_token:
  344. An ASCII string whose length is less than 64.
  345. The request will be idempotent if clientToken is provided.
  346. If the clientToken is not specified by the user, a random String generated by
  347. default algorithm will be used.
  348. :type client_token: string
  349. :param config:
  350. :type config: baidubce.BceClientConfiguration
  351. :return:
  352. :rtype baidubce.bce_response.BceResponse
  353. """
  354. path = b'/subnet/ipreserve/%s' % compat.convert_to_bytes(ip_reserve_id)
  355. params = {}
  356. if client_token is None:
  357. params[b'clientToken'] = generate_client_token()
  358. else:
  359. params[b'clientToken'] = client_token
  360. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  361. def generate_client_token_by_uuid():
  362. """
  363. The default method to generate the random string for client_token
  364. if the optional parameter client_token is not specified by the user.
  365. :return:
  366. :rtype string
  367. """
  368. return str(uuid.uuid4())
  369. generate_client_token = generate_client_token_by_uuid