refactor endpoints implementation
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
tools/endpoints.json
|
||||
initCoverage.info
|
||||
testCoverage.info
|
||||
coverageReport/
|
||||
|
||||
@@ -52,19 +52,11 @@ class ALIBABACLOUD_CORE_EXPORT EndpointProvider :
|
||||
using LocationClient::describeEndpoints;
|
||||
|
||||
private:
|
||||
struct Product {
|
||||
std::string code;
|
||||
std::string locationServiceCode;
|
||||
std::string documentId;
|
||||
std::map<std::string, std::string> regionalEndpoints;
|
||||
std::string globalEndpoint;
|
||||
std::string regionalEndpointPattern;
|
||||
};
|
||||
|
||||
EndpointOutcome loadRemoteEndpoint();
|
||||
bool checkExpiry()const;
|
||||
bool loadInternalProductsInfo();
|
||||
std::string internalEndpoint(const std::string regionId,
|
||||
|
||||
bool loadLocalProductsInfo();
|
||||
std::string localEndpoint(const std::string regionId,
|
||||
const std::string product);
|
||||
|
||||
std::mutex cachedMutex_;
|
||||
@@ -74,7 +66,6 @@ class ALIBABACLOUD_CORE_EXPORT EndpointProvider :
|
||||
std::string regionId_;
|
||||
std::string product_;
|
||||
std::string serviceCode_;
|
||||
std::list<Product> internalProductsInfo_;
|
||||
};
|
||||
} // namespace AlibabaCloud
|
||||
#endif // CORE_INCLUDE_ALIBABACLOUD_CORE_ENDPOINTPROVIDER_H_
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include <json/json.h>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include "LocalEndpoints.h"
|
||||
|
||||
namespace AlibabaCloud {
|
||||
|
||||
@@ -29,55 +31,62 @@ namespace {
|
||||
# include <strings.h>
|
||||
#endif
|
||||
|
||||
const std::string INTERNAL_ENDPOINTS_DATA = "{\"products\":["
|
||||
"{\"code\":\"aegis\",\"document_id\":\"28449\",\"location_service_code\":\"vipaegis\",\"regional_endpoints\":[],\"global_endpoint\":\"aegis.cn-hangzhou.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"alidns\",\"document_id\":\"29739\",\"location_service_code\":\"alidns\",\"regional_endpoints\":[],\"global_endpoint\":\"alidns.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"arms\",\"document_id\":\"42924\",\"location_service_code\":\"\",\"regional_endpoints\":[{\"region\":\"ap-southeast-1\",\"endpoint\":\"arms.ap-southeast-1.aliyuncs.com\"},{\"region\":\"cn-beijing\",\"endpoint\":\"arms.cn-beijing.aliyuncs.com\"},{\"region\":\"cn-hangzhou\",\"endpoint\":\"arms.cn-hangzhou.aliyuncs.com\"},{\"region\":\"cn-hongkong\",\"endpoint\":\"arms.cn-hongkong.aliyuncs.com\"},{\"region\":\"cn-qingdao\",\"endpoint\":\"arms.cn-qingdao.aliyuncs.com\"},{\"region\":\"cn-shanghai\",\"endpoint\":\"arms.cn-shanghai.aliyuncs.com\"},{\"region\":\"cn-shenzhen\",\"endpoint\":\"arms.cn-shenzhen.aliyuncs.com\"}],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"arms.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"batchcompute\",\"document_id\":\"44717\",\"location_service_code\":\"\",\"regional_endpoints\":[{\"region\":\"ap-southeast-1\",\"endpoint\":\"batchcompute.ap-southeast-1.aliyuncs.com\"},{\"region\":\"cn-beijing\",\"endpoint\":\"batchcompute.cn-beijing.aliyuncs.com\"},{\"region\":\"cn-hangzhou\",\"endpoint\":\"batchcompute.cn-hangzhou.aliyuncs.com\"},{\"region\":\"cn-huhehaote\",\"endpoint\":\"batchcompute.cn-huhehaote.aliyuncs.com\"},{\"region\":\"cn-qingdao\",\"endpoint\":\"batchcompute.cn-qingdao.aliyuncs.com\"},{\"region\":\"cn-shanghai\",\"endpoint\":\"batchcompute.cn-shanghai.aliyuncs.com\"},{\"region\":\"cn-shenzhen\",\"endpoint\":\"batchcompute.cn-shenzhen.aliyuncs.com\"},{\"region\":\"cn-zhangjiakou\",\"endpoint\":\"batchcompute.cn-zhangjiakou.aliyuncs.com\"},{\"region\":\"us-west-1\",\"endpoint\":\"batchcompute.us-west-1.aliyuncs.com\"}],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"batchcompute.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"ccc\",\"document_id\":\"63027\",\"location_service_code\":\"ccc\",\"regional_endpoints\":[{\"region\":\"cn-hangzhou\",\"endpoint\":\"ccc.cn-hangzhou.aliyuncs.com\"},{\"region\":\"cn-shanghai\",\"endpoint\":\"ccc.cn-shanghai.aliyuncs.com\"}],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"ccc.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"cdn\",\"document_id\":\"27148\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"cdn.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"cds\",\"document_id\":\"62887\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"cds.cn-beijing.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"chatbot\",\"document_id\":\"60760\",\"location_service_code\":\"beebot\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"chatbot.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"cloudapi\",\"document_id\":\"43590\",\"location_service_code\":\"apigateway\",\"regional_endpoints\":[{\"region\":\"ap-northeast-1\",\"endpoint\":\"apigateway.ap-northeast-1.aliyuncs.com\"},{\"region\":\"us-west-1\",\"endpoint\":\"apigateway.us-west-1.aliyuncs.com\"}],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"apigateway.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"cloudauth\",\"document_id\":\"60687\",\"location_service_code\":\"cloudauth\",\"regional_endpoints\":[],\"global_endpoint\":\"cloudauth.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"cloudphoto\",\"document_id\":\"59902\",\"location_service_code\":\"cloudphoto\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"cloudphoto.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"cloudwf\",\"document_id\":\"58111\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"cloudwf.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"cms\",\"document_id\":\"28615\",\"location_service_code\":\"cms\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"cr\",\"document_id\":\"60716\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"cr.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"cs\",\"document_id\":\"26043\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"cs.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"csb\",\"document_id\":\"64837\",\"location_service_code\":\"\",\"regional_endpoints\":[{\"region\":\"cn-beijing\",\"endpoint\":\"csb.cn-beijing.aliyuncs.com\"},{\"region\":\"cn-hangzhou\",\"endpoint\":\"csb.cn-hangzhou.aliyuncs.com\"}],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"csb.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"dds\",\"document_id\":\"61715\",\"location_service_code\":\"dds\",\"regional_endpoints\":[],\"global_endpoint\":\"mongodb.aliyuncs.com\",\"regional_endpoint_pattern\":\"mongodb.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"dm\",\"document_id\":\"29434\",\"location_service_code\":\"\",\"regional_endpoints\":[{\"region\":\"ap-southeast-1\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"ap-southeast-2\",\"endpoint\":\"dm.ap-southeast-2.aliyuncs.com\"},{\"region\":\"cn-beijing\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"cn-hangzhou\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"cn-hongkong\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"cn-qingdao\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"cn-shanghai\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"cn-shenzhen\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"us-east-1\",\"endpoint\":\"dm.aliyuncs.com\"},{\"region\":\"us-west-1\",\"endpoint\":\"dm.aliyuncs.com\"}],\"global_endpoint\":\"dm.aliyuncs.com\",\"regional_endpoint_pattern\":\"dm.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"domain\",\"document_id\":\"42875\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"domain.aliyuncs.com\",\"regional_endpoint_pattern\":\"domain.aliyuncs.com\"},"
|
||||
"{\"code\":\"domain-intl\",\"document_id\":\"\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"domain-intl.aliyuncs.com\",\"regional_endpoint_pattern\":\"domain-intl.aliyuncs.com\"},"
|
||||
"{\"code\":\"drds\",\"document_id\":\"51111\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"drds.aliyuncs.com\",\"regional_endpoint_pattern\":\"drds.aliyuncs.com\"},"
|
||||
"{\"code\":\"ecs\",\"document_id\":\"25484\",\"location_service_code\":\"ecs\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"emr\",\"document_id\":\"28140\",\"location_service_code\":\"emr\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"emr.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"ess\",\"document_id\":\"25925\",\"location_service_code\":\"ess\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"ess.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"green\",\"document_id\":\"28427\",\"location_service_code\":\"green\",\"regional_endpoints\":[],\"global_endpoint\":\"green.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"hpc\",\"document_id\":\"35201\",\"location_service_code\":\"hpc\",\"regional_endpoints\":[],\"global_endpoint\":\"hpc.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"httpdns\",\"document_id\":\"52679\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"httpdns-api.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"iot\",\"document_id\":\"30557\",\"location_service_code\":\"iot\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"iot.[RegionId].aliyuncs.com\"},"
|
||||
"{\"code\":\"itaas\",\"document_id\":\"55759\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"itaas.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"jaq\",\"document_id\":\"35037\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"jaq.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"live\",\"document_id\":\"48207\",\"location_service_code\":\"live\",\"regional_endpoints\":[],\"global_endpoint\":\"live.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"mts\",\"document_id\":\"29212\",\"location_service_code\":\"mts\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"nas\",\"document_id\":\"62598\",\"location_service_code\":\"nas\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"ons\",\"document_id\":\"44416\",\"location_service_code\":\"ons\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"polardb\",\"document_id\":\"58764\",\"location_service_code\":\"polardb\",\"regional_endpoints\":[{\"region\":\"ap-south-1\",\"endpoint\":\"polardb.ap-south-1.aliyuncs.com\"},{\"region\":\"ap-southeast-5\",\"endpoint\":\"polardb.ap-southeast-5.aliyuncs.com\"}],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"polardb.aliyuncs.com\"},"
|
||||
"{\"code\":\"push\",\"document_id\":\"30074\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"cloudpush.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"qualitycheck\",\"document_id\":\"50807\",\"location_service_code\":\"\",\"regional_endpoints\":[{\"region\":\"cn-hangzhou\",\"endpoint\":\"qualitycheck.cn-hangzhou.aliyuncs.com\"}],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"r-kvstore\",\"document_id\":\"60831\",\"location_service_code\":\"redisa\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"ram\",\"document_id\":\"28672\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"ram.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"rds\",\"document_id\":\"26223\",\"location_service_code\":\"rds\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"ros\",\"document_id\":\"28899\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"ros.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"sas-api\",\"document_id\":\"28498\",\"location_service_code\":\"sas\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"slb\",\"document_id\":\"27565\",\"location_service_code\":\"slb\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"sts\",\"document_id\":\"28756\",\"location_service_code\":\"\",\"regional_endpoints\":[],\"global_endpoint\":\"sts.aliyuncs.com\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"vod\",\"document_id\":\"60574\",\"location_service_code\":\"vod\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"vpc\",\"document_id\":\"34962\",\"location_service_code\":\"vpc\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"},"
|
||||
"{\"code\":\"waf\",\"document_id\":\"62847\",\"location_service_code\":\"waf\",\"regional_endpoints\":[],\"global_endpoint\":\"\",\"regional_endpoint_pattern\":\"\"}"
|
||||
"]}";
|
||||
bool local_endpoints_loaded = false;
|
||||
typedef std::string productType;
|
||||
typedef std::string regionType;
|
||||
typedef std::string endpointType;
|
||||
typedef std::string mappingType;
|
||||
typedef std::vector<regionType> regionsType;
|
||||
typedef std::map<productType, endpointType> regionalType;
|
||||
typedef struct {
|
||||
regionsType regions;
|
||||
regionalType regional;
|
||||
} productInfoType;
|
||||
|
||||
static std::vector<regionType> allRegions;
|
||||
static std::vector<productType> allProductsInLocalEndpoints;
|
||||
static std::map<productType, productInfoType> allLocalEndpoints;
|
||||
|
||||
static void LoadLocalEndpoints() {
|
||||
Json::Reader reader;
|
||||
Json::Value value;
|
||||
|
||||
if (local_endpoints_loaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!reader.parse(LOCAL_ENDPOINTS_CONFIG, value)){
|
||||
return;
|
||||
}
|
||||
|
||||
auto regions = value["regions"];
|
||||
for (const auto ®ion : regions) {
|
||||
allRegions.push_back(region.asString());
|
||||
}
|
||||
|
||||
auto products = value["products"];
|
||||
for (const auto &product : products) {
|
||||
allProductsInLocalEndpoints.push_back(product.asString());
|
||||
}
|
||||
|
||||
auto endpoints = value["endpoints"];
|
||||
for (auto &product : allProductsInLocalEndpoints ) {
|
||||
auto endpoint_per_product = endpoints[product];
|
||||
productInfoType p;
|
||||
|
||||
auto regions = endpoint_per_product["regions"];
|
||||
auto regional = endpoint_per_product["regional"];
|
||||
|
||||
for (auto & r : regions) {
|
||||
const std::string region = r.asString();
|
||||
p.regions.push_back(region);
|
||||
p.regional[region] = endpoint_per_product["regional"][region].asString();
|
||||
}
|
||||
allLocalEndpoints[product] = p;
|
||||
}
|
||||
local_endpoints_loaded = true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
EndpointProvider::EndpointProvider(
|
||||
@@ -92,12 +101,11 @@ EndpointProvider::EndpointProvider(
|
||||
durationSeconds_(durationSeconds),
|
||||
cachedMutex_(),
|
||||
cachedEndpoint_(),
|
||||
expiry_(),
|
||||
internalProductsInfo_() {
|
||||
loadInternalProductsInfo();
|
||||
expiry_() {
|
||||
transform(product_.begin(), product_.end(), product_.begin(), ::tolower);
|
||||
loadLocalProductsInfo();
|
||||
}
|
||||
|
||||
|
||||
EndpointProvider::EndpointProvider(
|
||||
const Credentials& credentials,
|
||||
const ClientConfiguration &configuration,
|
||||
@@ -112,83 +120,87 @@ EndpointProvider::EndpointProvider(
|
||||
durationSeconds_(durationSeconds),
|
||||
cachedMutex_(),
|
||||
cachedEndpoint_(),
|
||||
expiry_(),
|
||||
internalProductsInfo_() {
|
||||
loadInternalProductsInfo();
|
||||
expiry_() {
|
||||
transform(product_.begin(), product_.end(), product_.begin(), ::tolower);
|
||||
loadLocalProductsInfo();
|
||||
}
|
||||
|
||||
EndpointProvider::~EndpointProvider() {
|
||||
}
|
||||
|
||||
bool EndpointProvider::loadInternalProductsInfo() {
|
||||
Json::Reader reader;
|
||||
Json::Value value;
|
||||
if (!reader.parse(INTERNAL_ENDPOINTS_DATA, value))
|
||||
return false;
|
||||
|
||||
auto productsNode = value["products"];
|
||||
for (auto value : productsNode) {
|
||||
Product p;
|
||||
p.code = value["code"].asString();
|
||||
p.documentId = value["document_id"].asString();
|
||||
p.locationServiceCode = value["location_service_code"].asString();
|
||||
p.globalEndpoint = value["global_endpoint"].asString();
|
||||
p.regionalEndpointPattern = value["regional_endpoint_pattern"].asString();
|
||||
|
||||
auto regionalEndpointsNode = value["regional_endpoints"];
|
||||
for (auto value : regionalEndpointsNode) {
|
||||
p.regionalEndpoints.insert(std::make_pair(value["region"].asString(),
|
||||
value["endpoint"].asString()));
|
||||
}
|
||||
internalProductsInfo_.push_back(p);
|
||||
}
|
||||
return true;
|
||||
bool EndpointProvider::loadLocalProductsInfo() {
|
||||
LoadLocalEndpoints();
|
||||
}
|
||||
|
||||
std::string EndpointProvider::localEndpoint(
|
||||
const std::string regionId, const std::string product) {
|
||||
|
||||
if (!local_endpoints_loaded) {
|
||||
// impossible
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::vector<regionType>::iterator allRegionsit;
|
||||
allRegionsit = std::find(allRegions.begin(), allRegions.end(), regionId);
|
||||
if (allRegionsit == allRegions.end()) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::vector<productType>::iterator allProductsInLocalEndpointsit;
|
||||
allProductsInLocalEndpointsit = std::find(allProductsInLocalEndpoints.begin(), allProductsInLocalEndpoints.end(), product);
|
||||
if (allProductsInLocalEndpointsit == allProductsInLocalEndpoints.end()) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::vector<regionType> vec = allLocalEndpoints[product].regions;
|
||||
std::vector<regionType>::iterator it;
|
||||
it = std::find(vec.begin(), vec.end(), regionId);
|
||||
if (it == vec.end()) {
|
||||
return std::string();
|
||||
}
|
||||
return allLocalEndpoints[product].regional[regionId];
|
||||
}
|
||||
|
||||
|
||||
bool EndpointProvider::checkExpiry()const {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto diff =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(now - expiry_).count();
|
||||
|
||||
return (diff > 0 - 60);
|
||||
}
|
||||
|
||||
std::string EndpointProvider::internalEndpoint(const std::string regionId,
|
||||
const std::string product) {
|
||||
for (Product p : internalProductsInfo_) {
|
||||
if (strcasecmp(p.code.c_str(), product.c_str()) != 0)
|
||||
continue;
|
||||
|
||||
for (auto e : p.regionalEndpoints) {
|
||||
if (strcasecmp(e.first.c_str(), regionId.c_str()) != 0)
|
||||
continue;
|
||||
|
||||
return e.second;
|
||||
}
|
||||
return p.globalEndpoint;
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
EndpointProvider::EndpointOutcome EndpointProvider::getEndpoint() {
|
||||
if (!configuration().endpoint().empty())
|
||||
// 1st priority: user specified via configuration
|
||||
if (!configuration().endpoint().empty()) {
|
||||
return EndpointOutcome(configuration().endpoint());
|
||||
|
||||
if (!serviceCode_.empty()) {
|
||||
EndpointOutcome outcome = loadRemoteEndpoint();
|
||||
if (outcome.isSuccess())
|
||||
return outcome;
|
||||
if (outcome.error().errorCode() != "InvalidRegionId"
|
||||
&& outcome.error().errorCode() != "Illegal Parameter")
|
||||
return outcome;
|
||||
}
|
||||
|
||||
std::string endpoint = internalEndpoint(regionId_, product_);
|
||||
if (!endpoint.empty())
|
||||
// 2nd priority: local configuration
|
||||
std::string endpoint = localEndpoint(regionId_, product_);
|
||||
if (!endpoint.empty()) {
|
||||
return EndpointOutcome(endpoint);
|
||||
}
|
||||
|
||||
return EndpointOutcome(
|
||||
Error("InvalidRegionId", "The specified region does not exist."));
|
||||
// service code is mandatory for location service.
|
||||
if (serviceCode_.empty()) {
|
||||
return EndpointOutcome(
|
||||
Error("InvalidRegionId",
|
||||
"Product[" + product_ + "] at region[" +
|
||||
regionId_ + "] does not exist."));
|
||||
}
|
||||
|
||||
// 3rd priority: request from location service
|
||||
EndpointOutcome outcome = loadRemoteEndpoint();
|
||||
if (outcome.isSuccess()) {
|
||||
return outcome;
|
||||
}
|
||||
|
||||
if (outcome.error().errorCode() == "Illegal Parameter") {
|
||||
return EndpointOutcome(Error("InvalidProduct",
|
||||
"Prodcut[" + serviceCode_ + "] does not exist."));
|
||||
}
|
||||
return outcome;
|
||||
}
|
||||
|
||||
EndpointProvider::EndpointOutcome EndpointProvider::loadRemoteEndpoint() {
|
||||
@@ -211,7 +223,6 @@ EndpointProvider::EndpointOutcome EndpointProvider::loadRemoteEndpoint() {
|
||||
expiry_ = std::chrono::system_clock::from_time_t(t);
|
||||
}
|
||||
}
|
||||
|
||||
return EndpointOutcome(cachedEndpoint_);
|
||||
}
|
||||
|
||||
|
||||
3504
core/src/LocalEndpoints.h
Normal file
3504
core/src/LocalEndpoints.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include "gtest/gtest.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "utils.h"
|
||||
#include "alibabacloud/core/EndpointProvider.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -27,32 +28,48 @@ class mockEndpointProvider: public EndpointProvider {
|
||||
MOCK_CONST_METHOD1(describeEndpoints, LocationClient::DescribeEndpointsOutcome(const Model::DescribeEndpointsRequest &request));
|
||||
};
|
||||
|
||||
TEST(EndpointProvider, service_code_empty) {
|
||||
TEST(EndpointProvider, basic_function) {
|
||||
const Credentials sub_user_credentials("key", "secret");
|
||||
ClientConfiguration config; // default is cn-hangzhou
|
||||
// config.setEndpoint("test-endpoint"); // endpoint should be empty
|
||||
|
||||
// user specified has 1st priority, even it is a wrong configuration
|
||||
config.setEndpoint("test-endpoint");
|
||||
mockEndpointProvider p0(sub_user_credentials, config, config.regionId(), "non-exist-product", "");
|
||||
EndpointProvider::EndpointOutcome out0 = p0.getEndpoint();
|
||||
EXPECT_TRUE(out0.result() == "test-endpoint");
|
||||
|
||||
config.setEndpoint(""); // set endpoint to empty
|
||||
// invalid product
|
||||
mockEndpointProvider provider1(sub_user_credentials, config, config.regionId(), "non-exist-product", "");
|
||||
EndpointProvider::EndpointOutcome out1 = provider1.getEndpoint();
|
||||
mockEndpointProvider p1(sub_user_credentials, config, config.regionId(), "non-exist-Product", "");
|
||||
EndpointProvider::EndpointOutcome out1 = p1.getEndpoint();
|
||||
EXPECT_TRUE(out1.error().errorCode() == "InvalidRegionId");
|
||||
EXPECT_TRUE(out1.error().errorMessage() == "Product[non-exist-product] at region[cn-hangzhou] does not exist.");
|
||||
|
||||
// ecs has no global_endpoint
|
||||
mockEndpointProvider provider2(sub_user_credentials, config, config.regionId(), "ecs", "");
|
||||
EndpointProvider::EndpointOutcome out2 = provider2.getEndpoint();
|
||||
mockEndpointProvider p2(sub_user_credentials, config, "non-exist-region", "ecs", "");
|
||||
EndpointProvider::EndpointOutcome out2 = p2.getEndpoint();
|
||||
EXPECT_TRUE(out2.error().errorCode() == "InvalidRegionId");
|
||||
EXPECT_TRUE(out2.error().errorMessage() == "Product[ecs] at region[non-exist-region] does not exist.");
|
||||
|
||||
// local_endpoints can provide the endpoint
|
||||
mockEndpointProvider p3(sub_user_credentials, config, config.regionId(), "ecs", "");
|
||||
EndpointProvider::EndpointOutcome out3 = p3.getEndpoint();
|
||||
EXPECT_TRUE(out3.result() == "ecs-cn-hangzhou.aliyuncs.com");
|
||||
|
||||
// aegis has global_endpoint
|
||||
mockEndpointProvider provider3(sub_user_credentials, config, config.regionId(), "aegis", "");
|
||||
EndpointProvider::EndpointOutcome out3 = provider3.getEndpoint();
|
||||
EXPECT_TRUE(out3.error().errorCode().empty());
|
||||
EXPECT_TRUE(out3.result() == "aegis.cn-hangzhou.aliyuncs.com");
|
||||
mockEndpointProvider p4(sub_user_credentials, config, config.regionId(), "aegis", "");
|
||||
EndpointProvider::EndpointOutcome out4 = p4.getEndpoint();
|
||||
EXPECT_TRUE(out4.result() == "aegis.cn-hangzhou.aliyuncs.com");
|
||||
|
||||
// arms has regional_endpoint, get from region
|
||||
mockEndpointProvider provider4(sub_user_credentials, config, config.regionId(), "arms", "");
|
||||
EndpointProvider::EndpointOutcome out4 = provider4.getEndpoint();
|
||||
EXPECT_TRUE(out4.error().errorCode().empty());
|
||||
EXPECT_TRUE(out4.result() == "arms.cn-hangzhou.aliyuncs.com");
|
||||
|
||||
// aegis has global_endpoint
|
||||
mockEndpointProvider p5(sub_user_credentials, config, config.regionId(), "Aegis", "");
|
||||
EndpointProvider::EndpointOutcome out5 = p5.getEndpoint();
|
||||
EXPECT_TRUE(out5.result() == "aegis.cn-hangzhou.aliyuncs.com");
|
||||
|
||||
mockEndpointProvider p6(sub_user_credentials, config, "cn-shanghai", "ensdisk", "");
|
||||
EndpointProvider::EndpointOutcome out6 = p6.getEndpoint();
|
||||
EXPECT_TRUE(out6.error().errorCode() == "InvalidRegionId");
|
||||
EXPECT_TRUE(out6.error().errorMessage() == "Product[ensdisk] at region[cn-shanghai] does not exist.");
|
||||
}
|
||||
|
||||
TEST(EndpointProvider, mock_remote) {
|
||||
@@ -60,7 +77,7 @@ TEST(EndpointProvider, mock_remote) {
|
||||
ClientConfiguration config; // default is cn-hangzhou
|
||||
// config.setEndpoint("test-endpoint");
|
||||
|
||||
mockEndpointProvider provider(sub_user_credentials, config, config.regionId(), "ecs", "ecs");
|
||||
mockEndpointProvider provider(sub_user_credentials, config, config.regionId(), "fake-ecs", "fake-ecs");
|
||||
|
||||
Model::DescribeEndpointsRequest request;
|
||||
|
||||
@@ -79,9 +96,8 @@ TEST(EndpointProvider, mock_remote) {
|
||||
TEST(EndpointProvider, mock_remote_error) {
|
||||
const Credentials sub_user_credentials("key", "secret");
|
||||
ClientConfiguration config; // default is cn-hangzhou
|
||||
// config.setEndpoint("test-endpoint");
|
||||
|
||||
mockEndpointProvider provider(sub_user_credentials, config, config.regionId(), "ecs", "ecs");
|
||||
mockEndpointProvider provider(sub_user_credentials, config, config.regionId(), "fake-ecs", "fake-ecs");
|
||||
LocationClient::DescribeEndpointsOutcome xout(Error("any-error-code", "any-error-message"));
|
||||
|
||||
DefaultValue<LocationClient::DescribeEndpointsOutcome>::Set(xout);
|
||||
@@ -90,3 +106,20 @@ TEST(EndpointProvider, mock_remote_error) {
|
||||
|
||||
EXPECT_TRUE(out.error().errorCode() == "any-error-code");
|
||||
}
|
||||
|
||||
TEST(EndpointProvider, serial_02) {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
|
||||
Credentials credentials(key, secret);
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
EndpointProvider ep(credentials, configuration, "cn-hangzhou", "arms", "arms");
|
||||
EndpointProvider::EndpointOutcome out = ep.getEndpoint();
|
||||
EXPECT_TRUE(out.result() == "arms.cn-hangzhou.aliyuncs.com");
|
||||
|
||||
EndpointProvider p1(credentials, configuration, "cn-shanghai", "fake-ecs", "fake-ecs");
|
||||
EndpointProvider::EndpointOutcome out1 = p1.getEndpoint();
|
||||
EXPECT_TRUE(out1.error().errorCode() == "InvalidProduct");
|
||||
EXPECT_TRUE(out1.error().errorMessage() == "Prodcut[fake-ecs] does not exist.");
|
||||
}
|
||||
|
||||
108
tools/convert_endpoints.js
Normal file
108
tools/convert_endpoints.js
Normal file
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const json = require('./endpoints.json');
|
||||
|
||||
const products = Object.keys(json.regional_endpoints);
|
||||
const regions = json.regions;
|
||||
const mapping = {};
|
||||
|
||||
const global_endpoints = json.global_endpoints;
|
||||
const regional_endpoints = json.regional_endpoints;
|
||||
const document_id = json.document_id;
|
||||
const pattern = json.regional_endpoint_pattern;
|
||||
const location_code_mapping = json.location_code_mapping;
|
||||
const p_in_mapping = Object.keys(location_code_mapping);
|
||||
|
||||
const tocpp = {};
|
||||
// assign global EP, as the lowest priority, can be overlopped by pattern and regional configuration
|
||||
const products_with_global_endpoint = Object.keys(global_endpoints);
|
||||
products_with_global_endpoint.forEach(function(p) {
|
||||
if (!products.includes(p)) {
|
||||
products.push(p);
|
||||
}
|
||||
|
||||
if (!tocpp[p]) {
|
||||
tocpp[p] = {};
|
||||
tocpp[p].regional = {};
|
||||
}
|
||||
let ep = global_endpoints[p];
|
||||
regions.forEach(function(r) {
|
||||
tocpp[p].regional[r] = ep;
|
||||
});
|
||||
});
|
||||
|
||||
// pattern
|
||||
const products_with_pattern = Object.keys(pattern);
|
||||
products_with_pattern.forEach(function (p) {
|
||||
if (!products.includes(p)) {
|
||||
products.push(p);
|
||||
}
|
||||
|
||||
if (!tocpp[p]) {
|
||||
tocpp[p] = {};
|
||||
tocpp[p].regional = {};
|
||||
}
|
||||
let p_pattern = pattern[p];
|
||||
|
||||
regions.forEach(function(r) {
|
||||
let pattern_ep = p_pattern.replace('[RegionId]', r);
|
||||
tocpp[p].regional[r] = pattern_ep;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// regional has the highest priority
|
||||
const products_with_regional = Object.keys(regional_endpoints);
|
||||
products_with_regional.forEach(function(p) {
|
||||
if (!tocpp[p]) {
|
||||
tocpp[p] = {};
|
||||
tocpp[p].regional = {};
|
||||
}
|
||||
|
||||
let ep_regional = regional_endpoints[p];
|
||||
Object.assign(tocpp[p].regional, ep_regional);
|
||||
});
|
||||
|
||||
const aliyun_products = Object.keys(tocpp);
|
||||
const mapped_products = Object.keys(location_code_mapping);
|
||||
|
||||
const mapped_tocpp = {};
|
||||
|
||||
aliyun_products.forEach(function(ap) {
|
||||
let found = false;
|
||||
for (var i = 0; i < mapped_products.length; i++) {
|
||||
let mp = mapped_products[i];
|
||||
if (ap === location_code_mapping[mp]) {
|
||||
mapped_tocpp[mp] = tocpp[ap];
|
||||
found = true;
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
mapped_tocpp[ap] = tocpp[ap];
|
||||
}
|
||||
});
|
||||
|
||||
const total_products = Object.keys(mapped_tocpp);
|
||||
|
||||
const cpp_header = {};
|
||||
cpp_header.products = total_products;
|
||||
cpp_header.regions = regions;
|
||||
cpp_header.endpoints = {};
|
||||
total_products.forEach(function(p) {
|
||||
let regional = mapped_tocpp[p].regional;
|
||||
cpp_header.endpoints[p] = {}
|
||||
cpp_header.endpoints[p].regions = Object.keys(regional);
|
||||
cpp_header.endpoints[p].regional = regional;
|
||||
});
|
||||
|
||||
var sorted_string = JSON.stringify(cpp_header, null, 2);
|
||||
|
||||
sorted_string = sorted_string.replace(/\"/g, "\\\"");
|
||||
sorted_string = sorted_string.replace(/\n/g,"\"\n\"");
|
||||
|
||||
console.log("#include <string.h>");
|
||||
console.log();
|
||||
console.log("const std::string LOCAL_ENDPOINTS_CONFIG = \"" + sorted_string + '\";');
|
||||
6244
tools/cpplint.py
vendored
Executable file
6244
tools/cpplint.py
vendored
Executable file
File diff suppressed because it is too large
Load Diff
7
tools/generate_local_endpoints.sh
Executable file
7
tools/generate_local_endpoints.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
curl -fsSLO --compressed "https://raw.githubusercontent.com/aliyun/aliyun-openapi-python-sdk/master/aliyun-python-sdk-core/aliyunsdkcore/data/endpoints.json"
|
||||
|
||||
DIR=`pwd`
|
||||
|
||||
node convert_endpoints.js > $DIR/../core/src/LocalEndpoints.h
|
||||
Reference in New Issue
Block a user