Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Add SpannerOptions auto-configuration for emulator #2356

Merged
merged 5 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/src/main/asciidoc/spanner.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ The schema for the tables will be "ON DELETE NO ACTION" if `false`. | No | `true
| `spring.cloud.gcp.spanner.writeSessionsFraction` | Fraction of sessions to be kept prepared for write transactions | No | 0.2 - Determined by Cloud Spanner client library
| `spring.cloud.gcp.spanner.keepAliveIntervalMinutes` | How long to keep idle sessions alive | No | 30 - Determined by Cloud Spanner client library
| `spring.cloud.gcp.spanner.failIfPoolExhausted` | If all sessions are in use, fail the request by throwing an exception. Otherwise, by default, block until a session becomes available. | No | `false`
| `spring.cloud.gcp.spanner.emulator.enabled` | Enables the usage of an emulator. If this is set to true, then you should set the `spring.cloud.gcp.spanner.emulator-host` to the host:port of your locally running emulator instance. | No | `false`
| `spring.cloud.gcp.spanner.emulator-host` | The host and port of the Spanner emulator; can be overridden to specify connecting to an already-running https://cloud.google.com/spanner/docs/emulator#installing_and_running_the_emulator[Spanner emulator] instance. | No | `localhost:9010`
|===

==== Repository settings
Expand Down Expand Up @@ -1435,10 +1437,11 @@ This command can be used to create Cloud Spanner instances:
$ gcloud spanner instances create <instance-name> --config=emulator-config --description="<description>" --nodes=1
----

Remember that you need to set the `SPANNER_EMULATOR_HOST` environment variable to use the emulator:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want to remove this.
Setting/unsetting the variable is a quick way to switch all your tests to using the emulator and back to normal. Might be still useful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with removing it. Even though it's available, it's not consistent with the other emulators. We can leave a note about it, but I'm afraid it will only confuse people.

Once the Spanner emulator is running, ensure that the following properties are set in your `application.properties` of your Spring application:

----
$ export SPANNER_EMULATOR_HOST=localhost:9010
spring.cloud.gcp.spanner.emulator.enabled=true
spring.cloud.gcp.spanner.emulator-host=${EMULATOR_HOSTPORT}
----

=== Sample
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gcp.autoconfigure.spanner;

import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.SpannerOptions;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
* Provides auto-configuration to use the Spanner emulator if enabled.
*
* @author Eddú Meléndez
* @since 1.2.3
*/
@Configuration
@AutoConfigureBefore(GcpSpannerAutoConfiguration.class)
@EnableConfigurationProperties(GcpSpannerProperties.class)
@ConditionalOnProperty(prefix = "spring.cloud.gcp.spanner.emulator", name = "enabled", havingValue = "true")
public class GcpSpannerEmulatorAutoConfiguration {

private final GcpSpannerProperties properties;

public GcpSpannerEmulatorAutoConfiguration(GcpSpannerProperties properties) {
this.properties = properties;
}

@Bean
@ConditionalOnMissingBean
public SpannerOptions spannerOptions() {
Assert.notNull(this.properties.getEmulatorHost(), "`spring.cloud.gcp.spanner.emulator-host` must be set.");
return SpannerOptions.newBuilder()
.setProjectId(this.properties.getProjectId())
.setCredentials(NoCredentials.getInstance())
.setEmulatorHost(this.properties.getEmulatorHost()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* Settings for Spring Data Cloud Spanner.
* @author Chengyuan Zhao
* @author Ray Tsang
* @author Eddú Meléndez
* @author Mike Eltsufin
*/
@ConfigurationProperties("spring.cloud.gcp.spanner")
public class GcpSpannerProperties implements CredentialsSupplier {
Expand Down Expand Up @@ -70,6 +72,9 @@ public class GcpSpannerProperties implements CredentialsSupplier {
// Otherwise, by default, block until a session becomes available.
private boolean failIfPoolExhausted = false;

// Host:port used to connect to the emulator, when the emulator is enabled.
private String emulatorHost = "localhost:9010";

public Credentials getCredentials() {
return this.credentials;
}
Expand Down Expand Up @@ -171,4 +176,12 @@ public boolean isFailIfPoolExhausted() {
public void setFailIfPoolExhausted(boolean failIfPoolExhausted) {
this.failIfPoolExhausted = failIfPoolExhausted;
}

public String getEmulatorHost() {
return this.emulatorHost;
}

public void setEmulatorHost(String emulatorHost) {
this.emulatorHost = emulatorHost;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"description": "Auto-configure Google Cloud Spanner components.",
"defaultValue": true
},
{
"name": "spring.cloud.gcp.spanner.emulator.enabled",
"type": "java.lang.Boolean",
"description": "Enables auto-configuration to use the Spanner emulator.",
"defaultValue": false
},
{
"name": "spring.cloud.gcp.sql.enabled",
"type": "java.lang.Boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ org.springframework.cloud.gcp.autoconfigure.logging.StackdriverLoggingAutoConfig
org.springframework.cloud.gcp.autoconfigure.pubsub.GcpPubSubAutoConfiguration,\
org.springframework.cloud.gcp.autoconfigure.pubsub.GcpPubSubReactiveAutoConfiguration,\
org.springframework.cloud.gcp.autoconfigure.spanner.GcpSpannerAutoConfiguration,\
org.springframework.cloud.gcp.autoconfigure.spanner.GcpSpannerEmulatorAutoConfiguration,\
org.springframework.cloud.gcp.autoconfigure.datastore.GcpDatastoreAutoConfiguration,\
org.springframework.cloud.gcp.autoconfigure.firestore.GcpFirestoreAutoConfiguration,\
org.springframework.cloud.gcp.autoconfigure.firestore.GcpFirestoreEmulatorAutoConfiguration,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gcp.autoconfigure.spanner;

import com.google.cloud.spanner.SpannerOptions;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Eddú Meléndez
*/
public class GcpSpannerEmulatorAutoConfigurationTests {

private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GcpSpannerEmulatorAutoConfiguration.class,
GcpSpannerAutoConfiguration.class, GcpContextAutoConfiguration.class))
.withPropertyValues("spring.cloud.gcp.spanner.project-id=test-project");

@Test
public void testEmulatorAutoConfigurationEnabled() {
this.contextRunner.withPropertyValues("spring.cloud.gcp.spanner.emulator.enabled=true")
.run(context -> {
SpannerOptions spannerOptions = context.getBean(SpannerOptions.class);
assertThat(spannerOptions.getEndpoint()).isEqualTo("localhost:9010");
});
}

@Test
public void testEmulatorAutoConfigurationEnabledCustomHostPort() {
this.contextRunner.withPropertyValues("spring.cloud.gcp.spanner.emulator.enabled=true",
"spring.cloud.gcp.spanner.emulator-host=localhost:9090")
.run(context -> {
SpannerOptions spannerOptions = context.getBean(SpannerOptions.class);
assertThat(spannerOptions.getEndpoint()).isEqualTo("localhost:9090");
});
}

@Test
public void testEmulatorAutoConfigurationDisabled() {
this.contextRunner
.run(context -> {
SpannerOptions spannerOptions = context.getBean(SpannerOptions.class);
assertThat(spannerOptions.getEndpoint()).isEqualTo("spanner.googleapis.com:443");
});
}
}