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