vector/aws/
region.rs

1//! Handles the region settings for AWS components.
2use aws_types::region::Region;
3use vector_lib::configurable::configurable_component;
4
5/// Configuration of the region/endpoint to use when interacting with an AWS service.
6#[configurable_component]
7#[derive(Clone, Debug, Default, PartialEq, Eq)]
8#[serde(default)]
9pub struct RegionOrEndpoint {
10    /// The [AWS region][aws_region] of the target service.
11    ///
12    /// [aws_region]: https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints
13    #[configurable(metadata(docs::examples = "us-east-1"))]
14    pub region: Option<String>,
15
16    /// Custom endpoint for use with AWS-compatible services.
17    #[configurable(metadata(docs::examples = "http://127.0.0.0:5000/path/to/service"))]
18    #[configurable(metadata(docs::advanced))]
19    pub endpoint: Option<String>,
20}
21
22impl RegionOrEndpoint {
23    /// Creates with the given region.
24    pub const fn with_region(region: String) -> Self {
25        Self {
26            region: Some(region),
27            endpoint: None,
28        }
29    }
30
31    /// Creates with both a region and an endpoint.
32    pub fn with_both(region: impl Into<String>, endpoint: impl Into<String>) -> Self {
33        Self {
34            region: Some(region.into()),
35            endpoint: Some(endpoint.into()),
36        }
37    }
38
39    /// Returns the endpoint.
40    pub fn endpoint(&self) -> Option<String> {
41        self.endpoint.clone()
42    }
43
44    /// Returns the region.
45    pub fn region(&self) -> Option<Region> {
46        self.region.clone().map(Region::new)
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use indoc::indoc;
53
54    use super::*;
55
56    #[test]
57    fn optional() {
58        assert!(toml::from_str::<RegionOrEndpoint>(indoc! {"
59        "})
60        .is_ok());
61    }
62
63    #[test]
64    fn region_optional() {
65        assert!(toml::from_str::<RegionOrEndpoint>(indoc! {r#"
66            endpoint = "http://localhost:8080"
67        "#})
68        .is_ok());
69    }
70
71    #[test]
72    fn endpoint_optional() {
73        assert!(toml::from_str::<RegionOrEndpoint>(indoc! {r#"
74            region = "us-east-1"
75        "#})
76        .is_ok());
77    }
78}