Skip to content

Commit 6873030

Browse files
committed
refactor code
1 parent 6baccb3 commit 6873030

File tree

1 file changed

+40
-69
lines changed

1 file changed

+40
-69
lines changed

src/aws-cpp-sdk-core/source/internal/AWSHttpResourceClient.cpp

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -208,27 +208,22 @@ namespace Aws
208208
#endif
209209
}
210210

211-
EC2MetadataClient::EC2MetadataClient(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialConfig,
212-
const char* endpoint)
213-
: AWSHttpResourceClient(
214-
[&credentialConfig]() -> ClientConfiguration{
215-
Aws::Client::ClientConfiguration clientConfig{credentialConfig.profile.c_str()};
216-
clientConfig.region = credentialConfig.region;
217-
clientConfig.credentialProviderConfig = credentialConfig;
218-
clientConfig.requestTimeoutMs = credentialConfig.imdsConfig.metadataServiceTimeout * 1000;
219-
if (credentialConfig.imdsConfig.imdsRetryStrategy) {
220-
clientConfig.retryStrategy = credentialConfig.imdsConfig.imdsRetryStrategy;
221-
} else {
222-
clientConfig.retryStrategy = Aws::MakeShared<Aws::Client::DefaultRetryStrategy>(
223-
EC2_METADATA_CLIENT_LOG_TAG, credentialConfig.imdsConfig.metadataServiceNumAttempts - 1, /*scaleMs=*/500);
224-
}
225-
return clientConfig;
226-
}(),
227-
EC2_METADATA_CLIENT_LOG_TAG),
228-
m_endpoint(endpoint),
229-
m_disableIMDS(credentialConfig.imdsConfig.disableImds),
230-
m_tokenRequired(true),
231-
m_disableIMDSV1(credentialConfig.imdsConfig.disableImdsV1) {
211+
EC2MetadataClient::EC2MetadataClient(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialConfig,
212+
const char* endpoint)
213+
: AWSHttpResourceClient(
214+
[&credentialConfig]() -> ClientConfiguration{
215+
Aws::Client::ClientConfiguration clientConfig{credentialConfig.profile.c_str()};
216+
clientConfig.region = credentialConfig.region;
217+
clientConfig.credentialProviderConfig = credentialConfig;
218+
clientConfig.requestTimeoutMs = credentialConfig.imdsConfig.metadataServiceTimeout * 1000;
219+
clientConfig.retryStrategy = credentialConfig.imdsConfig.imdsRetryStrategy;
220+
return clientConfig;
221+
}(),
222+
EC2_METADATA_CLIENT_LOG_TAG),
223+
m_endpoint(endpoint),
224+
m_disableIMDS(credentialConfig.imdsConfig.disableImds),
225+
m_tokenRequired(true),
226+
m_disableIMDSV1(credentialConfig.imdsConfig.disableImdsV1) {
232227
#if defined(DISABLE_IMDSV1)
233228
m_disableIMDSV1 = true;
234229
AWS_LOGSTREAM_TRACE(m_logtag.c_str(), "IMDSv1 had been disabled at the SDK build time");
@@ -437,21 +432,9 @@ namespace Aws
437432
return Aws::String(m_endpoint);
438433
}
439434

440-
#ifdef _MSC_VER
441-
// VS2015 compiler's bug, warning s_ec2metadataClient: symbol will be dynamically initialized (implementation limitation)
442-
AWS_SUPPRESS_WARNING(4592,
443-
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
444-
)
445-
#else
446-
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
447-
#endif
448-
449-
void InitEC2MetadataClient(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialConfig) {
450-
if (s_ec2metadataClient)
451-
{
452-
return;
453-
}
454-
Aws::String ec2MetadataServiceEndpoint = Aws::Environment::GetEnv("AWS_EC2_METADATA_SERVICE_ENDPOINT");
435+
static Aws::String CalculateEC2MetadataServiceEndpoint()
436+
{
437+
Aws::String ec2MetadataServiceEndpoint = Aws::Environment::GetEnv("AWS_EC2_METADATA_SERVICE_ENDPOINT");
455438
if (ec2MetadataServiceEndpoint.empty())
456439
{
457440
Aws::String ec2MetadataServiceEndpointMode = Aws::Environment::GetEnv("AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE").c_str();
@@ -482,8 +465,26 @@ namespace Aws
482465
}
483466
}
484467
}
485-
AWS_LOGSTREAM_INFO(EC2_METADATA_CLIENT_LOG_TAG, "Using IMDS endpoint: " << ec2MetadataServiceEndpoint);
486-
s_ec2metadataClient = Aws::MakeShared<EC2MetadataClient>(EC2_METADATA_CLIENT_LOG_TAG, credentialConfig, ec2MetadataServiceEndpoint.c_str());
468+
return ec2MetadataServiceEndpoint;
469+
}
470+
471+
#ifdef _MSC_VER
472+
// VS2015 compiler's bug, warning s_ec2metadataClient: symbol will be dynamically initialized (implementation limitation)
473+
AWS_SUPPRESS_WARNING(4592,
474+
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
475+
)
476+
#else
477+
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
478+
#endif
479+
480+
void InitEC2MetadataClient(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialConfig) {
481+
if (s_ec2metadataClient)
482+
{
483+
return;
484+
}
485+
Aws::String ec2MetadataServiceEndpoint = CalculateEC2MetadataServiceEndpoint();
486+
AWS_LOGSTREAM_INFO(EC2_METADATA_CLIENT_LOG_TAG, "Using IMDS endpoint: " << ec2MetadataServiceEndpoint);
487+
s_ec2metadataClient = Aws::MakeShared<EC2MetadataClient>(EC2_METADATA_CLIENT_LOG_TAG, credentialConfig, ec2MetadataServiceEndpoint.c_str());
487488
}
488489

489490
void InitEC2MetadataClient()
@@ -492,37 +493,7 @@ namespace Aws
492493
{
493494
return;
494495
}
495-
Aws::String ec2MetadataServiceEndpoint = Aws::Environment::GetEnv("AWS_EC2_METADATA_SERVICE_ENDPOINT");
496-
if (ec2MetadataServiceEndpoint.empty())
497-
{
498-
Aws::String ec2MetadataServiceEndpointMode = Aws::Environment::GetEnv("AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE").c_str();
499-
if (ec2MetadataServiceEndpointMode.length() == 0 )
500-
{
501-
ec2MetadataServiceEndpoint = "http://169.254.169.254"; //default to IPv4 default endpoint
502-
}
503-
else
504-
{
505-
if (ec2MetadataServiceEndpointMode.length() == 4 )
506-
{
507-
if (Aws::Utils::StringUtils::CaselessCompare(ec2MetadataServiceEndpointMode.c_str(), "ipv4"))
508-
{
509-
ec2MetadataServiceEndpoint = "http://169.254.169.254"; //default to IPv4 default endpoint
510-
}
511-
else if (Aws::Utils::StringUtils::CaselessCompare(ec2MetadataServiceEndpointMode.c_str(), "ipv6"))
512-
{
513-
ec2MetadataServiceEndpoint = "http://[fd00:ec2::254]";
514-
}
515-
else
516-
{
517-
AWS_LOGSTREAM_ERROR(EC2_METADATA_CLIENT_LOG_TAG, "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE can only be set to ipv4 or ipv6, received: " << ec2MetadataServiceEndpointMode );
518-
}
519-
}
520-
else
521-
{
522-
AWS_LOGSTREAM_ERROR(EC2_METADATA_CLIENT_LOG_TAG, "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE can only be set to ipv4 or ipv6, received: " << ec2MetadataServiceEndpointMode );
523-
}
524-
}
525-
}
496+
Aws::String ec2MetadataServiceEndpoint = CalculateEC2MetadataServiceEndpoint();
526497
AWS_LOGSTREAM_INFO(EC2_METADATA_CLIENT_LOG_TAG, "Using IMDS endpoint: " << ec2MetadataServiceEndpoint);
527498
s_ec2metadataClient = Aws::MakeShared<EC2MetadataClient>(EC2_METADATA_CLIENT_LOG_TAG, ec2MetadataServiceEndpoint.c_str());
528499
}

0 commit comments

Comments
 (0)