refactor endpoints implementation

This commit is contained in:
zhangzifa
2019-03-04 14:44:44 +08:00
committed by Jackson Tian
parent 563e907601
commit c17dc7c79a
8 changed files with 10040 additions and 141 deletions

View File

@@ -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_

View File

@@ -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 &region : 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

File diff suppressed because it is too large Load Diff