Search in sources :

Example 1 with CreateDatabaseRequest

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);
    }
}
Also used : Database(com.google.spanner.admin.database.v1.Database) Operation(com.google.longrunning.Operation) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata)

Example 2 with CreateDatabaseRequest

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()));
}
Also used : InstanceName(com.google.spanner.admin.database.v1.InstanceName) CreateDatabaseRequest(com.google.spanner.admin.database.v1.CreateDatabaseRequest) AbstractMessage(com.google.protobuf.AbstractMessage) Database(com.google.spanner.admin.database.v1.Database) ArrayList(java.util.ArrayList) Operation(com.google.longrunning.Operation) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 3 with CreateDatabaseRequest

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());
}
Also used : CreateDatabaseRequest(com.google.spanner.admin.database.v1.CreateDatabaseRequest) Database(com.google.spanner.admin.database.v1.Database) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Timestamp(com.google.protobuf.Timestamp)

Example 4 with CreateDatabaseRequest

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);
    }
}
Also used : CreateDatabaseRequest(com.google.spanner.admin.database.v1.CreateDatabaseRequest) CancelledException(com.google.api.gax.rpc.CancelledException) Database(com.google.spanner.admin.database.v1.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with CreateDatabaseRequest

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()));
}
Also used : CreateDatabaseRequest(com.google.spanner.admin.database.v1.CreateDatabaseRequest) AbstractMessage(com.google.protobuf.AbstractMessage) Database(com.google.spanner.admin.database.v1.Database) ArrayList(java.util.ArrayList) Operation(com.google.longrunning.Operation) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Aggregations

Database (com.google.spanner.admin.database.v1.Database)5 CreateDatabaseRequest (com.google.spanner.admin.database.v1.CreateDatabaseRequest)4 Operation (com.google.longrunning.Operation)3 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)3 AbstractMessage (com.google.protobuf.AbstractMessage)2 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 CancelledException (com.google.api.gax.rpc.CancelledException)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Timestamp (com.google.protobuf.Timestamp)1 InstanceName (com.google.spanner.admin.database.v1.InstanceName)1 ExecutionException (java.util.concurrent.ExecutionException)1