aws_sdk_datazone/config/
endpoint.rs1pub use ::aws_smithy_runtime_api::client::endpoint::EndpointFuture;
3pub use ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver;
4pub use ::aws_smithy_types::endpoint::Endpoint;
5
6#[cfg(test)]
7mod test {
8
9 #[test]
11 fn test_1() {
12 let params = crate::config::endpoint::Params::builder()
13 .region("us-east-1".to_string())
14 .use_fips(true)
15 .build()
16 .expect("invalid params");
17 let resolver = crate::config::endpoint::DefaultResolver::new();
18 let endpoint = resolver.resolve_endpoint(¶ms);
19 let endpoint = endpoint.expect("Expected valid endpoint: https://datazone-fips.us-east-1.api.aws");
20 assert_eq!(
21 endpoint,
22 ::aws_smithy_types::endpoint::Endpoint::builder()
23 .url("https://datazone-fips.us-east-1.api.aws")
24 .build()
25 );
26 }
27
28 #[test]
30 fn test_2() {
31 let params = crate::config::endpoint::Params::builder()
32 .region("us-east-1".to_string())
33 .use_fips(false)
34 .build()
35 .expect("invalid params");
36 let resolver = crate::config::endpoint::DefaultResolver::new();
37 let endpoint = resolver.resolve_endpoint(¶ms);
38 let endpoint = endpoint.expect("Expected valid endpoint: https://datazone.us-east-1.api.aws");
39 assert_eq!(
40 endpoint,
41 ::aws_smithy_types::endpoint::Endpoint::builder()
42 .url("https://datazone.us-east-1.api.aws")
43 .build()
44 );
45 }
46
47 #[test]
49 fn test_3() {
50 let params = crate::config::endpoint::Params::builder()
51 .region("cn-north-1".to_string())
52 .use_fips(true)
53 .build()
54 .expect("invalid params");
55 let resolver = crate::config::endpoint::DefaultResolver::new();
56 let endpoint = resolver.resolve_endpoint(¶ms);
57 let endpoint = endpoint.expect("Expected valid endpoint: https://datazone-fips.cn-north-1.api.amazonwebservices.com.cn");
58 assert_eq!(
59 endpoint,
60 ::aws_smithy_types::endpoint::Endpoint::builder()
61 .url("https://datazone-fips.cn-north-1.api.amazonwebservices.com.cn")
62 .build()
63 );
64 }
65
66 #[test]
68 fn test_4() {
69 let params = crate::config::endpoint::Params::builder()
70 .region("cn-north-1".to_string())
71 .use_fips(false)
72 .build()
73 .expect("invalid params");
74 let resolver = crate::config::endpoint::DefaultResolver::new();
75 let endpoint = resolver.resolve_endpoint(¶ms);
76 let endpoint = endpoint.expect("Expected valid endpoint: https://datazone.cn-north-1.api.amazonwebservices.com.cn");
77 assert_eq!(
78 endpoint,
79 ::aws_smithy_types::endpoint::Endpoint::builder()
80 .url("https://datazone.cn-north-1.api.amazonwebservices.com.cn")
81 .build()
82 );
83 }
84
85 #[test]
87 fn test_5() {
88 let params = crate::config::endpoint::Params::builder()
89 .region("us-gov-east-1".to_string())
90 .use_fips(true)
91 .build()
92 .expect("invalid params");
93 let resolver = crate::config::endpoint::DefaultResolver::new();
94 let endpoint = resolver.resolve_endpoint(¶ms);
95 let endpoint = endpoint.expect("Expected valid endpoint: https://datazone-fips.us-gov-east-1.api.aws");
96 assert_eq!(
97 endpoint,
98 ::aws_smithy_types::endpoint::Endpoint::builder()
99 .url("https://datazone-fips.us-gov-east-1.api.aws")
100 .build()
101 );
102 }
103
104 #[test]
106 fn test_6() {
107 let params = crate::config::endpoint::Params::builder()
108 .region("us-gov-east-1".to_string())
109 .use_fips(false)
110 .build()
111 .expect("invalid params");
112 let resolver = crate::config::endpoint::DefaultResolver::new();
113 let endpoint = resolver.resolve_endpoint(¶ms);
114 let endpoint = endpoint.expect("Expected valid endpoint: https://datazone.us-gov-east-1.api.aws");
115 assert_eq!(
116 endpoint,
117 ::aws_smithy_types::endpoint::Endpoint::builder()
118 .url("https://datazone.us-gov-east-1.api.aws")
119 .build()
120 );
121 }
122
123 #[test]
125 fn test_7() {
126 let params = crate::config::endpoint::Params::builder()
127 .region("us-east-1".to_string())
128 .use_fips(false)
129 .endpoint("https://example.com".to_string())
130 .build()
131 .expect("invalid params");
132 let resolver = crate::config::endpoint::DefaultResolver::new();
133 let endpoint = resolver.resolve_endpoint(¶ms);
134 let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
135 assert_eq!(
136 endpoint,
137 ::aws_smithy_types::endpoint::Endpoint::builder().url("https://example.com").build()
138 );
139 }
140
141 #[test]
143 fn test_8() {
144 let params = crate::config::endpoint::Params::builder()
145 .use_fips(false)
146 .endpoint("https://example.com".to_string())
147 .build()
148 .expect("invalid params");
149 let resolver = crate::config::endpoint::DefaultResolver::new();
150 let endpoint = resolver.resolve_endpoint(¶ms);
151 let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
152 assert_eq!(
153 endpoint,
154 ::aws_smithy_types::endpoint::Endpoint::builder().url("https://example.com").build()
155 );
156 }
157
158 #[test]
160 fn test_9() {
161 let params = crate::config::endpoint::Params::builder()
162 .region("us-east-1".to_string())
163 .use_fips(true)
164 .endpoint("https://example.com".to_string())
165 .build()
166 .expect("invalid params");
167 let resolver = crate::config::endpoint::DefaultResolver::new();
168 let endpoint = resolver.resolve_endpoint(¶ms);
169 let error = endpoint.expect_err("expected error: Invalid Configuration: FIPS and custom endpoint are not supported [For custom endpoint with fips enabled and dualstack disabled]");
170 assert_eq!(format!("{}", error), "Invalid Configuration: FIPS and custom endpoint are not supported")
171 }
172
173 #[test]
175 fn test_10() {
176 let params = crate::config::endpoint::Params::builder().build().expect("invalid params");
177 let resolver = crate::config::endpoint::DefaultResolver::new();
178 let endpoint = resolver.resolve_endpoint(¶ms);
179 let error = endpoint.expect_err("expected error: Invalid Configuration: Missing Region [Missing region]");
180 assert_eq!(format!("{}", error), "Invalid Configuration: Missing Region")
181 }
182}
183
184pub trait ResolveEndpoint: ::std::marker::Send + ::std::marker::Sync + ::std::fmt::Debug {
186 fn resolve_endpoint<'a>(&'a self, params: &'a crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a>;
188
189 fn into_shared_resolver(self) -> ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver
193 where
194 Self: Sized + 'static,
195 {
196 ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver::new(DowncastParams(self))
197 }
198}
199
200#[derive(Debug)]
201struct DowncastParams<T>(T);
202impl<T> ::aws_smithy_runtime_api::client::endpoint::ResolveEndpoint for DowncastParams<T>
203where
204 T: ResolveEndpoint,
205{
206 fn resolve_endpoint<'a>(
207 &'a self,
208 params: &'a ::aws_smithy_runtime_api::client::endpoint::EndpointResolverParams,
209 ) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a> {
210 let ep = match params.get::<crate::config::endpoint::Params>() {
211 Some(params) => self.0.resolve_endpoint(params),
212 None => ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(Err("params of expected type was not present".into())),
213 };
214 ep
215 }
216}
217
218#[derive(Debug, Default)]
220pub struct DefaultResolver {
221 partition_resolver: crate::endpoint_lib::partition::PartitionResolver,
222}
223
224impl DefaultResolver {
225 pub fn new() -> Self {
227 Self {
228 partition_resolver: crate::endpoint_lib::DEFAULT_PARTITION_RESOLVER.clone(),
229 }
230 }
231
232 fn resolve_endpoint(
233 &self,
234 params: &crate::config::endpoint::Params,
235 ) -> ::std::result::Result<::aws_smithy_types::endpoint::Endpoint, ::aws_smithy_runtime_api::box_error::BoxError> {
236 let mut diagnostic_collector = crate::endpoint_lib::diagnostic::DiagnosticCollector::new();
237 Ok(
238 crate::config::endpoint::internals::resolve_endpoint(params, &mut diagnostic_collector, &self.partition_resolver)
239 .map_err(|err| err.with_source(diagnostic_collector.take_last_error()))?,
240 )
241 }
242}
243
244impl crate::config::endpoint::ResolveEndpoint for DefaultResolver {
245 fn resolve_endpoint(&self, params: &crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'_> {
246 ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(self.resolve_endpoint(params))
247 }
248}
249
250#[non_exhaustive]
251#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
252pub struct Params {
254 pub(crate) region: ::std::option::Option<::std::string::String>,
256 pub(crate) use_fips: bool,
258 pub(crate) endpoint: ::std::option::Option<::std::string::String>,
260}
261impl Params {
262 pub fn builder() -> crate::config::endpoint::ParamsBuilder {
264 crate::config::endpoint::ParamsBuilder::default()
265 }
266 pub fn region(&self) -> ::std::option::Option<&str> {
268 self.region.as_deref()
269 }
270 pub fn use_fips(&self) -> ::std::option::Option<bool> {
272 Some(self.use_fips)
273 }
274 pub fn endpoint(&self) -> ::std::option::Option<&str> {
276 self.endpoint.as_deref()
277 }
278}
279
280#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
282pub struct ParamsBuilder {
283 region: ::std::option::Option<::std::string::String>,
284 use_fips: ::std::option::Option<bool>,
285 endpoint: ::std::option::Option<::std::string::String>,
286}
287impl ParamsBuilder {
288 pub fn build(self) -> ::std::result::Result<crate::config::endpoint::Params, crate::config::endpoint::InvalidParams> {
290 if let Some(region) = &self.region {
291 if !crate::endpoint_lib::host::is_valid_host_label(
292 region.as_ref() as &str,
293 true,
294 &mut crate::endpoint_lib::diagnostic::DiagnosticCollector::new(),
295 ) {
296 return Err(crate::config::endpoint::InvalidParams::invalid_value(
297 "region",
298 "must be a valid host label",
299 ));
300 }
301 };
302 Ok(
303 #[allow(clippy::unnecessary_lazy_evaluations)]
304 crate::config::endpoint::Params {
305 region: self.region,
306 use_fips: self
307 .use_fips
308 .or_else(|| Some(false))
309 .ok_or_else(|| crate::config::endpoint::InvalidParams::missing("use_fips"))?,
310 endpoint: self.endpoint,
311 },
312 )
313 }
314 pub fn region(mut self, value: impl Into<::std::string::String>) -> Self {
318 self.region = Some(value.into());
319 self
320 }
321
322 pub fn set_region(mut self, param: Option<::std::string::String>) -> Self {
326 self.region = param;
327 self
328 }
329 pub fn use_fips(mut self, value: impl Into<bool>) -> Self {
334 self.use_fips = Some(value.into());
335 self
336 }
337
338 pub fn set_use_fips(mut self, param: Option<bool>) -> Self {
343 self.use_fips = param;
344 self
345 }
346 pub fn endpoint(mut self, value: impl Into<::std::string::String>) -> Self {
350 self.endpoint = Some(value.into());
351 self
352 }
353
354 pub fn set_endpoint(mut self, param: Option<::std::string::String>) -> Self {
358 self.endpoint = param;
359 self
360 }
361}
362
363#[derive(Debug)]
365pub struct InvalidParams {
366 field: std::borrow::Cow<'static, str>,
367 kind: InvalidParamsErrorKind,
368}
369
370#[derive(Debug)]
372enum InvalidParamsErrorKind {
373 MissingField,
374 InvalidValue { message: &'static str },
375}
376
377impl InvalidParams {
378 #[allow(dead_code)]
379 fn missing(field: &'static str) -> Self {
380 Self {
381 field: field.into(),
382 kind: InvalidParamsErrorKind::MissingField,
383 }
384 }
385
386 #[allow(dead_code)]
387 fn invalid_value(field: &'static str, message: &'static str) -> Self {
388 Self {
389 field: field.into(),
390 kind: InvalidParamsErrorKind::InvalidValue { message },
391 }
392 }
393}
394
395impl std::fmt::Display for InvalidParams {
396 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
397 match self.kind {
398 InvalidParamsErrorKind::MissingField => write!(f, "a required field was missing: `{}`", self.field),
399 InvalidParamsErrorKind::InvalidValue { message } => write!(f, "invalid value for field: `{}` - {}", self.field, message),
400 }
401 }
402}
403
404impl std::error::Error for InvalidParams {}
405
406mod internals;