aws_config/environment/
region.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use crate::meta::region::{future, ProvideRegion};
use aws_types::os_shim_internal::Env;
use aws_types::region::Region;

/// Load a region from environment variables
///
/// This provider will first check the value of `AWS_REGION`, falling back to `AWS_DEFAULT_REGION`
/// when `AWS_REGION` is unset.
#[derive(Debug, Default)]
pub struct EnvironmentVariableRegionProvider {
    env: Env,
}

impl EnvironmentVariableRegionProvider {
    /// Create a new `EnvironmentVariableRegionProvider`
    pub fn new() -> Self {
        EnvironmentVariableRegionProvider { env: Env::real() }
    }

    /// Create an region provider from a given `Env`
    ///
    /// This method is used for tests that need to override environment variables.
    pub(crate) fn new_with_env(env: Env) -> Self {
        EnvironmentVariableRegionProvider { env }
    }
}

impl ProvideRegion for EnvironmentVariableRegionProvider {
    fn region(&self) -> future::ProvideRegion<'_> {
        let region = self
            .env
            .get("AWS_REGION")
            .or_else(|_| self.env.get("AWS_DEFAULT_REGION"))
            .map(Region::new)
            .ok();
        future::ProvideRegion::ready(region)
    }
}
#[cfg(test)]
mod test {
    use crate::environment::region::EnvironmentVariableRegionProvider;
    use crate::meta::region::ProvideRegion;
    use aws_types::os_shim_internal::Env;
    use aws_types::region::Region;
    use futures_util::FutureExt;

    fn test_provider(vars: &[(&str, &str)]) -> EnvironmentVariableRegionProvider {
        EnvironmentVariableRegionProvider::new_with_env(Env::from_slice(vars))
    }

    #[test]
    fn no_region() {
        assert_eq!(
            test_provider(&[])
                .region()
                .now_or_never()
                .expect("no polling"),
            None
        );
    }

    #[test]
    fn prioritize_aws_region() {
        let provider = test_provider(&[
            ("AWS_REGION", "us-east-1"),
            ("AWS_DEFAULT_REGION", "us-east-2"),
        ]);
        assert_eq!(
            provider.region().now_or_never().expect("no polling"),
            Some(Region::new("us-east-1"))
        );
    }

    #[test]
    fn fallback_to_default_region() {
        assert_eq!(
            test_provider(&[("AWS_DEFAULT_REGION", "us-east-2")])
                .region()
                .now_or_never()
                .expect("no polling"),
            Some(Region::new("us-east-2"))
        );
    }
}