1use std::collections::HashMap;
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7use winterbaume_core::{StateChangeNotifier, StateViewError, StatefulService};
8
9use crate::handlers::MemoryDbService;
10use crate::state::MemoryDbState;
11use crate::types::{AclData, Cluster, SnapshotData, SubnetGroupData, TagData, TagsMap};
12
13#[derive(Debug, Clone, Default, Serialize, Deserialize)]
15pub struct MemoryDbStateView {
16 #[serde(default)]
18 pub clusters: HashMap<String, ClusterView>,
19 #[serde(default)]
21 pub snapshots: HashMap<String, SnapshotView>,
22 #[serde(default)]
24 pub subnet_groups: HashMap<String, SubnetGroupView>,
25 #[serde(default)]
27 pub acls: HashMap<String, AclView>,
28 #[serde(default)]
30 pub tags: HashMap<String, Vec<TagView>>,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct ClusterView {
36 pub name: String,
37 pub arn: String,
38 pub status: String,
39 pub node_type: String,
40 pub num_shards: i32,
41 pub num_replicas_per_shard: i32,
42 pub description: String,
43 pub engine: String,
44 pub engine_version: String,
45 pub subnet_group_name: String,
46 pub security_group_ids: Vec<String>,
47 pub maintenance_window: String,
48 pub snapshot_retention_limit: i32,
49 pub snapshot_window: String,
50 pub acl_name: String,
51 pub parameter_group_name: String,
52 pub tls_enabled: bool,
53 pub auto_minor_version_upgrade: bool,
54 pub creation_time: Option<String>,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct SnapshotView {
60 pub name: String,
61 pub arn: String,
62 pub status: String,
63 pub source: String,
64 pub cluster_name: String,
65 pub cluster_description: String,
66 pub cluster_engine: String,
67 pub cluster_engine_version: String,
68 pub cluster_node_type: String,
69 pub cluster_num_shards: i32,
70 pub cluster_subnet_group_name: String,
71 pub cluster_snapshot_retention_limit: i32,
72 pub cluster_snapshot_window: String,
73 pub cluster_maintenance_window: String,
74 pub cluster_parameter_group_name: String,
75 pub kms_key_id: Option<String>,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct SubnetGroupView {
81 pub name: String,
82 pub arn: String,
83 pub description: String,
84 pub vpc_id: String,
85 pub subnet_ids: Vec<String>,
86}
87
88#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct AclView {
91 pub acl_name: String,
92 pub status: String,
93 pub user_names: Vec<String>,
94 pub minimum_engine_version: String,
95 pub arn: String,
96 #[serde(default)]
97 pub tags: Vec<TagView>,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct TagView {
103 pub key: String,
104 pub value: String,
105}
106
107impl From<&Cluster> for ClusterView {
112 fn from(c: &Cluster) -> Self {
113 ClusterView {
114 name: c.name.clone(),
115 arn: c.arn.clone(),
116 status: c.status.clone(),
117 node_type: c.node_type.clone(),
118 num_shards: c.num_shards,
119 num_replicas_per_shard: c.num_replicas_per_shard,
120 description: c.description.clone(),
121 engine: c.engine.clone(),
122 engine_version: c.engine_version.clone(),
123 subnet_group_name: c.subnet_group_name.clone(),
124 security_group_ids: c.security_group_ids.clone(),
125 maintenance_window: c.maintenance_window.clone(),
126 snapshot_retention_limit: c.snapshot_retention_limit,
127 snapshot_window: c.snapshot_window.clone(),
128 acl_name: c.acl_name.clone(),
129 parameter_group_name: c.parameter_group_name.clone(),
130 tls_enabled: c.tls_enabled,
131 auto_minor_version_upgrade: c.auto_minor_version_upgrade,
132 creation_time: Some(c.creation_time.to_rfc3339()),
133 }
134 }
135}
136
137impl From<ClusterView> for Cluster {
138 fn from(v: ClusterView) -> Self {
139 let creation_time = v
140 .creation_time
141 .as_deref()
142 .and_then(|s| DateTime::parse_from_rfc3339(s).ok())
143 .map(|dt| dt.with_timezone(&Utc))
144 .unwrap_or_else(Utc::now);
145 Cluster {
146 name: v.name,
147 arn: v.arn,
148 status: v.status,
149 node_type: v.node_type,
150 num_shards: v.num_shards,
151 num_replicas_per_shard: v.num_replicas_per_shard,
152 description: v.description,
153 engine: v.engine,
154 engine_version: v.engine_version,
155 subnet_group_name: v.subnet_group_name,
156 security_group_ids: v.security_group_ids,
157 maintenance_window: v.maintenance_window,
158 snapshot_retention_limit: v.snapshot_retention_limit,
159 snapshot_window: v.snapshot_window,
160 acl_name: v.acl_name,
161 parameter_group_name: v.parameter_group_name,
162 tls_enabled: v.tls_enabled,
163 auto_minor_version_upgrade: v.auto_minor_version_upgrade,
164 creation_time,
165 }
166 }
167}
168
169impl From<&SnapshotData> for SnapshotView {
170 fn from(s: &SnapshotData) -> Self {
171 SnapshotView {
172 name: s.name.clone(),
173 arn: s.arn.clone(),
174 status: s.status.clone(),
175 source: s.source.clone(),
176 cluster_name: s.cluster_name.clone(),
177 cluster_description: s.cluster_description.clone(),
178 cluster_engine: s.cluster_engine.clone(),
179 cluster_engine_version: s.cluster_engine_version.clone(),
180 cluster_node_type: s.cluster_node_type.clone(),
181 cluster_num_shards: s.cluster_num_shards,
182 cluster_subnet_group_name: s.cluster_subnet_group_name.clone(),
183 cluster_snapshot_retention_limit: s.cluster_snapshot_retention_limit,
184 cluster_snapshot_window: s.cluster_snapshot_window.clone(),
185 cluster_maintenance_window: s.cluster_maintenance_window.clone(),
186 cluster_parameter_group_name: s.cluster_parameter_group_name.clone(),
187 kms_key_id: s.kms_key_id.clone(),
188 }
189 }
190}
191
192impl From<SnapshotView> for SnapshotData {
193 fn from(v: SnapshotView) -> Self {
194 SnapshotData {
195 name: v.name,
196 arn: v.arn,
197 status: v.status,
198 source: v.source,
199 cluster_name: v.cluster_name,
200 cluster_description: v.cluster_description,
201 cluster_engine: v.cluster_engine,
202 cluster_engine_version: v.cluster_engine_version,
203 cluster_node_type: v.cluster_node_type,
204 cluster_num_shards: v.cluster_num_shards,
205 cluster_subnet_group_name: v.cluster_subnet_group_name,
206 cluster_snapshot_retention_limit: v.cluster_snapshot_retention_limit,
207 cluster_snapshot_window: v.cluster_snapshot_window,
208 cluster_maintenance_window: v.cluster_maintenance_window,
209 cluster_parameter_group_name: v.cluster_parameter_group_name,
210 kms_key_id: v.kms_key_id,
211 }
212 }
213}
214
215impl From<&SubnetGroupData> for SubnetGroupView {
216 fn from(sg: &SubnetGroupData) -> Self {
217 SubnetGroupView {
218 name: sg.name.clone(),
219 arn: sg.arn.clone(),
220 description: sg.description.clone(),
221 vpc_id: sg.vpc_id.clone(),
222 subnet_ids: sg.subnet_ids.clone(),
223 }
224 }
225}
226
227impl From<SubnetGroupView> for SubnetGroupData {
228 fn from(v: SubnetGroupView) -> Self {
229 SubnetGroupData {
230 name: v.name,
231 arn: v.arn,
232 description: v.description,
233 vpc_id: v.vpc_id,
234 subnet_ids: v.subnet_ids,
235 }
236 }
237}
238
239impl From<&TagData> for TagView {
240 fn from(t: &TagData) -> Self {
241 TagView {
242 key: t.key.clone(),
243 value: t.value.clone(),
244 }
245 }
246}
247
248impl From<TagView> for TagData {
249 fn from(v: TagView) -> Self {
250 TagData {
251 key: v.key,
252 value: v.value,
253 }
254 }
255}
256
257impl From<&AclData> for AclView {
258 fn from(a: &AclData) -> Self {
259 AclView {
260 acl_name: a.acl_name.clone(),
261 status: a.status.clone(),
262 user_names: a.user_names.clone(),
263 minimum_engine_version: a.minimum_engine_version.clone(),
264 arn: a.arn.clone(),
265 tags: a.tags.iter().map(TagView::from).collect(),
266 }
267 }
268}
269
270impl From<AclView> for AclData {
271 fn from(v: AclView) -> Self {
272 AclData {
273 acl_name: v.acl_name,
274 status: v.status,
275 user_names: v.user_names,
276 minimum_engine_version: v.minimum_engine_version,
277 arn: v.arn,
278 tags: v.tags.into_iter().map(TagData::from).collect(),
279 }
280 }
281}
282
283fn tags_map_to_view(tags: &TagsMap) -> HashMap<String, Vec<TagView>> {
284 tags.iter()
285 .map(|(arn, ts)| (arn.clone(), ts.iter().map(TagView::from).collect()))
286 .collect()
287}
288
289fn view_to_tags_map(tags: HashMap<String, Vec<TagView>>) -> TagsMap {
290 tags.into_iter()
291 .map(|(arn, ts)| (arn, ts.into_iter().map(TagData::from).collect()))
292 .collect()
293}
294
295impl StatefulService for MemoryDbService {
300 type StateView = MemoryDbStateView;
301
302 async fn snapshot(&self, account_id: &str, region: &str) -> Self::StateView {
303 let state = self.state.get(account_id, region);
304 let guard = state.read().await;
305
306 let clusters = guard
307 .clusters
308 .iter()
309 .map(|(k, v)| (k.clone(), ClusterView::from(v)))
310 .collect();
311 let snapshots = guard
312 .snapshots
313 .iter()
314 .map(|(k, v)| (k.clone(), SnapshotView::from(v)))
315 .collect();
316 let subnet_groups = guard
317 .subnet_groups
318 .iter()
319 .map(|(k, v)| (k.clone(), SubnetGroupView::from(v)))
320 .collect();
321 let acls = guard
322 .acls
323 .iter()
324 .map(|(k, v)| (k.clone(), AclView::from(v)))
325 .collect();
326 let tags = tags_map_to_view(&guard.tags);
327
328 MemoryDbStateView {
329 clusters,
330 snapshots,
331 subnet_groups,
332 acls,
333 tags,
334 }
335 }
336
337 async fn restore(
338 &self,
339 account_id: &str,
340 region: &str,
341 view: Self::StateView,
342 ) -> Result<(), StateViewError> {
343 let new_state = MemoryDbState {
344 clusters: view
345 .clusters
346 .into_values()
347 .map(|v| {
348 let c = Cluster::from(v);
349 (c.name.clone(), c)
350 })
351 .collect(),
352 snapshots: view
353 .snapshots
354 .into_values()
355 .map(|v| {
356 let s = SnapshotData::from(v);
357 (s.name.clone(), s)
358 })
359 .collect(),
360 subnet_groups: view
361 .subnet_groups
362 .into_values()
363 .map(|v| {
364 let sg = SubnetGroupData::from(v);
365 (sg.name.clone(), sg)
366 })
367 .collect(),
368 acls: view
369 .acls
370 .into_values()
371 .map(|v| {
372 let a = AclData::from(v);
373 (a.acl_name.clone(), a)
374 })
375 .collect(),
376 tags: view_to_tags_map(view.tags),
377 };
378
379 {
380 let state = self.state.get(account_id, region);
381 *state.write().await = new_state;
382 }
383 self.notify_state_changed(account_id, region).await;
384 Ok(())
385 }
386
387 async fn merge(
388 &self,
389 account_id: &str,
390 region: &str,
391 view: Self::StateView,
392 ) -> Result<(), StateViewError> {
393 let state = self.state.get(account_id, region);
394 {
395 let mut guard = state.write().await;
396 for v in view.clusters.into_values() {
397 let c = Cluster::from(v);
398 guard.clusters.insert(c.name.clone(), c);
399 }
400 for v in view.snapshots.into_values() {
401 let s = SnapshotData::from(v);
402 guard.snapshots.insert(s.name.clone(), s);
403 }
404 for v in view.subnet_groups.into_values() {
405 let sg = SubnetGroupData::from(v);
406 guard.subnet_groups.insert(sg.name.clone(), sg);
407 }
408 for v in view.acls.into_values() {
409 let a = AclData::from(v);
410 guard.acls.insert(a.acl_name.clone(), a);
411 }
412 for (arn, ts) in view_to_tags_map(view.tags) {
413 guard.tags.insert(arn, ts);
414 }
415 }
416 self.notify_state_changed(account_id, region).await;
417 Ok(())
418 }
419
420 fn notifier(&self) -> &StateChangeNotifier<Self::StateView> {
421 &self.notifier
422 }
423}