You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
9.3 KiB

4 years ago
  1. # Copyright (c) 2006-2010 Mitch Garnaat http://garnaat.org/
  2. # Copyright (c) 2010, Eucalyptus Systems, Inc.
  3. # All rights reserved.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish, dis-
  9. # tribute, sublicense, and/or sell copies of the Software, and to permit
  10. # persons to whom the Software is furnished to do so, subject to the fol-
  11. # lowing conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included
  14. # in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  18. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  19. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. # IN THE SOFTWARE.
  23. import os
  24. import boto
  25. from boto.compat import json
  26. from boto.exception import BotoClientError
  27. from boto.endpoints import BotoEndpointResolver
  28. from boto.endpoints import StaticEndpointBuilder
  29. _endpoints_cache = {}
  30. def load_endpoint_json(path):
  31. """
  32. Loads a given JSON file & returns it.
  33. :param path: The path to the JSON file
  34. :type path: string
  35. :returns: The loaded data
  36. """
  37. return _load_json_file(path)
  38. def _load_json_file(path):
  39. """
  40. Loads a given JSON file & returns it.
  41. :param path: The path to the JSON file
  42. :type path: string
  43. :returns: The loaded data
  44. """
  45. with open(path, 'r') as endpoints_file:
  46. return json.load(endpoints_file)
  47. def merge_endpoints(defaults, additions):
  48. """
  49. Given an existing set of endpoint data, this will deep-update it with
  50. any similarly structured data in the additions.
  51. :param defaults: The existing endpoints data
  52. :type defaults: dict
  53. :param defaults: The additional endpoints data
  54. :type defaults: dict
  55. :returns: The modified endpoints data
  56. :rtype: dict
  57. """
  58. # We can't just do an ``defaults.update(...)`` here, as that could
  59. # *overwrite* regions if present in both.
  60. # We'll iterate instead, essentially doing a deeper merge.
  61. for service, region_info in additions.items():
  62. # Set the default, if not present, to an empty dict.
  63. defaults.setdefault(service, {})
  64. defaults[service].update(region_info)
  65. return defaults
  66. def load_regions():
  67. """
  68. Actually load the region/endpoint information from the JSON files.
  69. By default, this loads from the default included ``boto/endpoints.json``
  70. file.
  71. Users can override/extend this by supplying either a ``BOTO_ENDPOINTS``
  72. environment variable or a ``endpoints_path`` config variable, either of
  73. which should be an absolute path to the user's JSON file.
  74. :returns: The endpoints data
  75. :rtype: dict
  76. """
  77. # Load the defaults first.
  78. endpoints = _load_builtin_endpoints()
  79. additional_path = None
  80. # Try the ENV var. If not, check the config file.
  81. if os.environ.get('BOTO_ENDPOINTS'):
  82. additional_path = os.environ['BOTO_ENDPOINTS']
  83. elif boto.config.get('Boto', 'endpoints_path'):
  84. additional_path = boto.config.get('Boto', 'endpoints_path')
  85. # If there's a file provided, we'll load it & additively merge it into
  86. # the endpoints.
  87. if additional_path:
  88. additional = load_endpoint_json(additional_path)
  89. endpoints = merge_endpoints(endpoints, additional)
  90. return endpoints
  91. def _load_builtin_endpoints(_cache=_endpoints_cache):
  92. """Loads the builtin endpoints in the legacy format."""
  93. # If there's a cached response, return it
  94. if _cache:
  95. return _cache
  96. # Load the endpoints file
  97. endpoints = _load_json_file(boto.ENDPOINTS_PATH)
  98. # Build the endpoints into the legacy format
  99. resolver = BotoEndpointResolver(endpoints)
  100. builder = StaticEndpointBuilder(resolver)
  101. endpoints = builder.build_static_endpoints()
  102. # Cache the endpoints and then return them
  103. _cache.update(endpoints)
  104. return _cache
  105. def get_regions(service_name, region_cls=None, connection_cls=None):
  106. """
  107. Given a service name (like ``ec2``), returns a list of ``RegionInfo``
  108. objects for that service.
  109. This leverages the ``endpoints.json`` file (+ optional user overrides) to
  110. configure/construct all the objects.
  111. :param service_name: The name of the service to construct the ``RegionInfo``
  112. objects for. Ex: ``ec2``, ``s3``, ``sns``, etc.
  113. :type service_name: string
  114. :param region_cls: (Optional) The class to use when constructing. By
  115. default, this is ``RegionInfo``.
  116. :type region_cls: class
  117. :param connection_cls: (Optional) The connection class for the
  118. ``RegionInfo`` object. Providing this allows the ``connect`` method on
  119. the ``RegionInfo`` to work. Default is ``None`` (no connection).
  120. :type connection_cls: class
  121. :returns: A list of configured ``RegionInfo`` objects
  122. :rtype: list
  123. """
  124. endpoints = load_regions()
  125. if service_name not in endpoints:
  126. raise BotoClientError(
  127. "Service '%s' not found in endpoints." % service_name
  128. )
  129. if region_cls is None:
  130. region_cls = RegionInfo
  131. region_objs = []
  132. for region_name, endpoint in endpoints.get(service_name, {}).items():
  133. region_objs.append(
  134. region_cls(
  135. name=region_name,
  136. endpoint=endpoint,
  137. connection_cls=connection_cls
  138. )
  139. )
  140. return region_objs
  141. def connect(service_name, region_name, region_cls=None,
  142. connection_cls=None, **kw_params):
  143. """Create a connection class for a given service in a given region.
  144. :param service_name: The name of the service to construct the
  145. ``RegionInfo`` object for, e.g. ``ec2``, ``s3``, etc.
  146. :type service_name: str
  147. :param region_name: The name of the region to connect to, e.g.
  148. ``us-west-2``, ``eu-central-1``, etc.
  149. :type region_name: str
  150. :param region_cls: (Optional) The class to use when constructing. By
  151. default, this is ``RegionInfo``.
  152. :type region_cls: class
  153. :param connection_cls: (Optional) The connection class for the
  154. ``RegionInfo`` object. Providing this allows the ``connect`` method on
  155. the ``RegionInfo`` to work. Default is ``None`` (no connection).
  156. :type connection_cls: class
  157. :returns: A configured connection class.
  158. """
  159. if region_cls is None:
  160. region_cls = RegionInfo
  161. region = _get_region(service_name, region_name, region_cls, connection_cls)
  162. if region is None and _use_endpoint_heuristics():
  163. region = _get_region_with_heuristics(
  164. service_name, region_name, region_cls, connection_cls
  165. )
  166. if region is None:
  167. return None
  168. return region.connect(**kw_params)
  169. def _get_region(service_name, region_name, region_cls=None,
  170. connection_cls=None):
  171. """Finds the region by searching through the known regions."""
  172. for region in get_regions(service_name, region_cls, connection_cls):
  173. if region.name == region_name:
  174. return region
  175. return None
  176. def _get_region_with_heuristics(service_name, region_name, region_cls=None,
  177. connection_cls=None):
  178. """Finds the region using known regions and heuristics."""
  179. endpoints = load_endpoint_json(boto.ENDPOINTS_PATH)
  180. resolver = BotoEndpointResolver(endpoints)
  181. hostname = resolver.resolve_hostname(service_name, region_name)
  182. return region_cls(
  183. name=region_name,
  184. endpoint=hostname,
  185. connection_cls=connection_cls
  186. )
  187. def _use_endpoint_heuristics():
  188. env_var = os.environ.get('BOTO_USE_ENDPOINT_HEURISTICS', 'false').lower()
  189. config_var = boto.config.getbool('Boto', 'use_endpoint_heuristics', False)
  190. return env_var == 'true' or config_var
  191. class RegionInfo(object):
  192. """
  193. Represents an AWS Region
  194. """
  195. def __init__(self, connection=None, name=None, endpoint=None,
  196. connection_cls=None):
  197. self.connection = connection
  198. self.name = name
  199. self.endpoint = endpoint
  200. self.connection_cls = connection_cls
  201. def __repr__(self):
  202. return 'RegionInfo:%s' % self.name
  203. def startElement(self, name, attrs, connection):
  204. return None
  205. def endElement(self, name, value, connection):
  206. if name == 'regionName':
  207. self.name = value
  208. elif name == 'regionEndpoint':
  209. self.endpoint = value
  210. else:
  211. setattr(self, name, value)
  212. def connect(self, **kw_params):
  213. """
  214. Connect to this Region's endpoint. Returns an connection
  215. object pointing to the endpoint associated with this region.
  216. You may pass any of the arguments accepted by the connection
  217. class's constructor as keyword arguments and they will be
  218. passed along to the connection object.
  219. :rtype: Connection object
  220. :return: The connection to this regions endpoint
  221. """
  222. if self.connection_cls:
  223. return self.connection_cls(region=self, **kw_params)