#[cfg(feature = "aws")]
mod aws;
#[cfg(feature = "azure")]
mod azure;
#[cfg(feature = "gcp")]
mod gcp;
#[cfg(feature = "scaleway")]
mod scaleway;
#[cfg(feature = "aws")]
pub use aws::AwsArnTransformer;
#[cfg(feature = "azure")]
pub use azure::AzureArnTransformer;
#[cfg(feature = "gcp")]
pub use gcp::GcpArnTransformer;
#[cfg(feature = "scaleway")]
pub use scaleway::ScalewayArnTransformer;
use crate::arn::types::WamiArn;
use crate::error::Result;
pub trait ArnTransformer {
#[allow(clippy::result_large_err)]
fn to_provider_arn(&self, arn: &WamiArn) -> Result<String>;
#[allow(clippy::wrong_self_convention, clippy::result_large_err)]
fn from_provider_arn(&self, provider_arn: &str) -> Result<ProviderArnInfo>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProviderArnInfo {
pub provider: String,
pub account_id: String,
pub service: String,
pub resource_type: String,
pub resource_id: String,
pub region: Option<String>,
}
pub fn get_transformer(provider: &str) -> Option<Box<dyn ArnTransformer>> {
match provider {
#[cfg(feature = "aws")]
"aws" => Some(Box::new(AwsArnTransformer)),
#[cfg(feature = "gcp")]
"gcp" => Some(Box::new(GcpArnTransformer)),
#[cfg(feature = "azure")]
"azure" => Some(Box::new(AzureArnTransformer)),
#[cfg(feature = "scaleway")]
"scaleway" => Some(Box::new(ScalewayArnTransformer)),
_ => None,
}
}
pub fn available_providers() -> &'static [&'static str] {
&[
#[cfg(feature = "aws")]
"aws",
#[cfg(feature = "gcp")]
"gcp",
#[cfg(feature = "azure")]
"azure",
#[cfg(feature = "scaleway")]
"scaleway",
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_inventory_and_the_registry_agree() {
for provider in available_providers() {
assert!(
get_transformer(provider).is_some(),
"{provider} is advertised but cannot be built"
);
}
assert!(get_transformer("unknown").is_none());
}
#[test]
fn no_provider_is_available_unless_asked_for() {
#[cfg(not(any(
feature = "aws",
feature = "gcp",
feature = "azure",
feature = "scaleway"
)))]
{
assert!(available_providers().is_empty());
assert!(get_transformer("aws").is_none());
}
#[cfg(feature = "aws")]
assert!(available_providers().contains(&"aws"));
}
}