Skip to content

Commit 45c1f61

Browse files
meltsufinkioie
andauthored
Support spring.cloud.gcp.project-id property for Spanner Emulator config (#123)
Port of spring-attic/spring-cloud-gcp#2592. Co-authored-by: Chief <[email protected]>
1 parent eef0530 commit 45c1f61

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/spanner/GcpSpannerEmulatorAutoConfiguration.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.cloud.NoCredentials;
2020
import com.google.cloud.spanner.SpannerOptions;
21+
import com.google.cloud.spring.core.GcpProjectIdProvider;
2122

2223
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -31,6 +32,7 @@
3132
* Provides auto-configuration to use the Spanner emulator if enabled.
3233
*
3334
* @author Eddú Meléndez
35+
* @author Eddy Kioi
3436
* @since 1.2.3
3537
*/
3638
@Configuration(proxyBeanMethods = false)
@@ -41,7 +43,15 @@ public class GcpSpannerEmulatorAutoConfiguration {
4143

4244
private final GcpSpannerProperties properties;
4345

44-
public GcpSpannerEmulatorAutoConfiguration(GcpSpannerProperties properties) {
46+
private final String projectId;
47+
48+
public GcpSpannerEmulatorAutoConfiguration(GcpSpannerProperties properties,
49+
GcpProjectIdProvider projectIdProvider) {
50+
51+
this.projectId = (properties.getProjectId() != null)
52+
? properties.getProjectId()
53+
: projectIdProvider.getProjectId();
54+
4555
this.properties = properties;
4656
}
4757

@@ -50,7 +60,7 @@ public GcpSpannerEmulatorAutoConfiguration(GcpSpannerProperties properties) {
5060
public SpannerOptions spannerOptions() {
5161
Assert.notNull(this.properties.getEmulatorHost(), "`spring.cloud.gcp.spanner.emulator-host` must be set.");
5262
return SpannerOptions.newBuilder()
53-
.setProjectId(this.properties.getProjectId())
63+
.setProjectId(this.projectId)
5464
.setCredentials(NoCredentials.getInstance())
5565
.setEmulatorHost(this.properties.getEmulatorHost()).build();
5666
}

0 commit comments

Comments
 (0)