use super::profile_file::ProfileFiles;
use crate::provider_config::ProviderConfig;
use aws_types::app_name::AppName;
#[doc = include_str!("location_of_profile_files.md")]
#[derive(Debug, Default)]
pub struct ProfileFileAppNameProvider {
provider_config: ProviderConfig,
}
impl ProfileFileAppNameProvider {
pub fn new() -> Self {
Self {
provider_config: ProviderConfig::default(),
}
}
pub fn builder() -> Builder {
Builder::default()
}
pub async fn app_name(&self) -> Option<AppName> {
let app_id = self.provider_config.profile().await?.get("sdk-ua-app-id")?;
match AppName::new(app_id.to_owned()) {
Ok(app_name) => Some(app_name),
Err(err) => {
tracing::warn!(err = %err, "`sdk-ua-app-id` property `{}` was invalid", app_id);
None
}
}
}
}
#[derive(Debug, Default)]
pub struct Builder {
config: Option<ProviderConfig>,
profile_override: Option<String>,
profile_files: Option<ProfileFiles>,
}
impl Builder {
pub fn configure(mut self, config: &ProviderConfig) -> Self {
self.config = Some(config.clone());
self
}
pub fn profile_name(mut self, profile_name: impl Into<String>) -> Self {
self.profile_override = Some(profile_name.into());
self
}
pub fn build(self) -> ProfileFileAppNameProvider {
let conf = self
.config
.unwrap_or_default()
.with_profile_config(self.profile_files, self.profile_override);
ProfileFileAppNameProvider {
provider_config: conf,
}
}
}
#[cfg(test)]
mod tests {
use super::ProfileFileAppNameProvider;
use crate::provider_config::ProviderConfig;
use crate::test_case::no_traffic_connector;
use aws_sdk_sts::config::AppName;
use aws_types::os_shim_internal::{Env, Fs};
use tracing_test::traced_test;
fn provider_config(config_contents: &str) -> ProviderConfig {
let fs = Fs::from_slice(&[("test_config", config_contents)]);
let env = Env::from_slice(&[("AWS_CONFIG_FILE", "test_config")]);
ProviderConfig::empty()
.with_fs(fs)
.with_env(env)
.with_http_connector(no_traffic_connector())
}
fn default_provider(config_contents: &str) -> ProfileFileAppNameProvider {
ProfileFileAppNameProvider::builder()
.configure(&provider_config(config_contents))
.build()
}
#[tokio::test]
async fn no_app_name() {
assert_eq!(None, default_provider("[default]\n").app_name().await);
}
#[tokio::test]
async fn app_name_default_profile() {
assert_eq!(
Some(AppName::new("test").unwrap()),
default_provider("[default]\nsdk-ua-app-id = test")
.app_name()
.await
);
}
#[tokio::test]
async fn app_name_other_profiles() {
let config = "\
[default]\n\
sdk-ua-app-id = test\n\
\n\
[profile other]\n\
sdk-ua-app-id = bar\n
";
assert_eq!(
Some(AppName::new("bar").unwrap()),
ProfileFileAppNameProvider::builder()
.profile_name("other")
.configure(&provider_config(config))
.build()
.app_name()
.await
);
}
#[traced_test]
#[tokio::test]
async fn invalid_app_name() {
assert_eq!(
None,
default_provider("[default]\nsdk-ua-app-id = definitely invalid")
.app_name()
.await
);
assert!(logs_contain(
"`sdk-ua-app-id` property `definitely invalid` was invalid"
));
}
}