Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: add autoConfigEmulator connection option (#931)
Adds `autoConfigEmulator` connection option. When this option is set to true for a connection: 1. The connection will by default try to connect to `localhost:9010` (unless a specific host/port is set in the connection URL). 2. Plain text communication will be enabled. 3. Authentication will be disabled. 4. The instance and database in the connection string will automatically be created on the emulator if any of them do not yet exist. Any existing instance and/or database will remain untouched. Towards googleapis/java-spanner-jdbc#380
- Loading branch information
Showing
5 changed files
with
462 additions
and
6 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
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
85 changes: 85 additions & 0 deletions
85
google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/EmulatorUtil.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,85 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* 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 | ||
* | ||
* http://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 com.google.cloud.spanner.connection; | ||
|
||
import com.google.cloud.NoCredentials; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.ErrorCode; | ||
import com.google.cloud.spanner.InstanceConfigId; | ||
import com.google.cloud.spanner.InstanceInfo; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerException; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* Util class for automatically generating a test instance and test database on a Cloud Spanner | ||
* emulator instance. This makes it easier to automatically start a working emulator and test an | ||
* application when working with JDBC. | ||
*/ | ||
class EmulatorUtil { | ||
|
||
/** | ||
* Creates the instance and the database that are specified in the connection string on the | ||
* emulator that the given {@link Spanner} instance connects to if these do not already exist. | ||
* | ||
* @param spanner a {@link Spanner} instance that connects to an emulator instance | ||
* @param databaseId the id of the instance and the database to create | ||
*/ | ||
static void maybeCreateInstanceAndDatabase(Spanner spanner, DatabaseId databaseId) { | ||
Preconditions.checkArgument( | ||
NoCredentials.getInstance().equals(spanner.getOptions().getCredentials())); | ||
try { | ||
spanner | ||
.getInstanceAdminClient() | ||
.createInstance( | ||
InstanceInfo.newBuilder(databaseId.getInstanceId()) | ||
.setDisplayName("Automatically Generated Test Instance") | ||
.setNodeCount(1) | ||
.setInstanceConfigId( | ||
InstanceConfigId.of( | ||
databaseId.getInstanceId().getProject(), "emulator-config")) | ||
.build()) | ||
.get(); | ||
} catch (ExecutionException executionException) { | ||
SpannerException spannerException = (SpannerException) executionException.getCause(); | ||
if (spannerException.getErrorCode() != ErrorCode.ALREADY_EXISTS) { | ||
throw spannerException; | ||
} | ||
} catch (InterruptedException e) { | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} | ||
try { | ||
spanner | ||
.getDatabaseAdminClient() | ||
.createDatabase( | ||
databaseId.getInstanceId().getInstance(), | ||
databaseId.getDatabase(), | ||
ImmutableList.<String>of()) | ||
.get(); | ||
} catch (ExecutionException executionException) { | ||
SpannerException spannerException = (SpannerException) executionException.getCause(); | ||
if (spannerException.getErrorCode() != ErrorCode.ALREADY_EXISTS) { | ||
throw spannerException; | ||
} | ||
} catch (InterruptedException e) { | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} | ||
} | ||
} |
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.