use std::collections::BTreeMap;
use std::sync::Arc;
use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use fakecloud_core::multi_account::{AccountState, MultiAccountState};
pub const RAM_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
pub type TagMap = BTreeMap<String, String>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssociationRecord {
pub associated_entity: String,
pub association_type: String,
pub status: String,
pub status_message: Option<String>,
pub creation_time: DateTime<Utc>,
pub last_updated_time: DateTime<Utc>,
pub external: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceShareRecord {
pub arn: String,
pub name: String,
pub owning_account_id: String,
pub allow_external_principals: bool,
pub status: String,
pub status_message: Option<String>,
pub creation_time: DateTime<Utc>,
pub last_updated_time: DateTime<Utc>,
pub feature_set: String,
pub tags: TagMap,
pub retain_sharing: Option<bool>,
pub resource_associations: Vec<AssociationRecord>,
pub principal_associations: Vec<AssociationRecord>,
pub permission_arns: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionVersionRecord {
pub version: u32,
pub policy_template: String,
pub is_default: bool,
pub creation_time: DateTime<Utc>,
pub last_updated_time: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionRecord {
pub arn: String,
pub name: String,
pub resource_type: String,
pub status: String,
pub feature_set: String,
pub creation_time: DateTime<Utc>,
pub last_updated_time: DateTime<Utc>,
pub default_version: u32,
pub versions: Vec<PermissionVersionRecord>,
pub tags: TagMap,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvitationRecord {
pub arn: String,
pub resource_share_arn: String,
pub resource_share_name: String,
pub sender_account_id: String,
pub receiver_account_id: String,
pub invitation_timestamp: DateTime<Utc>,
pub status: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RamState {
#[serde(default)]
pub resource_shares: BTreeMap<String, ResourceShareRecord>,
#[serde(default)]
pub permissions: BTreeMap<String, PermissionRecord>,
#[serde(default)]
pub invitations: BTreeMap<String, InvitationRecord>,
#[serde(default)]
pub replace_works: BTreeMap<String, serde_json::Value>,
#[serde(default)]
pub tags: BTreeMap<String, TagMap>,
}
impl AccountState for RamState {
fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
Self::default()
}
}
pub type SharedRamState = Arc<RwLock<MultiAccountState<RamState>>>;
#[derive(Debug, Serialize, Deserialize)]
pub struct RamSnapshot {
pub schema_version: u32,
pub accounts: MultiAccountState<RamState>,
}
pub fn resource_share_arn(region: &str, account_id: &str, id: &str) -> String {
format!("arn:aws:ram:{region}:{account_id}:resource-share/{id}")
}
pub fn invitation_arn(region: &str, account_id: &str, id: &str) -> String {
format!("arn:aws:ram:{region}:{account_id}:resource-share-invitation/{id}")
}
pub fn permission_arn(region: &str, account_id: &str, name: &str) -> String {
format!("arn:aws:ram:{region}:{account_id}:permission/{name}")
}
pub fn managed_permission_arn(name: &str) -> String {
format!("arn:aws:ram::aws:permission/{name}")
}