From 59c478afcd4820f46ff9ba9abb24698fe34c4b72 Mon Sep 17 00:00:00 2001 From: Lex Cao Date: Sun, 16 Jun 2024 02:04:56 +0800 Subject: [PATCH] Support credential JSON for Spanner Closes #1941 Signed-off-by: Lex Cao --- internal/datastore/spanner/options.go | 9 +++++++++ internal/datastore/spanner/spanner.go | 1 + 2 files changed, 10 insertions(+) diff --git a/internal/datastore/spanner/options.go b/internal/datastore/spanner/options.go index 8651ea078f..150d7ec1d9 100644 --- a/internal/datastore/spanner/options.go +++ b/internal/datastore/spanner/options.go @@ -14,6 +14,7 @@ type spannerOptions struct { followerReadDelay time.Duration maxRevisionStalenessPercent float64 credentialsFilePath string + credentialsJSON []byte emulatorHost string disableStats bool readMaxOpen int @@ -141,6 +142,14 @@ func CredentialsFile(path string) Option { } } +// CredentialsJSON is the json containing credentials for a service +// account that can access the cloud spanner instance +func CredentialsJSON(json []byte) Option { + return func(so *spannerOptions) { + so.credentialsJSON = json + } +} + // EmulatorHost is the URI of a Spanner emulator to connect to for // development and testing use func EmulatorHost(uri string) Option { diff --git a/internal/datastore/spanner/spanner.go b/internal/datastore/spanner/spanner.go index 2296368d15..33331adedc 100644 --- a/internal/datastore/spanner/spanner.go +++ b/internal/datastore/spanner/spanner.go @@ -144,6 +144,7 @@ func NewSpannerDatastore(ctx context.Context, database string, opts ...Option) ( client, err := spanner.NewClientWithConfig(context.Background(), database, spanner.ClientConfig{SessionPoolConfig: cfg}, option.WithCredentialsFile(config.credentialsFilePath), + option.WithCredentialsJSON(config.credentialsJSON), option.WithGRPCConnectionPool(max(config.readMaxOpen, config.writeMaxOpen)), option.WithGRPCDialOption( grpc.WithStatsHandler(otelgrpc.NewClientHandler()),