fakecloud-elasticache 0.9.2

Amazon ElastiCache implementation for FakeCloud
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use fakecloud_aws::arn::Arn;
use parking_lot::RwLock;

pub type SharedElastiCacheState = Arc<RwLock<ElastiCacheState>>;

#[derive(Debug, Clone)]
pub struct CacheEngineVersion {
    pub engine: String,
    pub engine_version: String,
    pub cache_parameter_group_family: String,
    pub cache_engine_description: String,
    pub cache_engine_version_description: String,
}

#[derive(Debug, Clone)]
pub struct CacheParameterGroup {
    pub cache_parameter_group_name: String,
    pub cache_parameter_group_family: String,
    pub description: String,
    pub is_global: bool,
    pub arn: String,
}

#[derive(Debug, Clone)]
pub struct EngineDefaultParameter {
    pub parameter_name: String,
    pub parameter_value: String,
    pub description: String,
    pub source: String,
    pub data_type: String,
    pub allowed_values: String,
    pub is_modifiable: bool,
    pub minimum_engine_version: String,
}

#[derive(Debug, Clone)]
pub struct CacheSubnetGroup {
    pub cache_subnet_group_name: String,
    pub cache_subnet_group_description: String,
    pub vpc_id: String,
    pub subnet_ids: Vec<String>,
    pub arn: String,
}

#[derive(Debug, Clone)]
pub struct RecurringCharge {
    pub recurring_charge_amount: f64,
    pub recurring_charge_frequency: String,
}

#[derive(Debug, Clone)]
pub struct ReservedCacheNode {
    pub reserved_cache_node_id: String,
    pub reserved_cache_nodes_offering_id: String,
    pub cache_node_type: String,
    pub start_time: String,
    pub duration: i32,
    pub fixed_price: f64,
    pub usage_price: f64,
    pub cache_node_count: i32,
    pub product_description: String,
    pub offering_type: String,
    pub state: String,
    pub recurring_charges: Vec<RecurringCharge>,
    pub reservation_arn: String,
}

#[derive(Debug, Clone)]
pub struct ReservedCacheNodesOffering {
    pub reserved_cache_nodes_offering_id: String,
    pub cache_node_type: String,
    pub duration: i32,
    pub fixed_price: f64,
    pub usage_price: f64,
    pub product_description: String,
    pub offering_type: String,
    pub recurring_charges: Vec<RecurringCharge>,
}

#[derive(Debug, Clone)]
pub struct CacheCluster {
    pub cache_cluster_id: String,
    pub cache_node_type: String,
    pub engine: String,
    pub engine_version: String,
    pub cache_cluster_status: String,
    pub num_cache_nodes: i32,
    pub preferred_availability_zone: String,
    pub cache_subnet_group_name: Option<String>,
    pub auto_minor_version_upgrade: bool,
    pub arn: String,
    pub created_at: String,
    pub endpoint_address: String,
    pub endpoint_port: u16,
    pub container_id: String,
    pub host_port: u16,
    pub replication_group_id: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ReplicationGroup {
    pub replication_group_id: String,
    pub description: String,
    pub global_replication_group_id: Option<String>,
    pub global_replication_group_role: Option<String>,
    pub status: String,
    pub cache_node_type: String,
    pub engine: String,
    pub engine_version: String,
    pub num_cache_clusters: i32,
    pub automatic_failover_enabled: bool,
    pub endpoint_address: String,
    pub endpoint_port: u16,
    pub arn: String,
    pub created_at: String,
    pub container_id: String,
    pub host_port: u16,
    pub member_clusters: Vec<String>,
    pub snapshot_retention_limit: i32,
    pub snapshot_window: String,
}

#[derive(Debug, Clone)]
pub struct GlobalReplicationGroupMember {
    pub replication_group_id: String,
    pub replication_group_region: String,
    pub role: String,
    pub automatic_failover: bool,
    pub status: String,
}

#[derive(Debug, Clone)]
pub struct GlobalReplicationGroup {
    pub global_replication_group_id: String,
    pub global_replication_group_description: String,
    pub status: String,
    pub cache_node_type: String,
    pub engine: String,
    pub engine_version: String,
    pub members: Vec<GlobalReplicationGroupMember>,
    pub cluster_enabled: bool,
    pub arn: String,
}

#[derive(Debug, Clone)]
pub struct ElastiCacheUser {
    pub user_id: String,
    pub user_name: String,
    pub engine: String,
    pub access_string: String,
    pub status: String,
    pub authentication_type: String,
    pub password_count: i32,
    pub arn: String,
    pub minimum_engine_version: String,
    pub user_group_ids: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct ElastiCacheUserGroup {
    pub user_group_id: String,
    pub engine: String,
    pub status: String,
    pub user_ids: Vec<String>,
    pub arn: String,
    pub minimum_engine_version: String,
    pub pending_changes: Option<UserGroupPendingChanges>,
    pub replication_groups: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct UserGroupPendingChanges {
    pub user_ids_to_add: Vec<String>,
    pub user_ids_to_remove: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct CacheSnapshot {
    pub snapshot_name: String,
    pub replication_group_id: String,
    pub replication_group_description: String,
    pub snapshot_status: String,
    pub cache_node_type: String,
    pub engine: String,
    pub engine_version: String,
    pub num_cache_clusters: i32,
    pub arn: String,
    pub created_at: String,
    pub snapshot_source: String,
}

#[derive(Debug, Clone, Default)]
pub struct ServerlessCacheUsageLimits {
    pub data_storage: Option<ServerlessCacheDataStorage>,
    pub ecpu_per_second: Option<ServerlessCacheEcpuPerSecond>,
}

#[derive(Debug, Clone)]
pub struct ServerlessCacheDataStorage {
    pub maximum: Option<i32>,
    pub minimum: Option<i32>,
    pub unit: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ServerlessCacheEcpuPerSecond {
    pub maximum: Option<i32>,
    pub minimum: Option<i32>,
}

#[derive(Debug, Clone)]
pub struct ServerlessCacheEndpoint {
    pub address: String,
    pub port: u16,
}

#[derive(Debug, Clone)]
pub struct ServerlessCache {
    pub serverless_cache_name: String,
    pub description: String,
    pub engine: String,
    pub major_engine_version: String,
    pub full_engine_version: String,
    pub status: String,
    pub endpoint: ServerlessCacheEndpoint,
    pub reader_endpoint: ServerlessCacheEndpoint,
    pub arn: String,
    pub created_at: String,
    pub cache_usage_limits: Option<ServerlessCacheUsageLimits>,
    pub security_group_ids: Vec<String>,
    pub subnet_ids: Vec<String>,
    pub kms_key_id: Option<String>,
    pub user_group_id: Option<String>,
    pub snapshot_retention_limit: Option<i32>,
    pub daily_snapshot_time: Option<String>,
    pub container_id: String,
    pub host_port: u16,
}

#[derive(Debug, Clone)]
pub struct ServerlessCacheSnapshot {
    pub serverless_cache_snapshot_name: String,
    pub arn: String,
    pub kms_key_id: Option<String>,
    pub snapshot_type: String,
    pub status: String,
    pub create_time: String,
    pub expiry_time: Option<String>,
    pub bytes_used_for_cache: Option<String>,
    pub serverless_cache_name: String,
    pub engine: String,
    pub major_engine_version: String,
}

#[derive(Debug)]
pub struct ElastiCacheState {
    pub account_id: String,
    pub region: String,
    pub parameter_groups: Vec<CacheParameterGroup>,
    pub subnet_groups: HashMap<String, CacheSubnetGroup>,
    pub reserved_cache_nodes: HashMap<String, ReservedCacheNode>,
    pub reserved_cache_nodes_offerings: Vec<ReservedCacheNodesOffering>,
    pub cache_clusters: HashMap<String, CacheCluster>,
    pub replication_groups: HashMap<String, ReplicationGroup>,
    pub global_replication_groups: HashMap<String, GlobalReplicationGroup>,
    pub users: HashMap<String, ElastiCacheUser>,
    pub user_groups: HashMap<String, ElastiCacheUserGroup>,
    pub snapshots: HashMap<String, CacheSnapshot>,
    pub serverless_caches: HashMap<String, ServerlessCache>,
    pub serverless_cache_snapshots: HashMap<String, ServerlessCacheSnapshot>,
    pub tags: HashMap<String, Vec<(String, String)>>,
    in_progress_cache_cluster_ids: HashSet<String>,
    in_progress_replication_group_ids: HashSet<String>,
    in_progress_serverless_cache_names: HashSet<String>,
}

impl ElastiCacheState {
    pub fn new(account_id: &str, region: &str) -> Self {
        let parameter_groups = default_parameter_groups(account_id, region);
        let subnet_groups = default_subnet_groups(account_id, region);
        let users = default_users(account_id, region);
        let mut tags: HashMap<String, Vec<(String, String)>> = subnet_groups
            .values()
            .map(|g| (g.arn.clone(), Vec::new()))
            .collect();
        for user in users.values() {
            tags.insert(user.arn.clone(), Vec::new());
        }
        Self {
            account_id: account_id.to_string(),
            region: region.to_string(),
            parameter_groups,
            subnet_groups,
            reserved_cache_nodes: HashMap::new(),
            reserved_cache_nodes_offerings: default_reserved_cache_nodes_offerings(),
            cache_clusters: HashMap::new(),
            replication_groups: HashMap::new(),
            global_replication_groups: HashMap::new(),
            users,
            user_groups: HashMap::new(),
            snapshots: HashMap::new(),
            serverless_caches: HashMap::new(),
            serverless_cache_snapshots: HashMap::new(),
            tags,
            in_progress_cache_cluster_ids: HashSet::new(),
            in_progress_replication_group_ids: HashSet::new(),
            in_progress_serverless_cache_names: HashSet::new(),
        }
    }

    pub fn reset(&mut self) {
        self.parameter_groups = default_parameter_groups(&self.account_id, &self.region);
        self.subnet_groups = default_subnet_groups(&self.account_id, &self.region);
        self.reserved_cache_nodes.clear();
        self.reserved_cache_nodes_offerings = default_reserved_cache_nodes_offerings();
        self.cache_clusters.clear();
        self.replication_groups.clear();
        self.global_replication_groups.clear();
        self.users = default_users(&self.account_id, &self.region);
        self.user_groups.clear();
        self.snapshots.clear();
        self.serverless_caches.clear();
        self.serverless_cache_snapshots.clear();
        self.tags.clear();
        for g in self.subnet_groups.values() {
            self.tags.insert(g.arn.clone(), Vec::new());
        }
        for user in self.users.values() {
            self.tags.insert(user.arn.clone(), Vec::new());
        }
        self.in_progress_cache_cluster_ids.clear();
        self.in_progress_replication_group_ids.clear();
        self.in_progress_serverless_cache_names.clear();
    }

    pub fn begin_cache_cluster_creation(&mut self, cache_cluster_id: &str) -> bool {
        if self.cache_clusters.contains_key(cache_cluster_id)
            || self
                .in_progress_cache_cluster_ids
                .contains(cache_cluster_id)
        {
            return false;
        }
        self.in_progress_cache_cluster_ids
            .insert(cache_cluster_id.to_string());
        true
    }

    pub fn finish_cache_cluster_creation(&mut self, cluster: CacheCluster) {
        self.in_progress_cache_cluster_ids
            .remove(&cluster.cache_cluster_id);
        self.tags.insert(cluster.arn.clone(), Vec::new());
        self.cache_clusters
            .insert(cluster.cache_cluster_id.clone(), cluster);
    }

    pub fn cancel_cache_cluster_creation(&mut self, cache_cluster_id: &str) {
        self.in_progress_cache_cluster_ids.remove(cache_cluster_id);
    }

    pub fn begin_replication_group_creation(&mut self, replication_group_id: &str) -> bool {
        if self.replication_groups.contains_key(replication_group_id)
            || self
                .in_progress_replication_group_ids
                .contains(replication_group_id)
        {
            return false;
        }
        self.in_progress_replication_group_ids
            .insert(replication_group_id.to_string());
        true
    }

    pub fn finish_replication_group_creation(&mut self, group: ReplicationGroup) {
        self.in_progress_replication_group_ids
            .remove(&group.replication_group_id);
        self.tags.insert(group.arn.clone(), Vec::new());
        self.replication_groups
            .insert(group.replication_group_id.clone(), group);
    }

    pub fn cancel_replication_group_creation(&mut self, replication_group_id: &str) {
        self.in_progress_replication_group_ids
            .remove(replication_group_id);
    }

    pub fn begin_serverless_cache_creation(&mut self, serverless_cache_name: &str) -> bool {
        if self.serverless_caches.contains_key(serverless_cache_name)
            || self
                .in_progress_serverless_cache_names
                .contains(serverless_cache_name)
        {
            return false;
        }
        self.in_progress_serverless_cache_names
            .insert(serverless_cache_name.to_string());
        true
    }

    pub fn finish_serverless_cache_creation(&mut self, cache: ServerlessCache) {
        self.in_progress_serverless_cache_names
            .remove(&cache.serverless_cache_name);
        self.tags.insert(cache.arn.clone(), Vec::new());
        self.serverless_caches
            .insert(cache.serverless_cache_name.clone(), cache);
    }

    pub fn cancel_serverless_cache_creation(&mut self, serverless_cache_name: &str) {
        self.in_progress_serverless_cache_names
            .remove(serverless_cache_name);
    }

    pub fn register_arn(&mut self, arn: &str) {
        self.tags.entry(arn.to_string()).or_default();
    }

    pub fn has_arn(&self, arn: &str) -> bool {
        self.tags.contains_key(arn)
    }
}

fn default_reserved_cache_nodes_offerings() -> Vec<ReservedCacheNodesOffering> {
    vec![
        ReservedCacheNodesOffering {
            reserved_cache_nodes_offering_id: "off-cache-t3-micro-redis-1yr-no-upfront".to_string(),
            cache_node_type: "cache.t3.micro".to_string(),
            duration: 31_536_000,
            fixed_price: 0.0,
            usage_price: 0.011,
            product_description: "redis".to_string(),
            offering_type: "No Upfront".to_string(),
            recurring_charges: Vec::new(),
        },
        ReservedCacheNodesOffering {
            reserved_cache_nodes_offering_id: "off-cache-t3-small-redis-1yr-partial-upfront"
                .to_string(),
            cache_node_type: "cache.t3.small".to_string(),
            duration: 31_536_000,
            fixed_price: 120.0,
            usage_price: 0.007,
            product_description: "redis".to_string(),
            offering_type: "Partial Upfront".to_string(),
            recurring_charges: Vec::new(),
        },
        ReservedCacheNodesOffering {
            reserved_cache_nodes_offering_id: "off-cache-m5-large-memcached-3yr-no-upfront"
                .to_string(),
            cache_node_type: "cache.m5.large".to_string(),
            duration: 94_608_000,
            fixed_price: 0.0,
            usage_price: 0.033,
            product_description: "memcached".to_string(),
            offering_type: "No Upfront".to_string(),
            recurring_charges: Vec::new(),
        },
        ReservedCacheNodesOffering {
            reserved_cache_nodes_offering_id: "off-cache-r6g-large-redis-3yr-all-upfront"
                .to_string(),
            cache_node_type: "cache.r6g.large".to_string(),
            duration: 94_608_000,
            fixed_price: 1_550.0,
            usage_price: 0.0,
            product_description: "redis".to_string(),
            offering_type: "All Upfront".to_string(),
            recurring_charges: vec![RecurringCharge {
                recurring_charge_amount: 0.0,
                recurring_charge_frequency: "Hourly".to_string(),
            }],
        },
    ]
}

pub fn default_engine_versions() -> Vec<CacheEngineVersion> {
    vec![
        CacheEngineVersion {
            engine: "redis".to_string(),
            engine_version: "7.1".to_string(),
            cache_parameter_group_family: "redis7".to_string(),
            cache_engine_description: "Redis".to_string(),
            cache_engine_version_description: "Redis 7.1".to_string(),
        },
        CacheEngineVersion {
            engine: "valkey".to_string(),
            engine_version: "8.0".to_string(),
            cache_parameter_group_family: "valkey8".to_string(),
            cache_engine_description: "Valkey".to_string(),
            cache_engine_version_description: "Valkey 8.0".to_string(),
        },
    ]
}

fn default_parameter_groups(account_id: &str, region: &str) -> Vec<CacheParameterGroup> {
    vec![
        CacheParameterGroup {
            cache_parameter_group_name: "default.redis7".to_string(),
            cache_parameter_group_family: "redis7".to_string(),
            description: "Default parameter group for redis7".to_string(),
            is_global: false,
            arn: Arn::new(
                "elasticache",
                region,
                account_id,
                "parametergroup:default.redis7",
            )
            .to_string(),
        },
        CacheParameterGroup {
            cache_parameter_group_name: "default.valkey8".to_string(),
            cache_parameter_group_family: "valkey8".to_string(),
            description: "Default parameter group for valkey8".to_string(),
            is_global: false,
            arn: Arn::new(
                "elasticache",
                region,
                account_id,
                "parametergroup:default.valkey8",
            )
            .to_string(),
        },
    ]
}

fn default_subnet_groups(account_id: &str, region: &str) -> HashMap<String, CacheSubnetGroup> {
    let default_group = CacheSubnetGroup {
        cache_subnet_group_name: "default".to_string(),
        cache_subnet_group_description: "Default CacheSubnetGroup".to_string(),
        vpc_id: "vpc-00000000".to_string(),
        subnet_ids: vec!["subnet-00000000".to_string()],
        arn: Arn::new("elasticache", region, account_id, "subnetgroup:default").to_string(),
    };
    let mut map = HashMap::new();
    map.insert("default".to_string(), default_group);
    map
}

pub fn default_parameters_for_family(family: &str) -> Vec<EngineDefaultParameter> {
    match family {
        "redis7" => vec![
            EngineDefaultParameter {
                parameter_name: "maxmemory-policy".to_string(),
                parameter_value: "volatile-lru".to_string(),
                description: "Max memory policy".to_string(),
                source: "system".to_string(),
                data_type: "string".to_string(),
                allowed_values: "volatile-lru,allkeys-lru,volatile-lfu,allkeys-lfu,volatile-random,allkeys-random,volatile-ttl,noeviction".to_string(),
                is_modifiable: true,
                minimum_engine_version: "7.0.0".to_string(),
            },
            EngineDefaultParameter {
                parameter_name: "cluster-enabled".to_string(),
                parameter_value: "no".to_string(),
                description: "Enable or disable Redis Cluster mode".to_string(),
                source: "system".to_string(),
                data_type: "string".to_string(),
                allowed_values: "yes,no".to_string(),
                is_modifiable: false,
                minimum_engine_version: "7.0.0".to_string(),
            },
            EngineDefaultParameter {
                parameter_name: "activedefrag".to_string(),
                parameter_value: "no".to_string(),
                description: "Enable active defragmentation".to_string(),
                source: "system".to_string(),
                data_type: "string".to_string(),
                allowed_values: "yes,no".to_string(),
                is_modifiable: true,
                minimum_engine_version: "7.0.0".to_string(),
            },
        ],
        "valkey8" => vec![
            EngineDefaultParameter {
                parameter_name: "maxmemory-policy".to_string(),
                parameter_value: "volatile-lru".to_string(),
                description: "Max memory policy".to_string(),
                source: "system".to_string(),
                data_type: "string".to_string(),
                allowed_values: "volatile-lru,allkeys-lru,volatile-lfu,allkeys-lfu,volatile-random,allkeys-random,volatile-ttl,noeviction".to_string(),
                is_modifiable: true,
                minimum_engine_version: "8.0.0".to_string(),
            },
            EngineDefaultParameter {
                parameter_name: "cluster-enabled".to_string(),
                parameter_value: "no".to_string(),
                description: "Enable or disable cluster mode".to_string(),
                source: "system".to_string(),
                data_type: "string".to_string(),
                allowed_values: "yes,no".to_string(),
                is_modifiable: false,
                minimum_engine_version: "8.0.0".to_string(),
            },
            EngineDefaultParameter {
                parameter_name: "activedefrag".to_string(),
                parameter_value: "no".to_string(),
                description: "Enable active defragmentation".to_string(),
                source: "system".to_string(),
                data_type: "string".to_string(),
                allowed_values: "yes,no".to_string(),
                is_modifiable: true,
                minimum_engine_version: "8.0.0".to_string(),
            },
        ],
        _ => Vec::new(),
    }
}

fn default_users(account_id: &str, region: &str) -> HashMap<String, ElastiCacheUser> {
    let mut map = HashMap::new();
    map.insert(
        "default".to_string(),
        ElastiCacheUser {
            user_id: "default".to_string(),
            user_name: "default".to_string(),
            engine: "redis".to_string(),
            access_string: "on ~* +@all".to_string(),
            status: "active".to_string(),
            authentication_type: "no-password".to_string(),
            password_count: 0,
            arn: Arn::new("elasticache", region, account_id, "user:default").to_string(),
            minimum_engine_version: "6.0".to_string(),
            user_group_ids: Vec::new(),
        },
    );
    map
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_engine_versions_contains_redis_and_valkey() {
        let versions = default_engine_versions();
        assert_eq!(versions.len(), 2);
        assert_eq!(versions[0].engine, "redis");
        assert_eq!(versions[0].engine_version, "7.1");
        assert_eq!(versions[1].engine, "valkey");
        assert_eq!(versions[1].engine_version, "8.0");
    }

    #[test]
    fn state_new_creates_default_parameter_groups() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert_eq!(state.parameter_groups.len(), 2);
        assert_eq!(
            state.parameter_groups[0].cache_parameter_group_name,
            "default.redis7"
        );
        assert_eq!(
            state.parameter_groups[1].cache_parameter_group_name,
            "default.valkey8"
        );
    }

    #[test]
    fn state_new_creates_default_subnet_group() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert_eq!(state.subnet_groups.len(), 1);
        let default = state.subnet_groups.get("default").unwrap();
        assert_eq!(default.cache_subnet_group_name, "default");
        assert_eq!(
            default.cache_subnet_group_description,
            "Default CacheSubnetGroup"
        );
        assert_eq!(default.vpc_id, "vpc-00000000");
        assert!(!default.subnet_ids.is_empty());
        assert!(default.arn.contains("subnetgroup:default"));
    }

    #[test]
    fn reset_restores_default_parameter_groups() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.parameter_groups.clear();
        assert!(state.parameter_groups.is_empty());
        state.reset();
        assert_eq!(state.parameter_groups.len(), 2);
    }

    #[test]
    fn reset_restores_default_subnet_groups() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.subnet_groups.clear();
        assert!(state.subnet_groups.is_empty());
        state.reset();
        assert_eq!(state.subnet_groups.len(), 1);
        assert!(state.subnet_groups.contains_key("default"));
    }

    #[test]
    fn default_parameters_for_redis7_returns_parameters() {
        let params = default_parameters_for_family("redis7");
        assert_eq!(params.len(), 3);
        assert_eq!(params[0].parameter_name, "maxmemory-policy");
    }

    #[test]
    fn default_parameters_for_unknown_family_returns_empty() {
        let params = default_parameters_for_family("unknown");
        assert!(params.is_empty());
    }

    #[test]
    fn state_new_has_empty_replication_groups() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert!(state.replication_groups.is_empty());
    }

    #[test]
    fn state_new_has_empty_global_replication_groups() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert!(state.global_replication_groups.is_empty());
    }

    #[test]
    fn state_new_has_empty_cache_clusters() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert!(state.cache_clusters.is_empty());
    }

    #[test]
    fn state_new_has_empty_serverless_caches() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert!(state.serverless_caches.is_empty());
        assert!(state.serverless_cache_snapshots.is_empty());
    }

    #[test]
    fn begin_cache_cluster_creation_rejects_duplicate_ids() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");

        assert!(state.begin_cache_cluster_creation("cluster-1"));
        assert!(!state.begin_cache_cluster_creation("cluster-1"));

        state.cancel_cache_cluster_creation("cluster-1");
        assert!(state.begin_cache_cluster_creation("cluster-1"));
    }

    #[test]
    fn begin_replication_group_creation_rejects_duplicate_ids() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");

        assert!(state.begin_replication_group_creation("rg-1"));
        assert!(!state.begin_replication_group_creation("rg-1"));

        state.cancel_replication_group_creation("rg-1");
        assert!(state.begin_replication_group_creation("rg-1"));
    }

    #[test]
    fn begin_serverless_cache_creation_rejects_duplicate_names() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");

        assert!(state.begin_serverless_cache_creation("cache-1"));
        assert!(!state.begin_serverless_cache_creation("cache-1"));

        state.cancel_serverless_cache_creation("cache-1");
        assert!(state.begin_serverless_cache_creation("cache-1"));
    }

    #[test]
    fn finish_serverless_cache_creation_registers_cache_and_tags() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        assert!(state.begin_serverless_cache_creation("cache-1"));

        let cache = ServerlessCache {
            serverless_cache_name: "cache-1".to_string(),
            description: "test".to_string(),
            engine: "redis".to_string(),
            major_engine_version: "7.1".to_string(),
            full_engine_version: "7.1".to_string(),
            status: "available".to_string(),
            endpoint: ServerlessCacheEndpoint {
                address: "127.0.0.1".to_string(),
                port: 6379,
            },
            reader_endpoint: ServerlessCacheEndpoint {
                address: "127.0.0.1".to_string(),
                port: 6379,
            },
            arn: "arn:aws:elasticache:us-east-1:123456789012:serverlesscache:cache-1".to_string(),
            created_at: "2024-01-01T00:00:00Z".to_string(),
            cache_usage_limits: None,
            security_group_ids: Vec::new(),
            subnet_ids: Vec::new(),
            kms_key_id: None,
            user_group_id: None,
            snapshot_retention_limit: None,
            daily_snapshot_time: None,
            container_id: "cid".to_string(),
            host_port: 6379,
        };

        state.finish_serverless_cache_creation(cache.clone());

        assert!(state.serverless_caches.contains_key("cache-1"));
        assert!(state.tags.contains_key(&cache.arn));
    }

    #[test]
    fn state_new_creates_default_user() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert_eq!(state.users.len(), 1);
        let default = state.users.get("default").unwrap();
        assert_eq!(default.user_id, "default");
        assert_eq!(default.user_name, "default");
        assert_eq!(default.engine, "redis");
        assert_eq!(default.access_string, "on ~* +@all");
        assert_eq!(default.status, "active");
        assert_eq!(default.authentication_type, "no-password");
        assert_eq!(default.password_count, 0);
        assert!(default.arn.contains("user:default"));
    }

    #[test]
    fn state_new_has_empty_user_groups() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert!(state.user_groups.is_empty());
    }

    #[test]
    fn reset_restores_default_user() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.users.clear();
        assert!(state.users.is_empty());
        state.reset();
        assert_eq!(state.users.len(), 1);
        assert!(state.users.contains_key("default"));
    }

    #[test]
    fn reset_clears_user_groups() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.user_groups.insert(
            "my-group".to_string(),
            ElastiCacheUserGroup {
                user_group_id: "my-group".to_string(),
                engine: "redis".to_string(),
                status: "active".to_string(),
                user_ids: vec!["default".to_string()],
                arn: "arn:aws:elasticache:us-east-1:123456789012:usergroup:my-group".to_string(),
                minimum_engine_version: "6.0".to_string(),
                pending_changes: None,
                replication_groups: Vec::new(),
            },
        );
        assert_eq!(state.user_groups.len(), 1);
        state.reset();
        assert!(state.user_groups.is_empty());
    }

    #[test]
    fn state_new_has_empty_snapshots() {
        let state = ElastiCacheState::new("123456789012", "us-east-1");
        assert!(state.snapshots.is_empty());
    }

    #[test]
    fn reset_clears_snapshots() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.snapshots.insert(
            "my-snapshot".to_string(),
            CacheSnapshot {
                snapshot_name: "my-snapshot".to_string(),
                replication_group_id: "rg-1".to_string(),
                replication_group_description: "test".to_string(),
                snapshot_status: "available".to_string(),
                cache_node_type: "cache.t3.micro".to_string(),
                engine: "redis".to_string(),
                engine_version: "7.1".to_string(),
                num_cache_clusters: 1,
                arn: "arn:aws:elasticache:us-east-1:123456789012:snapshot:my-snapshot".to_string(),
                created_at: "2024-01-01T00:00:00Z".to_string(),
                snapshot_source: "manual".to_string(),
            },
        );
        assert_eq!(state.snapshots.len(), 1);
        state.reset();
        assert!(state.snapshots.is_empty());
    }

    #[test]
    fn reset_clears_replication_groups() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.replication_groups.insert(
            "my-group".to_string(),
            ReplicationGroup {
                replication_group_id: "my-group".to_string(),
                description: "test".to_string(),
                global_replication_group_id: None,
                global_replication_group_role: None,
                status: "available".to_string(),
                cache_node_type: "cache.t3.micro".to_string(),
                engine: "redis".to_string(),
                engine_version: "7.1".to_string(),
                num_cache_clusters: 1,
                automatic_failover_enabled: false,
                endpoint_address: "127.0.0.1".to_string(),
                endpoint_port: 6379,
                arn: "arn:aws:elasticache:us-east-1:123456789012:replicationgroup:my-group"
                    .to_string(),
                created_at: "2024-01-01T00:00:00Z".to_string(),
                container_id: "abc123".to_string(),
                host_port: 12345,
                member_clusters: vec!["my-group-001".to_string()],
                snapshot_retention_limit: 0,
                snapshot_window: "05:00-09:00".to_string(),
            },
        );
        assert_eq!(state.replication_groups.len(), 1);
        state.reset();
        assert!(state.replication_groups.is_empty());
    }

    #[test]
    fn reset_clears_global_replication_groups() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.global_replication_groups.insert(
            "global-rg".to_string(),
            GlobalReplicationGroup {
                global_replication_group_id: "global-rg".to_string(),
                global_replication_group_description: "test".to_string(),
                status: "available".to_string(),
                cache_node_type: "cache.t3.micro".to_string(),
                engine: "redis".to_string(),
                engine_version: "7.1".to_string(),
                members: vec![GlobalReplicationGroupMember {
                    replication_group_id: "rg-1".to_string(),
                    replication_group_region: "us-east-1".to_string(),
                    role: "primary".to_string(),
                    automatic_failover: false,
                    status: "associated".to_string(),
                }],
                cluster_enabled: false,
                arn: "arn:aws:elasticache:us-east-1:123456789012:globalreplicationgroup:global-rg"
                    .to_string(),
            },
        );
        assert_eq!(state.global_replication_groups.len(), 1);
        state.reset();
        assert!(state.global_replication_groups.is_empty());
    }

    #[test]
    fn reset_clears_cache_clusters() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.cache_clusters.insert(
            "classic-cluster".to_string(),
            CacheCluster {
                cache_cluster_id: "classic-cluster".to_string(),
                cache_node_type: "cache.t3.micro".to_string(),
                engine: "redis".to_string(),
                engine_version: "7.1".to_string(),
                cache_cluster_status: "available".to_string(),
                num_cache_nodes: 1,
                preferred_availability_zone: "us-east-1a".to_string(),
                cache_subnet_group_name: Some("default".to_string()),
                auto_minor_version_upgrade: true,
                arn: "arn:aws:elasticache:us-east-1:123456789012:cluster:classic-cluster"
                    .to_string(),
                created_at: "2024-01-01T00:00:00Z".to_string(),
                endpoint_address: "127.0.0.1".to_string(),
                endpoint_port: 6379,
                container_id: "abc123".to_string(),
                host_port: 12345,
                replication_group_id: None,
            },
        );
        assert_eq!(state.cache_clusters.len(), 1);
        state.reset();
        assert!(state.cache_clusters.is_empty());
    }

    #[test]
    fn reset_restores_reserved_cache_node_metadata() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.reserved_cache_nodes.insert(
            "rcn-a".to_string(),
            ReservedCacheNode {
                reserved_cache_node_id: "rcn-a".to_string(),
                reserved_cache_nodes_offering_id: "offering-a".to_string(),
                cache_node_type: "cache.t3.micro".to_string(),
                start_time: "2024-01-01T00:00:00Z".to_string(),
                duration: 31_536_000,
                fixed_price: 0.0,
                usage_price: 0.011,
                cache_node_count: 1,
                product_description: "redis".to_string(),
                offering_type: "No Upfront".to_string(),
                state: "payment-pending".to_string(),
                recurring_charges: Vec::new(),
                reservation_arn:
                    "arn:aws:elasticache:us-east-1:123456789012:reserved-instance:test".to_string(),
            },
        );
        state.reserved_cache_nodes_offerings.clear();

        state.reset();

        assert!(state.reserved_cache_nodes.is_empty());
        assert!(!state.reserved_cache_nodes_offerings.is_empty());
    }

    #[test]
    fn reset_clears_serverless_cache_state() {
        let mut state = ElastiCacheState::new("123456789012", "us-east-1");
        state.serverless_caches.insert(
            "serverless".to_string(),
            ServerlessCache {
                serverless_cache_name: "serverless".to_string(),
                description: "test".to_string(),
                engine: "redis".to_string(),
                major_engine_version: "7.1".to_string(),
                full_engine_version: "7.1".to_string(),
                status: "available".to_string(),
                endpoint: ServerlessCacheEndpoint {
                    address: "127.0.0.1".to_string(),
                    port: 6379,
                },
                reader_endpoint: ServerlessCacheEndpoint {
                    address: "127.0.0.1".to_string(),
                    port: 6379,
                },
                arn: "arn:aws:elasticache:us-east-1:123456789012:serverlesscache:serverless"
                    .to_string(),
                created_at: "2024-01-01T00:00:00Z".to_string(),
                cache_usage_limits: None,
                security_group_ids: Vec::new(),
                subnet_ids: Vec::new(),
                kms_key_id: None,
                user_group_id: None,
                snapshot_retention_limit: None,
                daily_snapshot_time: None,
                container_id: "cid".to_string(),
                host_port: 6379,
            },
        );
        state.serverless_cache_snapshots.insert(
            "snap-1".to_string(),
            ServerlessCacheSnapshot {
                serverless_cache_snapshot_name: "snap-1".to_string(),
                arn: "arn:aws:elasticache:us-east-1:123456789012:serverlesssnapshot:snap-1"
                    .to_string(),
                kms_key_id: None,
                snapshot_type: "manual".to_string(),
                status: "available".to_string(),
                create_time: "2024-01-01T00:00:00Z".to_string(),
                expiry_time: None,
                bytes_used_for_cache: None,
                serverless_cache_name: "serverless".to_string(),
                engine: "redis".to_string(),
                major_engine_version: "7.1".to_string(),
            },
        );

        state.reset();

        assert!(state.serverless_caches.is_empty());
        assert!(state.serverless_cache_snapshots.is_empty());
    }
}