This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support auto-configuration of the Datastore emulator (#1661)
- Support for `spring.cloud.gcp.datastore.emulator.enabled` property - `spring.cloud.gcp.datastore.emulator-host` deprecated in favor of `spring.cloud.gcp.datastore.host` - The autoconfiguration itself manage the lifecycle to start and stop the emulator Fixes #1642.
- Loading branch information
1 parent
db02ef2
commit 99f76da
Showing
9 changed files
with
451 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
...src/main/java/org/springframework/cloud/gcp/autoconfigure/datastore/EmulatorSettings.java
This file contains 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-2019 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.datastore; | ||
|
||
/** | ||
* Properties for configuring Cloud Datastore Emulator. | ||
* | ||
* @author Lucas Soares | ||
* | ||
* @since 1.2 | ||
*/ | ||
public class EmulatorSettings { | ||
/** | ||
* If enabled the Datastore client will connect to an local datastore emulator. | ||
*/ | ||
private boolean enabled; | ||
|
||
/** | ||
* Is the datastore emulator port. Default: {@code 8081} | ||
*/ | ||
private int port = 8081; | ||
|
||
/** | ||
* Consistency to use creating the Datastore server instance. Default: {@code 0.9} | ||
*/ | ||
private double consistency = 0.9D; | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
|
||
public double getConsistency() { | ||
return consistency; | ||
} | ||
|
||
public void setConsistency(double consistency) { | ||
this.consistency = consistency; | ||
} | ||
} |
This file contains 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
120 changes: 120 additions & 0 deletions
120
...ingframework/cloud/gcp/autoconfigure/datastore/GcpDatastoreEmulatorAutoConfiguration.java
This file contains 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,120 @@ | ||
/* | ||
* Copyright 2017-2019 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.datastore; | ||
|
||
import com.google.cloud.datastore.testing.LocalDatastoreHelper; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
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.SmartLifecycle; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* If spring.cloud.gcp.datastore.emulator.enabled is set to true the emulator will be | ||
* started as a local datastore server using the | ||
* {@link com.google.cloud.datastore.testing.LocalDatastoreHelper}. | ||
* | ||
* @author Lucas Soares | ||
* | ||
* @since 1.2 | ||
*/ | ||
@Configuration | ||
@ConditionalOnProperty("spring.cloud.gcp.datastore.emulator.enabled") | ||
@AutoConfigureBefore(GcpDatastoreAutoConfiguration.class) | ||
@EnableConfigurationProperties(GcpDatastoreProperties.class) | ||
@ConditionalOnMissingBean(LocalDatastoreHelper.class) | ||
public class GcpDatastoreEmulatorAutoConfiguration implements SmartLifecycle { | ||
|
||
private static final Log LOGGER = LogFactory.getLog(GcpDatastoreEmulatorAutoConfiguration.class); | ||
|
||
private LocalDatastoreHelper helper; | ||
|
||
private volatile boolean running; | ||
|
||
@Bean | ||
public LocalDatastoreHelper createDatastoreHelper( | ||
GcpDatastoreProperties datastoreProperties) { | ||
EmulatorSettings settings = datastoreProperties.getEmulator(); | ||
|
||
this.helper = LocalDatastoreHelper.create(settings.getConsistency(), | ||
settings.getPort()); | ||
|
||
return this.helper; | ||
} | ||
|
||
/** Stops the instance of the emulator. */ | ||
@Override | ||
public void stop() { | ||
if (!isRunning()) { | ||
LOGGER.warn("The datastore emulator is not running."); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
LOGGER.info("Stopping datastore emulator."); | ||
|
||
this.helper.stop(); | ||
|
||
LOGGER.info("Datastore emulator stopped."); | ||
|
||
this.running = false; | ||
} | ||
catch (Exception e) { | ||
LOGGER.warn("Failed to stop datastore instance.", e); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the instance is running. This will be <code>true</code> after a successful | ||
* execution of the method {@link #start()} and <code>false</code> after a successful | ||
* execution of the method {@link #stop()}. method is called. | ||
*/ | ||
@Override | ||
public boolean isRunning() { | ||
return this.running; | ||
} | ||
|
||
/** | ||
* Starts the instance of the emulator. | ||
*/ | ||
@Override | ||
public void start() { | ||
if (isRunning()) { | ||
LOGGER.warn("The datastore emulator is already running."); | ||
return; | ||
} | ||
|
||
try { | ||
LOGGER.info("Starting datastore emulator."); | ||
|
||
this.helper.start(); | ||
|
||
LOGGER.info("Datastore emulator started."); | ||
|
||
this.running = true; | ||
} | ||
catch (Exception e) { | ||
LOGGER.error("Error constructing datastore instance."); | ||
} | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.