1use chrono::{DateTime, Utc};
2use fakecloud_aws::arn::Arn;
3use parking_lot::RwLock;
4use serde_json::Value;
5use std::collections::HashMap;
6use std::sync::Arc;
7
8#[derive(Debug, Clone)]
9pub struct EventBus {
10 pub name: String,
11 pub arn: String,
12 pub tags: HashMap<String, String>,
13 pub policy: Option<Value>,
14 pub description: Option<String>,
15 pub kms_key_identifier: Option<String>,
16 pub dead_letter_config: Option<Value>,
17 pub creation_time: DateTime<Utc>,
18 pub last_modified_time: DateTime<Utc>,
19}
20
21#[derive(Debug, Clone)]
22pub struct EventRule {
23 pub name: String,
24 pub arn: String,
25 pub event_bus_name: String,
26 pub event_pattern: Option<String>,
27 pub schedule_expression: Option<String>,
28 pub state: String,
29 pub description: Option<String>,
30 pub role_arn: Option<String>,
31 pub managed_by: Option<String>,
32 pub created_by: Option<String>,
33 pub targets: Vec<EventTarget>,
34 pub tags: HashMap<String, String>,
35 pub last_fired: Option<DateTime<Utc>>,
36}
37
38pub type RuleKey = (String, String);
40
41#[derive(Debug, Clone)]
42pub struct EventTarget {
43 pub id: String,
44 pub arn: String,
45 pub input: Option<String>,
46 pub input_path: Option<String>,
47 pub input_transformer: Option<Value>,
48 pub sqs_parameters: Option<Value>,
49}
50
51#[derive(Debug, Clone)]
52pub struct PutEvent {
53 pub event_id: String,
54 pub source: String,
55 pub detail_type: String,
56 pub detail: String,
57 pub event_bus_name: String,
58 pub time: DateTime<Utc>,
59 pub resources: Vec<String>,
60}
61
62#[derive(Debug, Clone)]
63pub struct Archive {
64 pub name: String,
65 pub arn: String,
66 pub event_source_arn: String,
67 pub description: Option<String>,
68 pub event_pattern: Option<String>,
69 pub retention_days: i64,
70 pub state: String,
71 pub creation_time: DateTime<Utc>,
72 pub event_count: i64,
73 pub size_bytes: i64,
74 pub events: Vec<PutEvent>,
75}
76
77#[derive(Debug, Clone)]
78pub struct Connection {
79 pub name: String,
80 pub arn: String,
81 pub description: Option<String>,
82 pub authorization_type: String,
83 pub auth_parameters: Value,
84 pub connection_state: String,
85 pub secret_arn: String,
86 pub creation_time: DateTime<Utc>,
87 pub last_modified_time: DateTime<Utc>,
88 pub last_authorized_time: DateTime<Utc>,
89}
90
91#[derive(Debug, Clone)]
92pub struct ApiDestination {
93 pub name: String,
94 pub arn: String,
95 pub description: Option<String>,
96 pub connection_arn: String,
97 pub invocation_endpoint: String,
98 pub http_method: String,
99 pub invocation_rate_limit_per_second: Option<i64>,
100 pub state: String,
101 pub creation_time: DateTime<Utc>,
102 pub last_modified_time: DateTime<Utc>,
103}
104
105#[derive(Debug, Clone)]
106pub struct Replay {
107 pub name: String,
108 pub arn: String,
109 pub description: Option<String>,
110 pub event_source_arn: String,
111 pub destination: Value,
112 pub event_start_time: DateTime<Utc>,
113 pub event_end_time: DateTime<Utc>,
114 pub state: String,
115 pub replay_start_time: DateTime<Utc>,
116 pub replay_end_time: Option<DateTime<Utc>>,
117}
118
119#[derive(Debug, Clone)]
120pub struct Endpoint {
121 pub name: String,
122 pub arn: String,
123 pub endpoint_id: String,
124 pub endpoint_url: Option<String>,
125 pub description: Option<String>,
126 pub routing_config: Value,
127 pub replication_config: Option<Value>,
128 pub event_buses: Vec<Value>,
129 pub role_arn: Option<String>,
130 pub state: String,
131 pub creation_time: DateTime<Utc>,
132 pub last_modified_time: DateTime<Utc>,
133}
134
135#[derive(Debug, Clone)]
136pub struct PartnerEventSource {
137 pub name: String,
138 pub arn: String,
139 pub account: String,
140 pub creation_time: DateTime<Utc>,
141 pub expiration_time: Option<DateTime<Utc>>,
142 pub state: String,
143}
144
145#[derive(Debug, Clone)]
147pub struct LambdaInvocation {
148 pub function_arn: String,
149 pub payload: String,
150 pub timestamp: DateTime<Utc>,
151}
152
153#[derive(Debug, Clone)]
155pub struct LogDelivery {
156 pub log_group_arn: String,
157 pub payload: String,
158 pub timestamp: DateTime<Utc>,
159}
160
161#[derive(Debug, Clone)]
163pub struct StepFunctionExecution {
164 pub state_machine_arn: String,
165 pub payload: String,
166 pub timestamp: DateTime<Utc>,
167}
168
169pub struct EventBridgeState {
170 pub account_id: String,
171 pub region: String,
172 pub buses: HashMap<String, EventBus>,
173 pub rules: HashMap<RuleKey, EventRule>,
174 pub events: Vec<PutEvent>,
175 pub archives: HashMap<String, Archive>,
176 pub connections: HashMap<String, Connection>,
177 pub api_destinations: HashMap<String, ApiDestination>,
178 pub replays: HashMap<String, Replay>,
179 pub partner_event_sources: HashMap<String, PartnerEventSource>,
181 pub endpoints: HashMap<String, Endpoint>,
183 pub lambda_invocations: Vec<LambdaInvocation>,
185 pub log_deliveries: Vec<LogDelivery>,
187 pub step_function_executions: Vec<StepFunctionExecution>,
189}
190
191impl EventBridgeState {
192 pub fn new(account_id: &str, region: &str) -> Self {
193 let now = Utc::now();
194 let default_bus_arn =
195 Arn::new("events", region, account_id, "event-bus/default").to_string();
196 let mut buses = HashMap::new();
197 buses.insert(
198 "default".to_string(),
199 EventBus {
200 name: "default".to_string(),
201 arn: default_bus_arn,
202 tags: HashMap::new(),
203 policy: None,
204 description: None,
205 kms_key_identifier: None,
206 dead_letter_config: None,
207 creation_time: now,
208 last_modified_time: now,
209 },
210 );
211
212 Self {
213 account_id: account_id.to_string(),
214 region: region.to_string(),
215 buses,
216 rules: HashMap::new(),
217 events: Vec::new(),
218 archives: HashMap::new(),
219 connections: HashMap::new(),
220 api_destinations: HashMap::new(),
221 replays: HashMap::new(),
222 partner_event_sources: HashMap::new(),
223 endpoints: HashMap::new(),
224 lambda_invocations: Vec::new(),
225 log_deliveries: Vec::new(),
226 step_function_executions: Vec::new(),
227 }
228 }
229
230 pub fn resolve_bus_name(&self, name_or_arn: &str) -> String {
232 if name_or_arn.starts_with("arn:") {
233 name_or_arn
235 .rsplit_once("event-bus/")
236 .map(|(_, n)| n.to_string())
237 .unwrap_or_else(|| name_or_arn.to_string())
238 } else {
239 name_or_arn.to_string()
240 }
241 }
242
243 pub fn reset(&mut self) {
244 self.buses.clear();
245 self.rules.clear();
246 self.events.clear();
247 self.partner_event_sources.clear();
248 self.endpoints.clear();
249 self.lambda_invocations.clear();
250 self.log_deliveries.clear();
251 self.step_function_executions.clear();
252 let default_bus_arn = format!(
254 "arn:aws:events:{}:{}:event-bus/default",
255 self.region, self.account_id
256 );
257 self.buses.insert(
258 "default".to_string(),
259 EventBus {
260 name: "default".to_string(),
261 arn: default_bus_arn,
262 tags: HashMap::new(),
263 policy: None,
264 description: None,
265 kms_key_identifier: None,
266 dead_letter_config: None,
267 creation_time: Utc::now(),
268 last_modified_time: Utc::now(),
269 },
270 );
271 }
272}
273
274pub type SharedEventBridgeState = Arc<RwLock<EventBridgeState>>;