Skip to main content

fakecloud_cloudfront/
state.rs

1//! In-memory state for CloudFront resources.
2
3use 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, Serialize, Deserialize)]
31pub struct CloudFrontAccounts {
32    pub accounts: BTreeMap<String, AccountState>,
33}
34
35impl CloudFrontAccounts {
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    pub fn account_count(&self) -> usize {
41        self.accounts.len()
42    }
43
44    pub fn entry(&mut self, account_id: &str) -> &mut AccountState {
45        self.accounts.entry(account_id.to_string()).or_default()
46    }
47
48    pub fn get(&self, account_id: &str) -> Option<&AccountState> {
49        self.accounts.get(account_id)
50    }
51}
52
53#[derive(Debug, Default, Serialize, Deserialize)]
54pub struct AccountState {
55    pub distributions: BTreeMap<String, StoredDistribution>,
56    pub invalidations: BTreeMap<String, StoredInvalidation>,
57    /// Tags keyed by ARN.
58    pub tags: BTreeMap<String, Vec<Tag>>,
59    pub origin_access_controls: BTreeMap<String, StoredOriginAccessControl>,
60    pub cache_policies: BTreeMap<String, StoredCachePolicy>,
61    pub origin_request_policies: BTreeMap<String, StoredOriginRequestPolicy>,
62    pub response_headers_policies: BTreeMap<String, StoredResponseHeadersPolicy>,
63    pub continuous_deployment_policies: BTreeMap<String, StoredContinuousDeploymentPolicy>,
64    pub functions: BTreeMap<String, StoredFunction>,
65    pub public_keys: BTreeMap<String, StoredPublicKey>,
66    pub key_groups: BTreeMap<String, StoredKeyGroup>,
67    pub key_value_stores: BTreeMap<String, StoredKeyValueStore>,
68    pub origin_access_identities: BTreeMap<String, StoredOriginAccessIdentity>,
69    /// Per-distribution monitoring subscription, keyed by distribution id.
70    pub monitoring_subscriptions: BTreeMap<String, StoredMonitoringSubscription>,
71    pub streaming_distributions: BTreeMap<String, StoredStreamingDistribution>,
72    pub field_level_encryptions: BTreeMap<String, StoredFieldLevelEncryption>,
73    pub field_level_encryption_profiles: BTreeMap<String, StoredFieldLevelEncryptionProfile>,
74    /// Realtime log configs keyed by ARN.
75    pub realtime_log_configs: BTreeMap<String, StoredRealtimeLogConfig>,
76    pub vpc_origins: BTreeMap<String, StoredVpcOrigin>,
77    pub anycast_ip_lists: BTreeMap<String, StoredAnycastIpList>,
78    pub trust_stores: BTreeMap<String, StoredTrustStore>,
79    /// Resource policies keyed by resource ARN.
80    pub resource_policies: BTreeMap<String, StoredResourcePolicy>,
81    pub connection_groups: BTreeMap<String, StoredConnectionGroup>,
82    pub distribution_tenants: BTreeMap<String, StoredDistributionTenant>,
83    pub tenant_invalidations: BTreeMap<String, StoredTenantInvalidation>,
84    pub connection_functions: BTreeMap<String, StoredConnectionFunction>,
85}
86
87impl CloudFrontAccounts {
88    /// Pre-seed the AWS-managed Cache, Origin Request, and Response
89    /// Headers policies into the default account so callers that look
90    /// them up by their well-known IDs (Terraform, CDK) get the same
91    /// shape they get against AWS. The IDs and names mirror the AWS
92    /// console output verbatim — the easiest way to keep tests source
93    /// of truth.
94    pub fn seed_managed_policies(&mut self, account_id: &str) {
95        let account = self.entry(account_id);
96        crate::policies::seed_managed(account);
97    }
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct StoredDistribution {
102    pub id: String,
103    pub arn: String,
104    pub status: String,
105    pub last_modified_time: DateTime<Utc>,
106    pub domain_name: String,
107    pub in_progress_invalidation_batches: u32,
108    pub etag: String,
109    pub config: DistributionConfig,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct StoredInvalidation {
114    pub id: String,
115    pub distribution_id: String,
116    pub status: String,
117    pub create_time: DateTime<Utc>,
118    pub batch: InvalidationBatch,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
122pub struct Tag {
123    pub key: String,
124    pub value: Option<String>,
125}