1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use crate::provider_config::ProviderConfig;
use crate::retry::error::{RetryConfigError, RetryConfigErrorKind};
use crate::standard_property::{PropertyResolutionError, StandardProperty};
use aws_smithy_types::error::display::DisplayErrorContext;
use aws_smithy_types::retry::{RetryConfig, RetryMode};
use std::str::FromStr;
pub fn default_provider() -> Builder {
Builder::default()
}
mod env {
pub(super) const MAX_ATTEMPTS: &str = "AWS_MAX_ATTEMPTS";
pub(super) const RETRY_MODE: &str = "AWS_RETRY_MODE";
}
mod profile_keys {
pub(super) const MAX_ATTEMPTS: &str = "max_attempts";
pub(super) const RETRY_MODE: &str = "retry_mode";
}
#[derive(Debug, Default)]
pub struct Builder {
provider_config: ProviderConfig,
}
impl Builder {
pub fn configure(mut self, configuration: &ProviderConfig) -> Self {
self.provider_config = configuration.clone();
self
}
pub fn profile_name(mut self, name: &str) -> Self {
self.provider_config = self.provider_config.with_profile_name(name.to_string());
self
}
pub async fn retry_config(self) -> RetryConfig {
match self.try_retry_config().await {
Ok(conf) => conf,
Err(e) => panic!("{}", DisplayErrorContext(e)),
}
}
pub(crate) async fn try_retry_config(
self,
) -> Result<RetryConfig, PropertyResolutionError<RetryConfigError>> {
let mut retry_config = RetryConfig::standard();
let max_attempts = StandardProperty::new()
.env(env::MAX_ATTEMPTS)
.profile(profile_keys::MAX_ATTEMPTS)
.validate(&self.provider_config, validate_max_attempts);
let retry_mode = StandardProperty::new()
.env(env::RETRY_MODE)
.profile(profile_keys::RETRY_MODE)
.validate(&self.provider_config, |s| {
RetryMode::from_str(s)
.map_err(|err| RetryConfigErrorKind::InvalidRetryMode { source: err }.into())
});
if let Some(max_attempts) = max_attempts.await? {
retry_config = retry_config.with_max_attempts(max_attempts);
}
if let Some(retry_mode) = retry_mode.await? {
retry_config = retry_config.with_retry_mode(retry_mode);
}
Ok(retry_config)
}
}
fn validate_max_attempts(max_attempts: &str) -> Result<u32, RetryConfigError> {
match max_attempts.parse::<u32>() {
Ok(max_attempts) if max_attempts == 0 => {
Err(RetryConfigErrorKind::MaxAttemptsMustNotBeZero.into())
}
Ok(max_attempts) => Ok(max_attempts),
Err(source) => Err(RetryConfigErrorKind::FailedToParseMaxAttempts { source }.into()),
}
}
#[cfg(test)]
mod test {
use crate::default_provider::retry_config::env;
use crate::provider_config::ProviderConfig;
use crate::retry::{
error::RetryConfigError, error::RetryConfigErrorKind, RetryConfig, RetryMode,
};
use crate::standard_property::PropertyResolutionError;
use aws_types::os_shim_internal::{Env, Fs};
async fn test_provider(
vars: &[(&str, &str)],
) -> Result<RetryConfig, PropertyResolutionError<RetryConfigError>> {
super::Builder::default()
.configure(&ProviderConfig::no_configuration().with_env(Env::from_slice(vars)))
.try_retry_config()
.await
}
#[tokio::test]
async fn test_returns_default_retry_config_from_empty_profile() {
let env = Env::from_slice(&[("AWS_CONFIG_FILE", "config")]);
let fs = Fs::from_slice(&[("config", "[default]\n")]);
let provider_config = ProviderConfig::no_configuration().with_env(env).with_fs(fs);
let actual_retry_config = super::default_provider()
.configure(&provider_config)
.retry_config()
.await;
let expected_retry_config = RetryConfig::standard();
assert_eq!(actual_retry_config, expected_retry_config);
assert_eq!(actual_retry_config.max_attempts(), 3);
assert_eq!(actual_retry_config.mode(), RetryMode::Standard);
}
#[tokio::test]
async fn test_no_retry_config_in_empty_profile() {
let env = Env::from_slice(&[("AWS_CONFIG_FILE", "config")]);
let fs = Fs::from_slice(&[("config", "[default]\n")]);
let provider_config = ProviderConfig::no_configuration().with_env(env).with_fs(fs);
let actual_retry_config = super::default_provider()
.configure(&provider_config)
.retry_config()
.await;
let expected_retry_config = RetryConfig::standard();
assert_eq!(actual_retry_config, expected_retry_config)
}
#[tokio::test]
async fn test_creation_of_retry_config_from_profile() {
let env = Env::from_slice(&[("AWS_CONFIG_FILE", "config")]);
let fs = Fs::from_slice(&[(
"config",
r#"[default]
max_attempts = 1
retry_mode = standard
"#,
)]);
let provider_config = ProviderConfig::no_configuration().with_env(env).with_fs(fs);
let actual_retry_config = super::default_provider()
.configure(&provider_config)
.retry_config()
.await;
let expected_retry_config = RetryConfig::standard().with_max_attempts(1);
assert_eq!(actual_retry_config, expected_retry_config)
}
#[tokio::test]
async fn test_env_retry_config_takes_precedence_over_profile_retry_config() {
let env = Env::from_slice(&[
("AWS_CONFIG_FILE", "config"),
("AWS_MAX_ATTEMPTS", "42"),
("AWS_RETRY_MODE", "standard"),
]);
let fs = Fs::from_slice(&[(
"config",
r#"[default]
max_attempts = 88
retry_mode = standard
"#,
)]);
let provider_config = ProviderConfig::no_configuration().with_env(env).with_fs(fs);
let actual_retry_config = super::default_provider()
.configure(&provider_config)
.retry_config()
.await;
let expected_retry_config = RetryConfig::standard().with_max_attempts(42);
assert_eq!(actual_retry_config, expected_retry_config)
}
#[tokio::test]
#[should_panic = "failed to parse max attempts. source: profile `default`, key: `max_attempts`: invalid digit found in string"]
async fn test_invalid_profile_retry_config_panics() {
let env = Env::from_slice(&[("AWS_CONFIG_FILE", "config")]);
let fs = Fs::from_slice(&[(
"config",
r#"[default]
max_attempts = potato
"#,
)]);
let provider_config = ProviderConfig::no_configuration().with_env(env).with_fs(fs);
let _ = super::default_provider()
.configure(&provider_config)
.retry_config()
.await;
}
#[tokio::test]
async fn defaults() {
let built = test_provider(&[]).await.unwrap();
assert_eq!(built.mode(), RetryMode::Standard);
assert_eq!(built.max_attempts(), 3);
}
#[tokio::test]
async fn max_attempts_is_read_correctly() {
assert_eq!(
test_provider(&[(env::MAX_ATTEMPTS, "88")]).await.unwrap(),
RetryConfig::standard().with_max_attempts(88)
);
}
#[tokio::test]
async fn max_attempts_errors_when_it_cant_be_parsed_as_an_integer() {
assert!(matches!(
test_provider(&[(env::MAX_ATTEMPTS, "not an integer")])
.await
.unwrap_err()
.err,
RetryConfigError {
kind: RetryConfigErrorKind::FailedToParseMaxAttempts { .. }
}
));
}
#[tokio::test]
async fn retry_mode_is_read_correctly() {
assert_eq!(
test_provider(&[(env::RETRY_MODE, "standard")])
.await
.unwrap(),
RetryConfig::standard()
);
}
#[tokio::test]
async fn both_fields_can_be_set_at_once() {
assert_eq!(
test_provider(&[(env::RETRY_MODE, "standard"), (env::MAX_ATTEMPTS, "13")])
.await
.unwrap(),
RetryConfig::standard().with_max_attempts(13)
);
}
#[tokio::test]
async fn disallow_zero_max_attempts() {
let err = test_provider(&[(env::MAX_ATTEMPTS, "0")])
.await
.unwrap_err()
.err;
assert!(matches!(
err,
RetryConfigError {
kind: RetryConfigErrorKind::MaxAttemptsMustNotBeZero { .. }
}
));
}
}