class DefaultEndpointResolver(EndpointResolver): # Deprecated use for add_endpoint and modify_endpoint # Not recommended predefined_endpoint_resolver = UserCustomizedEndpointResolver() 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), EndpointResolverRules(), LocationServiceEndpointResolver(client), ] self._resolver = ChainedEndpointResolver(endpoint_resolvers) def resolve(self, request): return self._resolver.resolve(request) def put_endpoint_entry(self, region_id, product_code, endpoint): self._user_customized_endpoint_resolver.put_endpoint_entry( region_id, product_code, endpoint)
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, 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): 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 be resolved request = ResolveEndpointRequest("cn-huhehaote", "ecs", "", "") self.assertEqual(resolver.resolve(request), "my-endpoint-for-cnhuhehaote-ecs")
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)
class NewEndpointTest(SDKTestBase): 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 resolve(self, region_id, product_code, location_service_code=None, endpoint_type=None): request = ResolveEndpointRequest(region_id, product_code, location_service_code, endpoint_type) return self._endpoint_resolver.resolve(request) def test_products_with_location_service(self): request = DescribeRegionsRequest() response = self.client.do_action_with_exception(request) def test_products_without_location_service(self): request = ListAccessKeysRequest() response = self.client.do_action_with_exception(request) def test_add_new_endpoint_manually(self): my_client = self.init_client("cn-ningbo") request = DescribeRegionsRequest() try: response = my_client.do_action_with_exception(request) assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertEqual( "No endpoint in the region 'cn-ningbo' for product 'Ecs'.\n" "You can set an endpoint for your request explicitly.\n" "Or you can use the other available regions: 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\n" "See https://www.alibabacloud.com/help/doc-detail/92074.htm\n", e.get_error_msg()) my_client.add_endpoint( "cn-ningbo", # which does not exist at all "Ecs", "abc.cn-ningbo.endpoint-test.exception.com") with patch.object( my_client._endpoint_resolver, 'resolve', wraps=my_client._endpoint_resolver.resolve) as monkey: monkey.side_effect = ClientException( error_code.SDK_HTTP_ERROR, "abc.cn-ningbo.endpoint-test.exception.com") request2 = DescribeRegionsRequest() try: response2 = my_client.do_action_with_exception(request2) assert False except ClientException as e: self.assertEqual(error_code.SDK_HTTP_ERROR, e.get_error_code()) self.assertEqual("abc.cn-ningbo.endpoint-test.exception.com", e.get_error_msg()) def test_add_existing_endpoint_manually(self): my_client = self.init_client("cn-hangzhou") request = DescribeRegionsRequest() response = my_client.do_action_with_exception(request) my_client.add_endpoint("cn-hangzhou", "Ecs", "abc.cn-hangzhou.endpoint-test.exception.com") with patch.object(my_client._endpoint_resolver, 'resolve') as monkey: monkey.side_effect = ClientException( error_code.SDK_HTTP_ERROR, "abc.cn-hangzhou.endpoint-test.exception.com") request2 = DescribeRegionsRequest() try: response2 = my_client.do_action_with_exception(request2) assert False except ClientException as e: self.assertEqual(error_code.SDK_HTTP_ERROR, e.get_error_code()) self.assertEqual("abc.cn-hangzhou.endpoint-test.exception.com", e.get_error_msg()) def test_regional_endpoint_comes_from_local_config(self): test_config = """ { "regional_endpoints" : { "abc" : { "mars-ningbo" : "ecs.mars-ningbo.aliyuncs.com" } } } """ self.init_env(test_config) self.assertEqual("ecs.mars-ningbo.aliyuncs.com", self.resolve("mars-ningbo", "abc")) def test_global_endpoint_comes_from_local_config(self): test_config = """ { "regional_endpoints" : { "abc" : { "mars-ningbo" : "ecs.mars-ningbo.aliyuncs.com" } }, "global_endpoints" : { "abc" : "ecs.mars.aliyuncs.com" }, "regions" : ["mars-ningbo", "mars-hangzhou", "mars-shanghai"] } """ self.init_env(test_config) self.assertEqual("ecs.mars-ningbo.aliyuncs.com", self.resolve("mars-ningbo", "abc")) self.assertEqual("ecs.mars.aliyuncs.com", self.resolve("mars-hangzhou", "abc")) self.assertEqual("ecs.mars.aliyuncs.com", self.resolve("mars-shanghai", "abc")) def test_endpoint_comes_from_location_service(self): self.init_env("{}") # empty local config with patch.object(self._location_service_endpoint_resolver, '_call_location_service', wraps=self._location_service_endpoint_resolver. _call_location_service) as monkey: for i in range(3): self.assertEqual( "ecs-cn-hangzhou.aliyuncs.com", self.resolve("cn-hangzhou", "ecs", "ecs", None)) self.assertEqual(1, monkey.call_count) def test_location_service_miss(self): self.init_env("{}") # empty local config with patch.object(self._location_service_endpoint_resolver, '_call_location_service', wraps=self._location_service_endpoint_resolver. _call_location_service) as monkey: self.assertEqual(0, monkey.call_count) # No openAPI data for i in range(3): try: self.resolve("cn-hangzhou", "Ram", "ram", "openAPI") assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertTrue(e.get_error_msg().startswith( "No endpoint in the region 'cn-hangzhou' for product 'Ram'." )) self.assertEqual(1, monkey.call_count) # Bad region ID for i in range(3): try: self.resolve("mars", "Ram", "ram", "openAPI") assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertEqual( "No such region 'mars'. Please check your region ID.", e.get_error_msg()) with patch.object(self._location_service_endpoint_resolver, '_call_location_service', wraps=self._location_service_endpoint_resolver. _call_location_service) as monkey: self.assertEqual(0, monkey.call_count) # Bad region ID with another product try: self.resolve("mars", "Ecs", "ecs", "openAPI") assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) print(e.get_error_msg()) self.assertEqual( "No such region 'mars'. Please check your region ID.", e.get_error_msg()) self.assertEqual(0, monkey.call_count) # Bad product code for i in range(3): try: self.resolve("cn-hangzhou", "InvalidProductCode", "InvalidProductCode", "openAPI") assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertTrue(e.get_error_msg().startswith( "No endpoint for product 'InvalidProductCode'.\n" "Please check the product code, " "or set an endpoint for your request explicitly.\n")) with patch.object(self._location_service_endpoint_resolver, '_call_location_service', wraps=self._location_service_endpoint_resolver. _call_location_service) as monkey: # Bad product code with another region ID try: self.resolve("cn-beijing", "InvalidProductCode", "InvalidProductCode", "openAPI") assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertTrue(e.get_error_msg().startswith( "No endpoint for product 'InvalidProductCode'.\n" "Please check the product code, " "or set an endpoint for your request explicitly.\n")) self.assertEqual(0, monkey.call_count) def test_try_to_get_endpoint_with_invalid_region_id(self): self.init_env() try: print(self.resolve("mars", "Ecs")) assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertEqual( "No such region 'mars'. Please check your region ID.", e.get_error_msg()) def test_try_to_get_endpoint_with_invalid_product_code(self): self.init_env() try: self.resolve("cn-beijing", "InvalidProductCode") assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertTrue(e.get_error_msg().startswith( "No endpoint for product 'InvalidProductCode'.\n" "Please check the product code, " "or set an endpoint for your request explicitly.\n")) def test_inner_api_endpoint(self): self.init_env() self.assertEqual("ram-share.aliyuncs.com", self.resolve("cn-hangzhou", "Ram", "ram", "innerAPI")) def test_get_inner_api_endpoint_bypass_local_config(self): test_config = """ { "regional_endpoints" : { "ram" : { "cn-hangzhou" : "ram.mars-ningbo.aliyuncs.com" } }, "global_endpoints" : { "ram" : "ram.mars.aliyuncs.com" } } """ self.init_env(test_config) self.assertEqual("ram-share.aliyuncs.com", self.resolve("cn-hangzhou", "Ram", "ram", "innerAPI")) def test_get_inner_api_endpoint_by_manually_adding(self): self.init_env() self._user_customized_endpoint_resolver.put_endpoint_entry( "cn-hangzhou", "Ram", "ram.cn-hangzhou.endpoint-test.exception.com") self.assertEqual("ram.cn-hangzhou.endpoint-test.exception.com", self.resolve("cn-hangzhou", "Ram", "ram", "innerAPI")) def test_can_not_connect_location_service(self): self.init_env() self._location_service_endpoint_resolver.set_location_service_endpoint( "location-on-mars.aliyuncs.com") try: self.resolve("cn-hangzhou", "Ecs", "ecs", "innerAPI") assert False except ClientException as e: self.assertEqual("SDK.HttpError", e.get_error_code()) def test_invalid_access_key_id(self): client = AcsClient("BadAccessKeyId", self.access_key_secret, "cn-hangzhou") self.init_env(None, client) try: self.resolve("cn-hangzhou", "Ecs", "ecs", "innerAPI") assert False except ServerException as e: self.assertEqual("InvalidAccessKeyId.NotFound", e.get_error_code()) def test_invalid_access_key_secret(self): client = AcsClient(self.access_key_id, "BadAccessKeySecret", "cn-hangzhou") self.init_env(None, client) try: self.resolve("cn-hangzhou", "Ecs", "ecs", "innerAPI") assert False except ServerException as e: self.assertEqual("SignatureDoesNotMatch", e.get_error_code()) def test_local_clock_screw_when_call_location_service(self): # Not implemented pass def test_call_rpc_request_with_client(self): request = DescribeRegionsRequest() response = self.client.do_action_with_exception(request) def test_call_roa_request_with_client(self): request = DescribeResourcesRequest() request.set_StackId("StackId") request.set_StackName("StackName") try: response = self.client.do_action_with_exception(request) assert False except ServerException as e: self.assertEqual("StackNotFound", e.get_error_code()) def test_location_service_code_not_equals_product_code(self): request = DescribeApisRequest() response = self.client.do_action_with_exception(request) def test_location_service_code_not_equals_product_code2(self): self.init_env("{}") self.client._endpoint_resolver = self._endpoint_resolver with patch.object(self._location_service_endpoint_resolver, '_call_location_service', wraps=self._location_service_endpoint_resolver. _call_location_service) as monkey: for i in range(3): request = DescribeApisRequest() response = self.client.do_action_with_exception(request) self.assertEqual(1, monkey.call_count) self.init_env() self.client._endpoint_resolver = self._endpoint_resolver def test_add_endpoint_static(self): from aliyunsdkcore.profile.region_provider import add_endpoint, modify_point my_client = self.init_client("cn-ningbo") request = DescribeRegionsRequest() try: response = my_client.do_action_with_exception(request) assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertEqual( "No such region 'cn-ningbo'. Please check your region ID.", e.get_error_msg()) add_endpoint( "Ecs", # which does not exist at all "cn-ningbo", "abc.cn-ningbo.endpoint-test.exception.com") with patch.object( my_client._endpoint_resolver, 'resolve', wraps=my_client._endpoint_resolver.resolve) as monkey: monkey.side_effect = ClientException( error_code.SDK_HTTP_ERROR, "abc.cn-ningbo.endpoint-test.exception.com") request2 = DescribeRegionsRequest() try: response2 = my_client.do_action_with_exception(request2) assert False except ClientException as e: self.assertEqual(error_code.SDK_HTTP_ERROR, e.get_error_code()) self.assertEqual("abc.cn-ningbo.endpoint-test.exception.com", e.get_error_msg()) DefaultEndpointResolver.predefined_endpoint_resolver.reset() def test_doc_help_sample(self): from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest request = DescribeInstancesRequest() request.set_endpoint("ecs-cn-hangzhou.aliyuncs.com") response = self.client.do_action_with_exception(request) def test_r_kvstore(self): resolver = DefaultEndpointResolver(self.client) request = ResolveEndpointRequest("cn-hangzhou", "R-kvstore", None, None) self.assertEqual("r-kvstore.aliyuncs.com", resolver.resolve(request)) def test_dts_regions(self): resolver = DefaultEndpointResolver(self.client) request = ResolveEndpointRequest("cn-chengdu", "dts", None, None) expected_message = "No endpoint in the region 'cn-chengdu' for product 'dts'.\n" \ "You can set an endpoint for your request explicitly.\n" \ "Or you can use the other available regions: ap-southeast-1 " \ "cn-beijing cn-hangzhou cn-hongkong cn-huhehaote cn-qingdao " \ "cn-shanghai cn-shenzhen cn-zhangjiakou\n" \ "See https://www.alibabacloud.com/help/doc-detail/92074.htm\n" try: resolver.resolve(request) assert False except ClientException as e: self.assertEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code()) self.assertEqual(expected_message, e.get_error_msg()) def test_bssopenapi_resolve(self): resolver = DefaultEndpointResolver(self.client) request = ResolveEndpointRequest("cn-hangzhou", "BssOpenApi", None, None) self.assertEqual("business.aliyuncs.com", resolver.resolve(request)) request = ResolveEndpointRequest("eu-west-1", "BssOpenApi", None, None) self.assertEqual("business.ap-southeast-1.aliyuncs.com", resolver.resolve(request)) from aliyunsdkbssopenapi.request.v20171214.GetOrderDetailRequest \ import GetOrderDetailRequest request = GetOrderDetailRequest() request.set_OrderId("blah") try: self.client.do_action_with_exception(request) except ServerException as e: self.assertEqual("InternalError", e.get_error_code()) self.assertEqual( "The request processing has failed due to some unknown error.", e.get_error_msg()) def test_faas_resolve(self): resolver = DefaultEndpointResolver(self.client) request = ResolveEndpointRequest("cn-hangzhou", "faas", None, None) self.assertEqual("faas.cn-hangzhou.aliyuncs.com", resolver.resolve(request)) from aliyunsdkfaas.request.v20170824.DescribeLoadTaskStatusRequest \ import DescribeLoadTaskStatusRequest request = DescribeLoadTaskStatusRequest() request.set_FpgaUUID("blah") request.set_InstanceId("blah") request.set_RoleArn("blah") try: self.client.do_action_with_exception(request) assert False except ServerException as e: self.assertNotEqual(error_code.SDK_ENDPOINT_RESOLVING_ERROR, e.get_error_code())
def setUp(self): SDKTestBase.setUp(self) DefaultEndpointResolver.predefined_endpoint_resolver = UserCustomizedEndpointResolver( )
def test_resolver(self): resolver = UserCustomizedEndpointResolver() 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.assertTrue(resolver.is_region_id_valid(request)) resolver.put_endpoint_entry("cn-huhehaote", "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)) resolver.reset() self.assertEqual(resolver.resolve(request), None)