This repository was archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 689
Add SpannerOptions auto-configuration for emulator #2356
Merged
meltsufin
merged 5 commits into
spring-attic:master
from
eddumelendez:spanner_emulator_support
May 7, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
27bfbe7
Add SpannerOptions auto-configuration for emulator
eddumelendez f1f8394
Update docs
eddumelendez 6c5c52a
Rename property to host-port and add assert
eddumelendez 6353eca
Polish
eddumelendez 5b6c7de
Rename back to emulator-host and give it a default
meltsufin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
.../springframework/cloud/gcp/autoconfigure/spanner/GcpSpannerEmulatorAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
meltsufin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...ngframework/cloud/gcp/autoconfigure/spanner/GcpSpannerEmulatorAutoConfigurationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.