Skip to content

Commit 3081eec

Browse files
authored
Feature default config (#2368)
Adds opt in feature that allows pre-selected default configurations to be selected
1 parent a7802ac commit 3081eec

File tree

9 files changed

+929
-0
lines changed

9 files changed

+929
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"type": "feature",
4+
"category": "",
5+
"description": "This commit adds defaults config: an opt-in feature which allows users to specify default configuration options to be loaded from a shared file"
6+
}
7+
]

src/ClientResolver.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Aws\Exception\InvalidRegionException;
2222
use Aws\Retry\ConfigurationInterface as RetryConfigInterface;
2323
use Aws\Retry\ConfigurationProvider as RetryConfigProvider;
24+
use Aws\DefaultsMode\ConfigurationInterface as ConfigModeInterface;
25+
use Aws\DefaultsMode\ConfigurationProvider as ConfigModeProvider;
2426
use Aws\Signature\SignatureProvider;
2527
use Aws\Endpoint\EndpointProvider;
2628
use GuzzleHttp\Promise\PromiseInterface;
@@ -105,6 +107,13 @@ class ClientResolver
105107
'fn' => [__CLASS__, '_apply_api_provider'],
106108
'default' => [ApiProvider::class, 'defaultProvider'],
107109
],
110+
'configuration_mode' => [
111+
'type' => 'value',
112+
'valid' => [ConfigModeInterface::class, CacheInterface::class, 'string', 'closure'],
113+
'doc' => "Sets the default configuration mode. Otherwise provide an instance of Aws\DefaultsMode\ConfigurationInterface, an instance of Aws\CacheInterface, or a string containing a valid mode",
114+
'fn' => [__CLASS__, '_apply_defaults'],
115+
'default' => [ConfigModeProvider::class, 'defaultProvider']
116+
],
108117
'use_fips_endpoint' => [
109118
'type' => 'value',
110119
'valid' => ['bool', UseFipsEndpointConfiguration::class, CacheInterface::class, 'callable'],
@@ -453,6 +462,44 @@ public static function _apply_retries($value, array &$args, HandlerList $list)
453462
}
454463
}
455464

465+
public static function _apply_defaults($value, array &$args, HandlerList $list)
466+
{
467+
$config = ConfigModeProvider::unwrap($value);
468+
if ($config->getMode() !== 'legacy') {
469+
if (!isset($args['retries']) && !is_null($config->getRetryMode())) {
470+
$args['retries'] = ['mode' => $config->getRetryMode()];
471+
}
472+
if (
473+
!isset($args['sts_regional_endpoints'])
474+
&& !is_null($config->getStsRegionalEndpoints())
475+
) {
476+
$args['sts_regional_endpoints'] = ['mode' => $config->getStsRegionalEndpoints()];
477+
}
478+
if (
479+
!isset($args['s3_us_east_1_regional_endpoint'])
480+
&& !is_null($config->getS3UsEast1RegionalEndpoints())
481+
) {
482+
$args['s3_us_east_1_regional_endpoint'] = ['mode' => $config->getS3UsEast1RegionalEndpoints()];
483+
}
484+
485+
if (!isset($args['http'])) {
486+
$args['http'] = [];
487+
}
488+
if (
489+
!isset($args['http']['connect_timeout'])
490+
&& !is_null($config->getConnectTimeoutInMillis())
491+
) {
492+
$args['http']['connect_timeout'] = $config->getConnectTimeoutInMillis() / 1000;
493+
}
494+
if (
495+
!isset($args['http']['timeout'])
496+
&& !is_null($config->getHttpRequestTimeoutInMillis())
497+
) {
498+
$args['http']['timeout'] = $config->getHttpRequestTimeoutInMillis() / 1000;
499+
}
500+
}
501+
}
502+
456503
public static function _apply_credentials($value, array &$args)
457504
{
458505
if (is_callable($value)) {

src/DefaultsMode/Configuration.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
namespace Aws\DefaultsMode;
3+
4+
use Aws\DefaultsMode\Exception\ConfigurationException;
5+
6+
class Configuration implements ConfigurationInterface
7+
{
8+
private $mode;
9+
private $retryMode;
10+
private $stsRegionalEndpoints;
11+
private $s3UsEast1RegionalEndpoints;
12+
private $connectTimeoutInMillis;
13+
private $httpRequestTimeoutInMillis;
14+
private $validModes = [
15+
'legacy',
16+
'standard',
17+
'cross-region',
18+
'in-region',
19+
'mobile',
20+
'auto',
21+
];
22+
23+
public function __construct($mode = 'legacy')
24+
{
25+
$mode = strtolower($mode);
26+
if (!in_array($mode, $this->validModes)) {
27+
throw new \InvalidArgumentException("'{$mode}' is not a valid mode."
28+
. " The mode has to be 'legacy', 'standard', 'cross-region', 'in-region',"
29+
. " 'mobile', or 'auto'.");
30+
}
31+
32+
$this->mode = $mode;
33+
if ($this->mode == 'legacy') {
34+
return;
35+
}
36+
37+
$data = \Aws\load_compiled_json(
38+
__DIR__ . '/../data/sdk-default-configuration.json'
39+
);
40+
41+
$this->retryMode = $data['base']['retryMode'];
42+
$this->stsRegionalEndpoints = $data['base']['stsRegionalEndpoints'];
43+
$this->s3UsEast1RegionalEndpoints = $data['base']['s3UsEast1RegionalEndpoints'];
44+
$this->connectTimeoutInMillis = $data['base']['connectTimeoutInMillis'];
45+
46+
if (isset($data['modes'][$mode])) {
47+
$modeData = $data['modes'][$mode];
48+
foreach ($modeData as $settingName => $settingValue) {
49+
if (isset($this->$settingName)) {
50+
if (isset($settingValue['override'])) {
51+
$this->$settingName = $settingValue['override'];
52+
} else if (isset($settingValue['multiply'])) {
53+
$this->$settingName *= $settingValue['multiply'];
54+
} else if (isset($settingValue['add'])) {
55+
$this->$settingName += $settingValue['add'];
56+
}
57+
} else {
58+
if (isset($settingValue['override'])) {
59+
$this->$settingName = $settingValue['override'];
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function getMode()
70+
{
71+
return $this->mode;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function getRetryMode()
78+
{
79+
return $this->retryMode;
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
public function getStsRegionalEndpoints()
86+
{
87+
return $this->stsRegionalEndpoints;
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function getS3UsEast1RegionalEndpoints()
94+
{
95+
return $this->s3UsEast1RegionalEndpoints;
96+
}
97+
98+
/**
99+
* {@inheritdoc}
100+
*/
101+
public function getConnectTimeoutInMillis()
102+
{
103+
return $this->connectTimeoutInMillis;
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function getHttpRequestTimeoutInMillis()
110+
{
111+
return $this->httpRequestTimeoutInMillis;
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function toArray()
118+
{
119+
return [
120+
'mode' => $this->getMode(),
121+
'retry_mode' => $this->getRetryMode(),
122+
'sts_regional_endpoints' => $this->getStsRegionalEndpoints(),
123+
's3_us_east_1_regional_endpoint' => $this->getS3UsEast1RegionalEndpoints(),
124+
'connect_timeout_in_milliseconds' => $this->getConnectTimeoutInMillis(),
125+
'http_request_timeout_in_milliseconds' => $this->getHttpRequestTimeoutInMillis(),
126+
];
127+
}
128+
129+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace Aws\DefaultsMode;
3+
4+
/**
5+
* Provides access to defaultsMode configuration
6+
*/
7+
interface ConfigurationInterface
8+
{
9+
/**
10+
* Returns the configuration mode. Available modes include 'legacy', 'standard', and
11+
* 'adapative'.
12+
*
13+
* @return string
14+
*/
15+
public function getMode();
16+
17+
/**
18+
* Returns the sts regional endpoints option
19+
*
20+
* @return bool
21+
*/
22+
public function getStsRegionalEndpoints();
23+
24+
/**
25+
* Returns the s3 us-east-1 regional endpoints option
26+
*
27+
* @return bool
28+
*/
29+
public function getS3UsEast1RegionalEndpoints();
30+
31+
/**
32+
* Returns the connection timeout in milliseconds
33+
*
34+
* @return int
35+
*/
36+
public function getConnectTimeoutInMillis();
37+
38+
/**
39+
* Returns the http request timeout in milliseconds
40+
*
41+
* @return int
42+
*/
43+
public function getHttpRequestTimeoutInMillis();
44+
45+
/**
46+
* Returns the configuration as an associative array
47+
*
48+
* @return array
49+
*/
50+
public function toArray();
51+
}

0 commit comments

Comments
 (0)