use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProviderInfo {
pub provider_type: String,
pub native_arn: String,
pub resource_id: Option<String>,
pub account_id: String,
pub synced_at: DateTime<Utc>,
}
impl ProviderInfo {
pub fn new(
provider_type: impl Into<String>,
native_arn: impl Into<String>,
resource_id: Option<String>,
account_id: impl Into<String>,
) -> Self {
Self {
provider_type: provider_type.into(),
native_arn: native_arn.into(),
resource_id,
account_id: account_id.into(),
synced_at: Utc::now(),
}
}
pub fn is_aws(&self) -> bool {
self.provider_type == "aws"
}
pub fn is_gcp(&self) -> bool {
self.provider_type == "gcp"
}
pub fn is_azure(&self) -> bool {
self.provider_type == "azure"
}
pub fn is_custom(&self) -> bool {
!self.is_aws() && !self.is_gcp() && !self.is_azure()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_provider_info_creation() {
let info = ProviderInfo::new(
"aws",
"arn:aws:iam::123456789012:user/alice",
Some("AIDACKCEVSQ6C2EXAMPLE".to_string()),
"123456789012",
);
assert_eq!(info.provider_type, "aws");
assert!(info.is_aws());
assert!(!info.is_gcp());
}
#[test]
fn test_provider_type_checks() {
let aws = ProviderInfo::new("aws", "arn:...", None, "123");
let gcp = ProviderInfo::new("gcp", "projects/...", None, "project-id");
let azure = ProviderInfo::new("azure", "/subscriptions/...", None, "sub-id");
let custom = ProviderInfo::new("mycloud", "mycloud://...", None, "tenant-123");
assert!(aws.is_aws());
assert!(gcp.is_gcp());
assert!(azure.is_azure());
assert!(custom.is_custom());
}
}