Skip to main content

aws_sdk_interconnect/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
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    /// For custom endpoint with region not set and fips disabled
33    #[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(&params);
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    /// For custom endpoint with fips enabled
50    #[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(&params);
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    /// For region us-east-1 with FIPS enabled and DualStack enabled
65    #[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(&params);
74        let endpoint = endpoint.expect("Expected valid endpoint: https://interconnect-fips.us-east-1.api.aws");
75        assert_eq!(
76            endpoint,
77            ::aws_smithy_types::endpoint::Endpoint::builder()
78                .url("https://interconnect-fips.us-east-1.api.aws")
79                .build()
80        );
81    }
82
83    /// For region us-east-1 with FIPS disabled and DualStack enabled
84    #[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(&params);
93        let endpoint = endpoint.expect("Expected valid endpoint: https://interconnect.us-east-1.api.aws");
94        assert_eq!(
95            endpoint,
96            ::aws_smithy_types::endpoint::Endpoint::builder()
97                .url("https://interconnect.us-east-1.api.aws")
98                .build()
99        );
100    }
101
102    /// For region cn-northwest-1 with FIPS enabled and DualStack enabled
103    #[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(&params);
112        let endpoint = endpoint.expect("Expected valid endpoint: https://interconnect-fips.cn-northwest-1.api.amazonwebservices.com.cn");
113        assert_eq!(
114            endpoint,
115            ::aws_smithy_types::endpoint::Endpoint::builder()
116                .url("https://interconnect-fips.cn-northwest-1.api.amazonwebservices.com.cn")
117                .build()
118        );
119    }
120
121    /// For region cn-northwest-1 with FIPS disabled and DualStack enabled
122    #[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(&params);
131        let endpoint = endpoint.expect("Expected valid endpoint: https://interconnect.cn-northwest-1.api.amazonwebservices.com.cn");
132        assert_eq!(
133            endpoint,
134            ::aws_smithy_types::endpoint::Endpoint::builder()
135                .url("https://interconnect.cn-northwest-1.api.amazonwebservices.com.cn")
136                .build()
137        );
138    }
139
140    /// For region us-gov-west-1 with FIPS enabled and DualStack enabled
141    #[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(&params);
150        let endpoint = endpoint.expect("Expected valid endpoint: https://interconnect-fips.us-gov-west-1.api.aws");
151        assert_eq!(
152            endpoint,
153            ::aws_smithy_types::endpoint::Endpoint::builder()
154                .url("https://interconnect-fips.us-gov-west-1.api.aws")
155                .build()
156        );
157    }
158
159    /// For region us-gov-west-1 with FIPS disabled and DualStack enabled
160    #[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(&params);
169        let endpoint = endpoint.expect("Expected valid endpoint: https://interconnect.us-gov-west-1.api.aws");
170        assert_eq!(
171            endpoint,
172            ::aws_smithy_types::endpoint::Endpoint::builder()
173                .url("https://interconnect.us-gov-west-1.api.aws")
174                .build()
175        );
176    }
177
178    /// Missing region
179    #[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(&params);
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
189/// Endpoint resolver trait specific to Interconnect
190pub trait ResolveEndpoint: ::std::marker::Send + ::std::marker::Sync + ::std::fmt::Debug {
191    /// Resolve an endpoint with the given parameters
192    fn resolve_endpoint<'a>(&'a self, params: &'a crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a>;
193
194    /// Convert this service-specific resolver into a `SharedEndpointResolver`
195    ///
196    /// The resulting resolver will downcast `EndpointResolverParams` into `crate::config::endpoint::Params`.
197    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/// The default endpoint resolver
224#[derive(Debug, Default)]
225pub struct DefaultResolver {
226    partition_resolver: crate::endpoint_lib::partition::PartitionResolver,
227}
228
229impl DefaultResolver {
230    /// Create a new endpoint resolver with default settings
231    pub fn new() -> Self {
232        Self {
233            partition_resolver: crate::endpoint_lib::DEFAULT_PARTITION_RESOLVER.clone(),
234        }
235    }
236
237    fn resolve_endpoint(
238        &self,
239        params: &crate::config::endpoint::Params,
240    ) -> ::std::result::Result<::aws_smithy_types::endpoint::Endpoint, ::aws_smithy_runtime_api::box_error::BoxError> {
241        let mut diagnostic_collector = crate::endpoint_lib::diagnostic::DiagnosticCollector::new();
242        Ok(
243            crate::config::endpoint::internals::resolve_endpoint(params, &mut diagnostic_collector, &self.partition_resolver)
244                .map_err(|err| err.with_source(diagnostic_collector.take_last_error()))?,
245        )
246    }
247}
248
249impl crate::config::endpoint::ResolveEndpoint for DefaultResolver {
250    fn resolve_endpoint(&self, params: &crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'_> {
251        ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(self.resolve_endpoint(params))
252    }
253}
254
255#[non_exhaustive]
256#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
257/// Configuration parameters for resolving the correct endpoint
258pub struct Params {
259    /// 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.
260    pub(crate) use_fips: bool,
261    /// Override the endpoint used to send this request
262    pub(crate) endpoint: ::std::option::Option<::std::string::String>,
263    /// The AWS region used to dispatch the request.
264    pub(crate) region: ::std::option::Option<::std::string::String>,
265}
266impl Params {
267    /// Create a builder for [`Params`]
268    pub fn builder() -> crate::config::endpoint::ParamsBuilder {
269        crate::config::endpoint::ParamsBuilder::default()
270    }
271    /// 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.
272    pub fn use_fips(&self) -> ::std::option::Option<bool> {
273        Some(self.use_fips)
274    }
275    /// Override the endpoint used to send this request
276    pub fn endpoint(&self) -> ::std::option::Option<&str> {
277        self.endpoint.as_deref()
278    }
279    /// The AWS region used to dispatch the request.
280    pub fn region(&self) -> ::std::option::Option<&str> {
281        self.region.as_deref()
282    }
283}
284
285/// Builder for [`Params`]
286#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
287pub struct ParamsBuilder {
288    use_fips: ::std::option::Option<bool>,
289    endpoint: ::std::option::Option<::std::string::String>,
290    region: ::std::option::Option<::std::string::String>,
291}
292impl ParamsBuilder {
293    /// Consume this builder, creating [`Params`].
294    pub fn build(self) -> ::std::result::Result<crate::config::endpoint::Params, crate::config::endpoint::InvalidParams> {
295        if let Some(region) = &self.region {
296            if !crate::endpoint_lib::host::is_valid_host_label(
297                region.as_ref() as &str,
298                true,
299                &mut crate::endpoint_lib::diagnostic::DiagnosticCollector::new(),
300            ) {
301                return Err(crate::config::endpoint::InvalidParams::invalid_value(
302                    "region",
303                    "must be a valid host label",
304                ));
305            }
306        };
307        Ok(
308            #[allow(clippy::unnecessary_lazy_evaluations)]
309            crate::config::endpoint::Params {
310                use_fips: self
311                    .use_fips
312                    .or_else(|| Some(false))
313                    .ok_or_else(|| crate::config::endpoint::InvalidParams::missing("use_fips"))?,
314                endpoint: self.endpoint,
315                region: self.region,
316            },
317        )
318    }
319    /// Sets the value for use_fips
320    ///
321    /// When unset, this parameter has a default value of `false`.
322    /// 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.
323    pub fn use_fips(mut self, value: impl Into<bool>) -> Self {
324        self.use_fips = Some(value.into());
325        self
326    }
327
328    /// Sets the value for use_fips
329    ///
330    /// When unset, this parameter has a default value of `false`.
331    /// 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.
332    pub fn set_use_fips(mut self, param: Option<bool>) -> Self {
333        self.use_fips = param;
334        self
335    }
336    /// Sets the value for endpoint
337    ///
338    /// Override the endpoint used to send this request
339    pub fn endpoint(mut self, value: impl Into<::std::string::String>) -> Self {
340        self.endpoint = Some(value.into());
341        self
342    }
343
344    /// Sets the value for endpoint
345    ///
346    /// Override the endpoint used to send this request
347    pub fn set_endpoint(mut self, param: Option<::std::string::String>) -> Self {
348        self.endpoint = param;
349        self
350    }
351    /// Sets the value for region
352    ///
353    /// The AWS region used to dispatch the request.
354    pub fn region(mut self, value: impl Into<::std::string::String>) -> Self {
355        self.region = Some(value.into());
356        self
357    }
358
359    /// Sets the value for region
360    ///
361    /// The AWS region used to dispatch the request.
362    pub fn set_region(mut self, param: Option<::std::string::String>) -> Self {
363        self.region = param;
364        self
365    }
366}
367
368/// An error that occurred during endpoint resolution
369#[derive(Debug)]
370pub struct InvalidParams {
371    field: std::borrow::Cow<'static, str>,
372    kind: InvalidParamsErrorKind,
373}
374
375/// The kind of invalid parameter error
376#[derive(Debug)]
377enum InvalidParamsErrorKind {
378    MissingField,
379    InvalidValue { message: &'static str },
380}
381
382impl InvalidParams {
383    #[allow(dead_code)]
384    fn missing(field: &'static str) -> Self {
385        Self {
386            field: field.into(),
387            kind: InvalidParamsErrorKind::MissingField,
388        }
389    }
390
391    #[allow(dead_code)]
392    fn invalid_value(field: &'static str, message: &'static str) -> Self {
393        Self {
394            field: field.into(),
395            kind: InvalidParamsErrorKind::InvalidValue { message },
396        }
397    }
398}
399
400impl std::fmt::Display for InvalidParams {
401    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
402        match self.kind {
403            InvalidParamsErrorKind::MissingField => write!(f, "a required field was missing: `{}`", self.field),
404            InvalidParamsErrorKind::InvalidValue { message } => write!(f, "invalid value for field: `{}` - {}", self.field, message),
405        }
406    }
407}
408
409impl std::error::Error for InvalidParams {}
410
411mod internals;