endpoint_resolver_rules.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.endpoint.local_config_regional_endpoint_resolver \
  20. import LocalConfigRegionalEndpointResolver
  21. class EndpointResolverRules(LocalConfigRegionalEndpointResolver):
  22. def __init__(self, *args, **kwargs):
  23. LocalConfigRegionalEndpointResolver.__init__(self)
  24. self.product_code_valid = False
  25. self.region_id_valid = False
  26. self.endpoint_map = None
  27. self.endpoint_regional = None
  28. self.request_network = 'public'
  29. self.product_suffix = ''
  30. def resolve(self, request):
  31. if request.endpoint_map is None or request.endpoint_regional is None:
  32. return None
  33. request_network = "public" if not request.request_network else request.request_network
  34. endpoint_regional = request.endpoint_regional
  35. endpoint = ""
  36. if request_network == "public":
  37. endpoint = request.endpoint_map.get(request.region_id, "")
  38. if endpoint == "":
  39. if endpoint_regional == "regional":
  40. if request.region_id not in self._valid_region_ids:
  41. return None
  42. endpoint_domain = ".{region_id}.aliyuncs.com".format(
  43. region_id=request.region_id.lower())
  44. elif endpoint_regional == "central":
  45. endpoint_domain = ".aliyuncs.com"
  46. else:
  47. return None
  48. network = "" if request_network == "public" else "-" + request_network
  49. suffix = "-" + request.product_suffix if request.product_suffix else ""
  50. endpoint_param_list = [request.product_code_lower, suffix, network, endpoint_domain]
  51. endpoint = "".join(list(filter(lambda x: x, endpoint_param_list)))
  52. return endpoint
  53. def is_product_code_valid(self, request):
  54. return self.product_code_valid
  55. def is_region_id_valid(self, request):
  56. return self.region_id_valid
  57. @classmethod
  58. def get_valid_region_ids_by_product(cls, product_code):
  59. return None