Skip to content

Commit

Permalink
Directly authenticate Spanner Admin Client (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-james-dev authored Aug 12, 2024
1 parent 5fc08f8 commit 1e98ed4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
38 changes: 38 additions & 0 deletions spanner/src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ impl Default for AdminClientConfig {
}
}

#[cfg(feature = "auth")]
pub use google_cloud_auth;

#[cfg(feature = "auth")]
impl AdminClientConfig {
pub async fn with_auth(mut self) -> Result<Self, google_cloud_auth::error::Error> {
if let Environment::GoogleCloud(_) = self.environment {
let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new(Self::auth_config()).await?;
self.environment = Environment::GoogleCloud(Box::new(ts))
}
Ok(self)
}

pub async fn with_credentials(
mut self,
credentials: google_cloud_auth::credentials::CredentialsFile,
) -> Result<Self, google_cloud_auth::error::Error> {
if let Environment::GoogleCloud(_) = self.environment {
let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new_with_credentials(
Self::auth_config(),
Box::new(credentials),
)
.await?;
self.environment = Environment::GoogleCloud(Box::new(ts))
}
Ok(self)
}

fn auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: Some(crate::apiv1::conn_pool::AUDIENCE),
scopes: Some(&crate::apiv1::conn_pool::SCOPES),
sub: None,
..Default::default()
}
}
}

pub fn default_retry_setting() -> RetrySetting {
RetrySetting {
from_millis: 50,
Expand Down
5 changes: 5 additions & 0 deletions spanner/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ where
}
}

/// Return metadata for all columns
pub fn columns_metadata(&self) -> &Arc<Vec<Field>> {
&self.rs.fields
}

pub fn column_metadata(&self, column_name: &str) -> Option<(usize, Field)> {
for (i, val) in self.rs.fields.iter().enumerate() {
if val.name == column_name {
Expand Down

0 comments on commit 1e98ed4

Please sign in to comment.