aws_sdk_networkflowmonitor/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#[derive(Debug, Default)]
8pub(crate) struct EndpointOverrideFeatureTrackerInterceptor;
9
10#[::aws_smithy_runtime_api::client::interceptors::dyn_dispatch_hint]
11impl ::aws_smithy_runtime_api::client::interceptors::Intercept for EndpointOverrideFeatureTrackerInterceptor {
12 fn name(&self) -> &'static str {
13 "EndpointOverrideFeatureTrackerInterceptor"
14 }
15
16 fn read_before_execution(
17 &self,
18 _context: &::aws_smithy_runtime_api::client::interceptors::context::BeforeSerializationInterceptorContextRef<'_>,
19 cfg: &mut ::aws_smithy_types::config_bag::ConfigBag,
20 ) -> ::std::result::Result<(), ::aws_smithy_runtime_api::box_error::BoxError> {
21 if cfg.load::<::aws_types::endpoint_config::EndpointUrl>().is_some() {
22 cfg.interceptor_state()
23 .store_append(::aws_runtime::sdk_feature::AwsSdkFeature::EndpointOverride);
24 }
25 ::std::result::Result::Ok(())
26 }
27}
28
29#[cfg(test)]
30mod test {
31
32 #[test]
34 fn test_1() {
35 let params = crate::config::endpoint::Params::builder()
36 .endpoint("https://example.com".to_string())
37 .use_fips(false)
38 .build()
39 .expect("invalid params");
40 let resolver = crate::config::endpoint::DefaultResolver::new();
41 let endpoint = resolver.resolve_endpoint(¶ms);
42 let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
43 assert_eq!(
44 endpoint,
45 ::aws_smithy_types::endpoint::Endpoint::builder().url("https://example.com").build()
46 );
47 }
48
49 #[test]
51 fn test_2() {
52 let params = crate::config::endpoint::Params::builder()
53 .endpoint("https://example.com".to_string())
54 .use_fips(true)
55 .build()
56 .expect("invalid params");
57 let resolver = crate::config::endpoint::DefaultResolver::new();
58 let endpoint = resolver.resolve_endpoint(¶ms);
59 let error = endpoint
60 .expect_err("expected error: Invalid Configuration: FIPS and custom endpoint are not supported [For custom endpoint with fips enabled]");
61 assert_eq!(format!("{}", error), "Invalid Configuration: FIPS and custom endpoint are not supported")
62 }
63
64 #[test]
66 fn test_3() {
67 let params = crate::config::endpoint::Params::builder()
68 .region("us-east-1".to_string())
69 .use_fips(true)
70 .build()
71 .expect("invalid params");
72 let resolver = crate::config::endpoint::DefaultResolver::new();
73 let endpoint = resolver.resolve_endpoint(¶ms);
74 let endpoint = endpoint.expect("Expected valid endpoint: https://networkflowmonitor-fips.us-east-1.api.aws");
75 assert_eq!(
76 endpoint,
77 ::aws_smithy_types::endpoint::Endpoint::builder()
78 .url("https://networkflowmonitor-fips.us-east-1.api.aws")
79 .build()
80 );
81 }
82
83 #[test]
85 fn test_4() {
86 let params = crate::config::endpoint::Params::builder()
87 .region("us-east-1".to_string())
88 .use_fips(false)
89 .build()
90 .expect("invalid params");
91 let resolver = crate::config::endpoint::DefaultResolver::new();
92 let endpoint = resolver.resolve_endpoint(¶ms);
93 let endpoint = endpoint.expect("Expected valid endpoint: https://networkflowmonitor.us-east-1.api.aws");
94 assert_eq!(
95 endpoint,
96 ::aws_smithy_types::endpoint::Endpoint::builder()
97 .url("https://networkflowmonitor.us-east-1.api.aws")
98 .build()
99 );
100 }
101
102 #[test]
104 fn test_5() {
105 let params = crate::config::endpoint::Params::builder()
106 .region("cn-northwest-1".to_string())
107 .use_fips(true)
108 .build()
109 .expect("invalid params");
110 let resolver = crate::config::endpoint::DefaultResolver::new();
111 let endpoint = resolver.resolve_endpoint(¶ms);
112 let endpoint = endpoint.expect("Expected valid endpoint: https://networkflowmonitor-fips.cn-northwest-1.api.amazonwebservices.com.cn");
113 assert_eq!(
114 endpoint,
115 ::aws_smithy_types::endpoint::Endpoint::builder()
116 .url("https://networkflowmonitor-fips.cn-northwest-1.api.amazonwebservices.com.cn")
117 .build()
118 );
119 }
120
121 #[test]
123 fn test_6() {
124 let params = crate::config::endpoint::Params::builder()
125 .region("cn-northwest-1".to_string())
126 .use_fips(false)
127 .build()
128 .expect("invalid params");
129 let resolver = crate::config::endpoint::DefaultResolver::new();
130 let endpoint = resolver.resolve_endpoint(¶ms);
131 let endpoint = endpoint.expect("Expected valid endpoint: https://networkflowmonitor.cn-northwest-1.api.amazonwebservices.com.cn");
132 assert_eq!(
133 endpoint,
134 ::aws_smithy_types::endpoint::Endpoint::builder()
135 .url("https://networkflowmonitor.cn-northwest-1.api.amazonwebservices.com.cn")
136 .build()
137 );
138 }
139
140 #[test]
142 fn test_7() {
143 let params = crate::config::endpoint::Params::builder()
144 .region("us-gov-west-1".to_string())
145 .use_fips(true)
146 .build()
147 .expect("invalid params");
148 let resolver = crate::config::endpoint::DefaultResolver::new();
149 let endpoint = resolver.resolve_endpoint(¶ms);
150 let endpoint = endpoint.expect("Expected valid endpoint: https://networkflowmonitor-fips.us-gov-west-1.api.aws");
151 assert_eq!(
152 endpoint,
153 ::aws_smithy_types::endpoint::Endpoint::builder()
154 .url("https://networkflowmonitor-fips.us-gov-west-1.api.aws")
155 .build()
156 );
157 }
158
159 #[test]
161 fn test_8() {
162 let params = crate::config::endpoint::Params::builder()
163 .region("us-gov-west-1".to_string())
164 .use_fips(false)
165 .build()
166 .expect("invalid params");
167 let resolver = crate::config::endpoint::DefaultResolver::new();
168 let endpoint = resolver.resolve_endpoint(¶ms);
169 let endpoint = endpoint.expect("Expected valid endpoint: https://networkflowmonitor.us-gov-west-1.api.aws");
170 assert_eq!(
171 endpoint,
172 ::aws_smithy_types::endpoint::Endpoint::builder()
173 .url("https://networkflowmonitor.us-gov-west-1.api.aws")
174 .build()
175 );
176 }
177
178 #[test]
180 fn test_9() {
181 let params = crate::config::endpoint::Params::builder().build().expect("invalid params");
182 let resolver = crate::config::endpoint::DefaultResolver::new();
183 let endpoint = resolver.resolve_endpoint(¶ms);
184 let error = endpoint.expect_err("expected error: Invalid Configuration: Missing Region [Missing region]");
185 assert_eq!(format!("{}", error), "Invalid Configuration: Missing Region")
186 }
187}
188
189pub trait ResolveEndpoint: ::std::marker::Send + ::std::marker::Sync + ::std::fmt::Debug {
191 fn resolve_endpoint<'a>(&'a self, params: &'a crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a>;
193
194 fn into_shared_resolver(self) -> ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver
198 where
199 Self: Sized + 'static,
200 {
201 ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver::new(DowncastParams(self))
202 }
203}
204
205#[derive(Debug)]
206struct DowncastParams<T>(T);
207impl<T> ::aws_smithy_runtime_api::client::endpoint::ResolveEndpoint for DowncastParams<T>
208where
209 T: ResolveEndpoint,
210{
211 fn resolve_endpoint<'a>(
212 &'a self,
213 params: &'a ::aws_smithy_runtime_api::client::endpoint::EndpointResolverParams,
214 ) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a> {
215 let ep = match params.get::<crate::config::endpoint::Params>() {
216 Some(params) => self.0.resolve_endpoint(params),
217 None => ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(Err("params of expected type was not present".into())),
218 };
219 ep
220 }
221}
222
223#[derive(Debug)]
224pub struct DefaultResolver {
226 partition_resolver: &'static crate::endpoint_lib::partition::PartitionResolver,
227 endpoint_cache: ::arc_swap::ArcSwap<::std::option::Option<(Params, ::aws_smithy_types::endpoint::Endpoint)>>,
228}
229
230impl Default for DefaultResolver {
231 fn default() -> Self {
232 Self::new()
233 }
234}
235
236impl DefaultResolver {
237 pub fn new() -> Self {
239 Self {
240 partition_resolver: &crate::endpoint_lib::DEFAULT_PARTITION_RESOLVER,
241 endpoint_cache: ::arc_swap::ArcSwap::from_pointee(None),
242 }
243 }
244
245 #[allow(
246 unused_variables,
247 unused_parens,
248 clippy::double_parens,
249 clippy::useless_conversion,
250 clippy::bool_comparison,
251 clippy::comparison_to_empty,
252 clippy::needless_borrow,
253 clippy::useless_asref,
254 clippy::redundant_closure_call,
255 clippy::clone_on_copy
256 )]
257 fn resolve_endpoint<'a>(
258 &'a self,
259 params: &'a crate::config::endpoint::Params,
260 ) -> ::std::result::Result<::aws_smithy_types::endpoint::Endpoint, ::aws_smithy_runtime_api::box_error::BoxError> {
261 let mut _diagnostic_collector = crate::endpoint_lib::diagnostic::DiagnosticCollector::new();
262 #[allow(unused_mut)]
263 let mut context = ConditionContext::default();
264
265 let use_fips = ¶ms.use_fips;
267 let endpoint = ¶ms.endpoint;
268 let region = ¶ms.region;
269
270 let mut current_ref: i32 = 2;
271 loop {
272 match current_ref {
273 ref_val if ref_val >= 100_000_000 => {
274 return match (ref_val - 100_000_000) as usize {
275 0 => ::std::result::Result::Err(Box::new(::aws_smithy_http::endpoint::ResolveEndpointError::message(
276 "No endpoint rule matched",
277 )) as ::aws_smithy_runtime_api::box_error::BoxError),
278 1 => ::std::result::Result::Err(Box::new(::aws_smithy_http::endpoint::ResolveEndpointError::message(
279 "Invalid Configuration: FIPS and custom endpoint are not supported".to_string(),
280 )) as ::aws_smithy_runtime_api::box_error::BoxError),
281 2 => {
282 let endpoint = params.endpoint.as_deref().unwrap_or_default();
283 ::std::result::Result::Ok(::aws_smithy_types::endpoint::Endpoint::builder().url(endpoint.to_owned()).build())
284 }
285 3 => {
286 let region = params.region.as_deref().unwrap_or_default();
287 let partition_result = context.partition_result.as_ref().expect("Guaranteed to have a value by earlier checks.");
288 ::std::result::Result::Ok(
289 ::aws_smithy_types::endpoint::Endpoint::builder()
290 .url({
291 let mut out = String::new();
292 out.push_str("https://networkflowmonitor-fips.");
293 #[allow(clippy::needless_borrow)]
294 out.push_str(®ion.as_ref());
295 out.push('.');
296 #[allow(clippy::needless_borrow)]
297 out.push_str(&partition_result.dual_stack_dns_suffix());
298 out
299 })
300 .build(),
301 )
302 }
303 4 => {
304 let region = params.region.as_deref().unwrap_or_default();
305 let partition_result = context.partition_result.as_ref().expect("Guaranteed to have a value by earlier checks.");
306 ::std::result::Result::Ok(
307 ::aws_smithy_types::endpoint::Endpoint::builder()
308 .url({
309 let mut out = String::new();
310 out.push_str("https://networkflowmonitor.");
311 #[allow(clippy::needless_borrow)]
312 out.push_str(®ion.as_ref());
313 out.push('.');
314 #[allow(clippy::needless_borrow)]
315 out.push_str(&partition_result.dual_stack_dns_suffix());
316 out
317 })
318 .build(),
319 )
320 }
321 5 => ::std::result::Result::Err(Box::new(::aws_smithy_http::endpoint::ResolveEndpointError::message(
322 "Invalid Configuration: Missing Region".to_string(),
323 )) as ::aws_smithy_runtime_api::box_error::BoxError),
324 _ => ::std::result::Result::Err(Box::new(::aws_smithy_http::endpoint::ResolveEndpointError::message(
325 "No endpoint rule matched",
326 )) as ::aws_smithy_runtime_api::box_error::BoxError),
327 };
328 }
329 1 | -1 => {
330 return ::std::result::Result::Err(
331 Box::new(::aws_smithy_http::endpoint::ResolveEndpointError::message("No endpoint rule matched"))
332 as ::aws_smithy_runtime_api::box_error::BoxError,
333 )
334 }
335 ref_val => {
336 let is_complement = ref_val < 0;
337 let node = &NODES[(ref_val.unsigned_abs() as usize) - 1];
338 let condition_result = match node.condition_index {
339 0 => endpoint.is_some(),
340 1 => region.is_some(),
341 2 => (|_diagnostic_collector: &mut crate::endpoint_lib::diagnostic::DiagnosticCollector| -> bool {
342 let partition_result = &mut context.partition_result;
343 let partition_resolver = &self.partition_resolver;
344 {
345 *partition_result = partition_resolver
346 .resolve_partition(if let Some(param) = region { param } else { return false }, _diagnostic_collector)
347 .map(|inner| inner.into());
348 partition_result.is_some()
349 }
350 })(&mut _diagnostic_collector),
351 3 => (use_fips) == (&true),
352 _ => unreachable!("Invalid condition index"),
353 };
354 current_ref = if is_complement ^ condition_result { node.high_ref } else { node.low_ref };
355 }
356 }
357 }
358 }
359}
360
361impl crate::config::endpoint::ResolveEndpoint for DefaultResolver {
362 fn resolve_endpoint<'a>(&'a self, params: &'a crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a> {
363 let cached = self.endpoint_cache.load();
365 if let Some((cached_params, cached_endpoint)) = cached.as_ref() {
366 if cached_params == params {
367 return ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(::std::result::Result::Ok(cached_endpoint.clone()));
368 }
369 }
370 drop(cached);
371 let result = self.resolve_endpoint(params);
372 if let ::std::result::Result::Ok(ref endpoint) = result {
373 self.endpoint_cache.store(::std::sync::Arc::new(Some((params.clone(), endpoint.clone()))));
374 }
375 ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(result)
376 }
377}
378const NODES: [crate::endpoint_lib::bdd_interpreter::BddNode; 6] = [
379 crate::endpoint_lib::bdd_interpreter::BddNode {
380 condition_index: -1,
381 high_ref: 1,
382 low_ref: -1,
383 },
384 crate::endpoint_lib::bdd_interpreter::BddNode {
385 condition_index: 0,
386 high_ref: 6,
387 low_ref: 3,
388 },
389 crate::endpoint_lib::bdd_interpreter::BddNode {
390 condition_index: 1,
391 high_ref: 4,
392 low_ref: 100000005,
393 },
394 crate::endpoint_lib::bdd_interpreter::BddNode {
395 condition_index: 2,
396 high_ref: 5,
397 low_ref: 100000005,
398 },
399 crate::endpoint_lib::bdd_interpreter::BddNode {
400 condition_index: 3,
401 high_ref: 100000003,
402 low_ref: 100000004,
403 },
404 crate::endpoint_lib::bdd_interpreter::BddNode {
405 condition_index: 3,
406 high_ref: 100000001,
407 low_ref: 100000002,
408 },
409];
410#[derive(Default)]
413#[allow(unused_lifetimes)]
414pub(crate) struct ConditionContext<'a> {
415 pub(crate) partition_result: Option<crate::endpoint_lib::partition::Partition<'a>>,
416 phantom: std::marker::PhantomData<&'a ()>,
418}
419
420#[non_exhaustive]
421#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
422pub struct Params {
424 pub(crate) use_fips: bool,
426 pub(crate) endpoint: ::std::option::Option<::std::string::String>,
428 pub(crate) region: ::std::option::Option<::std::string::String>,
430}
431impl Params {
432 pub fn builder() -> crate::config::endpoint::ParamsBuilder {
434 crate::config::endpoint::ParamsBuilder::default()
435 }
436 pub fn use_fips(&self) -> ::std::option::Option<bool> {
438 Some(self.use_fips)
439 }
440 pub fn endpoint(&self) -> ::std::option::Option<&str> {
442 self.endpoint.as_deref()
443 }
444 pub fn region(&self) -> ::std::option::Option<&str> {
446 self.region.as_deref()
447 }
448}
449
450#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
452pub struct ParamsBuilder {
453 use_fips: ::std::option::Option<bool>,
454 endpoint: ::std::option::Option<::std::string::String>,
455 region: ::std::option::Option<::std::string::String>,
456}
457impl ParamsBuilder {
458 pub fn build(self) -> ::std::result::Result<crate::config::endpoint::Params, crate::config::endpoint::InvalidParams> {
460 if let Some(region) = &self.region {
461 if !crate::endpoint_lib::host::is_valid_host_label(
462 region.as_ref() as &str,
463 true,
464 &mut crate::endpoint_lib::diagnostic::DiagnosticCollector::new(),
465 ) {
466 return Err(crate::config::endpoint::InvalidParams::invalid_value(
467 "region",
468 "must be a valid host label",
469 ));
470 }
471 };
472 Ok(
473 #[allow(clippy::unnecessary_lazy_evaluations)]
474 crate::config::endpoint::Params {
475 use_fips: self
476 .use_fips
477 .or_else(|| Some(false))
478 .ok_or_else(|| crate::config::endpoint::InvalidParams::missing("use_fips"))?,
479 endpoint: self.endpoint,
480 region: self.region,
481 },
482 )
483 }
484 pub fn use_fips(mut self, value: impl Into<bool>) -> Self {
489 self.use_fips = Some(value.into());
490 self
491 }
492
493 pub fn set_use_fips(mut self, param: Option<bool>) -> Self {
498 self.use_fips = param;
499 self
500 }
501 pub fn endpoint(mut self, value: impl Into<::std::string::String>) -> Self {
505 self.endpoint = Some(value.into());
506 self
507 }
508
509 pub fn set_endpoint(mut self, param: Option<::std::string::String>) -> Self {
513 self.endpoint = param;
514 self
515 }
516 pub fn region(mut self, value: impl Into<::std::string::String>) -> Self {
520 self.region = Some(value.into());
521 self
522 }
523
524 pub fn set_region(mut self, param: Option<::std::string::String>) -> Self {
528 self.region = param;
529 self
530 }
531}
532
533#[derive(Debug)]
535pub struct InvalidParams {
536 field: std::borrow::Cow<'static, str>,
537 kind: InvalidParamsErrorKind,
538}
539
540#[derive(Debug)]
542enum InvalidParamsErrorKind {
543 MissingField,
544 InvalidValue { message: &'static str },
545}
546
547impl InvalidParams {
548 #[allow(dead_code)]
549 fn missing(field: &'static str) -> Self {
550 Self {
551 field: field.into(),
552 kind: InvalidParamsErrorKind::MissingField,
553 }
554 }
555
556 #[allow(dead_code)]
557 fn invalid_value(field: &'static str, message: &'static str) -> Self {
558 Self {
559 field: field.into(),
560 kind: InvalidParamsErrorKind::InvalidValue { message },
561 }
562 }
563}
564
565impl std::fmt::Display for InvalidParams {
566 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
567 match self.kind {
568 InvalidParamsErrorKind::MissingField => write!(f, "a required field was missing: `{}`", self.field),
569 InvalidParamsErrorKind::InvalidValue { message } => write!(f, "invalid value for field: `{}` - {}", self.field, message),
570 }
571 }
572}
573
574impl std::error::Error for InvalidParams {}