winterbaume-memorydb 0.2.0

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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::collections::HashMap;

use chrono::Utc;
use thiserror::Error;

use crate::types::*;

/// In-memory state for the MemoryDB service.
#[derive(Debug, Default)]
pub struct MemoryDbState {
    /// Clusters keyed by cluster name.
    pub clusters: HashMap<String, Cluster>,
    /// Snapshots keyed by snapshot name.
    pub snapshots: HashMap<String, SnapshotData>,
    /// Subnet groups keyed by subnet group name.
    pub subnet_groups: HashMap<String, SubnetGroupData>,
    /// ACLs keyed by ACL name.
    pub acls: HashMap<String, AclData>,
    /// Tags keyed by resource ARN.
    pub tags: TagsMap,
}

/// Error type for MemoryDB operations.
#[derive(Debug, Error)]
pub enum MemoryDbError {
    #[error("Cluster {0} already exists.")]
    ClusterAlreadyExists(String),
    #[error("Cluster {0} not found.")]
    ClusterNotFound(String),
    #[error("Snapshot {0} already exists.")]
    SnapshotAlreadyExists(String),
    #[error("Snapshot {0} not found.")]
    SnapshotNotFound(String),
    #[error("Subnet group {0} already exists.")]
    SubnetGroupAlreadyExists(String),
    #[error("Subnet group {0} not found.")]
    SubnetGroupNotFound(String),
    #[error("Subnet group {0} is in use and cannot be deleted.")]
    SubnetGroupInUse(String),
    #[error("The default subnet group cannot be deleted.")]
    DefaultSubnetGroupCannotBeDeleted,
    #[error("Tag with key {0} not found.")]
    TagNotFound(String),
    #[error("The resource with ARN {0} was not found.")]
    InvalidArn(String),
}

impl MemoryDbState {
    pub fn create_cluster(
        &mut self,
        account_id: &str,
        region: &str,
        name: &str,
        node_type: &str,
        acl_name: &str,
        description: Option<&str>,
        num_shards: Option<i32>,
        num_replicas_per_shard: Option<i32>,
        subnet_group_name: Option<&str>,
        security_group_ids: Vec<String>,
        engine_version: Option<&str>,
        maintenance_window: Option<&str>,
        snapshot_retention_limit: Option<i32>,
        snapshot_window: Option<&str>,
        parameter_group_name: Option<&str>,
        tls_enabled: Option<bool>,
        auto_minor_version_upgrade: Option<bool>,
        tags: Vec<TagData>,
    ) -> Result<&Cluster, MemoryDbError> {
        if self.clusters.contains_key(name) {
            return Err(MemoryDbError::ClusterAlreadyExists(name.to_string()));
        }

        let arn = format!("arn:aws:memorydb:{region}:{account_id}:cluster/{name}");
        let cluster = Cluster {
            name: name.to_string(),
            arn: arn.clone(),
            status: "available".to_string(),
            node_type: node_type.to_string(),
            num_shards: num_shards.unwrap_or(1),
            num_replicas_per_shard: num_replicas_per_shard.unwrap_or(1),
            description: description.unwrap_or("").to_string(),
            engine: "redis".to_string(),
            engine_version: engine_version.unwrap_or("7.1").to_string(),
            subnet_group_name: subnet_group_name.unwrap_or("default").to_string(),
            security_group_ids,
            maintenance_window: maintenance_window
                .unwrap_or("wed:03:00-wed:04:00")
                .to_string(),
            snapshot_retention_limit: snapshot_retention_limit.unwrap_or(0),
            snapshot_window: snapshot_window.unwrap_or("05:00-06:00").to_string(),
            acl_name: acl_name.to_string(),
            parameter_group_name: parameter_group_name
                .unwrap_or("default.memorydb-redis7")
                .to_string(),
            tls_enabled: tls_enabled.unwrap_or(true),
            auto_minor_version_upgrade: auto_minor_version_upgrade.unwrap_or(true),
            creation_time: Utc::now(),
        };

        self.clusters.insert(name.to_string(), cluster);
        if !tags.is_empty() {
            self.tags.insert(arn, tags);
        }
        Ok(self.clusters.get(name).unwrap())
    }

    pub fn describe_clusters(
        &self,
        cluster_name: Option<&str>,
    ) -> Result<Vec<&Cluster>, MemoryDbError> {
        match cluster_name {
            Some(name) => {
                if let Some(cluster) = self.clusters.get(name) {
                    Ok(vec![cluster])
                } else {
                    Err(MemoryDbError::ClusterNotFound(name.to_string()))
                }
            }
            None => Ok(self.clusters.values().collect()),
        }
    }

    pub fn delete_cluster(
        &mut self,
        name: &str,
        final_snapshot_name: Option<&str>,
        account_id: &str,
        region: &str,
    ) -> Result<Cluster, MemoryDbError> {
        if !self.clusters.contains_key(name) {
            return Err(MemoryDbError::ClusterNotFound(name.to_string()));
        }

        // Create final snapshot if requested (before removing cluster)
        if let Some(snap_name) = final_snapshot_name {
            self.create_snapshot(account_id, region, snap_name, name, None, vec![])?;
        }

        let mut cluster = self.clusters.remove(name).unwrap();
        cluster.status = "deleting".to_string();
        Ok(cluster)
    }

    pub fn update_cluster(
        &mut self,
        name: &str,
        description: Option<&str>,
        security_group_ids: Option<Vec<String>>,
        maintenance_window: Option<&str>,
        snapshot_retention_limit: Option<i32>,
        snapshot_window: Option<&str>,
        parameter_group_name: Option<&str>,
        engine_version: Option<&str>,
        num_shards: Option<i32>,
        num_replicas_per_shard: Option<i32>,
        node_type: Option<&str>,
        acl_name: Option<&str>,
    ) -> Result<&Cluster, MemoryDbError> {
        let cluster = self
            .clusters
            .get_mut(name)
            .ok_or_else(|| MemoryDbError::ClusterNotFound(name.to_string()))?;

        if let Some(desc) = description {
            cluster.description = desc.to_string();
        }
        if let Some(sgs) = security_group_ids {
            cluster.security_group_ids = sgs;
        }
        if let Some(mw) = maintenance_window {
            cluster.maintenance_window = mw.to_string();
        }
        if let Some(srl) = snapshot_retention_limit {
            cluster.snapshot_retention_limit = srl;
        }
        if let Some(sw) = snapshot_window {
            cluster.snapshot_window = sw.to_string();
        }
        if let Some(pgn) = parameter_group_name {
            cluster.parameter_group_name = pgn.to_string();
        }
        if let Some(ev) = engine_version {
            cluster.engine_version = ev.to_string();
        }
        if let Some(ns) = num_shards {
            cluster.num_shards = ns;
        }
        if let Some(nr) = num_replicas_per_shard {
            cluster.num_replicas_per_shard = nr;
        }
        if let Some(nt) = node_type {
            cluster.node_type = nt.to_string();
        }
        if let Some(acl) = acl_name {
            cluster.acl_name = acl.to_string();
        }

        Ok(self.clusters.get(name).unwrap())
    }

    // --- Snapshot operations ---

    pub fn create_snapshot(
        &mut self,
        account_id: &str,
        region: &str,
        snapshot_name: &str,
        cluster_name: &str,
        kms_key_id: Option<&str>,
        tags: Vec<TagData>,
    ) -> Result<&SnapshotData, MemoryDbError> {
        if self.snapshots.contains_key(snapshot_name) {
            return Err(MemoryDbError::SnapshotAlreadyExists(
                snapshot_name.to_string(),
            ));
        }

        let cluster = self
            .clusters
            .get(cluster_name)
            .ok_or_else(|| MemoryDbError::ClusterNotFound(cluster_name.to_string()))?;

        let arn = format!("arn:aws:memorydb:{region}:{account_id}:snapshot/{snapshot_name}");
        let snapshot = SnapshotData {
            name: snapshot_name.to_string(),
            arn: arn.clone(),
            status: "available".to_string(),
            source: "manual".to_string(),
            cluster_name: cluster.name.clone(),
            cluster_description: cluster.description.clone(),
            cluster_engine: cluster.engine.clone(),
            cluster_engine_version: cluster.engine_version.clone(),
            cluster_node_type: cluster.node_type.clone(),
            cluster_num_shards: cluster.num_shards,
            cluster_subnet_group_name: cluster.subnet_group_name.clone(),
            cluster_snapshot_retention_limit: cluster.snapshot_retention_limit,
            cluster_snapshot_window: cluster.snapshot_window.clone(),
            cluster_maintenance_window: cluster.maintenance_window.clone(),
            cluster_parameter_group_name: cluster.parameter_group_name.clone(),
            kms_key_id: kms_key_id.map(|s| s.to_string()),
        };

        self.snapshots.insert(snapshot_name.to_string(), snapshot);
        if !tags.is_empty() {
            self.tags.insert(arn, tags);
        }
        Ok(self.snapshots.get(snapshot_name).unwrap())
    }

    pub fn delete_snapshot(&mut self, snapshot_name: &str) -> Result<SnapshotData, MemoryDbError> {
        match self.snapshots.remove(snapshot_name) {
            Some(mut snapshot) => {
                // Clean up tags
                self.tags.remove(&snapshot.arn);
                snapshot.status = "deleting".to_string();
                Ok(snapshot)
            }
            None => Err(MemoryDbError::SnapshotNotFound(snapshot_name.to_string())),
        }
    }

    pub fn describe_snapshots(
        &self,
        cluster_name: Option<&str>,
        snapshot_name: Option<&str>,
        source: Option<&str>,
    ) -> Result<Vec<&SnapshotData>, MemoryDbError> {
        // When snapshot_name is specified, filter by it (and also by cluster if given)
        if let Some(name) = snapshot_name {
            if let Some(snapshot) = self.snapshots.get(name) {
                // If cluster_name is also given, verify it matches
                if let Some(cn) = cluster_name
                    && snapshot.cluster_name != cn
                {
                    return Err(MemoryDbError::SnapshotNotFound(name.to_string()));
                }
                return Ok(vec![snapshot]);
            } else {
                return Err(MemoryDbError::SnapshotNotFound(name.to_string()));
            }
        }

        let mut results: Vec<&SnapshotData> = self.snapshots.values().collect();

        if let Some(cn) = cluster_name {
            results.retain(|s| s.cluster_name == cn);
        }
        if let Some(src) = source {
            results.retain(|s| s.source == src);
        }

        Ok(results)
    }

    // --- Subnet group operations ---

    pub fn create_subnet_group(
        &mut self,
        account_id: &str,
        region: &str,
        name: &str,
        description: Option<&str>,
        subnet_ids: Vec<String>,
        tags: Vec<TagData>,
    ) -> Result<&SubnetGroupData, MemoryDbError> {
        if self.subnet_groups.contains_key(name) {
            return Err(MemoryDbError::SubnetGroupAlreadyExists(name.to_string()));
        }

        let arn = format!("arn:aws:memorydb:{region}:{account_id}:subnetgroup/{name}");
        let subnet_group = SubnetGroupData {
            name: name.to_string(),
            arn: arn.clone(),
            description: description.unwrap_or("").to_string(),
            vpc_id: format!(
                "vpc-{:012x}",
                name.as_bytes()
                    .iter()
                    .fold(0u64, |acc, &b| acc.wrapping_mul(31).wrapping_add(b as u64))
            ),
            subnet_ids,
        };

        self.subnet_groups.insert(name.to_string(), subnet_group);
        if !tags.is_empty() {
            self.tags.insert(arn, tags);
        }
        Ok(self.subnet_groups.get(name).unwrap())
    }

    pub fn delete_subnet_group(&mut self, name: &str) -> Result<SubnetGroupData, MemoryDbError> {
        if name == "default" {
            return Err(MemoryDbError::DefaultSubnetGroupCannotBeDeleted);
        }

        if !self.subnet_groups.contains_key(name) {
            return Err(MemoryDbError::SubnetGroupNotFound(name.to_string()));
        }

        // Check if the subnet group is in use by any cluster
        let in_use = self.clusters.values().any(|c| c.subnet_group_name == name);
        if in_use {
            return Err(MemoryDbError::SubnetGroupInUse(name.to_string()));
        }

        let sg = self.subnet_groups.remove(name).unwrap();
        self.tags.remove(&sg.arn);
        Ok(sg)
    }

    pub fn describe_subnet_groups(
        &self,
        name: Option<&str>,
    ) -> Result<Vec<&SubnetGroupData>, MemoryDbError> {
        match name {
            Some(n) => {
                if let Some(sg) = self.subnet_groups.get(n) {
                    Ok(vec![sg])
                } else {
                    Err(MemoryDbError::SubnetGroupNotFound(n.to_string()))
                }
            }
            None => Ok(self.subnet_groups.values().collect()),
        }
    }

    // --- Tag operations ---

    pub fn tag_resource(
        &mut self,
        resource_arn: &str,
        tags: Vec<TagData>,
    ) -> Result<Vec<TagData>, MemoryDbError> {
        // Validate that the resource exists
        self.validate_resource_arn(resource_arn)?;

        let existing = self.tags.entry(resource_arn.to_string()).or_default();
        for tag in tags {
            // Update existing tag or add new one
            if let Some(pos) = existing.iter().position(|t| t.key == tag.key) {
                existing[pos] = tag;
            } else {
                existing.push(tag);
            }
        }
        Ok(existing.clone())
    }

    pub fn untag_resource(
        &mut self,
        resource_arn: &str,
        tag_keys: &[String],
    ) -> Result<Vec<TagData>, MemoryDbError> {
        self.validate_resource_arn(resource_arn)?;

        let existing = self.tags.entry(resource_arn.to_string()).or_default();

        // Validate that all tag keys exist
        for key in tag_keys {
            if !existing.iter().any(|t| &t.key == key) {
                return Err(MemoryDbError::TagNotFound(key.clone()));
            }
        }

        existing.retain(|t| !tag_keys.contains(&t.key));
        Ok(existing.clone())
    }

    pub fn list_tags(&self, resource_arn: &str) -> Result<Vec<TagData>, MemoryDbError> {
        self.validate_resource_arn(resource_arn)?;

        Ok(self.tags.get(resource_arn).cloned().unwrap_or_default())
    }

    fn validate_resource_arn(&self, arn: &str) -> Result<(), MemoryDbError> {
        // Parse the resource type from the ARN to return a type-specific error
        // ARN format: arn:aws:memorydb:region:account:resource-type/name
        if let Some(resource_part) = arn.rsplit(':').next() {
            if resource_part.starts_with("cluster/") {
                let name = resource_part.strip_prefix("cluster/").unwrap_or("");
                let is_cluster = self.clusters.values().any(|c| c.arn == arn);
                if is_cluster {
                    return Ok(());
                }
                return Err(MemoryDbError::ClusterNotFound(name.to_string()));
            } else if resource_part.starts_with("snapshot/") {
                let name = resource_part.strip_prefix("snapshot/").unwrap_or("");
                let is_snapshot = self.snapshots.values().any(|s| s.arn == arn);
                if is_snapshot {
                    return Ok(());
                }
                return Err(MemoryDbError::SnapshotNotFound(name.to_string()));
            } else if resource_part.starts_with("subnetgroup/") {
                let name = resource_part.strip_prefix("subnetgroup/").unwrap_or("");
                let is_subnet_group = self.subnet_groups.values().any(|sg| sg.arn == arn);
                if is_subnet_group {
                    return Ok(());
                }
                return Err(MemoryDbError::SubnetGroupNotFound(name.to_string()));
            }
        }

        // Check all resources as fallback
        let is_cluster = self.clusters.values().any(|c| c.arn == arn);
        let is_snapshot = self.snapshots.values().any(|s| s.arn == arn);
        let is_subnet_group = self.subnet_groups.values().any(|sg| sg.arn == arn);

        if is_cluster || is_snapshot || is_subnet_group {
            Ok(())
        } else {
            Err(MemoryDbError::InvalidArn(arn.to_string()))
        }
    }
}