winterbaume-memorydb 0.2.1

Amazon MemoryDB service implementation for winterbaume
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Serde-compatible view types for MemoryDB state snapshots.

use std::collections::HashMap;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use winterbaume_core::{StateChangeNotifier, StateViewError, StatefulService};

use crate::handlers::MemoryDbService;
use crate::state::MemoryDbState;
use crate::types::{AclData, Cluster, SnapshotData, SubnetGroupData, TagData, TagsMap};

/// Serializable view of the entire MemoryDB state for one account/region.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MemoryDbStateView {
    /// Clusters keyed by cluster name.
    #[serde(default)]
    pub clusters: HashMap<String, ClusterView>,
    /// Snapshots keyed by snapshot name.
    #[serde(default)]
    pub snapshots: HashMap<String, SnapshotView>,
    /// Subnet groups keyed by subnet group name.
    #[serde(default)]
    pub subnet_groups: HashMap<String, SubnetGroupView>,
    /// ACLs keyed by ACL name.
    #[serde(default)]
    pub acls: HashMap<String, AclView>,
    /// Tags keyed by resource ARN.
    #[serde(default)]
    pub tags: HashMap<String, Vec<TagView>>,
}

/// Serializable view of a MemoryDB cluster.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterView {
    pub name: String,
    pub arn: String,
    pub status: String,
    pub node_type: String,
    pub num_shards: i32,
    pub num_replicas_per_shard: i32,
    pub description: String,
    pub engine: String,
    pub engine_version: String,
    pub subnet_group_name: String,
    pub security_group_ids: Vec<String>,
    pub maintenance_window: String,
    pub snapshot_retention_limit: i32,
    pub snapshot_window: String,
    pub acl_name: String,
    pub parameter_group_name: String,
    pub tls_enabled: bool,
    pub auto_minor_version_upgrade: bool,
    pub creation_time: Option<String>,
}

/// Serializable view of a MemoryDB snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotView {
    pub name: String,
    pub arn: String,
    pub status: String,
    pub source: String,
    pub cluster_name: String,
    pub cluster_description: String,
    pub cluster_engine: String,
    pub cluster_engine_version: String,
    pub cluster_node_type: String,
    pub cluster_num_shards: i32,
    pub cluster_subnet_group_name: String,
    pub cluster_snapshot_retention_limit: i32,
    pub cluster_snapshot_window: String,
    pub cluster_maintenance_window: String,
    pub cluster_parameter_group_name: String,
    pub kms_key_id: Option<String>,
}

/// Serializable view of a MemoryDB subnet group.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubnetGroupView {
    pub name: String,
    pub arn: String,
    pub description: String,
    pub vpc_id: String,
    pub subnet_ids: Vec<String>,
}

/// Serializable view of a MemoryDB ACL.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AclView {
    pub acl_name: String,
    pub status: String,
    pub user_names: Vec<String>,
    pub minimum_engine_version: String,
    pub arn: String,
    #[serde(default)]
    pub tags: Vec<TagView>,
}

/// Serializable view of a tag key-value pair.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TagView {
    pub key: String,
    pub value: String,
}

// ---------------------------------------------------------------------------
// From conversions
// ---------------------------------------------------------------------------

impl From<&Cluster> for ClusterView {
    fn from(c: &Cluster) -> Self {
        ClusterView {
            name: c.name.clone(),
            arn: c.arn.clone(),
            status: c.status.clone(),
            node_type: c.node_type.clone(),
            num_shards: c.num_shards,
            num_replicas_per_shard: c.num_replicas_per_shard,
            description: c.description.clone(),
            engine: c.engine.clone(),
            engine_version: c.engine_version.clone(),
            subnet_group_name: c.subnet_group_name.clone(),
            security_group_ids: c.security_group_ids.clone(),
            maintenance_window: c.maintenance_window.clone(),
            snapshot_retention_limit: c.snapshot_retention_limit,
            snapshot_window: c.snapshot_window.clone(),
            acl_name: c.acl_name.clone(),
            parameter_group_name: c.parameter_group_name.clone(),
            tls_enabled: c.tls_enabled,
            auto_minor_version_upgrade: c.auto_minor_version_upgrade,
            creation_time: Some(c.creation_time.to_rfc3339()),
        }
    }
}

impl From<ClusterView> for Cluster {
    fn from(v: ClusterView) -> Self {
        let creation_time = v
            .creation_time
            .as_deref()
            .and_then(|s| DateTime::parse_from_rfc3339(s).ok())
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or_else(Utc::now);
        Cluster {
            name: v.name,
            arn: v.arn,
            status: v.status,
            node_type: v.node_type,
            num_shards: v.num_shards,
            num_replicas_per_shard: v.num_replicas_per_shard,
            description: v.description,
            engine: v.engine,
            engine_version: v.engine_version,
            subnet_group_name: v.subnet_group_name,
            security_group_ids: v.security_group_ids,
            maintenance_window: v.maintenance_window,
            snapshot_retention_limit: v.snapshot_retention_limit,
            snapshot_window: v.snapshot_window,
            acl_name: v.acl_name,
            parameter_group_name: v.parameter_group_name,
            tls_enabled: v.tls_enabled,
            auto_minor_version_upgrade: v.auto_minor_version_upgrade,
            creation_time,
        }
    }
}

impl From<&SnapshotData> for SnapshotView {
    fn from(s: &SnapshotData) -> Self {
        SnapshotView {
            name: s.name.clone(),
            arn: s.arn.clone(),
            status: s.status.clone(),
            source: s.source.clone(),
            cluster_name: s.cluster_name.clone(),
            cluster_description: s.cluster_description.clone(),
            cluster_engine: s.cluster_engine.clone(),
            cluster_engine_version: s.cluster_engine_version.clone(),
            cluster_node_type: s.cluster_node_type.clone(),
            cluster_num_shards: s.cluster_num_shards,
            cluster_subnet_group_name: s.cluster_subnet_group_name.clone(),
            cluster_snapshot_retention_limit: s.cluster_snapshot_retention_limit,
            cluster_snapshot_window: s.cluster_snapshot_window.clone(),
            cluster_maintenance_window: s.cluster_maintenance_window.clone(),
            cluster_parameter_group_name: s.cluster_parameter_group_name.clone(),
            kms_key_id: s.kms_key_id.clone(),
        }
    }
}

impl From<SnapshotView> for SnapshotData {
    fn from(v: SnapshotView) -> Self {
        SnapshotData {
            name: v.name,
            arn: v.arn,
            status: v.status,
            source: v.source,
            cluster_name: v.cluster_name,
            cluster_description: v.cluster_description,
            cluster_engine: v.cluster_engine,
            cluster_engine_version: v.cluster_engine_version,
            cluster_node_type: v.cluster_node_type,
            cluster_num_shards: v.cluster_num_shards,
            cluster_subnet_group_name: v.cluster_subnet_group_name,
            cluster_snapshot_retention_limit: v.cluster_snapshot_retention_limit,
            cluster_snapshot_window: v.cluster_snapshot_window,
            cluster_maintenance_window: v.cluster_maintenance_window,
            cluster_parameter_group_name: v.cluster_parameter_group_name,
            kms_key_id: v.kms_key_id,
        }
    }
}

impl From<&SubnetGroupData> for SubnetGroupView {
    fn from(sg: &SubnetGroupData) -> Self {
        SubnetGroupView {
            name: sg.name.clone(),
            arn: sg.arn.clone(),
            description: sg.description.clone(),
            vpc_id: sg.vpc_id.clone(),
            subnet_ids: sg.subnet_ids.clone(),
        }
    }
}

impl From<SubnetGroupView> for SubnetGroupData {
    fn from(v: SubnetGroupView) -> Self {
        SubnetGroupData {
            name: v.name,
            arn: v.arn,
            description: v.description,
            vpc_id: v.vpc_id,
            subnet_ids: v.subnet_ids,
        }
    }
}

impl From<&TagData> for TagView {
    fn from(t: &TagData) -> Self {
        TagView {
            key: t.key.clone(),
            value: t.value.clone(),
        }
    }
}

impl From<TagView> for TagData {
    fn from(v: TagView) -> Self {
        TagData {
            key: v.key,
            value: v.value,
        }
    }
}

impl From<&AclData> for AclView {
    fn from(a: &AclData) -> Self {
        AclView {
            acl_name: a.acl_name.clone(),
            status: a.status.clone(),
            user_names: a.user_names.clone(),
            minimum_engine_version: a.minimum_engine_version.clone(),
            arn: a.arn.clone(),
            tags: a.tags.iter().map(TagView::from).collect(),
        }
    }
}

impl From<AclView> for AclData {
    fn from(v: AclView) -> Self {
        AclData {
            acl_name: v.acl_name,
            status: v.status,
            user_names: v.user_names,
            minimum_engine_version: v.minimum_engine_version,
            arn: v.arn,
            tags: v.tags.into_iter().map(TagData::from).collect(),
        }
    }
}

fn tags_map_to_view(tags: &TagsMap) -> HashMap<String, Vec<TagView>> {
    tags.iter()
        .map(|(arn, ts)| (arn.clone(), ts.iter().map(TagView::from).collect()))
        .collect()
}

fn view_to_tags_map(tags: HashMap<String, Vec<TagView>>) -> TagsMap {
    tags.into_iter()
        .map(|(arn, ts)| (arn, ts.into_iter().map(TagData::from).collect()))
        .collect()
}

// ---------------------------------------------------------------------------
// StatefulService implementation
// ---------------------------------------------------------------------------

impl StatefulService for MemoryDbService {
    type StateView = MemoryDbStateView;

    async fn snapshot(&self, account_id: &str, region: &str) -> Self::StateView {
        let state = self.state.get(account_id, region);
        let guard = state.read().await;

        let clusters = guard
            .clusters
            .iter()
            .map(|(k, v)| (k.clone(), ClusterView::from(v)))
            .collect();
        let snapshots = guard
            .snapshots
            .iter()
            .map(|(k, v)| (k.clone(), SnapshotView::from(v)))
            .collect();
        let subnet_groups = guard
            .subnet_groups
            .iter()
            .map(|(k, v)| (k.clone(), SubnetGroupView::from(v)))
            .collect();
        let acls = guard
            .acls
            .iter()
            .map(|(k, v)| (k.clone(), AclView::from(v)))
            .collect();
        let tags = tags_map_to_view(&guard.tags);

        MemoryDbStateView {
            clusters,
            snapshots,
            subnet_groups,
            acls,
            tags,
        }
    }

    async fn restore(
        &self,
        account_id: &str,
        region: &str,
        view: Self::StateView,
    ) -> Result<(), StateViewError> {
        let new_state = MemoryDbState {
            clusters: view
                .clusters
                .into_values()
                .map(|v| {
                    let c = Cluster::from(v);
                    (c.name.clone(), c)
                })
                .collect(),
            snapshots: view
                .snapshots
                .into_values()
                .map(|v| {
                    let s = SnapshotData::from(v);
                    (s.name.clone(), s)
                })
                .collect(),
            subnet_groups: view
                .subnet_groups
                .into_values()
                .map(|v| {
                    let sg = SubnetGroupData::from(v);
                    (sg.name.clone(), sg)
                })
                .collect(),
            acls: view
                .acls
                .into_values()
                .map(|v| {
                    let a = AclData::from(v);
                    (a.acl_name.clone(), a)
                })
                .collect(),
            tags: view_to_tags_map(view.tags),
        };

        {
            let state = self.state.get(account_id, region);
            *state.write().await = new_state;
        }
        self.notify_state_changed(account_id, region).await;
        Ok(())
    }

    async fn merge(
        &self,
        account_id: &str,
        region: &str,
        view: Self::StateView,
    ) -> Result<(), StateViewError> {
        let state = self.state.get(account_id, region);
        {
            let mut guard = state.write().await;
            for v in view.clusters.into_values() {
                let c = Cluster::from(v);
                guard.clusters.insert(c.name.clone(), c);
            }
            for v in view.snapshots.into_values() {
                let s = SnapshotData::from(v);
                guard.snapshots.insert(s.name.clone(), s);
            }
            for v in view.subnet_groups.into_values() {
                let sg = SubnetGroupData::from(v);
                guard.subnet_groups.insert(sg.name.clone(), sg);
            }
            for v in view.acls.into_values() {
                let a = AclData::from(v);
                guard.acls.insert(a.acl_name.clone(), a);
            }
            for (arn, ts) in view_to_tags_map(view.tags) {
                guard.tags.insert(arn, ts);
            }
        }
        self.notify_state_changed(account_id, region).await;
        Ok(())
    }

    fn notifier(&self) -> &StateChangeNotifier<Self::StateView> {
        &self.notifier
    }
}