esg_client.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 esg.
  14. """
  15. from __future__ import unicode_literals
  16. import copy
  17. import json
  18. import uuid
  19. from baidubce import bce_base_client
  20. from baidubce import compat
  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.services.esg import esg_model
  26. from baidubce.utils import required
  27. class EsgClient(bce_base_client.BceBaseClient):
  28. """
  29. Bcc 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. if config is None:
  36. return self.config
  37. else:
  38. new_config = copy.copy(self.config)
  39. new_config.merge_non_none_values(config)
  40. return new_config
  41. def _send_request(self, http_method, path,
  42. body=None, headers=None, params=None,
  43. config=None, body_parser=None, prefix=None):
  44. config = self._merge_config(config)
  45. if body_parser is None:
  46. body_parser = handler.parse_json
  47. if prefix is None:
  48. prefix = EsgClient.prefix
  49. return bce_http_client.send_request(
  50. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  51. http_method, prefix + path, body, headers, params)
  52. @required(name=(bytes, str), # ***Unicode***
  53. rules=list)
  54. def create_enterprise_security_group(self, name, rules=None,
  55. desc=None,
  56. tags=None,
  57. client_token=None,
  58. config=None):
  59. """
  60. Creating a newly esg with specified rules.
  61. :param name:
  62. The name of esg that will be created.
  63. :type name: string
  64. :param rules:
  65. The list of rules which define how the esg works.
  66. :type rules: list<esg_model.EnterpriseSecurityGroupRuleModel>
  67. :param desc:
  68. The optional parameter to describe the esg that will be created.
  69. :type desc: string
  70. :param tags:
  71. The optional list of tag to be bonded.
  72. :type tags: list<esg_model.TagModel>
  73. :param client_token:
  74. An ASCII string whose length is less than 64.
  75. The request will be idempotent if client token is provided.
  76. If the clientToken is not specified by the user,
  77. a random String generated by default algorithm will be used.
  78. :type client_token: string
  79. :param config:
  80. :type config: baidubce.BceClientConfiguration
  81. :return:
  82. :rtype baidubce.bce_response.BceResponse
  83. """
  84. path = b'/enterprise/security'
  85. params = {
  86. 'clientToken': generate_client_token() if client_token is None else client_token
  87. }
  88. rule_list = [rule.__dict__ for rule in rules]
  89. body = {
  90. 'name': name,
  91. 'desc': desc,
  92. 'rules': rule_list
  93. }
  94. if desc is not None:
  95. body['desc'] = desc
  96. if tags is not None:
  97. tag_list = [tag.__dict__ for tag in tags]
  98. body['tags'] = tag_list
  99. return self._send_request(http_methods.POST, path, json.dumps(body), params=params, config=config)
  100. def list_enterprise_security_groups(self, instance_id=None, marker=None, max_keys=None, config=None):
  101. """
  102. Listing EnterpriseSecurityGroup owned by the authenticated user.
  103. :param instance_id:
  104. The id of instance. The optional parameter to list the SecurityGroup.
  105. If it's specified,only the SecurityGroup related to the specified instance will be listed
  106. :type instance_id: string
  107. :param marker:
  108. The optional parameter marker specified in the original request to specify
  109. where in the results to begin listing.
  110. Together with the marker, specifies the list result which listing should begin.
  111. If the marker is not specified, the list result will listing from the first one.
  112. :type marker: string
  113. :param max_keys:
  114. The optional parameter to specifies the max number of list result to return.
  115. The default value is 1000.
  116. :type max_keys: int
  117. :param config:
  118. :type config: baidubce.BceClientConfiguration
  119. :return:
  120. :rtype baidubce.bce_response.BceResponse
  121. """
  122. path = b'/enterprise/security'
  123. params = {}
  124. if instance_id is not None:
  125. params['instanceId'] = instance_id
  126. if marker is not None:
  127. params['marker'] = marker
  128. if max_keys is not None:
  129. params['maxKeys'] = max_keys
  130. return self._send_request(http_methods.GET, path, params=params, config=config)
  131. @required(enterprise_security_group_id=(bytes, str)) # ***Unicode***
  132. def delete_enterprise_security_group(self, enterprise_security_group_id, config=None):
  133. """
  134. Deleting the specified EnterpriseSecurityGroup.
  135. :param enterprise_security_group_id:
  136. The id of SecurityGroup that will be deleted.
  137. :type enterprise_security_group_id: string
  138. :param config:
  139. :type config: baidubce.BceClientConfiguration
  140. :return:
  141. :rtype baidubce.bce_response.BceResponse
  142. """
  143. enterprise_security_group_id = compat.convert_to_bytes(enterprise_security_group_id)
  144. path = b'/enterprise/security/%s' % enterprise_security_group_id
  145. return self._send_request(http_methods.DELETE, path, config=config)
  146. @required(enterprise_security_group_id=(bytes, str), # ***Unicode***
  147. rule=esg_model.EnterpriseSecurityGroupRuleModel)
  148. def authorize_enterprise_security_group_rule(self, enterprise_security_group_id, rules,
  149. client_token=None,
  150. config=None):
  151. """
  152. authorize a security group rule to the specified security group
  153. :param enterprise_security_group_id:
  154. The id of EnterpriseSecurityGroup that will be authorized.
  155. :type enterprise_security_group_id: string
  156. :param rules:
  157. The list of rules which define how the esg works.
  158. :type rules: list<esg_model.EnterpriseSecurityGroupRuleModel>
  159. :param client_token:
  160. An ASCII string whose length is less than 64.
  161. The request will be idempotent if client token is provided.
  162. If the clientToken is not specified by the user,
  163. a random String generated by default algorithm will be used.
  164. :type client_token: string
  165. :param config:
  166. :type config: baidubce.BceClientConfiguration
  167. :return:
  168. :rtype baidubce.bce_response.BceResponse
  169. """
  170. enterprise_security_group_id = compat.convert_to_bytes(enterprise_security_group_id)
  171. path = b'/enterprise/security/%s' % enterprise_security_group_id
  172. params = {'authorizeRule': ''}
  173. if client_token is None:
  174. params['clientToken'] = generate_client_token()
  175. else:
  176. params['clientToken'] = client_token
  177. rule_list = [rule.__dict__ for rule in rules]
  178. body = {
  179. 'rules': rule_list
  180. }
  181. return self._send_request(http_methods.PUT, path, json.dumps(body), params=params, config=config)
  182. @required(enterprise_security_group_rule_id=(bytes, str)) # ***Unicode***
  183. def update_enterprise_security_group_rule(self, enterprise_security_group_rule_id,
  184. remark=None,
  185. protocol=None,
  186. portrange=None,
  187. source_ip=None,
  188. dest_ip=None,
  189. action=None,
  190. local_ip=None,
  191. priority=None,
  192. source_portrange=None,
  193. config=None):
  194. """
  195. uodate a enterprise security group rule from the specified security group
  196. :param enterprise_security_group_rule_id:
  197. The id of EnterpriseSecurityGroupRule that will be updated.
  198. :param: remark:
  199. The remark for the rule.
  200. :param: portrange:
  201. The port range to specify the port which the rule will work on.
  202. Available range is rang [0, 65535], the fault value is "" for all port.
  203. :param: source_portrange:
  204. The source port range to specify the port which the rule will work on.
  205. Available range is rang [0, 65535], the fault value is "" for all port.
  206. :param: protocol:
  207. The parameter specify which protocol will the rule work on, the fault value is "" for all protocol.
  208. Available protocol are tcp, udp and icmp.
  209. :param: source_ip:
  210. The source ip range with CIDR formats. The default value 0.0.0.0/0 (allow all ip address),
  211. other supported formats such as {ip_addr}/12 or {ip_addr}. Only supports IPV4.
  212. Only works for direction = "ingress".
  213. :param: dest_ip:
  214. The destination ip range with CIDR formats. The default value 0.0.0.0/0 (allow all ip address),
  215. other supported formats such as {ip_addr}/12 or {ip_addr}. Only supports IPV4.
  216. Only works for direction = "egress".
  217. :param: local_ip:
  218. The parameter specify the localIP (allow all ip address: all).
  219. :param: priority:
  220. The parameter specify the priority of the rule(range 1-1000).
  221. :param: action:
  222. The parameter specify the action of the rule, available value are "allow/deny".
  223. :param config:
  224. :type config: baidubce.BceClientConfiguration
  225. :return:
  226. :rtype baidubce.bce_response.BceResponse
  227. """
  228. enterprise_security_group_rule_id = compat.convert_to_bytes(enterprise_security_group_rule_id)
  229. path = b'/enterprise/security/rule/%s' % enterprise_security_group_rule_id
  230. body = {
  231. 'remark': remark,
  232. 'protocol': protocol,
  233. 'portRange': portrange,
  234. 'sourceIp': source_ip,
  235. 'destIp': dest_ip,
  236. 'action': action,
  237. 'localIp': local_ip,
  238. 'priority': priority,
  239. 'sourcePortRange': source_portrange
  240. }
  241. return self._send_request(http_methods.PUT, path, json.dumps(body), params=None, config=config)
  242. @required(enterprise_security_group_rule_id=(bytes, str)) # ***Unicode***
  243. def delete_enterprise_security_group_rule(self, enterprise_security_group_rule_id, config=None):
  244. """
  245. delete a enterprise security group rule from the specified security group
  246. :param enterprise_security_group_rule_id:
  247. The id of EnterpriseSecurityGroupRule that will be deleted.
  248. :type enterprise_security_group_id: string
  249. :param config:
  250. :type config: baidubce.BceClientConfiguration
  251. :return:
  252. :rtype baidubce.bce_response.BceResponse
  253. """
  254. enterprise_security_group_rule_id = compat.convert_to_bytes(enterprise_security_group_rule_id)
  255. path = b'/enterprise/security/rule/%s' % enterprise_security_group_rule_id
  256. return self._send_request(http_methods.DELETE, path, params=None, config=config)
  257. def generate_client_token_by_uuid():
  258. """
  259. The default method to generate the random string for client_token
  260. if the optional parameter client_token is not specified by the user.
  261. :return:
  262. :rtype string
  263. """
  264. return str(uuid.uuid4())
  265. generate_client_token = generate_client_token_by_uuid