I ended up going in another direction.
- You can authenticate and use a remote Datastore, like Amar mentioned;
- You can also get credentials, store it locally, reference it in your
application.properties
using the spring.cloud.gcp.datastore.credentials.location=file://FULL_PATH_TO_THE_CREDENTIALS_FILE
and still use a remote Datastore;
- But my favorite option is to run it full local using a Datastore emulator, so no authentication is needed at all:
src/test/resources/application.properties
# if it exists, comment out or delete the following property:
# spring.cloud.gcp.datastore.credentials.location
# add the following
spring.cloud.gcp.emulator-enabled=true
# if you're not using environment variables, make sure the following property exists
spring.cloud.gcp.project-id=your-project-id
Be aware that if your running integration tests, or mixing remote and local tests, you might need different .properties
files for each test.
Your test class
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static LocalDatastoreHelper helper;
@BeforeClass
public static void setUpClass() throws IOException, InterruptedException {
logger.info("[Datastore-Emulator] start");
helper = LocalDatastoreHelper.create();
helper.start();
logger.info("[Datastore-Emulator] listening on port {}", helper.getPort());
System.setProperty(DatastoreHelper.LOCAL_HOST_ENV_VAR, "localhost:" + helper.getPort());
}
@AfterClass
public static void cleanUpClass() throws InterruptedException, TimeoutException, IOException {
logger.info("[Datastore-Emulator] stop");
helper.stop();
}
@Before
public void init() throws IOException {
logger.info("[Datastore-Emulator] cleaning data");
helper.reset();
}
This approach works great for Repositories (org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository
), but if you want to use a Datastore
directly, you can check this.
Refs