gcloud_spanner/admin/
mod.rs1use 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 pub environment: Environment,
21}
22
23impl Default for AdminClientConfig {
24 fn default() -> Self {
25 AdminClientConfig {
26 environment: match var("SPANNER_EMULATOR_HOST").ok() {
27 Some(v) => Environment::Emulator(v),
28 None => Environment::GoogleCloud(Box::new(NoopTokenSourceProvider {})),
29 },
30 }
31 }
32}
33
34#[cfg(feature = "auth")]
35pub use google_cloud_auth;
36
37#[cfg(feature = "auth")]
38impl AdminClientConfig {
39 pub async fn with_auth(mut self) -> Result<Self, google_cloud_auth::error::Error> {
40 if let Environment::GoogleCloud(_) = self.environment {
41 let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new(Self::auth_config()).await?;
42 self.environment = Environment::GoogleCloud(Box::new(ts))
43 }
44 Ok(self)
45 }
46
47 pub async fn with_credentials(
48 mut self,
49 credentials: google_cloud_auth::credentials::CredentialsFile,
50 ) -> Result<Self, google_cloud_auth::error::Error> {
51 if let Environment::GoogleCloud(_) = self.environment {
52 let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new_with_credentials(
53 Self::auth_config(),
54 Box::new(credentials),
55 )
56 .await?;
57 self.environment = Environment::GoogleCloud(Box::new(ts))
58 }
59 Ok(self)
60 }
61
62 fn auth_config() -> google_cloud_auth::project::Config<'static> {
63 google_cloud_auth::project::Config::default()
64 .with_audience(crate::apiv1::conn_pool::AUDIENCE)
65 .with_scopes(&crate::apiv1::conn_pool::SCOPES)
66 }
67}
68
69pub fn default_retry_setting() -> RetrySetting {
70 RetrySetting {
71 from_millis: 50,
72 max_delay: Some(Duration::from_secs(10)),
73 factor: 1u64,
74 take: 20,
75 codes: vec![Code::Unavailable, Code::Unknown, Code::DeadlineExceeded],
76 }
77}