From dc45fd8df0996ef82d7e869c696b5156acf8d872 Mon Sep 17 00:00:00 2001 From: Marcin Lachowicz Date: Tue, 12 Sep 2023 16:37:37 +0200 Subject: [PATCH] feat: add support for Datastore database name configuration (#2150) Datastore part of #2145. --- docs/src/main/asciidoc/datastore.adoc | 1 + .../datastore/GcpDatastoreAutoConfiguration.java | 7 +++++++ .../datastore/GcpDatastoreProperties.java | 10 ++++++++++ .../datastore/GcpDatastoreAutoConfigurationTests.java | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/docs/src/main/asciidoc/datastore.adoc b/docs/src/main/asciidoc/datastore.adoc index 018d744835..db30a04b73 100644 --- a/docs/src/main/asciidoc/datastore.adoc +++ b/docs/src/main/asciidoc/datastore.adoc @@ -68,6 +68,7 @@ The following configuration options are available: | Name | Description | Required | Default value | `spring.cloud.gcp.datastore.enabled` | Enables the Cloud Datastore client | No | `true` | `spring.cloud.gcp.datastore.project-id` | Google Cloud project ID where the Google Cloud Datastore API is hosted, if different from the one in the <> | No | +| `spring.cloud.gcp.datastore.database-id` | Google Cloud project can host multiple databases. You can specify which database will be used. | No | | `spring.cloud.gcp.datastore.credentials.location` | OAuth2 credentials for authenticating with the Google Cloud Datastore API, if different from the ones in the <> | No | | `spring.cloud.gcp.datastore.credentials.encoded-key` | Base64-encoded OAuth2 credentials for authenticating with the Google Cloud Datastore API, if different from the ones in the <> | No | | `spring.cloud.gcp.datastore.credentials.scopes` | https://developers.google.com/identity/protocols/googlescopes[OAuth2 scope] for Spring Framework on Google CloudDatastore credentials | No | https://www.googleapis.com/auth/datastore diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfiguration.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfiguration.java index 512914998b..24f3509501 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfiguration.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfiguration.java @@ -70,6 +70,8 @@ public class GcpDatastoreAutoConfiguration { private final String namespace; + private final String databaseId; + private final Credentials credentials; private final String host; @@ -85,6 +87,7 @@ public class GcpDatastoreAutoConfiguration { ? gcpDatastoreProperties.getProjectId() : projectIdProvider.getProjectId(); this.namespace = gcpDatastoreProperties.getNamespace(); + this.databaseId = gcpDatastoreProperties.getDatabaseId(); String hostToConnect = gcpDatastoreProperties.getHost(); if (gcpDatastoreProperties.getEmulator().isEnabled()) { @@ -193,6 +196,10 @@ private Datastore getDatastore(String namespace) { builder.setNamespace(namespace); } + if (databaseId != null) { + builder.setDatabaseId(databaseId); + } + if (this.host != null) { builder.setHost(this.host); } diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreProperties.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreProperties.java index 1e9c73e236..df8ef8aa64 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreProperties.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreProperties.java @@ -42,6 +42,8 @@ public class GcpDatastoreProperties implements CredentialsSupplier { private String projectId; + private String databaseId; + private String namespace; @Override @@ -61,6 +63,14 @@ public void setProjectId(String projectId) { this.projectId = projectId; } + public String getDatabaseId() { + return databaseId; + } + + public void setDatabaseId(String databaseId) { + this.databaseId = databaseId; + } + public String getNamespace() { return this.namespace; } diff --git a/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfigurationTests.java b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfigurationTests.java index bd38cf73b2..e65a028dd4 100644 --- a/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfigurationTests.java +++ b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/datastore/GcpDatastoreAutoConfigurationTests.java @@ -62,6 +62,7 @@ class GcpDatastoreAutoConfigurationTests { .withUserConfiguration(TestConfiguration.class) .withPropertyValues( "spring.cloud.gcp.datastore.project-id=test-project", + "spring.cloud.gcp.datastore.database-id=test-database", "spring.cloud.gcp.datastore.namespace=testNamespace", "spring.cloud.gcp.datastore.host=localhost:8081", "management.health.datastore.enabled=false"); @@ -74,6 +75,7 @@ void testUserDatastoreBean() { .withUserConfiguration(TestConfigurationWithDatastoreBean.class) .withPropertyValues( "spring.cloud.gcp.datastore.project-id=test-project", + "spring.cloud.gcp.datastore.database-id=test-database", "spring.cloud.gcp.datastore.namespace=testNamespace", "spring.cloud.gcp.datastore.host=localhost:8081", "management.health.datastore.enabled=false"); @@ -94,6 +96,7 @@ void testUserDatastoreBeanNamespace() { .withUserConfiguration(TestConfigurationWithDatastoreBeanNamespaceProvider.class) .withPropertyValues( "spring.cloud.gcp.datastore.project-id=test-project", + "spring.cloud.gcp.datastore.database-id=test-database", "spring.cloud.gcp.datastore.namespace=testNamespace", "spring.cloud.gcp.datastore.host=localhost:8081", "management.health.datastore.enabled=false"); @@ -114,6 +117,7 @@ void testDatastoreOptionsCorrectlySet() { context -> { DatastoreOptions datastoreOptions = getDatastoreBean(context).getOptions(); assertThat(datastoreOptions.getProjectId()).isEqualTo("test-project"); + assertThat(datastoreOptions.getDatabaseId()).isEqualTo("test-database"); assertThat(datastoreOptions.getNamespace()).isEqualTo("testNamespace"); assertThat(datastoreOptions.getHost()).isEqualTo("localhost:8081"); });