1use std::collections::BTreeMap;
4use std::sync::Arc;
5
6use chrono::{DateTime, Utc};
7use parking_lot::RwLock;
8use serde::{Deserialize, Serialize};
9
10use crate::cfunctions::StoredConnectionFunction;
11use crate::extras::{StoredAnycastIpList, StoredResourcePolicy, StoredTrustStore, StoredVpcOrigin};
12use crate::extras2::StoredConnectionGroup;
13use crate::fle::{
14 StoredFieldLevelEncryption, StoredFieldLevelEncryptionProfile, StoredRealtimeLogConfig,
15};
16use crate::functions::{
17 StoredFunction, StoredKeyGroup, StoredKeyValueStore, StoredMonitoringSubscription,
18 StoredOriginAccessIdentity, StoredPublicKey,
19};
20use crate::model::{DistributionConfig, InvalidationBatch};
21use crate::policies::{
22 StoredCachePolicy, StoredContinuousDeploymentPolicy, StoredOriginAccessControl,
23 StoredOriginRequestPolicy, StoredResponseHeadersPolicy,
24};
25use crate::streaming::StoredStreamingDistribution;
26use crate::tenants::{StoredDistributionTenant, StoredTenantInvalidation};
27
28pub type SharedCloudFrontState = Arc<RwLock<CloudFrontAccounts>>;
29
30#[derive(Debug, Default, Clone, Serialize, Deserialize)]
31pub struct CloudFrontAccounts {
32 pub accounts: BTreeMap<String, AccountState>,
33}
34
35#[derive(Clone, Serialize, Deserialize)]
38pub struct CloudFrontSnapshot {
39 pub schema_version: u32,
40 #[serde(default)]
41 pub accounts: Option<CloudFrontAccounts>,
42}
43
44pub const CLOUDFRONT_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
45
46impl CloudFrontAccounts {
47 pub fn new() -> Self {
48 Self::default()
49 }
50
51 pub fn account_count(&self) -> usize {
52 self.accounts.len()
53 }
54
55 pub fn entry(&mut self, account_id: &str) -> &mut AccountState {
56 self.accounts.entry(account_id.to_string()).or_default()
57 }
58
59 pub fn get(&self, account_id: &str) -> Option<&AccountState> {
60 self.accounts.get(account_id)
61 }
62
63 pub fn all_distributions(&self) -> impl Iterator<Item = (&String, &StoredDistribution)> {
67 self.accounts.iter().flat_map(|(account_id, state)| {
68 state.distributions.values().map(move |d| (account_id, d))
69 })
70 }
71}
72
73#[derive(Debug, Default, Clone, Serialize, Deserialize)]
74pub struct AccountState {
75 pub distributions: BTreeMap<String, StoredDistribution>,
76 pub invalidations: BTreeMap<String, StoredInvalidation>,
77 pub tags: BTreeMap<String, Vec<Tag>>,
79 pub origin_access_controls: BTreeMap<String, StoredOriginAccessControl>,
80 pub cache_policies: BTreeMap<String, StoredCachePolicy>,
81 pub origin_request_policies: BTreeMap<String, StoredOriginRequestPolicy>,
82 pub response_headers_policies: BTreeMap<String, StoredResponseHeadersPolicy>,
83 pub continuous_deployment_policies: BTreeMap<String, StoredContinuousDeploymentPolicy>,
84 pub functions: BTreeMap<String, StoredFunction>,
85 pub public_keys: BTreeMap<String, StoredPublicKey>,
86 pub key_groups: BTreeMap<String, StoredKeyGroup>,
87 pub key_value_stores: BTreeMap<String, StoredKeyValueStore>,
88 pub origin_access_identities: BTreeMap<String, StoredOriginAccessIdentity>,
89 pub monitoring_subscriptions: BTreeMap<String, StoredMonitoringSubscription>,
91 pub streaming_distributions: BTreeMap<String, StoredStreamingDistribution>,
92 pub field_level_encryptions: BTreeMap<String, StoredFieldLevelEncryption>,
93 pub field_level_encryption_profiles: BTreeMap<String, StoredFieldLevelEncryptionProfile>,
94 pub realtime_log_configs: BTreeMap<String, StoredRealtimeLogConfig>,
96 pub vpc_origins: BTreeMap<String, StoredVpcOrigin>,
97 pub anycast_ip_lists: BTreeMap<String, StoredAnycastIpList>,
98 pub trust_stores: BTreeMap<String, StoredTrustStore>,
99 pub resource_policies: BTreeMap<String, StoredResourcePolicy>,
101 pub connection_groups: BTreeMap<String, StoredConnectionGroup>,
102 pub distribution_tenants: BTreeMap<String, StoredDistributionTenant>,
103 pub tenant_invalidations: BTreeMap<String, StoredTenantInvalidation>,
104 pub connection_functions: BTreeMap<String, StoredConnectionFunction>,
105}
106
107impl CloudFrontAccounts {
108 pub fn seed_managed_policies(&mut self, account_id: &str) {
115 let account = self.entry(account_id);
116 crate::policies::seed_managed(account);
117 }
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct StoredDistribution {
122 pub id: String,
123 pub arn: String,
124 pub status: String,
125 pub last_modified_time: DateTime<Utc>,
126 pub domain_name: String,
127 pub in_progress_invalidation_batches: u32,
128 pub etag: String,
129 pub config: DistributionConfig,
130 #[serde(skip)]
136 pub bound_port: Option<u16>,
137}
138
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct StoredInvalidation {
141 pub id: String,
142 pub distribution_id: String,
143 pub status: String,
144 pub create_time: DateTime<Utc>,
145 pub batch: InvalidationBatch,
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
149pub struct Tag {
150 pub key: String,
151 pub value: Option<String>,
152}