Skip to main content

fakecloud_codeconnections/
state.rs

1//! Account-partitioned, serializable state for AWS CodeConnections.
2//!
3//! Everything the service owns for one AWS account: connections, self-managed
4//! hosts, repository links, and sync configurations. Tag maps live inline on
5//! each taggable record (connections, hosts, repository links). Nested request
6//! objects that fakecloud round-trips verbatim (a host's `VpcConfiguration`)
7//! are stored as the raw request `Value`.
8
9use std::collections::BTreeMap;
10use std::sync::Arc;
11
12use parking_lot::RwLock;
13use serde::{Deserialize, Serialize};
14use serde_json::Value;
15
16use fakecloud_core::multi_account::{AccountState, MultiAccountState};
17
18pub const CODECONNECTIONS_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
19
20/// Ordered key/value tag map.
21pub type TagMap = BTreeMap<String, String>;
22
23/// A connection to a third-party source-code provider.
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct ConnectionRecord {
26    pub connection_arn: String,
27    pub connection_name: String,
28    /// One of the `ProviderType` enum values, or `None` when the connection was
29    /// created against a host (the provider type is then carried by the host).
30    #[serde(default)]
31    pub provider_type: Option<String>,
32    pub owner_account_id: String,
33    /// `PENDING` | `AVAILABLE` | `ERROR`. A freshly-created connection is
34    /// `PENDING` until its console handshake completes.
35    pub connection_status: String,
36    #[serde(default)]
37    pub host_arn: Option<String>,
38    #[serde(default)]
39    pub tags: TagMap,
40}
41
42/// A self-managed host for an installed provider type (e.g. GitHub Enterprise
43/// Server, GitLab Self Managed).
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct HostRecord {
46    pub host_arn: String,
47    pub name: String,
48    pub provider_type: String,
49    pub provider_endpoint: String,
50    /// `PENDING` | `AVAILABLE` | `VPC_CONFIG_INITIALIZING` |
51    /// `VPC_CONFIG_FAILED_INITIALIZATION` | `VPC_CONFIG_DELETING` | `ERROR`.
52    pub status: String,
53    /// Raw `VpcConfiguration` value, round-tripped verbatim on describe.
54    #[serde(default)]
55    pub vpc_configuration: Option<Value>,
56    #[serde(default)]
57    pub tags: TagMap,
58}
59
60/// A link between a connection and a specific third-party repository.
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct RepositoryLinkRecord {
63    pub repository_link_id: String,
64    pub repository_link_arn: String,
65    pub connection_arn: String,
66    pub owner_id: String,
67    pub repository_name: String,
68    pub provider_type: String,
69    #[serde(default)]
70    pub encryption_key_arn: Option<String>,
71    #[serde(default)]
72    pub tags: TagMap,
73}
74
75/// A sync configuration (CloudFormation Git sync) tying a resource to a branch
76/// of a linked repository. Keyed by `(sync_type, resource_name)`.
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct SyncConfigurationRecord {
79    pub branch: String,
80    #[serde(default)]
81    pub config_file: Option<String>,
82    pub owner_id: String,
83    pub provider_type: String,
84    pub repository_link_id: String,
85    pub repository_name: String,
86    pub resource_name: String,
87    pub role_arn: String,
88    pub sync_type: String,
89    #[serde(default)]
90    pub publish_deployment_status: Option<String>,
91    #[serde(default)]
92    pub trigger_resource_update_on: Option<String>,
93    #[serde(default)]
94    pub pull_request_comment: Option<String>,
95}
96
97/// The account-scoped CodeConnections state for one AWS account.
98#[derive(Debug, Clone, Default, Serialize, Deserialize)]
99pub struct CodeConnectionsState {
100    /// Connections keyed by connection ARN.
101    #[serde(default)]
102    pub connections: BTreeMap<String, ConnectionRecord>,
103    /// Hosts keyed by host ARN.
104    #[serde(default)]
105    pub hosts: BTreeMap<String, HostRecord>,
106    /// Repository links keyed by repository-link id.
107    #[serde(default)]
108    pub repository_links: BTreeMap<String, RepositoryLinkRecord>,
109    /// Sync configurations keyed by `<syncType>\u{1}<resourceName>`.
110    #[serde(default)]
111    pub sync_configurations: BTreeMap<String, SyncConfigurationRecord>,
112}
113
114impl AccountState for CodeConnectionsState {
115    fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
116        Self::default()
117    }
118}
119
120pub type SharedCodeConnectionsState = Arc<RwLock<MultiAccountState<CodeConnectionsState>>>;
121
122#[derive(Debug, Serialize, Deserialize)]
123pub struct CodeConnectionsSnapshot {
124    pub schema_version: u32,
125    pub accounts: MultiAccountState<CodeConnectionsState>,
126}
127
128/// Composite key joiner using a control char that cannot appear in identifiers.
129pub fn skey(sync_type: &str, resource_name: &str) -> String {
130    format!("{sync_type}\u{1}{resource_name}")
131}