def init_env(self, test_local_config=None, client=None): resolver_chain = [] self._user_customized_endpoint_resolver = UserCustomizedEndpointResolver( ) if test_local_config is None: self._local_config_regional_endpoint_resolver = LocalConfigRegionalEndpointResolver( ) self._local_config_global_endpoint_resolver = LocalConfigGlobalEndpointResolver( ) else: self._local_config_regional_endpoint_resolver = \ LocalConfigRegionalEndpointResolver(test_local_config) self._local_config_global_endpoint_resolver = \ LocalConfigGlobalEndpointResolver(test_local_config) if client is not None: self._location_service_endpoint_resolver = LocationServiceEndpointResolver( client) else: self._location_service_endpoint_resolver = LocationServiceEndpointResolver( self.client) resolver_chain.append(self._user_customized_endpoint_resolver) resolver_chain.append(self._local_config_regional_endpoint_resolver) resolver_chain.append(self._local_config_global_endpoint_resolver) resolver_chain.append(self._location_service_endpoint_resolver) self._endpoint_resolver = ChainedEndpointResolver(resolver_chain)
def __init__(self, *args, **kwargs): LocalConfigRegionalEndpointResolver.__init__(self) self.product_code_valid = False self.region_id_valid = False self.endpoint_map = None self.endpoint_regional = None self.request_network = 'public' self.product_suffix = ''
class AliyunFacade: def __init__(self, credentials: AliyunCredentials): self._credentials = credentials self._instantiate_facades() self._resolver = LocalConfigRegionalEndpointResolver() def _instantiate_facades(self): self.actiontrail = ActiontrailFacade(self._credentials) self.ram = RAMFacade(self._credentials) self.ecs = ECSFacade(self._credentials) self.rds = RDSFacade(self._credentials) self.vpc = VPCFacade(self._credentials) self.kms = KMSFacade(self._credentials) async def build_region_list(self, service: str, chosen_regions=None): # TODO could need this for service ids # service = 'ec2containerservice' if service == 'ecs' else service # TODO does a similar endpoint exist? # available_services = await run_concurrently(lambda: Session().get_available_services()) # if service not in available_services: # raise Exception('Service ' + service + ' is not available.') regions = await run_concurrently( lambda: self._resolver.get_valid_region_ids_by_product(product_code =service)) if chosen_regions: return list( (Counter(regions) & Counter(chosen_regions)).elements()) else: return regions
def test_resolver(self): user = UserCustomizedEndpointResolver() chain = [ user ] resolver = ChainedEndpointResolver(chain) # can not be resolved request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") with self.assertRaises(ClientException) as ex: resolver.resolve(request) self.assertEqual(ex.exception.error_code, "SDK.EndpointResolvingError") self.assertEqual(ex.exception.message, "No endpoint for product 'ecs'.\n" "Please check the product code, or set an endpoint for your request " "explicitly.\n" "See https://www.alibabacloud.com/help/doc-detail/92074.htm\n") user.put_endpoint_entry("cn-huhehaote", "ecs", "my-endpoint-for-cnhuhehaote-ecs") # can not be resolved with cn-hangzhou request = ResolveEndpointRequest("cn-hangzhou", "ecs", "", "") with self.assertRaises(ClientException) as ex: resolver.resolve(request) self.assertEqual(ex.exception.error_code, "SDK.EndpointResolvingError") self.assertEqual( ex.exception.message, "No endpoint in the region 'cn-hangzhou' for product 'ecs'.\n" "You can set an endpoint for your request explicitly.\n" "See https://www.alibabacloud.com/help/doc-detail/92074.htm\n" ) # cn-hangzhou and ecs is valid user.put_endpoint_entry("cn-hangzhou", "rds", "my-endpoint-for-cn-hangzhou-rds") with self.assertRaises(ClientException) as ex: resolver.resolve(request) self.assertEqual(ex.exception.error_code, "SDK.EndpointResolvingError") self.assertEqual( ex.exception.message, "No endpoint in the region 'cn-hangzhou' for product 'ecs'.\n" "You can set an endpoint for your request explicitly.\n" "See https://www.alibabacloud.com/help/doc-detail/92074.htm\n") # can be resolved request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") self.assertEqual(resolver.resolve(request), "my-endpoint-for-cnhuhehaote-ecs") chain = [ LocalConfigRegionalEndpointResolver(), user ] resolver = ChainedEndpointResolver(chain) request.request_network = 'inner' endpoint = resolver.resolve(request) self.assertEqual('my-endpoint-for-cnhuhehaote-ecs', endpoint)
def __init__(self, client, user_config=None): self._user_customized_endpoint_resolver = UserCustomizedEndpointResolver() endpoint_resolvers = [ self.predefined_endpoint_resolver, self._user_customized_endpoint_resolver, LocalConfigRegionalEndpointResolver(user_config), LocalConfigGlobalEndpointResolver(user_config), LocationServiceEndpointResolver(client), ] self._resolver = ChainedEndpointResolver(endpoint_resolvers)
def test_resolver(self): resolver = LocalConfigRegionalEndpointResolver() request = ResolveEndpointRequest("", "", "", "") self.assertEqual(resolver.resolve(request), None) self.assertEqual( resolver._make_endpoint_entry_key("ecs", "cn-huhehaote"), "ecs.cn-huhehaote") request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") self.assertEqual(resolver.resolve(request), 'ecs.cn-huhehaote.aliyuncs.com') self.assertTrue(resolver.is_region_id_valid(request)) # resolver.put_endpoint_entry("ecs", "my-endpoint-for-cnhuhehaote-ecs") # request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") # self.assertEqual(resolver.resolve(request), "my-endpoint-for-cnhuhehaote-ecs") # self.assertTrue(resolver.is_region_id_valid(request)) request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "innerAPI") self.assertEqual(resolver.resolve(request), None) # _get_normalized_product_code self.assertEqual(resolver._get_normalized_product_code("cloudapi"), "apigateway") self.assertEqual(resolver._get_normalized_product_code("ecs"), "ecs") self.assertListEqual(resolver.get_valid_region_ids_by_product('ecs'), [ 'ap-northeast-1', 'ap-south-1', 'ap-southeast-2', 'ap-southeast-3', 'ap-southeast-5', 'cn-huhehaote', 'cn-zhangjiakou', 'eu-central-1', 'me-east-1' ]) self.assertIsNone(resolver.get_valid_region_ids_by_product('xxx')) self.assertTrue(resolver.is_product_code_valid(request))
def test_resolver_with_jsonstr(self): resolver = LocalConfigRegionalEndpointResolver("{}") request = ResolveEndpointRequest("", "", "", "") self.assertEqual(resolver.resolve(request), None) self.assertEqual( resolver._make_endpoint_entry_key("ecs", "cn-huhehaote"), "ecs.cn-huhehaote") request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") self.assertEqual(resolver.resolve(request), None) self.assertFalse(resolver.is_region_id_valid(request)) resolver.put_endpoint_entry("ecs.cn-huhehaote", "my-endpoint-for-cnhuhehaote-ecs") request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") self.assertEqual(resolver.resolve(request), "my-endpoint-for-cnhuhehaote-ecs") self.assertFalse(resolver.is_region_id_valid(request)) request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "innerAPI") self.assertEqual(resolver.resolve(request), None) # _get_normalized_product_code self.assertEqual(resolver._get_normalized_product_code("cloudapi"), "cloudapi") self.assertEqual(resolver._get_normalized_product_code("ecs"), "ecs")
def test_resolver(self): resolver = LocalConfigRegionalEndpointResolver() request = ResolveEndpointRequest("", "", "", "") self.assertEqual(resolver.resolve(request), None) self.assertEqual( resolver._make_endpoint_entry_key("ecs", "cn-huhehaote"), "ecs.cn-huhehaote") request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") self.assertEqual(resolver.resolve(request), 'ecs.cn-huhehaote.aliyuncs.com') self.assertTrue(resolver.is_region_id_valid(request)) # resolver.put_endpoint_entry("ecs", "my-endpoint-for-cnhuhehaote-ecs") # request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") # self.assertEqual(resolver.resolve(request), "my-endpoint-for-cnhuhehaote-ecs") # self.assertTrue(resolver.is_region_id_valid(request)) request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "innerAPI") self.assertEqual(resolver.resolve(request), None) # _get_normalized_product_code self.assertEqual(resolver._get_normalized_product_code("cloudapi"), "apigateway") self.assertEqual(resolver._get_normalized_product_code("ecs"), "ecs") self.assertEqual(len(resolver.get_valid_region_ids_by_product('ecs')), 19) self.assertIsNone(resolver.get_valid_region_ids_by_product('xxx')) self.assertTrue(resolver.is_product_code_valid(request))
def __init__(self, credentials: AliyunCredentials): self._credentials = credentials self._instantiate_facades() self._resolver = LocalConfigRegionalEndpointResolver()