Skip to content

Commit

Permalink
feat: add support for Datastore database name configuration (#2150)
Browse files Browse the repository at this point in the history
Datastore part of #2145.
  • Loading branch information
MarcinLachowicz authored Sep 12, 2023
1 parent 170948e commit dc45fd8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/datastore.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<spring-cloud-gcp-core,Spring Framework on Google Cloud Core Module>> | 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 <<spring-cloud-gcp-core,Spring Framework on Google Cloud Core Module>> | 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 <<spring-cloud-gcp-core,Spring Framework on Google Cloud Core Module>> | 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class GcpDatastoreAutoConfiguration {

private final String namespace;

private final String databaseId;

private final Credentials credentials;

private final String host;
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class GcpDatastoreProperties implements CredentialsSupplier {

private String projectId;

private String databaseId;

private String namespace;

@Override
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
});
Expand Down

0 comments on commit dc45fd8

Please sign in to comment.