Skip to main content

fakecloud_redshift/
state.rs

1//! In-memory, per-account state for Amazon Redshift.
2
3use std::collections::BTreeMap;
4use std::sync::Arc;
5
6use chrono::{DateTime, Utc};
7use parking_lot::RwLock;
8use serde::{Deserialize, Serialize};
9
10pub type SharedRedshiftState = Arc<RwLock<RedshiftAccounts>>;
11
12#[derive(Debug, Default, Clone, Serialize, Deserialize)]
13pub struct RedshiftAccounts {
14    pub accounts: BTreeMap<String, RedshiftState>,
15}
16
17impl RedshiftAccounts {
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    pub fn account(&mut self, account_id: &str) -> &mut RedshiftState {
23        self.accounts
24            .entry(account_id.to_string())
25            .or_insert_with(|| RedshiftState::new_for_account(account_id))
26    }
27}
28
29/// On-disk snapshot envelope for Redshift state. Versioned so format changes
30/// fail loudly on upgrade rather than silently mis-parsing.
31#[derive(Clone, Serialize, Deserialize)]
32pub struct RedshiftSnapshot {
33    pub schema_version: u32,
34    #[serde(default)]
35    pub accounts: Option<RedshiftAccounts>,
36}
37
38pub const REDSHIFT_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
39
40/// All Redshift control-plane resources for a single AWS account.
41#[derive(Debug, Default, Clone, Serialize, Deserialize)]
42pub struct RedshiftState {
43    #[serde(default)]
44    pub account_id: String,
45    #[serde(default)]
46    pub clusters: BTreeMap<String, Cluster>,
47    #[serde(default)]
48    pub parameter_groups: BTreeMap<String, ClusterParameterGroup>,
49    #[serde(default)]
50    pub security_groups: BTreeMap<String, ClusterSecurityGroup>,
51    #[serde(default)]
52    pub subnet_groups: BTreeMap<String, ClusterSubnetGroup>,
53    #[serde(default)]
54    pub snapshots: BTreeMap<String, Snapshot>,
55    #[serde(default)]
56    pub event_subscriptions: BTreeMap<String, EventSubscription>,
57    #[serde(default)]
58    pub hsm_client_certificates: BTreeMap<String, HsmClientCertificate>,
59    #[serde(default)]
60    pub hsm_configurations: BTreeMap<String, HsmConfiguration>,
61    #[serde(default)]
62    pub snapshot_copy_grants: BTreeMap<String, SnapshotCopyGrant>,
63    #[serde(default)]
64    pub snapshot_schedules: BTreeMap<String, SnapshotSchedule>,
65    #[serde(default)]
66    pub scheduled_actions: BTreeMap<String, ScheduledAction>,
67    #[serde(default)]
68    pub usage_limits: BTreeMap<String, UsageLimit>,
69    #[serde(default)]
70    pub endpoint_access: BTreeMap<String, EndpointAccess>,
71    #[serde(default)]
72    pub endpoint_authorizations: BTreeMap<String, EndpointAuthorization>,
73    #[serde(default)]
74    pub authentication_profiles: BTreeMap<String, AuthenticationProfile>,
75    #[serde(default)]
76    pub datashares: BTreeMap<String, DataShare>,
77    #[serde(default)]
78    pub partners: BTreeMap<String, Partner>,
79    #[serde(default)]
80    pub reserved_nodes: BTreeMap<String, ReservedNode>,
81    #[serde(default)]
82    pub integrations: BTreeMap<String, Integration>,
83    #[serde(default)]
84    pub idc_applications: BTreeMap<String, RedshiftIdcApplication>,
85    #[serde(default)]
86    pub custom_domains: BTreeMap<String, CustomDomainAssociation>,
87    #[serde(default)]
88    pub table_restore_status: BTreeMap<String, TableRestoreStatus>,
89    /// Cluster-logging status keyed by cluster identifier.
90    #[serde(default)]
91    pub logging: BTreeMap<String, LoggingStatus>,
92    /// IAM resource policies keyed by resource ARN.
93    #[serde(default)]
94    pub resource_policies: BTreeMap<String, String>,
95    /// Cross-region snapshot copy config keyed by cluster identifier.
96    #[serde(default)]
97    pub registered_namespaces: BTreeMap<String, String>,
98}
99
100impl RedshiftState {
101    pub fn new_for_account(account_id: &str) -> Self {
102        Self {
103            account_id: account_id.to_string(),
104            ..Default::default()
105        }
106    }
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct Tag {
111    pub key: String,
112    pub value: String,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct Cluster {
117    pub cluster_identifier: String,
118    pub node_type: String,
119    pub cluster_status: String,
120    pub cluster_availability_status: String,
121    pub master_username: String,
122    pub db_name: String,
123    pub endpoint_address: String,
124    pub endpoint_port: i32,
125    pub cluster_create_time: DateTime<Utc>,
126    pub automated_snapshot_retention_period: i32,
127    pub manual_snapshot_retention_period: i32,
128    pub cluster_security_groups: Vec<String>,
129    pub vpc_security_group_ids: Vec<String>,
130    pub cluster_parameter_group_name: String,
131    pub cluster_subnet_group_name: Option<String>,
132    pub vpc_id: Option<String>,
133    pub availability_zone: String,
134    pub preferred_maintenance_window: String,
135    pub cluster_version: String,
136    pub allow_version_upgrade: bool,
137    pub number_of_nodes: i32,
138    pub publicly_accessible: bool,
139    pub encrypted: bool,
140    pub multi_az: bool,
141    pub cluster_type: String,
142    pub kms_key_id: Option<String>,
143    pub enhanced_vpc_routing: bool,
144    pub maintenance_track_name: String,
145    pub elastic_ip: Option<String>,
146    pub availability_zone_relocation_status: String,
147    pub aqua_configuration_status: String,
148    pub default_iam_role_arn: Option<String>,
149    pub iam_roles: Vec<String>,
150    pub snapshot_schedule_identifier: Option<String>,
151    pub total_storage_capacity_in_mega_bytes: i64,
152    pub next_maintenance_window_start_time: Option<DateTime<Utc>>,
153    pub snapshot_copy: Option<SnapshotCopyStatus>,
154    pub tags: Vec<Tag>,
155}
156
157/// Cross-region snapshot-copy configuration set via `EnableSnapshotCopy`.
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct SnapshotCopyStatus {
160    pub destination_region: String,
161    pub retention_period: i64,
162    pub manual_snapshot_retention_period: i32,
163    pub snapshot_copy_grant_name: Option<String>,
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct Parameter {
168    pub parameter_name: String,
169    pub parameter_value: String,
170    pub description: String,
171    pub source: String,
172    pub data_type: String,
173    pub allowed_values: String,
174    pub apply_type: String,
175    pub is_modifiable: bool,
176}
177
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct ClusterParameterGroup {
180    pub parameter_group_name: String,
181    pub parameter_group_family: String,
182    pub description: String,
183    pub parameters: Vec<Parameter>,
184    pub tags: Vec<Tag>,
185}
186
187#[derive(Debug, Clone, Serialize, Deserialize)]
188pub struct Ec2SecurityGroup {
189    pub status: String,
190    pub ec2_security_group_name: String,
191    pub ec2_security_group_owner_id: Option<String>,
192}
193
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct IpRange {
196    pub status: String,
197    pub cidrip: String,
198}
199
200#[derive(Debug, Clone, Serialize, Deserialize)]
201pub struct ClusterSecurityGroup {
202    pub cluster_security_group_name: String,
203    pub description: String,
204    pub ec2_security_groups: Vec<Ec2SecurityGroup>,
205    pub ip_ranges: Vec<IpRange>,
206    pub tags: Vec<Tag>,
207}
208
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct Subnet {
211    pub subnet_identifier: String,
212    pub subnet_availability_zone: String,
213    pub subnet_status: String,
214}
215
216#[derive(Debug, Clone, Serialize, Deserialize)]
217pub struct ClusterSubnetGroup {
218    pub cluster_subnet_group_name: String,
219    pub description: String,
220    pub vpc_id: String,
221    pub subnet_group_status: String,
222    pub subnets: Vec<Subnet>,
223    pub tags: Vec<Tag>,
224}
225
226#[derive(Debug, Clone, Serialize, Deserialize)]
227pub struct Snapshot {
228    pub snapshot_identifier: String,
229    pub snapshot_arn: String,
230    pub cluster_identifier: String,
231    pub snapshot_create_time: DateTime<Utc>,
232    pub status: String,
233    pub port: i32,
234    pub availability_zone: String,
235    pub cluster_create_time: DateTime<Utc>,
236    pub master_username: String,
237    pub cluster_version: String,
238    pub snapshot_type: String,
239    pub node_type: String,
240    pub number_of_nodes: i32,
241    pub db_name: String,
242    pub vpc_id: Option<String>,
243    pub encrypted: bool,
244    pub kms_key_id: Option<String>,
245    pub manual_snapshot_retention_period: i32,
246    pub total_backup_size_in_mega_bytes: f64,
247    pub actual_incremental_backup_size_in_mega_bytes: f64,
248    pub accounts_with_restore_access: Vec<String>,
249    pub owner_account: String,
250    pub source_region: Option<String>,
251    pub tags: Vec<Tag>,
252}
253
254#[derive(Debug, Clone, Serialize, Deserialize)]
255pub struct EventSubscription {
256    pub subscription_name: String,
257    pub customer_aws_id: String,
258    pub sns_topic_arn: String,
259    pub status: String,
260    pub subscription_creation_time: DateTime<Utc>,
261    pub source_type: Option<String>,
262    pub source_ids: Vec<String>,
263    pub event_categories: Vec<String>,
264    pub severity: Option<String>,
265    pub enabled: bool,
266    pub tags: Vec<Tag>,
267}
268
269#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct HsmClientCertificate {
271    pub hsm_client_certificate_identifier: String,
272    pub hsm_client_certificate_public_key: String,
273    pub tags: Vec<Tag>,
274}
275
276#[derive(Debug, Clone, Serialize, Deserialize)]
277pub struct HsmConfiguration {
278    pub hsm_configuration_identifier: String,
279    pub description: String,
280    pub hsm_ip_address: String,
281    pub hsm_partition_name: String,
282    pub tags: Vec<Tag>,
283}
284
285#[derive(Debug, Clone, Serialize, Deserialize)]
286pub struct SnapshotCopyGrant {
287    pub snapshot_copy_grant_name: String,
288    pub kms_key_id: String,
289    pub tags: Vec<Tag>,
290}
291
292#[derive(Debug, Clone, Serialize, Deserialize)]
293pub struct SnapshotSchedule {
294    pub schedule_identifier: String,
295    pub schedule_description: String,
296    pub schedule_definitions: Vec<String>,
297    pub tags: Vec<Tag>,
298    pub associated_cluster_count: i32,
299    pub associated_clusters: Vec<String>,
300}
301
302#[derive(Debug, Clone, Serialize, Deserialize)]
303pub struct ScheduledAction {
304    pub scheduled_action_name: String,
305    pub target_action: Option<String>,
306    pub schedule: String,
307    pub iam_role: String,
308    pub scheduled_action_description: String,
309    pub state: String,
310    pub start_time: Option<DateTime<Utc>>,
311    pub end_time: Option<DateTime<Utc>>,
312    pub enable: bool,
313}
314
315#[derive(Debug, Clone, Serialize, Deserialize)]
316pub struct UsageLimit {
317    pub usage_limit_id: String,
318    pub cluster_identifier: String,
319    pub feature_type: String,
320    pub limit_type: String,
321    pub amount: i64,
322    pub period: String,
323    pub breach_action: String,
324    pub tags: Vec<Tag>,
325}
326
327#[derive(Debug, Clone, Serialize, Deserialize)]
328pub struct EndpointAccess {
329    pub endpoint_name: String,
330    pub cluster_identifier: String,
331    pub subnet_group_name: String,
332    pub endpoint_status: String,
333    pub endpoint_create_time: DateTime<Utc>,
334    pub resource_owner: String,
335    pub port: i32,
336    pub address: String,
337    pub vpc_security_group_ids: Vec<String>,
338}
339
340#[derive(Debug, Clone, Serialize, Deserialize)]
341pub struct EndpointAuthorization {
342    pub grantor: String,
343    pub grantee: String,
344    pub cluster_identifier: String,
345    pub authorize_time: DateTime<Utc>,
346    pub cluster_status: String,
347    pub status: String,
348    pub allowed_all_vpcs: bool,
349    pub allowed_vpcs: Vec<String>,
350    pub endpoint_count: i32,
351}
352
353#[derive(Debug, Clone, Serialize, Deserialize)]
354pub struct AuthenticationProfile {
355    pub authentication_profile_name: String,
356    pub authentication_profile_content: String,
357}
358
359#[derive(Debug, Clone, Serialize, Deserialize)]
360pub struct DataShare {
361    pub data_share_arn: String,
362    pub producer_arn: String,
363    pub allow_publicly_accessible_consumers: bool,
364    pub managed_by: Option<String>,
365    pub data_share_type: String,
366    pub status: String,
367}
368
369#[derive(Debug, Clone, Serialize, Deserialize)]
370pub struct Partner {
371    pub database_name: String,
372    pub partner_name: String,
373    pub cluster_identifier: String,
374    pub account_id: String,
375    pub status: String,
376    pub status_message: Option<String>,
377}
378
379#[derive(Debug, Clone, Serialize, Deserialize)]
380pub struct ReservedNode {
381    pub reserved_node_id: String,
382    pub reserved_node_offering_id: String,
383    pub node_type: String,
384    pub start_time: DateTime<Utc>,
385    pub duration: i32,
386    pub fixed_price: f64,
387    pub usage_price: f64,
388    pub currency_code: String,
389    pub node_count: i32,
390    pub state: String,
391    pub offering_type: String,
392    pub reserved_node_offering_type: String,
393}
394
395#[derive(Debug, Clone, Serialize, Deserialize)]
396pub struct Integration {
397    pub integration_arn: String,
398    pub integration_name: String,
399    pub source_arn: String,
400    pub target_arn: String,
401    pub status: String,
402    pub create_time: DateTime<Utc>,
403    pub description: Option<String>,
404    pub kms_key_id: Option<String>,
405    pub tags: Vec<Tag>,
406}
407
408#[derive(Debug, Clone, Serialize, Deserialize)]
409pub struct RedshiftIdcApplication {
410    pub redshift_idc_application_name: String,
411    pub redshift_idc_application_arn: String,
412    pub identity_namespace: Option<String>,
413    pub idc_instance_arn: String,
414    pub idc_display_name: String,
415    pub iam_role_arn: String,
416    pub idc_managed_application_arn: String,
417    pub authorized_token_issuer_list: Vec<String>,
418    pub service_integrations: Vec<String>,
419}
420
421#[derive(Debug, Clone, Serialize, Deserialize)]
422pub struct CustomDomainAssociation {
423    pub custom_domain_name: String,
424    pub custom_domain_certificate_arn: String,
425    pub cluster_identifier: String,
426    pub custom_domain_certificate_expiry_date: DateTime<Utc>,
427}
428
429#[derive(Debug, Clone, Serialize, Deserialize)]
430pub struct TableRestoreStatus {
431    pub table_restore_request_id: String,
432    pub status: String,
433    pub cluster_identifier: String,
434    pub snapshot_identifier: String,
435    pub source_database_name: String,
436    pub source_table_name: String,
437    pub new_table_name: String,
438    pub target_database_name: String,
439    pub request_time: DateTime<Utc>,
440}
441
442#[derive(Debug, Clone, Serialize, Deserialize)]
443pub struct LoggingStatus {
444    pub logging_enabled: bool,
445    pub bucket_name: Option<String>,
446    pub s3_key_prefix: Option<String>,
447    pub log_destination_type: Option<String>,
448    pub log_exports: Vec<String>,
449    pub last_successful_delivery_time: Option<DateTime<Utc>>,
450}