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
use std::str::FromStr;
use aws_smithy_types::retry::{RetryConfigBuilder, RetryConfigErr, RetryMode};
use aws_types::os_shim_internal::{Env, Fs};
use crate::provider_config::ProviderConfig;
#[derive(Debug, Default)]
pub struct ProfileFileRetryConfigProvider {
fs: Fs,
env: Env,
profile_override: Option<String>,
}
#[derive(Debug, Default)]
pub struct Builder {
config: Option<ProviderConfig>,
profile_override: Option<String>,
}
impl Builder {
pub fn configure(mut self, config: &ProviderConfig) -> Self {
self.config = Some(config.clone());
self
}
pub fn profile_name(mut self, profile_name: impl Into<String>) -> Self {
self.profile_override = Some(profile_name.into());
self
}
pub fn build(self) -> ProfileFileRetryConfigProvider {
let conf = self.config.unwrap_or_default();
ProfileFileRetryConfigProvider {
env: conf.env(),
fs: conf.fs(),
profile_override: self.profile_override,
}
}
}
impl ProfileFileRetryConfigProvider {
pub fn new() -> Self {
Self {
fs: Fs::real(),
env: Env::real(),
profile_override: None,
}
}
pub fn builder() -> Builder {
Builder::default()
}
pub async fn retry_config_builder(&self) -> Result<RetryConfigBuilder, RetryConfigErr> {
let profile = match super::parser::load(&self.fs, &self.env).await {
Ok(profile) => profile,
Err(err) => {
tracing::warn!(err = %err, "failed to parse profile");
return Ok(RetryConfigBuilder::new());
}
};
let selected_profile = self
.profile_override
.as_deref()
.unwrap_or_else(|| profile.selected_profile());
let selected_profile = match profile.get_profile(selected_profile) {
Some(profile) => profile,
None => {
if self.profile_override.is_some() {
tracing::warn!("failed to get selected '{}' profile", selected_profile);
}
return Ok(RetryConfigBuilder::new());
}
};
let max_attempts = match selected_profile.get("max_attempts") {
Some(max_attempts) => match max_attempts.parse::<u32>() {
Ok(max_attempts) if max_attempts == 0 => {
return Err(RetryConfigErr::MaxAttemptsMustNotBeZero {
set_by: "aws profile".into(),
});
}
Ok(max_attempts) => Some(max_attempts),
Err(source) => {
return Err(RetryConfigErr::FailedToParseMaxAttempts {
set_by: "aws profile".into(),
source,
});
}
},
None => None,
};
let retry_mode = match selected_profile.get("retry_mode") {
Some(retry_mode) => match RetryMode::from_str(retry_mode) {
Ok(retry_mode) => Some(retry_mode),
Err(retry_mode_err) => {
return Err(RetryConfigErr::InvalidRetryMode {
set_by: "aws profile".into(),
source: retry_mode_err,
});
}
},
None => None,
};
let mut retry_config_builder = RetryConfigBuilder::new();
retry_config_builder
.set_max_attempts(max_attempts)
.set_mode(retry_mode);
Ok(retry_config_builder)
}
}