aws_sdk_bcmdashboards/config/
endpoint.rs

1// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
2pub 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/// Interceptor that tracks endpoint override business metric.
7#[derive(Debug, Default)]
8pub(crate) struct EndpointOverrideFeatureTrackerInterceptor;
9
10impl ::aws_smithy_runtime_api::client::interceptors::Intercept for EndpointOverrideFeatureTrackerInterceptor {
11    fn name(&self) -> &'static str {
12        "EndpointOverrideFeatureTrackerInterceptor"
13    }
14
15    fn read_before_execution(
16        &self,
17        _context: &::aws_smithy_runtime_api::client::interceptors::context::BeforeSerializationInterceptorContextRef<'_>,
18        cfg: &mut ::aws_smithy_types::config_bag::ConfigBag,
19    ) -> ::std::result::Result<(), ::aws_smithy_runtime_api::box_error::BoxError> {
20        if cfg.load::<::aws_types::endpoint_config::EndpointUrl>().is_some() {
21            cfg.interceptor_state()
22                .store_append(::aws_runtime::sdk_feature::AwsSdkFeature::EndpointOverride);
23        }
24        ::std::result::Result::Ok(())
25    }
26}
27
28#[cfg(test)]
29mod test {
30
31    /// For custom endpoint with region not set and fips disabled
32    #[test]
33    fn test_1() {
34        let params = crate::config::endpoint::Params::builder()
35            .endpoint("https://example.com".to_string())
36            .use_fips(false)
37            .build()
38            .expect("invalid params");
39        let resolver = crate::config::endpoint::DefaultResolver::new();
40        let endpoint = resolver.resolve_endpoint(&params);
41        let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
42        assert_eq!(
43            endpoint,
44            ::aws_smithy_types::endpoint::Endpoint::builder().url("https://example.com").build()
45        );
46    }
47
48    /// For custom endpoint with fips enabled
49    #[test]
50    fn test_2() {
51        let params = crate::config::endpoint::Params::builder()
52            .endpoint("https://example.com".to_string())
53            .use_fips(true)
54            .build()
55            .expect("invalid params");
56        let resolver = crate::config::endpoint::DefaultResolver::new();
57        let endpoint = resolver.resolve_endpoint(&params);
58        let error = endpoint
59            .expect_err("expected error: Invalid Configuration: FIPS and custom endpoint are not supported [For custom endpoint with fips enabled]");
60        assert_eq!(format!("{}", error), "Invalid Configuration: FIPS and custom endpoint are not supported")
61    }
62
63    /// For region us-east-1 with FIPS enabled and DualStack enabled
64    #[test]
65    fn test_3() {
66        let params = crate::config::endpoint::Params::builder()
67            .region("us-east-1".to_string())
68            .use_fips(true)
69            .build()
70            .expect("invalid params");
71        let resolver = crate::config::endpoint::DefaultResolver::new();
72        let endpoint = resolver.resolve_endpoint(&params);
73        let endpoint = endpoint.expect("Expected valid endpoint: https://bcm-dashboards-fips.us-east-1.api.aws");
74        assert_eq!(
75            endpoint,
76            ::aws_smithy_types::endpoint::Endpoint::builder()
77                .url("https://bcm-dashboards-fips.us-east-1.api.aws")
78                .property(
79                    "authSchemes",
80                    vec![{
81                        let mut out = ::std::collections::HashMap::<String, ::aws_smithy_types::Document>::new();
82                        out.insert("name".to_string(), "sigv4".to_string().into());
83                        out.insert("signingRegion".to_string(), "us-east-1".to_string().into());
84                        out
85                    }
86                    .into()]
87                )
88                .build()
89        );
90    }
91
92    /// For region us-east-1 with FIPS disabled and DualStack enabled
93    #[test]
94    fn test_4() {
95        let params = crate::config::endpoint::Params::builder()
96            .region("us-east-1".to_string())
97            .use_fips(false)
98            .build()
99            .expect("invalid params");
100        let resolver = crate::config::endpoint::DefaultResolver::new();
101        let endpoint = resolver.resolve_endpoint(&params);
102        let endpoint = endpoint.expect("Expected valid endpoint: https://bcm-dashboards.us-east-1.api.aws");
103        assert_eq!(
104            endpoint,
105            ::aws_smithy_types::endpoint::Endpoint::builder()
106                .url("https://bcm-dashboards.us-east-1.api.aws")
107                .property(
108                    "authSchemes",
109                    vec![{
110                        let mut out = ::std::collections::HashMap::<String, ::aws_smithy_types::Document>::new();
111                        out.insert("name".to_string(), "sigv4".to_string().into());
112                        out.insert("signingRegion".to_string(), "us-east-1".to_string().into());
113                        out
114                    }
115                    .into()]
116                )
117                .build()
118        );
119    }
120
121    /// For region cn-northwest-1 with FIPS enabled and DualStack enabled
122    #[test]
123    fn test_5() {
124        let params = crate::config::endpoint::Params::builder()
125            .region("cn-northwest-1".to_string())
126            .use_fips(true)
127            .build()
128            .expect("invalid params");
129        let resolver = crate::config::endpoint::DefaultResolver::new();
130        let endpoint = resolver.resolve_endpoint(&params);
131        let endpoint = endpoint.expect("Expected valid endpoint: https://bcm-dashboards-fips.cn-northwest-1.api.amazonwebservices.com.cn");
132        assert_eq!(
133            endpoint,
134            ::aws_smithy_types::endpoint::Endpoint::builder()
135                .url("https://bcm-dashboards-fips.cn-northwest-1.api.amazonwebservices.com.cn")
136                .property(
137                    "authSchemes",
138                    vec![{
139                        let mut out = ::std::collections::HashMap::<String, ::aws_smithy_types::Document>::new();
140                        out.insert("name".to_string(), "sigv4".to_string().into());
141                        out.insert("signingRegion".to_string(), "cn-northwest-1".to_string().into());
142                        out
143                    }
144                    .into()]
145                )
146                .build()
147        );
148    }
149
150    /// For region cn-northwest-1 with FIPS disabled and DualStack enabled
151    #[test]
152    fn test_6() {
153        let params = crate::config::endpoint::Params::builder()
154            .region("cn-northwest-1".to_string())
155            .use_fips(false)
156            .build()
157            .expect("invalid params");
158        let resolver = crate::config::endpoint::DefaultResolver::new();
159        let endpoint = resolver.resolve_endpoint(&params);
160        let endpoint = endpoint.expect("Expected valid endpoint: https://bcm-dashboards.cn-northwest-1.api.amazonwebservices.com.cn");
161        assert_eq!(
162            endpoint,
163            ::aws_smithy_types::endpoint::Endpoint::builder()
164                .url("https://bcm-dashboards.cn-northwest-1.api.amazonwebservices.com.cn")
165                .property(
166                    "authSchemes",
167                    vec![{
168                        let mut out = ::std::collections::HashMap::<String, ::aws_smithy_types::Document>::new();
169                        out.insert("name".to_string(), "sigv4".to_string().into());
170                        out.insert("signingRegion".to_string(), "cn-northwest-1".to_string().into());
171                        out
172                    }
173                    .into()]
174                )
175                .build()
176        );
177    }
178
179    /// For region us-gov-west-1 with FIPS enabled and DualStack enabled
180    #[test]
181    fn test_7() {
182        let params = crate::config::endpoint::Params::builder()
183            .region("us-gov-west-1".to_string())
184            .use_fips(true)
185            .build()
186            .expect("invalid params");
187        let resolver = crate::config::endpoint::DefaultResolver::new();
188        let endpoint = resolver.resolve_endpoint(&params);
189        let endpoint = endpoint.expect("Expected valid endpoint: https://bcm-dashboards-fips.us-gov-west-1.api.aws");
190        assert_eq!(
191            endpoint,
192            ::aws_smithy_types::endpoint::Endpoint::builder()
193                .url("https://bcm-dashboards-fips.us-gov-west-1.api.aws")
194                .property(
195                    "authSchemes",
196                    vec![{
197                        let mut out = ::std::collections::HashMap::<String, ::aws_smithy_types::Document>::new();
198                        out.insert("name".to_string(), "sigv4".to_string().into());
199                        out.insert("signingRegion".to_string(), "us-gov-west-1".to_string().into());
200                        out
201                    }
202                    .into()]
203                )
204                .build()
205        );
206    }
207
208    /// For region us-gov-west-1 with FIPS disabled and DualStack enabled
209    #[test]
210    fn test_8() {
211        let params = crate::config::endpoint::Params::builder()
212            .region("us-gov-west-1".to_string())
213            .use_fips(false)
214            .build()
215            .expect("invalid params");
216        let resolver = crate::config::endpoint::DefaultResolver::new();
217        let endpoint = resolver.resolve_endpoint(&params);
218        let endpoint = endpoint.expect("Expected valid endpoint: https://bcm-dashboards.us-gov-west-1.api.aws");
219        assert_eq!(
220            endpoint,
221            ::aws_smithy_types::endpoint::Endpoint::builder()
222                .url("https://bcm-dashboards.us-gov-west-1.api.aws")
223                .property(
224                    "authSchemes",
225                    vec![{
226                        let mut out = ::std::collections::HashMap::<String, ::aws_smithy_types::Document>::new();
227                        out.insert("name".to_string(), "sigv4".to_string().into());
228                        out.insert("signingRegion".to_string(), "us-gov-west-1".to_string().into());
229                        out
230                    }
231                    .into()]
232                )
233                .build()
234        );
235    }
236
237    /// Missing region
238    #[test]
239    fn test_9() {
240        let params = crate::config::endpoint::Params::builder().build().expect("invalid params");
241        let resolver = crate::config::endpoint::DefaultResolver::new();
242        let endpoint = resolver.resolve_endpoint(&params);
243        let error = endpoint.expect_err("expected error: Invalid Configuration: Missing Region [Missing region]");
244        assert_eq!(format!("{}", error), "Invalid Configuration: Missing Region")
245    }
246}
247
248/// Endpoint resolver trait specific to AWS Billing and Cost Management Dashboards
249pub trait ResolveEndpoint: ::std::marker::Send + ::std::marker::Sync + ::std::fmt::Debug {
250    /// Resolve an endpoint with the given parameters
251    fn resolve_endpoint<'a>(&'a self, params: &'a crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a>;
252
253    /// Convert this service-specific resolver into a `SharedEndpointResolver`
254    ///
255    /// The resulting resolver will downcast `EndpointResolverParams` into `crate::config::endpoint::Params`.
256    fn into_shared_resolver(self) -> ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver
257    where
258        Self: Sized + 'static,
259    {
260        ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver::new(DowncastParams(self))
261    }
262}
263
264#[derive(Debug)]
265struct DowncastParams<T>(T);
266impl<T> ::aws_smithy_runtime_api::client::endpoint::ResolveEndpoint for DowncastParams<T>
267where
268    T: ResolveEndpoint,
269{
270    fn resolve_endpoint<'a>(
271        &'a self,
272        params: &'a ::aws_smithy_runtime_api::client::endpoint::EndpointResolverParams,
273    ) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a> {
274        let ep = match params.get::<crate::config::endpoint::Params>() {
275            Some(params) => self.0.resolve_endpoint(params),
276            None => ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(Err("params of expected type was not present".into())),
277        };
278        ep
279    }
280}
281
282/// The default endpoint resolver
283#[derive(Debug, Default)]
284pub struct DefaultResolver {
285    partition_resolver: crate::endpoint_lib::partition::PartitionResolver,
286}
287
288impl DefaultResolver {
289    /// Create a new endpoint resolver with default settings
290    pub fn new() -> Self {
291        Self {
292            partition_resolver: crate::endpoint_lib::DEFAULT_PARTITION_RESOLVER.clone(),
293        }
294    }
295
296    fn resolve_endpoint(
297        &self,
298        params: &crate::config::endpoint::Params,
299    ) -> ::std::result::Result<::aws_smithy_types::endpoint::Endpoint, ::aws_smithy_runtime_api::box_error::BoxError> {
300        let mut diagnostic_collector = crate::endpoint_lib::diagnostic::DiagnosticCollector::new();
301        Ok(
302            crate::config::endpoint::internals::resolve_endpoint(params, &mut diagnostic_collector, &self.partition_resolver)
303                .map_err(|err| err.with_source(diagnostic_collector.take_last_error()))?,
304        )
305    }
306}
307
308impl crate::config::endpoint::ResolveEndpoint for DefaultResolver {
309    fn resolve_endpoint(&self, params: &crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'_> {
310        ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(self.resolve_endpoint(params))
311    }
312}
313
314#[non_exhaustive]
315#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
316/// Configuration parameters for resolving the correct endpoint
317pub struct Params {
318    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
319    pub(crate) use_fips: bool,
320    /// Override the endpoint used to send this request
321    pub(crate) endpoint: ::std::option::Option<::std::string::String>,
322    /// The AWS region used to dispatch the request.
323    pub(crate) region: ::std::option::Option<::std::string::String>,
324}
325impl Params {
326    /// Create a builder for [`Params`]
327    pub fn builder() -> crate::config::endpoint::ParamsBuilder {
328        crate::config::endpoint::ParamsBuilder::default()
329    }
330    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
331    pub fn use_fips(&self) -> ::std::option::Option<bool> {
332        Some(self.use_fips)
333    }
334    /// Override the endpoint used to send this request
335    pub fn endpoint(&self) -> ::std::option::Option<&str> {
336        self.endpoint.as_deref()
337    }
338    /// The AWS region used to dispatch the request.
339    pub fn region(&self) -> ::std::option::Option<&str> {
340        self.region.as_deref()
341    }
342}
343
344/// Builder for [`Params`]
345#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
346pub struct ParamsBuilder {
347    use_fips: ::std::option::Option<bool>,
348    endpoint: ::std::option::Option<::std::string::String>,
349    region: ::std::option::Option<::std::string::String>,
350}
351impl ParamsBuilder {
352    /// Consume this builder, creating [`Params`].
353    pub fn build(self) -> ::std::result::Result<crate::config::endpoint::Params, crate::config::endpoint::InvalidParams> {
354        if let Some(region) = &self.region {
355            if !crate::endpoint_lib::host::is_valid_host_label(
356                region.as_ref() as &str,
357                true,
358                &mut crate::endpoint_lib::diagnostic::DiagnosticCollector::new(),
359            ) {
360                return Err(crate::config::endpoint::InvalidParams::invalid_value(
361                    "region",
362                    "must be a valid host label",
363                ));
364            }
365        };
366        Ok(
367            #[allow(clippy::unnecessary_lazy_evaluations)]
368            crate::config::endpoint::Params {
369                use_fips: self
370                    .use_fips
371                    .or_else(|| Some(false))
372                    .ok_or_else(|| crate::config::endpoint::InvalidParams::missing("use_fips"))?,
373                endpoint: self.endpoint,
374                region: self.region,
375            },
376        )
377    }
378    /// Sets the value for use_fips
379    ///
380    /// When unset, this parameter has a default value of `false`.
381    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
382    pub fn use_fips(mut self, value: impl Into<bool>) -> Self {
383        self.use_fips = Some(value.into());
384        self
385    }
386
387    /// Sets the value for use_fips
388    ///
389    /// When unset, this parameter has a default value of `false`.
390    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
391    pub fn set_use_fips(mut self, param: Option<bool>) -> Self {
392        self.use_fips = param;
393        self
394    }
395    /// Sets the value for endpoint
396    ///
397    /// Override the endpoint used to send this request
398    pub fn endpoint(mut self, value: impl Into<::std::string::String>) -> Self {
399        self.endpoint = Some(value.into());
400        self
401    }
402
403    /// Sets the value for endpoint
404    ///
405    /// Override the endpoint used to send this request
406    pub fn set_endpoint(mut self, param: Option<::std::string::String>) -> Self {
407        self.endpoint = param;
408        self
409    }
410    /// Sets the value for region
411    ///
412    /// The AWS region used to dispatch the request.
413    pub fn region(mut self, value: impl Into<::std::string::String>) -> Self {
414        self.region = Some(value.into());
415        self
416    }
417
418    /// Sets the value for region
419    ///
420    /// The AWS region used to dispatch the request.
421    pub fn set_region(mut self, param: Option<::std::string::String>) -> Self {
422        self.region = param;
423        self
424    }
425}
426
427/// An error that occurred during endpoint resolution
428#[derive(Debug)]
429pub struct InvalidParams {
430    field: std::borrow::Cow<'static, str>,
431    kind: InvalidParamsErrorKind,
432}
433
434/// The kind of invalid parameter error
435#[derive(Debug)]
436enum InvalidParamsErrorKind {
437    MissingField,
438    InvalidValue { message: &'static str },
439}
440
441impl InvalidParams {
442    #[allow(dead_code)]
443    fn missing(field: &'static str) -> Self {
444        Self {
445            field: field.into(),
446            kind: InvalidParamsErrorKind::MissingField,
447        }
448    }
449
450    #[allow(dead_code)]
451    fn invalid_value(field: &'static str, message: &'static str) -> Self {
452        Self {
453            field: field.into(),
454            kind: InvalidParamsErrorKind::InvalidValue { message },
455        }
456    }
457}
458
459impl std::fmt::Display for InvalidParams {
460    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
461        match self.kind {
462            InvalidParamsErrorKind::MissingField => write!(f, "a required field was missing: `{}`", self.field),
463            InvalidParamsErrorKind::InvalidValue { message } => write!(f, "invalid value for field: `{}` - {}", self.field, message),
464        }
465    }
466}
467
468impl std::error::Error for InvalidParams {}
469
470mod internals;