endpoint_client.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  11. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  12. # specific language governing permissions and limitations under the License.
  13. """
  14. This module provides a client class for ENDPOINT.
  15. """
  16. import copy
  17. import json
  18. import logging
  19. import uuid
  20. from baidubce.bce_base_client import BceBaseClient
  21. from baidubce.auth import bce_v1_signer
  22. from baidubce.http import bce_http_client
  23. from baidubce.http import handler
  24. from baidubce.http import http_methods
  25. from baidubce import compat
  26. _logger = logging.getLogger(__name__)
  27. class EndpointClient(BceBaseClient):
  28. """
  29. Endpoint base sdk client
  30. """
  31. prefix = b'/v1'
  32. path = b'/endpoint'
  33. def __init__(self, config=None):
  34. BceBaseClient.__init__(self, config)
  35. @staticmethod
  36. def _generate_default_client_token():
  37. """
  38. default client token by uuid1
  39. """
  40. return uuid.uuid1()
  41. def _merge_config(self, config):
  42. """
  43. :type config: baidubce.BceClientConfiguration
  44. :param config:
  45. :return:
  46. """
  47. if config is None:
  48. return self.config
  49. else:
  50. new_config = copy.copy(self.config)
  51. new_config.merge_non_none_values(config)
  52. return new_config
  53. def _send_request(self, http_method, path, body=None, headers=None,
  54. params=None, config=None, body_parser=None):
  55. """
  56. :param http_method:
  57. :param path:
  58. :param body:
  59. :param headers:
  60. :param params:
  61. :type config: baidubce.BceClientConfiguration
  62. :param config:
  63. :param body_parser:
  64. :return: baidubce.BceResponse
  65. """
  66. config = self._merge_config(config)
  67. if body_parser is None:
  68. body_parser = handler.parse_json
  69. if headers is None:
  70. headers = {b'Accept': b'*/*',
  71. b'Content-Type': b'application/json;charset=utf-8'}
  72. return bce_http_client.send_request(config, bce_v1_signer.sign,
  73. [handler.parse_error, body_parser],
  74. http_method, EndpointClient.prefix + path, body, headers,
  75. params)
  76. def list_services(self, config=None):
  77. """
  78. return all services
  79. """
  80. path = EndpointClient.path + b'/publicService'
  81. return self._send_request(http_methods.GET, path, config=config)
  82. def create_endpoint(self, vpc_id, subnet_id, name, service, billing, description=None, ip_address=None,
  83. client_token=None, config=None):
  84. """
  85. The method of endpoint to be created.
  86. :param vpc_id:
  87. vpc id
  88. :type vpc_id: str
  89. :param subnet_id:
  90. subnet id
  91. :type subnet_id: str
  92. :param name:
  93. name
  94. :type name: str
  95. :param service:
  96. service
  97. :type service: str
  98. :param billing:
  99. order_configuration
  100. :type billing:Billing
  101. :param ip_address:
  102. ip_address ipv4 address
  103. :type ip_address: str
  104. :param description:
  105. description .
  106. :type description: string
  107. :param client_token:
  108. An ASCII string whose length is less than 64.
  109. The request will be idempotent if clientToken is provided.
  110. If the clientToken is not specified by the user, a random String
  111. generated by default algorithm will be used.
  112. :type client_token: string
  113. """
  114. if client_token is None:
  115. client_token = self._generate_default_client_token()
  116. params = {
  117. b'clientToken': client_token
  118. }
  119. body = {
  120. "vpcId": vpc_id,
  121. "subnetId": subnet_id,
  122. "name": name,
  123. "service": service,
  124. "billing": {
  125. "paymentTiming": billing.payment_timing
  126. }
  127. }
  128. if description is not None:
  129. body['description'] = description
  130. if ip_address is not None:
  131. body['ipAddress'] = ip_address
  132. return self._send_request(http_methods.POST, EndpointClient.path, body=json.dumps(body), params=params,
  133. config=config)
  134. def delete_endpoint(self, endpoint_id, client_token=None, config=None):
  135. """
  136. release endpoint
  137. :param endpoint_id:
  138. The id of endpoint.
  139. :type endpoint_id: string
  140. :param client_token:
  141. An ASCII string whose length is less than 64.
  142. The request will be idempotent if clientToken is provided.
  143. If the clientToken is not specified by the user, a random String generated by default algorithm will
  144. be used.
  145. :type client_token: string
  146. :param config:
  147. :type config: baidubce.BceClientConfiguration
  148. :return:
  149. :rtype baidubce.bce_response.BceResponse
  150. """
  151. path = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
  152. if client_token is None:
  153. client_token = self._generate_default_client_token()
  154. params = {
  155. b'clientToken': client_token
  156. }
  157. return self._send_request(http_methods.DELETE, path, params=params, config=config)
  158. def list_endpoints(self, vpc_id, name=None, ip_address=None, status=None, subnet_id=None, service=None, marker=None,
  159. max_Keys=None, config=None):
  160. """
  161. return all endpoint about vpc
  162. :param vpc_id:
  163. vpc id
  164. :type vpc_id:string
  165. :param name:
  166. name
  167. :type name:string
  168. :param ip_address:
  169. ip address
  170. :type ip_address:string
  171. :param status:
  172. status
  173. :type status:string
  174. :param subnet_id:
  175. subnet id
  176. :type subnet_id:string
  177. :param service:
  178. service
  179. :type service:string
  180. :param marker:
  181. The optional parameter marker specified in the original request to specify
  182. where in the results to begin listing.
  183. Together with the marker, specifies the list result which listing should begin.
  184. If the marker is not specified, the list result will listing from the first one.
  185. :type marker: string
  186. :param max_Keys:
  187. The optional parameter to specifies the max number of list result to return.
  188. The default value is 1000.
  189. :type max_Keys: int
  190. :param config:
  191. :type config: baidubce.BceClientConfiguration
  192. :return:
  193. :rtype baidubce.bce_response.BceResponse
  194. """
  195. params = {b'vpcId': vpc_id}
  196. if name is not None:
  197. params[b'name'] = name
  198. if ip_address is not None:
  199. params[b'ipAddress'] = ip_address
  200. if status is not None:
  201. params[b'status'] = status
  202. if subnet_id is not None:
  203. params[b'subnetId'] = subnet_id
  204. if service is not None:
  205. params[b'service'] = service
  206. if marker is not None:
  207. params[b'marker'] = marker
  208. if max_Keys is not None:
  209. params[b'maxKeys'] = max_Keys
  210. return self._send_request(http_methods.GET, EndpointClient.path, params=params, config=config)
  211. def get_endpoint(self, endpoint_id, config=None):
  212. """
  213. Get the detail information of endpoint.
  214. :param endpoint_id:
  215. The id of endpoint.
  216. :type endpoint_id: string
  217. :param config:
  218. :type config: baidubce.BceClientConfiguration
  219. :return:
  220. :rtype baidubce.bce_response.BceResponse
  221. """
  222. path = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
  223. return self._send_request(http_methods.GET, path, config=config)
  224. def update_endpoint(self, endpoint_id, name=None, description=None, client_token=None, config=None):
  225. """
  226. The method of endpoint to be update.
  227. :param endpoint_id: endpoint id
  228. :type endpoint_id: str
  229. :param name: endpoint name
  230. :type name: str
  231. :param description: the description of endpoint
  232. :type description: str
  233. :param client_token:
  234. An ASCII string whose length is less than 64.
  235. The request will be idempotent if clientToken is provided.
  236. If the clientToken is not specified by the user, a random String
  237. generated by default algorithm will be used.
  238. :type client_token: string
  239. :param config:
  240. :type config: baidubce.BceClientConfiguration
  241. :return:
  242. :rtype baidubce.bce_response.BceResponse
  243. """
  244. path = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
  245. if client_token is None:
  246. client_token = self._generate_default_client_token()
  247. params = {
  248. b'clientToken': client_token
  249. }
  250. body = {}
  251. if description is not None:
  252. body['description'] = description
  253. if name is not None:
  254. body['name'] = name
  255. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
  256. config=config)
  257. def update_endpoint_sg(self, endpoint_id, security_group_list, action=b'bindSg', client_token=None, config=None):
  258. """
  259. The method of endpoint's security group to be update.
  260. :param endpoint_id: endpoint id
  261. :type endpoint_id: str
  262. :param security_group_list: the id list of security group to be bind with.
  263. :type security_group_list: list
  264. :param name: action
  265. :type name: str
  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 the user, a random String
  270. 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 = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
  278. if client_token is None:
  279. client_token = self._generate_default_client_token()
  280. params = {
  281. action: '',
  282. b'clientToken': client_token
  283. }
  284. body = {
  285. "securityGroupIds": security_group_list
  286. }
  287. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
  288. config=config)
  289. def update_endpoint_enterprise_sg(self, endpoint_id, enterprise_sg_list,
  290. action=b'bindEsg', client_token=None, config=None):
  291. """
  292. The method of endpoint's enterprise security group to be update.
  293. :param endpoint_id: endpoint id
  294. :type endpoint_id: str
  295. :param enterprise_sg_list: the id list of enterprise security group to be bind.
  296. :type enterprise_sg_list: list
  297. :param action: action
  298. :type action: str
  299. :param client_token:
  300. An ASCII string whose length is less than 64.
  301. The request will be idempotent if clientToken is provided.
  302. If the clientToken is not specified by the user, a random String
  303. generated by default algorithm will be used.
  304. :type client_token: string
  305. :param config:
  306. :type config: baidubce.BceClientConfiguration
  307. :return:
  308. :rtype baidubce.bce_response.BceResponse
  309. """
  310. path = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
  311. if client_token is None:
  312. client_token = self._generate_default_client_token()
  313. params = {
  314. action: '',
  315. b'clientToken': client_token
  316. }
  317. body = {
  318. b'enterpriseSecurityGroupIds': enterprise_sg_list
  319. }
  320. return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
  321. config=config)