chained_endpoint_resolver.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with self work for additional information
  5. # regarding copyright ownership. The ASF licenses self file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use self file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. from aliyunsdkcore.acs_exception.exceptions import ClientException
  20. from aliyunsdkcore.endpoint import EndpointResolver
  21. import aliyunsdkcore.acs_exception.error_code as error_code
  22. import aliyunsdkcore.acs_exception.error_msg as error_msg
  23. class ChainedEndpointResolver(EndpointResolver):
  24. def __init__(self, resolver_chain):
  25. EndpointResolver.__init__(self)
  26. self.endpoint_resolvers = resolver_chain
  27. def _check_product_code(self, request):
  28. for resolver in self.endpoint_resolvers:
  29. if resolver.is_product_code_valid(request):
  30. return
  31. raise ClientException(
  32. error_code.SDK_ENDPOINT_RESOLVING_ERROR,
  33. error_msg.ENDPOINT_NO_PRODUCT.format(
  34. product_code=request.product_code)
  35. )
  36. def _check_region_id(self, request):
  37. for resolver in self.endpoint_resolvers:
  38. if resolver.is_region_id_valid(request):
  39. return
  40. raise ClientException(
  41. error_code.SDK_ENDPOINT_RESOLVING_ERROR,
  42. error_msg.INVALID_REGION_ID.format(region_id=request.region_id)
  43. )
  44. def _get_available_regions_hint(self, product_code):
  45. regions = None
  46. hint = ""
  47. for resolver in self.endpoint_resolvers:
  48. regions = resolver.get_valid_region_ids_by_product(product_code)
  49. if regions is not None:
  50. hint = "\nOr you can use the other available regions:"
  51. for region in regions:
  52. hint += " " + region
  53. break
  54. return hint
  55. def resolve(self, request):
  56. for resolver in self.endpoint_resolvers:
  57. endpoint = resolver.resolve(request)
  58. if endpoint is not None:
  59. return endpoint
  60. self._check_product_code(request)
  61. self._check_region_id(request)
  62. raise ClientException(
  63. error_code.SDK_ENDPOINT_RESOLVING_ERROR,
  64. error_msg.ENDPOINT_NO_REGION.format(
  65. region_id=request.region_id,
  66. product_code=request.product_code,
  67. more=self._get_available_regions_hint(request.product_code)
  68. )
  69. )