1use std::collections::BTreeMap;
11use std::sync::Arc;
12
13use chrono::{DateTime, Utc};
14use parking_lot::RwLock;
15use serde::{Deserialize, Serialize};
16
17use fakecloud_core::multi_account::{AccountState, MultiAccountState};
18
19pub const RAM_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
20
21pub type TagMap = BTreeMap<String, String>;
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct AssociationRecord {
26 pub associated_entity: String,
29 pub association_type: String,
31 pub status: String,
33 pub status_message: Option<String>,
34 pub creation_time: DateTime<Utc>,
35 pub last_updated_time: DateTime<Utc>,
36 pub external: bool,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct ResourceShareRecord {
43 pub arn: String,
44 pub name: String,
45 pub owning_account_id: String,
46 pub allow_external_principals: bool,
47 pub status: String,
48 pub status_message: Option<String>,
49 pub creation_time: DateTime<Utc>,
50 pub last_updated_time: DateTime<Utc>,
51 pub feature_set: String,
52 pub tags: TagMap,
53 pub retain_sharing: Option<bool>,
55 pub resource_associations: Vec<AssociationRecord>,
56 pub principal_associations: Vec<AssociationRecord>,
57 pub permission_arns: Vec<String>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct PermissionVersionRecord {
63 pub version: u32,
64 pub policy_template: String,
65 pub is_default: bool,
66 pub creation_time: DateTime<Utc>,
67 pub last_updated_time: DateTime<Utc>,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct PermissionRecord {
73 pub arn: String,
74 pub name: String,
75 pub resource_type: String,
76 pub status: String,
77 pub feature_set: String,
78 pub creation_time: DateTime<Utc>,
79 pub last_updated_time: DateTime<Utc>,
80 pub default_version: u32,
81 pub versions: Vec<PermissionVersionRecord>,
82 pub tags: TagMap,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct InvitationRecord {
88 pub arn: String,
89 pub resource_share_arn: String,
90 pub resource_share_name: String,
91 pub sender_account_id: String,
92 pub receiver_account_id: String,
93 pub invitation_timestamp: DateTime<Utc>,
94 pub status: String,
95}
96
97#[derive(Debug, Clone, Default, Serialize, Deserialize)]
99pub struct RamState {
100 #[serde(default)]
101 pub resource_shares: BTreeMap<String, ResourceShareRecord>,
102 #[serde(default)]
103 pub permissions: BTreeMap<String, PermissionRecord>,
104 #[serde(default)]
105 pub invitations: BTreeMap<String, InvitationRecord>,
106 #[serde(default)]
109 pub replace_works: BTreeMap<String, serde_json::Value>,
110 #[serde(default)]
112 pub tags: BTreeMap<String, TagMap>,
113}
114
115impl AccountState for RamState {
116 fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
117 Self::default()
118 }
119}
120
121pub type SharedRamState = Arc<RwLock<MultiAccountState<RamState>>>;
122
123#[derive(Debug, Serialize, Deserialize)]
124pub struct RamSnapshot {
125 pub schema_version: u32,
126 pub accounts: MultiAccountState<RamState>,
127}
128
129pub fn resource_share_arn(region: &str, account_id: &str, id: &str) -> String {
134 format!("arn:aws:ram:{region}:{account_id}:resource-share/{id}")
135}
136
137pub fn invitation_arn(region: &str, account_id: &str, id: &str) -> String {
138 format!("arn:aws:ram:{region}:{account_id}:resource-share-invitation/{id}")
139}
140
141pub fn permission_arn(region: &str, account_id: &str, name: &str) -> String {
143 format!("arn:aws:ram:{region}:{account_id}:permission/{name}")
144}
145
146pub fn managed_permission_arn(name: &str) -> String {
148 format!("arn:aws:ram::aws:permission/{name}")
149}