Skip to content

Commit 883d4b7

Browse files
committed
refactor code to use defaults
1 parent 51545ef commit 883d4b7

File tree

6 files changed

+84
-143
lines changed

6 files changed

+84
-143
lines changed

src/aws-cpp-sdk-core/include/aws/core/config/EndpointResolver.h

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,32 @@ namespace Aws
2121
static const char ENDPOINT_RESOLVER_TAG[] = "EndpointResolver";
2222

2323
/**
24-
* Resolver that sources endpoints and sets them on endpoint providers.
24+
* Resolver that sources endpoints from configuration.
2525
*/
26-
class AWS_CORE_API EndpointResolver
26+
namespace EndpointResolver
2727
{
28-
public:
2928
/**
30-
* Sources endpoint and sets it on the endpoint provider.
31-
* This should be called after InitBuiltInParameters but before ResolveEndpoint.
29+
* Convert service ID to environment variable suffix format
30+
*/
31+
AWS_CORE_API Aws::String ToEnvSuffix(const Aws::String& serviceId);
32+
33+
/**
34+
* Sources endpoint URL from configuration.
3235
*
3336
* @param serviceId Service identifier (e.g., "s3", "Elastic Beanstalk")
3437
* @param profileName Profile name for shared config lookup
35-
* @param endpointProvider Endpoint provider to configure
38+
* @return Optional endpoint URL if found, empty if not configured
3639
*/
37-
template<typename EndpointProviderT>
38-
static void EndpointSource(const Aws::String& serviceId,
39-
const Aws::String& profileName,
40-
const Aws::Http::Scheme& scheme,
41-
EndpointProviderT& endpointProvider)
40+
AWS_CORE_API Aws::String EndpointSource(const Aws::String& serviceId,
41+
const Aws::String& profileName)
4242
{
4343
const Aws::String serviceKey = ToEnvSuffix(serviceId);
4444

4545
// 1) Check ignore flag from environment variable
4646
Aws::String ignoreEnv = Aws::Environment::GetEnv("AWS_IGNORE_CONFIGURED_ENDPOINT_URLS");
4747
if (!ignoreEnv.empty() && Utils::StringUtils::ConvertToBool(ignoreEnv.c_str())) {
4848
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "Configured endpoints ignored due to AWS_IGNORE_CONFIGURED_ENDPOINT_URLS=true");
49-
return;
49+
return "";
5050
}
5151

5252
// 1b) Check ignore flag from profile (early check)
@@ -55,7 +55,7 @@ namespace Aws
5555
const Aws::String ignoreVal = profile.GetValue("ignore_configured_endpoint_urls");
5656
if (!ignoreVal.empty() && Utils::StringUtils::ConvertToBool(ignoreVal.c_str())) {
5757
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "Configured endpoints ignored due to ignore_configured_endpoint_urls=true in profile: " << profileName);
58-
return;
58+
return "";
5959
}
6060
}
6161

@@ -67,8 +67,7 @@ namespace Aws
6767
if (!fromEnv.empty()) {
6868
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "Resolved configured endpoint from service-specific environment variable: " << service);
6969
AWS_LOGSTREAM_TRACE(ENDPOINT_RESOLVER_TAG, "Configured endpoint URL: " << fromEnv);
70-
endpointProvider.OverrideEndpoint(fromEnv, scheme);
71-
return;
70+
return fromEnv;
7271
}
7372
}
7473

@@ -78,15 +77,14 @@ namespace Aws
7877
if (!fromEnv.empty()) {
7978
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "Resolved configured endpoint from global environment variable: AWS_ENDPOINT_URL");
8079
AWS_LOGSTREAM_TRACE(ENDPOINT_RESOLVER_TAG, "Configured endpoint URL: " << fromEnv);
81-
endpointProvider.OverrideEndpoint(fromEnv, scheme);
82-
return;
80+
return fromEnv;
8381
}
8482
}
8583

8684
// Skip profile resolution no profile available
8785
if (profileName.empty() || !HasCachedConfigProfile(profileName)) {
8886
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "No configured endpoint found - no profile available or profile not cached");
89-
return;
87+
return "";
9088
}
9189

9290
Profile profile = GetCachedConfigProfile(profileName);
@@ -97,28 +95,21 @@ namespace Aws
9795
if (it != endpoints.end() && !it->second.empty()) {
9896
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "Resolved configured endpoint from service-specific profile setting for service: " << serviceKey << " in profile: " << profileName);
9997
AWS_LOGSTREAM_TRACE(ENDPOINT_RESOLVER_TAG, "Configured endpoint URL: " << it->second);
100-
endpointProvider.OverrideEndpoint(it->second, scheme);
101-
return;
98+
return it->second;
10299
}
103100

104101
// 5) Global profile endpoint
105102
auto endpoint = profile.GetGlobalEndpointUrl();
106103
if (!endpoint.empty()) {
107104
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "Resolved configured endpoint from global profile setting in profile: " << profileName);
108105
AWS_LOGSTREAM_TRACE(ENDPOINT_RESOLVER_TAG, "Configured endpoint URL: " << endpoint);
109-
endpointProvider.OverrideEndpoint(endpoint, scheme);
110-
return;
106+
return endpoint;
111107
}
112108

113109
AWS_LOGSTREAM_DEBUG(ENDPOINT_RESOLVER_TAG, "No configured endpoint found for service: " << serviceId);
110+
return "";
114111
}
115-
116-
private:
117-
/**
118-
* Convert service ID to environment variable suffix format
119-
*/
120-
static Aws::String ToEnvSuffix(const Aws::String& serviceId);
121-
};
112+
}
122113

123114
} // namespace Config
124115
} // namespace Aws

src/aws-cpp-sdk-core/include/aws/core/endpoint/BuiltInParameters.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ namespace Aws
2626

2727
virtual void SetFromClientConfiguration(const Client::ClientConfiguration& config);
2828
virtual void SetFromClientConfiguration(const Client::GenericClientConfiguration& config);
29-
virtual void SetFromClientConfiguration(const Client::ClientConfiguration& config, const Aws::String& serviceName);
30-
virtual void SetFromClientConfiguration(const Client::GenericClientConfiguration& config, const Aws::String& serviceName);
29+
void SetFromClientConfiguration(const Client::ClientConfiguration& config, const Aws::String& serviceName);
30+
void SetFromClientConfiguration(const Client::GenericClientConfiguration& config, const Aws::String& serviceName);
3131

3232
virtual void OverrideEndpoint(const Aws::String& endpoint, const Aws::Http::Scheme& scheme = Aws::Http::Scheme::HTTPS);
3333

@@ -38,6 +38,9 @@ namespace Aws
3838
void SetStringArrayParameter(Aws::String name, const Aws::Vector<Aws::String>&& value);
3939
const Aws::Vector<EndpointParameter>& GetAllParameters() const;
4040

41+
private:
42+
void SetFromClientConfigurationImpl(const Client::ClientConfiguration& config, const Aws::String& serviceName);
43+
4144
protected:
4245
Aws::Vector<EndpointParameter> m_params;
4346
};

src/aws-cpp-sdk-core/include/aws/core/endpoint/EndpointProviderBase.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ namespace Aws
5252
/**
5353
* Initialize client context parameters from a ClientConfiguration with service name
5454
*/
55-
virtual void InitBuiltInParameters(const ClientConfigurationT& config, const Aws::String& serviceName) = 0;
55+
virtual void InitBuiltInParameters(const ClientConfigurationT& config, const Aws::String& serviceName)
56+
{
57+
AWS_UNREFERENCED_PARAM(serviceName);
58+
InitBuiltInParameters(config);
59+
}
5660

5761
/**
5862
* Function to override endpoint, i.e. to set built-in parameter "AWS::Endpoint"

src/aws-cpp-sdk-core/source/config/EndpointResolver.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44
*/
55

66
#include <aws/core/config/EndpointResolver.h>
7-
#include <aws/core/platform/Environment.h>
87
#include <aws/core/utils/StringUtils.h>
98

10-
using namespace Aws::Config;
11-
using namespace Aws::Utils;
12-
13-
Aws::String EndpointResolver::ToEnvSuffix(const Aws::String& serviceId)
9+
namespace Aws
1410
{
15-
Aws::String result = StringUtils::ToUpper(serviceId.c_str());
16-
StringUtils::Replace(result, " ", "_");
17-
return result;
11+
namespace Config
12+
{
13+
namespace EndpointResolver
14+
{
15+
Aws::String ToEnvSuffix(const Aws::String& serviceId)
16+
{
17+
Aws::String result = serviceId;
18+
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
19+
std::replace(result.begin(), result.end(), ' ', '_');
20+
std::replace(result.begin(), result.end(), '-', '_');
21+
return result;
22+
}
23+
}
24+
}
1825
}

src/aws-cpp-sdk-core/source/endpoint/BuiltInParameters.cpp

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ namespace Endpoint
3434
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
3535
}
3636

37-
void BuiltInParameters::SetFromClientConfiguration(const Client::ClientConfiguration& config)
37+
void BuiltInParameters::SetFromClientConfigurationImpl(const Client::ClientConfiguration& config, const Aws::String& serviceName)
3838
{
3939
bool forceFIPS = false;
4040
static const char* AWS_REGION = "Region";
4141
if (!config.region.empty()) {
4242
static const char* FIPS_PREFIX = "fips-";
4343
static const char* FIPS_SUFFIX = "-fips";
4444
if (config.region.rfind(FIPS_PREFIX, 0) == 0) {
45-
// Backward compatibility layer for code hacking previous SDK version
4645
Aws::String regionOverride = config.region.substr(strlen(FIPS_PREFIX));
4746
forceFIPS = true;
4847
SetStringParameter(AWS_REGION, regionOverride);
@@ -63,65 +62,39 @@ namespace Endpoint
6362

6463
if (!config.endpointOverride.empty()) {
6564
OverrideEndpoint(config.endpointOverride, config.scheme);
66-
6765
if (config.region.empty()) {
6866
AWS_LOGSTREAM_WARN(ENDPOINT_BUILTIN_LOG_TAG,
6967
"Endpoint is overridden but region is not set. "
70-
"Region is required my many endpoint rule sets to resolve the endpoint. "
68+
"Region is required by many endpoint rule sets to resolve the endpoint. "
7169
"And it is required to compute an aws signature.");
72-
SetStringParameter(AWS_REGION, "region-not-set"); // dummy endpoint resolution parameter
70+
SetStringParameter(AWS_REGION, "region-not-set");
71+
}
72+
} else if (!serviceName.empty()) {
73+
Aws::String resolvedEndpoint = Aws::Config::EndpointResolver::EndpointSource(serviceName, config.profileName);
74+
if (!resolvedEndpoint.empty()) {
75+
OverrideEndpoint(resolvedEndpoint, config.scheme);
7376
}
7477
}
7578
}
7679

80+
void BuiltInParameters::SetFromClientConfiguration(const Client::ClientConfiguration& config)
81+
{
82+
SetFromClientConfiguration(config, "");
83+
}
84+
7785
void BuiltInParameters::SetFromClientConfiguration(const Client::GenericClientConfiguration& config)
7886
{
79-
return SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config));
87+
SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config), "");
8088
}
8189

8290
void BuiltInParameters::SetFromClientConfiguration(const Client::ClientConfiguration& config, const Aws::String& serviceName)
8391
{
84-
bool forceFIPS = false;
85-
static const char* AWS_REGION = "Region";
86-
if (!config.region.empty()) {
87-
static const char* FIPS_PREFIX = "fips-";
88-
static const char* FIPS_SUFFIX = "-fips";
89-
if (config.region.rfind(FIPS_PREFIX, 0) == 0) {
90-
Aws::String regionOverride = config.region.substr(strlen(FIPS_PREFIX));
91-
forceFIPS = true;
92-
SetStringParameter(AWS_REGION, regionOverride);
93-
} else if (StringEndsWith(config.region, FIPS_SUFFIX)) {
94-
Aws::String regionOverride = config.region.substr(0, config.region.size() - strlen(FIPS_SUFFIX));
95-
forceFIPS = true;
96-
SetStringParameter(AWS_REGION, regionOverride);
97-
} else {
98-
SetStringParameter(AWS_REGION, config.region);
99-
}
100-
}
101-
102-
static const char* AWS_USE_FIPS = "UseFIPS";
103-
SetBooleanParameter(AWS_USE_FIPS, config.useFIPS || forceFIPS);
104-
105-
static const char* AWS_USE_DUAL_STACK = "UseDualStack";
106-
SetBooleanParameter(AWS_USE_DUAL_STACK, config.useDualStack);
107-
108-
if (!config.endpointOverride.empty()) {
109-
OverrideEndpoint(config.endpointOverride, config.scheme);
110-
if (config.region.empty()) {
111-
AWS_LOGSTREAM_WARN(ENDPOINT_BUILTIN_LOG_TAG,
112-
"Endpoint is overridden but region is not set. "
113-
"Region is required by many endpoint rule sets to resolve the endpoint. "
114-
"And it is required to compute an aws signature.");
115-
SetStringParameter(AWS_REGION, "region-not-set");
116-
}
117-
} else if (!serviceName.empty()) {
118-
Aws::Config::EndpointResolver::EndpointSource(serviceName, config.profileName, config.scheme, *this);
119-
}
92+
SetFromClientConfigurationImpl(config, serviceName);
12093
}
12194

12295
void BuiltInParameters::SetFromClientConfiguration(const Client::GenericClientConfiguration& config, const Aws::String& serviceName)
12396
{
124-
return SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config), serviceName);
97+
SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config), serviceName);
12598
}
12699

127100
const BuiltInParameters::EndpointParameter& BuiltInParameters::GetParameter(const Aws::String& name) const

0 commit comments

Comments
 (0)