Skip to main content

rusty_cdk_core/appsync/
builder.rs

1use crate::appsync::{
2    AppSyncApi, AppSyncApiProperties, AppSyncApiRef, AppSyncApiType, AppSyncAuthMode, AuthProvider, ChannelNamespace,
3    ChannelNamespaceProperties, ChannelNamespaceRef, ChannelNamespaceType, CognitoConfig, EventConfig, EventLogConfig,
4    LambdaAuthorizerConfig, OpenIDConnectConfig,
5};
6use crate::shared::Id;
7use crate::stack::{Resource, StackBuilder};
8use crate::wrappers::{AppSyncApiName, ChannelNamespaceName};
9use serde_json::Value;
10
11// TODO add api key builder + DTO
12
13pub struct AppSyncApiBuilder {
14    id: Id,
15    name: String,
16    event_config: Option<EventConfig>,
17}
18
19impl AppSyncApiBuilder {
20    pub fn new(id: &str, app_sync_api_name: AppSyncApiName) -> Self {
21        Self {
22            id: Id(id.to_string()),
23            name: app_sync_api_name.0,
24            event_config: None,
25        }
26    }
27
28    pub fn event_config(self, event_config: EventConfig) -> Self {
29        Self {
30            event_config: Some(event_config),
31            ..self
32        }
33    }
34
35    pub fn build(self, stack_builder: &mut StackBuilder) -> AppSyncApiRef {
36        let resource_id = Resource::generate_id("AppSyncApi");
37        let api = AppSyncApi {
38            id: self.id,
39            resource_id: resource_id.clone(),
40            r#type: AppSyncApiType::AppSyncApiType,
41            properties: AppSyncApiProperties {
42                name: self.name,
43                event_config: self.event_config,
44            },
45        };
46        stack_builder.add_resource(api);
47
48        AppSyncApiRef::internal_new(resource_id)
49    }
50}
51
52pub struct EventConfigBuilder {
53    auth_providers: Vec<AuthProvider>,
54    connection_auth_modes: Vec<AppSyncAuthMode>,
55    default_auth_modes: Vec<AppSyncAuthMode>,
56    default_subscribe_auth_modes: Vec<AppSyncAuthMode>,
57    log_config: Option<EventLogConfig>,
58}
59
60impl EventConfigBuilder {
61    pub fn new(
62        auth_providers: Vec<AuthProvider>,
63        connection_auth_modes: Vec<AuthMode>,
64        default_auth_modes: Vec<AuthMode>,
65        default_subscribe_auth_modes: Vec<AuthMode>,
66    ) -> Self {
67        Self {
68            auth_providers,
69            connection_auth_modes: connection_auth_modes.into_iter().map(Into::into).collect(),
70            default_auth_modes: default_auth_modes.into_iter().map(Into::into).collect(),
71            default_subscribe_auth_modes: default_subscribe_auth_modes.into_iter().map(Into::into).collect(),
72            log_config: None,
73        }
74    }
75
76    pub fn log_config(self, config: EventLogConfig) -> Self {
77        Self {
78            log_config: Some(config),
79            ..self
80        }
81    }
82
83    pub fn build(self) -> EventConfig {
84        EventConfig {
85            auth_providers: self.auth_providers,
86            connection_auth_modes: self.connection_auth_modes,
87            default_auth_modes: self.default_auth_modes,
88            default_subscribe_auth_modes: self.default_subscribe_auth_modes,
89            log_config: self.log_config,
90        }
91    }
92}
93
94pub enum AuthType {
95    AmazonCognitoUserPools(CognitoConfig),
96    AwsIam,
97    ApiKey,
98    OpenidConnect(OpenIDConnectConfig),
99    AwsLambda(LambdaAuthorizerConfig),
100}
101
102#[derive(Debug, Clone)]
103pub enum AuthMode {
104    AmazonCognitoUserPools,
105    AwsIam,
106    ApiKey,
107    OpenidConnect,
108    AwsLambda,
109}
110
111impl From<AuthMode> for AppSyncAuthMode {
112    fn from(mode: AuthMode) -> AppSyncAuthMode {
113        match mode {
114            AuthMode::AmazonCognitoUserPools => AppSyncAuthMode {
115                auth_type: Some("AMAZON_COGNITO_USER_POOLS".to_string()),
116            },
117            AuthMode::AwsIam => AppSyncAuthMode {
118                auth_type: Some("AWS_IAM".to_string()),
119            },
120            AuthMode::ApiKey => AppSyncAuthMode {
121                auth_type: Some("API_KEY".to_string()),
122            },
123            AuthMode::OpenidConnect => AppSyncAuthMode {
124                auth_type: Some("OPENID_CONNECT".to_string()),
125            },
126            AuthMode::AwsLambda => AppSyncAuthMode {
127                auth_type: Some("AWS_LAMBDA".to_string()),
128            },
129        }
130    }
131}
132
133pub struct AuthProviderBuilder {
134    auth_type: String,
135    cognito_config: Option<CognitoConfig>,
136    lambda_auth_config: Option<LambdaAuthorizerConfig>,
137    open_id_connect_config: Option<OpenIDConnectConfig>,
138}
139
140impl AuthProviderBuilder {
141    pub fn new(auth_type: AuthType) -> Self {
142        match auth_type {
143            AuthType::AmazonCognitoUserPools(c) => Self {
144                auth_type: "AMAZON_COGNITO_USER_POOLS".to_string(),
145                cognito_config: Some(c),
146                lambda_auth_config: None,
147                open_id_connect_config: None,
148            },
149            AuthType::AwsIam => Self {
150                auth_type: "AWS_IAM".to_string(),
151                cognito_config: None,
152                lambda_auth_config: None,
153                open_id_connect_config: None,
154            },
155            AuthType::ApiKey => Self {
156                auth_type: "API_KEY".to_string(),
157                cognito_config: None,
158                lambda_auth_config: None,
159                open_id_connect_config: None,
160            },
161            AuthType::OpenidConnect(c) => Self {
162                auth_type: "OPENID_CONNECT".to_string(),
163                open_id_connect_config: Some(c),
164                cognito_config: None,
165                lambda_auth_config: None,
166            },
167            AuthType::AwsLambda(c) => Self {
168                auth_type: "AWS_LAMBDA".to_string(),
169                lambda_auth_config: Some(c),
170                cognito_config: None,
171                open_id_connect_config: None,
172            },
173        }
174    }
175
176    pub fn build(self) -> AuthProvider {
177        AuthProvider {
178            auth_type: self.auth_type,
179            cognito_config: self.cognito_config,
180            lambda_auth_config: self.lambda_auth_config,
181            open_id_connect_config: self.open_id_connect_config,
182        }
183    }
184}
185
186#[derive(Debug, Clone)]
187pub enum AppSyncApiLogLevel {
188    None,
189    Error,
190    All,
191    Info,
192    Debug,
193}
194
195impl From<AppSyncApiLogLevel> for String {
196    fn from(api: AppSyncApiLogLevel) -> String {
197        match api {
198            AppSyncApiLogLevel::None => "NONE".to_string(),
199            AppSyncApiLogLevel::Error => "ERROR".to_string(),
200            AppSyncApiLogLevel::All => "ALL".to_string(),
201            AppSyncApiLogLevel::Info => "INFO".to_string(),
202            AppSyncApiLogLevel::Debug => "DEBUG".to_string(),
203        }
204    }
205}
206
207pub struct EventLogConfigBuilder {
208    cloudwatch_logs_role_arn: String,
209    log_level: String,
210}
211
212impl EventLogConfigBuilder {
213    pub fn new(cloudwatch_logs_role_arn: String, log_level: AppSyncApiLogLevel) -> Self {
214        Self {
215            cloudwatch_logs_role_arn,
216            log_level: log_level.into(),
217        }
218    }
219
220    pub fn build(self) -> EventLogConfig {
221        EventLogConfig {
222            cloudwatch_logs_role_arn: self.cloudwatch_logs_role_arn,
223            log_level: self.log_level,
224        }
225    }
226}
227
228pub struct ChannelNamespaceBuilder {
229    id: Id,
230    api_id: Value,
231    name: String,
232    publish_auth_modes: Option<Vec<AppSyncAuthMode>>,
233    subscribe_auth_modes: Option<Vec<AppSyncAuthMode>>,
234}
235
236impl ChannelNamespaceBuilder {
237    pub fn new(id: &str, api_id: &AppSyncApiRef, name: ChannelNamespaceName) -> Self {
238        Self {
239            id: Id(id.to_string()),
240            api_id: api_id.get_att("ApiId"),
241            name: name.0,
242            publish_auth_modes: None,
243            subscribe_auth_modes: None,
244        }
245    }
246
247    pub fn publish_auth_modes(self, publish_auth_modes: Vec<AuthMode>) -> Self {
248        Self {
249            publish_auth_modes: Some(publish_auth_modes.into_iter().map(Into::into).collect()),
250            ..self
251        }
252    }
253
254    pub fn subscribe_auth_modes(self, subscribe_auth_modes: Vec<AuthMode>) -> Self {
255        Self {
256            subscribe_auth_modes: Some(subscribe_auth_modes.into_iter().map(Into::into).collect()),
257            ..self
258        }
259    }
260
261    pub fn build(self, stack_builder: &mut StackBuilder) -> ChannelNamespaceRef {
262        let resource_id = Resource::generate_id("ChannelNamespace");
263        let channel = ChannelNamespace {
264            id: self.id,
265            resource_id: resource_id.clone(),
266            r#type: ChannelNamespaceType::ChannelNamespaceType,
267            properties: ChannelNamespaceProperties {
268                api_id: self.api_id,
269                name: self.name,
270                publish_auth_modes: self.publish_auth_modes,
271                subscribe_auth_modes: self.subscribe_auth_modes,
272            },
273        };
274        stack_builder.add_resource(channel);
275
276        ChannelNamespaceRef::internal_new(resource_id)
277    }
278}