probe_client.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 probe.
  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 import utils
  28. from baidubce.utils import required
  29. from baidubce import compat
  30. _logger = logging.getLogger(__name__)
  31. if sys.version < '3':
  32. reload(sys)
  33. sys.setdefaultencoding('utf-8')
  34. class ProbeClient(bce_base_client.BceBaseClient):
  35. """
  36. Probe base sdk client
  37. """
  38. version = b'/v1'
  39. def __init__(self, config=None):
  40. bce_base_client.BceBaseClient.__init__(self, config)
  41. def _merge_config(self, config=None):
  42. """
  43. :param config:
  44. :type config: baidubce.BceClientConfiguration
  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,
  54. body=None, headers=None, params=None,
  55. config=None, body_parser=None):
  56. config = self._merge_config(config)
  57. if body_parser is None:
  58. body_parser = handler.parse_json
  59. if headers is None:
  60. headers = {b'Accept': b'*/*', b'Content-Type':
  61. b'application/json;charset=utf-8'}
  62. return bce_http_client.send_request(
  63. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  64. http_method, path, body, headers, params)
  65. @required(name=(bytes, str),
  66. vpc_id=(bytes, str),
  67. subnet_id=(bytes, str),
  68. protocol=(bytes, str),
  69. frequency=int,
  70. source_ips=list,
  71. source_ip_num=int)
  72. def create_probe(self, name, vpc_id, subnet_id, protocol,
  73. frequency, dst_ip, dst_port=None,
  74. source_ips=None, source_ip_num=None,
  75. description=None,
  76. payload=None, client_token=None, config=None):
  77. """
  78. The method to create a probe.
  79. :param name:
  80. The name of the probe.
  81. The name must be 1 to 64 characters in length,
  82. and can contain numbers, letters and underscores (_).
  83. :type name: string
  84. :param vpc_id:
  85. The id of the vpc to create the probe in.
  86. :type vpc_id: string
  87. :param subnet_id:
  88. The id of the subnet to create the probe in.
  89. :type subnet_id: string
  90. :param protocol:
  91. The protocol of the probe, TCP, UDP, ICMP or DNS.
  92. :type protocol: string
  93. :param frequency:
  94. The number of the probing per minute, 10, 20 or 30.
  95. Default value is 20.
  96. :type frequency: int
  97. :param dst_ip:
  98. The destination ip of the probe.
  99. :type dst_ip: string
  100. :param dst_port:
  101. The destination port of the probe.
  102. Must be specified when the protocol is TCP, UDP or DNS.
  103. :type dst_port: string
  104. :param source_ips:
  105. The specified list of source ips of the probe.
  106. :type source_ips: list
  107. :param source_ip_num:
  108. The number of the source ips to be auto generated.
  109. When source_ip_num is not specified,
  110. source_ips must at least contain one ip.
  111. :type source_ip_num: int
  112. :param description:
  113. The description of the probe.
  114. :type description: string
  115. :param payload:
  116. The probe string of the UDP probe
  117. or the domain of the DNS probe.
  118. Must be specified when the protocol is TCP, UDP or DNS.
  119. :type payload: string
  120. :param client_token:
  121. If the clientToken is not specified by the user, a random String
  122. generated by default algorithm will be used.
  123. :type client_token: string
  124. :param config:
  125. :type config: baidubce.BceClientConfiguration
  126. :return:
  127. :rtype: baidubce.bce_response.BceResponse
  128. """
  129. path = utils.append_uri(self.version, b'probe')
  130. if client_token is None:
  131. client_token = generate_client_token()
  132. params = {b'clientToken': client_token}
  133. if source_ips is None:
  134. source_ips = []
  135. body = {
  136. 'name': name,
  137. 'vpcId': vpc_id,
  138. 'subnetId': subnet_id,
  139. 'protocol': protocol,
  140. 'frequency': frequency,
  141. 'sourceIps': source_ips,
  142. 'sourceIpNum': source_ip_num,
  143. }
  144. if description is not None:
  145. body['description'] = description
  146. if dst_ip is not None:
  147. body['destIp'] = dst_ip
  148. if dst_port is not None:
  149. body['destPort'] = dst_port
  150. if payload is not None:
  151. body['payload'] = payload
  152. return self._send_request(http_methods.POST, path,
  153. body=json.dumps(body), params=params,
  154. config=config)
  155. def list_probes(self, marker=None, max_keys=None, config=None):
  156. """
  157. The method to list all probes owned by the authenticated user.
  158. :param marker:
  159. The optional parameter marker specified in the original request to specify
  160. where in the results to begin listing.
  161. Together with the marker, specifies the list result which listing should begin.
  162. If the marker is not specified, the list result will listing from the first one.
  163. :type marker: string
  164. :param max_Keys:
  165. The optional parameter to specifies the max number of list result to return.
  166. The default value is 1000.
  167. :type max_Keys: int
  168. :param config:
  169. :type config: baidubce.BceClientConfiguration
  170. :return:
  171. :rtype baidubce.bce_response.BceResponse
  172. """
  173. path = utils.append_uri(self.version, b'probe')
  174. params = {}
  175. if marker is not None:
  176. params[b'marker'] = marker
  177. if max_keys is not None:
  178. params[b'maxKeys'] = max_keys
  179. return self._send_request(http_methods.GET, path, params=params,
  180. config=config)
  181. @required(probe_id=(bytes, str))
  182. def get_probe(self, probe_id, config=None):
  183. """
  184. The method to get the detail informations of a probe.
  185. :param probe_id:
  186. The id of the probe.
  187. :type probe_id: string
  188. :param config:
  189. :type config: baidubce.BceClientConfiguration
  190. :return:
  191. :rtype baidubce.bce_response.BceResponse
  192. """
  193. path = utils.append_uri(self.version, b'probe', probe_id)
  194. return self._send_request(http_methods.GET, path, config=config)
  195. def update_probe(self, probe_id, name=None, description=None,
  196. dst_ip=None, dst_port=None, frequency=None,
  197. payload=None, client_token=None, config=None):
  198. """
  199. The method to update a probe.
  200. :param probe_id:
  201. The id of the probe.
  202. :type probe_id: string
  203. :param name:
  204. The name of the probe.
  205. :type name: string
  206. :param description:
  207. The description of the probe.
  208. :type description: string
  209. :param dst_ip:
  210. The destination ip of the probe.
  211. :type dst_ip: string
  212. :param dst_port:
  213. The destination port of the probe.
  214. :type dst_port: string
  215. :param frequency:
  216. The number of the probing per minute, 10, 20 or 30.
  217. :type frequency: int
  218. :param payload:
  219. The probe string of the UDP probe and the TCP probe,
  220. or the domain of the DNS probe.
  221. :type payload:string
  222. :param client_token:
  223. If the clientToken is not specified by the user, a random String
  224. generated by default algorithm will be used.
  225. :type client_token: string
  226. :param config:
  227. :type config: baidubce.BceClientConfiguration
  228. :return:
  229. :rtype: baidubce.bce_response.BceResponse
  230. """
  231. path = utils.append_uri(self.version, b'probe', probe_id)
  232. if client_token is None:
  233. client_token = generate_client_token()
  234. params = {b'clientToken': client_token}
  235. body = {}
  236. if name is not None:
  237. body['name'] = name
  238. if description is not None:
  239. body['description'] = description
  240. if dst_ip is not None:
  241. body['destIp'] = dst_ip
  242. if dst_port is not None:
  243. body['destPort'] = dst_port
  244. if frequency is not None:
  245. body['frequency'] = frequency
  246. if payload is not None:
  247. body['payload'] = payload
  248. return self._send_request(http_methods.PUT, path, body=json.dumps(body),
  249. params=params, config=config)
  250. @required(probe_id=(bytes, str))
  251. def delete_probe(self, probe_id, client_token=None, config=None):
  252. """
  253. The method to delete a probe.
  254. :param probe_id:
  255. The id of the probe.
  256. :type probe_id: string
  257. :param client_token:
  258. If the clientToken is not specified by the user, a random String
  259. generated by default algorithm will be used.
  260. :type client_token: string
  261. :param config:
  262. :type config: baidubce.BceClientConfiguration
  263. :return:
  264. :rtype: baidubce.bce_response.BceResponse
  265. """
  266. path = utils.append_uri(self.version, b'probe', probe_id)
  267. if client_token is None:
  268. client_token = generate_client_token()
  269. params = {b'clientToken': client_token}
  270. return self._send_request(http_methods.DELETE, path,
  271. params=params, config=config)
  272. def generate_client_token_by_uuid():
  273. """
  274. The default method to generate the random string for client_token
  275. if the optional parameter client_token is not specified by the user.
  276. :return:
  277. :rtype string
  278. """
  279. return str(uuid.uuid4())
  280. generate_client_token = generate_client_token_by_uuid