Skip to main content

feldera_types/transport/
pubsub.rs

1use serde::{Deserialize, Serialize};
2use utoipa::ToSchema;
3
4use crate::{
5    duration::{ConfigDuration, LegacyUnit},
6    duration_setting,
7};
8
9duration_setting!(duration_timeout, "timeout", LegacyUnit::Secs);
10duration_setting!(
11    duration_connect_timeout,
12    "connect_timeout",
13    LegacyUnit::Secs
14);
15
16// Subscription options docs: https://cloud.google.com/pubsub/docs/subscription-properties
17
18/// Google Pub/Sub input connector configuration.
19#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
20pub struct PubSubInputConfig {
21    /// Set in order to use a Pub/Sub [emulator](https://cloud.google.com/pubsub/docs/emulator)
22    /// instead of the production service, e.g., 'localhost:8681'.
23    pub emulator: Option<String>,
24
25    /// The content of a Google Cloud credentials JSON file.
26    ///
27    /// When this option is specified, the connector will use the provided credentials for
28    /// authentication.  Otherwise, it will use Application Default Credentials (ADC) configured
29    /// in the environment where the Feldera service is running.  See
30    /// [Google Cloud documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc)
31    /// for information on configuring application default credentials.
32    ///
33    /// When running Feldera in an environment where ADC are not configured,
34    /// e.g., a Docker container, use this option to ship Google Cloud credentials from another environment.
35    /// For example, if you use the
36    /// [`gcloud auth application-default login`](https://cloud.google.com/pubsub/docs/authentication#client-libs)
37    /// command for authentication in your local development environment, ADC are stored in the
38    /// `.config/gcloud/application_default_credentials.json` file in your home directory.
39    pub credentials: Option<String>,
40
41    /// Override the default service endpoint 'pubsub.googleapis.com'
42    pub endpoint: Option<String>,
43
44    /// gRPC channel pool size.
45    pub pool_size: Option<u32>,
46
47    /// gRPC request timeout, for example `30s`.
48    #[serde(
49        default,
50        alias = "timeout_seconds",
51        deserialize_with = "duration_timeout",
52        skip_serializing_if = "Option::is_none"
53    )]
54    #[schema(value_type = Option<String>)]
55    pub timeout: Option<ConfigDuration>,
56
57    /// gRPC connection timeout, for example `10s`.
58    #[serde(
59        default,
60        alias = "connect_timeout_seconds",
61        deserialize_with = "duration_connect_timeout",
62        skip_serializing_if = "Option::is_none"
63    )]
64    #[schema(value_type = Option<String>)]
65    pub connect_timeout: Option<ConfigDuration>,
66
67    /// Google Cloud project_id.
68    ///
69    /// When not specified, the connector will use the project id associated
70    /// with the authenticated account.
71    pub project_id: Option<String>,
72
73    /// Subscription name.
74    pub subscription: String,
75
76    /// Reset subscription's backlog to a given snapshot on startup,
77    /// using the Pub/Sub `Seek` API.
78    ///
79    /// This option is mutually exclusive with the `timestamp` option.
80    pub snapshot: Option<String>,
81
82    /// Reset subscription's backlog to a given timestamp on startup,
83    /// using the Pub/Sub `Seek` API.
84    ///
85    /// The value of this option is an ISO 8601-encoded UTC time, e.g., "2024-08-17T16:39:57-08:00".
86    ///
87    /// This option is mutually exclusive with the `snapshot` option.
88    pub timestamp: Option<String>,
89}
90
91impl crate::sensitive::SensitiveValues for PubSubInputConfig {
92    fn visit_sensitive_values(&self, visit: &mut dyn FnMut(&str, &str)) {
93        crate::sensitive::visit_optional("credentials", self.credentials.as_deref(), visit);
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    /// Wraps the fields under test in the `subscription` key the config
102    /// requires.
103    fn config_json(fields: &str) -> String {
104        let separator = if fields.is_empty() { "" } else { ", " };
105        format!(r#"{{"subscription": "test_subscription"{separator}{fields}}}"#)
106    }
107
108    /// The gRPC request timeout has no default of its own: with neither field
109    /// set the accessor yields `None`, which leaves the Pub/Sub client's own
110    /// timeout in place.  Both spellings reach one field, so writing both is
111    /// rejected as a duplicate.
112    #[test]
113    fn timeout_accepts_both_spellings() {
114        for (fields, expected) in [
115            ("", None),
116            (
117                r#""timeout": "750ms""#,
118                Some(ConfigDuration::from_millis(750)),
119            ),
120            (
121                r#""timeout_seconds": 20"#,
122                Some(ConfigDuration::from_secs(20)),
123            ),
124        ] {
125            let json = config_json(fields);
126            let config: PubSubInputConfig = serde_json::from_str(&json).unwrap();
127            assert_eq!(config.timeout, expected, "parsing {json}");
128        }
129    }
130
131    /// See [`timeout_accepts_both_spellings`].
132    #[test]
133    fn connect_timeout_accepts_both_spellings() {
134        for (fields, expected) in [
135            ("", None),
136            (
137                r#""connect_timeout": "5s""#,
138                Some(ConfigDuration::from_secs(5)),
139            ),
140            (
141                r#""connect_timeout_seconds": 30"#,
142                Some(ConfigDuration::from_secs(30)),
143            ),
144        ] {
145            let json = config_json(fields);
146            let config: PubSubInputConfig = serde_json::from_str(&json).unwrap();
147            assert_eq!(config.connect_timeout, expected, "parsing {json}");
148        }
149    }
150}