use of com.google.spanner.admin.database.v1.CreateDatabaseRequest in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImpl method createDatabase.
@Override
public void createDatabase(CreateDatabaseRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
try {
createDatabaseStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
String id = request.getCreateStatement().replace("CREATE DATABASE ", "");
if (id.startsWith("`") && id.endsWith("`")) {
id = id.substring(1, id.length() - 1);
}
String name = String.format("%s/databases/%s", request.getParent(), id);
MockDatabase db = new MockDatabase(name, request.getExtraStatementsList(), null);
if (databases.putIfAbsent(name, db) == null) {
CreateDatabaseMetadata metadata = CreateDatabaseMetadata.newBuilder().setDatabase(name).build();
Database database = Database.newBuilder().setName(name).setState(db.state).build();
Operation operation = Operation.newBuilder().setMetadata(Any.pack(metadata)).setResponse(Any.pack(database)).setDone(false).setName(operations.generateOperationName(name)).build();
operations.addOperation(operation, new CreateDatabaseCallable(operation.getName(), name));
createDatabaseResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.ALREADY_EXISTS.withDescription(String.format("Database with name %s already exists", name)).asRuntimeException());
}
} catch (Throwable t) {
responseObserver.onError(t);
}
}
use of com.google.spanner.admin.database.v1.CreateDatabaseRequest in project java-spanner by googleapis.
the class DatabaseAdminClientTest method createDatabaseTest.
@Test
public void createDatabaseTest() throws Exception {
Database expectedResponse = Database.newBuilder().setName(DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]").toString()).setCreateTime(Timestamp.newBuilder().build()).setRestoreInfo(RestoreInfo.newBuilder().build()).setEncryptionConfig(EncryptionConfig.newBuilder().build()).addAllEncryptionInfo(new ArrayList<EncryptionInfo>()).setVersionRetentionPeriod("versionRetentionPeriod-629783929").setEarliestVersionTime(Timestamp.newBuilder().build()).setDefaultLeader("defaultLeader759009962").setDatabaseDialect(DatabaseDialect.forNumber(0)).build();
Operation resultOperation = Operation.newBuilder().setName("createDatabaseTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
mockDatabaseAdmin.addResponse(resultOperation);
InstanceName parent = InstanceName.of("[PROJECT]", "[INSTANCE]");
String createStatement = "createStatement744686547";
Database actualResponse = client.createDatabaseAsync(parent, createStatement).get();
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateDatabaseRequest actualRequest = ((CreateDatabaseRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertEquals(createStatement, actualRequest.getCreateStatement());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.admin.database.v1.CreateDatabaseRequest in project java-spanner by googleapis.
the class GapicSpannerRpc method createDatabase.
@Override
public OperationFuture<Database, CreateDatabaseMetadata> createDatabase(final String instanceName, String createDatabaseStatement, Iterable<String> additionalStatements, com.google.cloud.spanner.Database databaseInfo) throws SpannerException {
final String databaseId = databaseInfo.getId().getDatabase();
CreateDatabaseRequest.Builder requestBuilder = CreateDatabaseRequest.newBuilder().setParent(instanceName).setCreateStatement(createDatabaseStatement).addAllExtraStatements(additionalStatements);
if (databaseInfo.getEncryptionConfig() != null) {
requestBuilder.setEncryptionConfig(EncryptionConfigProtoMapper.encryptionConfig(databaseInfo.getEncryptionConfig()));
}
if (databaseInfo.getDialect() != null) {
requestBuilder.setDatabaseDialect(databaseInfo.getDialect().toProto());
}
final CreateDatabaseRequest request = requestBuilder.build();
OperationFutureCallable<CreateDatabaseRequest, Database, CreateDatabaseMetadata> callable = new OperationFutureCallable<>(databaseAdminStub.createDatabaseOperationCallable(), request, DatabaseAdminGrpc.getCreateDatabaseMethod(), instanceName, nextPageToken -> listDatabaseOperations(instanceName, 0, String.format("(metadata.@type:type.googleapis.com/%s) AND (name:%s/operations/)", CreateDatabaseMetadata.getDescriptor().getFullName(), String.format("%s/databases/%s", instanceName, databaseId)), nextPageToken), input -> {
if (input.getDone() && input.hasResponse()) {
try {
Timestamp createTime = input.getResponse().unpack(Database.class).getCreateTime();
if (Timestamp.getDefaultInstance().equals(createTime)) {
// method to indicate that there is no known create time.
return null;
}
} catch (InvalidProtocolBufferException e) {
return null;
}
}
return null;
});
return RetryHelper.runWithRetries(callable, databaseAdminStubSettings.createDatabaseOperationSettings().getInitialCallSettings().getRetrySettings(), new OperationFutureRetryAlgorithm<>(), NanoClock.getDefaultClock());
}
use of com.google.spanner.admin.database.v1.CreateDatabaseRequest in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImplTest method createTestDb.
private Database createTestDb() {
CreateDatabaseRequest request = CreateDatabaseRequest.newBuilder().setCreateStatement("CREATE DATABASE `test-db`").addAllExtraStatements(Arrays.asList("CREATE TABLE FOO", "CREATE TABLE BAR")).setParent(TEST_PARENT).build();
OperationFuture<Database, CreateDatabaseMetadata> op = client.createDatabaseOperationCallable().futureCall(request);
try {
return op.get();
} catch (ExecutionException e) {
if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new CancelledException(e, FakeStatusCode.of(Code.CANCELLED), false);
}
}
use of com.google.spanner.admin.database.v1.CreateDatabaseRequest in project java-spanner by googleapis.
the class DatabaseAdminClientTest method createDatabaseTest2.
@Test
public void createDatabaseTest2() throws Exception {
Database expectedResponse = Database.newBuilder().setName(DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]").toString()).setCreateTime(Timestamp.newBuilder().build()).setRestoreInfo(RestoreInfo.newBuilder().build()).setEncryptionConfig(EncryptionConfig.newBuilder().build()).addAllEncryptionInfo(new ArrayList<EncryptionInfo>()).setVersionRetentionPeriod("versionRetentionPeriod-629783929").setEarliestVersionTime(Timestamp.newBuilder().build()).setDefaultLeader("defaultLeader759009962").setDatabaseDialect(DatabaseDialect.forNumber(0)).build();
Operation resultOperation = Operation.newBuilder().setName("createDatabaseTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
mockDatabaseAdmin.addResponse(resultOperation);
String parent = "parent-995424086";
String createStatement = "createStatement744686547";
Database actualResponse = client.createDatabaseAsync(parent, createStatement).get();
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateDatabaseRequest actualRequest = ((CreateDatabaseRequest) actualRequests.get(0));
Assert.assertEquals(parent, actualRequest.getParent());
Assert.assertEquals(createStatement, actualRequest.getCreateStatement());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations