Skip to main content

gcloud_spanner/admin/
mod.rs

1use std::env::var;
2use std::time::Duration;
3
4use google_cloud_gax::conn::Environment;
5use google_cloud_gax::grpc::Code;
6use google_cloud_gax::retry::RetrySetting;
7use token_source::NoopTokenSourceProvider;
8
9pub mod client;
10pub mod database;
11pub mod instance;
12
13pub const SCOPES: [&str; 2] = [
14    "https://www.googleapis.com/auth/cloud-platform",
15    "https://www.googleapis.com/auth/spanner.admin",
16];
17
18pub struct AdminClientConfig {
19    /// Runtime project
20    pub environment: Environment,
21    /// Timeout applied to each gRPC request on the admin channel.
22    pub timeout: Duration,
23    /// Timeout for establishing a new gRPC connection.
24    pub connect_timeout: Duration,
25    pub http2_keep_alive_interval: Option<Duration>,
26    pub keep_alive_timeout: Option<Duration>,
27    pub keep_alive_while_idle: Option<bool>,
28}
29
30impl Default for AdminClientConfig {
31    fn default() -> Self {
32        AdminClientConfig {
33            environment: match var("SPANNER_EMULATOR_HOST").ok() {
34                Some(v) => Environment::Emulator(v),
35                None => Environment::GoogleCloud(Box::new(NoopTokenSourceProvider {})),
36            },
37            timeout: Duration::from_secs(30),
38            connect_timeout: Duration::from_secs(30),
39            http2_keep_alive_interval: None,
40            keep_alive_timeout: None,
41            keep_alive_while_idle: None,
42        }
43    }
44}
45
46#[cfg(feature = "auth")]
47pub use google_cloud_auth;
48
49#[cfg(feature = "auth")]
50impl AdminClientConfig {
51    pub async fn with_auth(mut self) -> Result<Self, google_cloud_auth::error::Error> {
52        if let Environment::GoogleCloud(_) = self.environment {
53            let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new(Self::auth_config()).await?;
54            self.environment = Environment::GoogleCloud(Box::new(ts))
55        }
56        Ok(self)
57    }
58
59    pub async fn with_credentials(
60        mut self,
61        credentials: google_cloud_auth::credentials::CredentialsFile,
62    ) -> Result<Self, google_cloud_auth::error::Error> {
63        if let Environment::GoogleCloud(_) = self.environment {
64            let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new_with_credentials(
65                Self::auth_config(),
66                Box::new(credentials),
67            )
68            .await?;
69            self.environment = Environment::GoogleCloud(Box::new(ts))
70        }
71        Ok(self)
72    }
73
74    fn auth_config() -> google_cloud_auth::project::Config<'static> {
75        google_cloud_auth::project::Config::default()
76            .with_audience(crate::apiv1::conn_pool::AUDIENCE)
77            .with_scopes(&crate::apiv1::conn_pool::SCOPES)
78    }
79}
80
81pub fn default_retry_setting() -> RetrySetting {
82    RetrySetting {
83        from_millis: 50,
84        max_delay: Some(Duration::from_secs(10)),
85        factor: 1u64,
86        take: 20,
87        codes: vec![Code::Unavailable, Code::Unknown, Code::DeadlineExceeded],
88    }
89}